summaryrefslogtreecommitdiffstats
path: root/examples/test_account.py
blob: d35da78022c1824031e007486222e1ebd7c9595f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/python

import sys
import pywbem

def usage():
  print """Usage: %s <address> <username> <password> <command> [parameter...]
    Connect to CIM server at address and issue a command on the machine.

Available commands and their parameters:
  list_user - list users, parameter is user name or empty for all
  list_group - list groups, parameter is group name or empty for all
  group_members - list members of group, parameter is group name
  delete_user - delete account, needed parameter is account name
  delete_group - delete group, needed parameter is group name
  delete_identity - delete user or group, parameter InstanceID of identity
  create_account - creates a new account, parameters:
    [0] = Name: required, user login name

Example: %s https://127.0.0.1:5989 root redhat list_user""" % (sys.argv[0], sys.argv[0])

if len(sys.argv) < 5:
    usage()
    sys.exit(1)

url = sys.argv[1]

username = None
password = None

if sys.argv[2]:
    username = sys.argv[2]
if sys.argv[3]:
    password = sys.argv[3]

command = sys.argv[4]
parameters = sys.argv[5:]

cliconn = pywbem.WBEMConnection(url, (username, password))

if command == "list_user":
# Listintg users is simple, just query all instances from LMI_Account
# or select only by given Name
  slct = "select * from LMI_Account"
  if parameters:
    for i, parameter in enumerate(parameters):
      if i==0:
        slct += ' where Name = "%s"' % parameter
      else:
        slct += ' or Name = "%s"' % parameter

  instances = cliconn.ExecQuery('WQL', slct)
  for instance in instances:
    print instance.tomof()

elif command == "list_group":
# Listintg gruops is simple, just query all instances from LMI_Group
# or select only by given Name
  slct = "select * from LMI_Group"
  if parameters:
    for i, parameter in enumerate(parameters):
      if i==0:
        slct += ' where Name = "%s"' % parameter
      else:
        slct += ' or Name = "%s"' % parameter


  instances = cliconn.ExecQuery('WQL', slct)
  for instance in instances:
    print instance.tomof()

elif command == "group_members":
# Group members is a bit tricky. You need to select group (LMI_Group)
# by given Name, then you need to select identities (LMI_Identity), which
# are connected through LMI_MemberOfGroup to LMI_Group.
# And finally select all accounts (LMI_Account) which are connected through
# LMI_AssignedAccountIdentity with selected identities (this should be
# 1 to 1)
  if not parameters:
    usage()
    sys.exit(1)

  else:
    groups = cliconn.ExecQuery('WQL', 'select * from LMI_Group where Name = "%s"' % parameters[0])
  for group in groups:
    identities = cliconn.Associators(group.path, AssocClass='LMI_MemberOfGroup')
    for identity in identities:
        accounts = cliconn.Associators(identity.path, AssocClass='LMI_AssignedAccountIdentity')
        for account in accounts:
          print account.tomof()

elif command == "create_account":
# create a new account
# Firstly find system name, which is necessary parameter for method
# then invoke the method
  if not parameters:
    usage()
    sys.exit(1)

  computerSystems = cliconn.ExecQuery('WQL', 'select * from Linux_ComputerSystem')
  if not computerSystems:
    print >>sys.stderr, "No usable Linux_ComputerSystem instance found."
    sys.exit(2)

  if len(computerSystems) > 1:
    print >>sys.stderr, "More than one Linux_ComputerSystem instance found, don't know which to use."
    sys.exit(3)

  lams = cliconn.ExecQuery('WQL', 'select * from LMI_AccountManagementService')[0]

  print cliconn.InvokeMethod("CreateAccount", lams.path,
    Name = parameters[0],
    System = computerSystems[0].path)

elif command == "delete_user":
# Find user by given name and call DeleteInstance on the instance path
  if not parameters:
    usage()
    sys.exit(1)

  slct = 'select * from LMI_Account where Name = "%s"' % parameters[0]

  instances = cliconn.ExecQuery('WQL', slct)
  if instances:
    print cliconn.DeleteInstance(instances[0].path)
  else:
    print >> sys.stderr, "User does not exist: %s" %parameters[0]

elif command == "delete_group":
# Find group by given name and call DeleteInstance on the instance path
  if not parameters:
    usage()
    sys.exit(1)

  slct = 'select * from LMI_Group where Name = "%s"' % parameters[0]

  instances = cliconn.ExecQuery('WQL', slct)
  if instances:
    print cliconn.DeleteInstance(instances[0].path)
  else:
    print >> sys.stderr, "Group does not exist: %s" %parameters[0]

elif command == "delete_identity":
# Have to pass correct InstanceID
# It is in format:
# LMI:UID:user_id
# or
# LMI:GID:group_id
  if not parameters:
    usage()
    sys.exit(1)

  slct = 'select * from LMI_Identity where InstanceID = "%s"' % parameters[0]

  instances = cliconn.ExecQuery('WQL', slct)
  if instances:
    print cliconn.DeleteInstance(instances[0].path)
  else:
    print >> sys.stderr, "Identity does not exist: %s" %parameters[0]

else:
# unknown command
  print "Unknown command", command
  usage()
  sys.exit(1)