summaryrefslogtreecommitdiffstats
path: root/examples/test_account.py
blob: 5bf57826c943b1eb53a03180905e428a02ea4131 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/python
#
# Copyright (C) 2012 Red Hat, Inc.  All rights reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
# Authors: Roman Rakus <rrakus@redhat.com>
#

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, parameter required, user login name
  create_group - creates a new group, parameter required, group 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 == "create_group":
# create a new group
# 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("CreateGroup", 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_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)