summaryrefslogtreecommitdiffstats
path: root/examples/test_account.py
blob: 5b88ca323c3ba9562db9e0a459ddedff0f31958a (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
#!/usr/bin/python

import sys
import pywbem

def usage():
  print """Usage: %s <address> <command> <command parameter> [username] [password]
    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 * for all
  list_group - list groups, parameter is group name or * for all
  group_members - list members of group, parameter is group name

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


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

url = sys.argv[1]
command = sys.argv[2]
parameter = sys.argv[3]

username = None
password = None
if len(sys.argv) > 4:
    username = sys.argv[4]
if len(sys.argv) > 5:
    password = sys.argv[5]

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

if command == "list_user":
# Listintg users is simple, just query all instances from Cura_Account
# or select only by given Name
  if parameter == "*":
    instances =  cliconn.ExecQuery('WQL', 'select * from Cura_Account')
  else:
    instances = cliconn.ExecQuery('WQL', 'select * from Cura_Account where Name = "%s"' % parameter)
  for instance in instances:
    print instance.tomof()

elif command == "list_group":
# Listintg gruops is simple, just query all instances from Cura_Group
# or select only by given Name
  if parameter == "*":
    instances = cliconn.ExecQuery('WQL', 'select * from Cura_Group')
  else:
    instances = cliconn.ExecQuery('WQL', 'select * from Cura_Group where Name = "%s"' % parameter)
  for instance in instances:
    print instance.tomof()

elif command == "group_members":
# Group members is a bit tricky. You need to select group (Cura_Group)
# by given Name, then you need to select identities (Cura_Identity), which
# are connected through Cura_MemberOfGroup to Cura_Group.
# And finally select all accounts (Cura_Account) which are connected through
# Cura_AssignedAccountIdentity with selected identities (this should be
# 1 to 1)
  groups = cliconn.ExecQuery('WQL', 'select * from Cura_Group where Name = "%s"' % parameter)
  for group in groups:
    identities = cliconn.Associators(group.path, AssocClass='Cura_MemberOfGroup')
    for identity in identities:
        accounts = cliconn.Associators(identity.path, AssocClass='Cura_AssignedAccountIdentity')
        for account in accounts:
          print account.tomof()


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