General manipulation of users and groups are done with the objects from following classes:
Some common use cases are described in the following parts
List of users are provided by LMI_Account. Each one object of this class represents one user on the system. Both system and non-sytem users are directly in LMI_Account class:
# List user by name
print c.root.cimv2.LMI_Account.first_instance(key="name", value="root")
# List user by id
print c.root.cimv2.LMI_Account.first_instance(key="UserID", value="0")
Similarly like users, groups are represented by objects of LMI_Group class:
# List group by name
print c.root.cimv2.LMI_Group.first_instance(key="Name", value="root")
# List group by id
print c.root.cimv2.LMI_Group.first_instance(key="InstanceID", value="LMI:GID:0")
LMI_Identity is class representing users and groups on the system. Group membership is represented by LMI_MemberOfGroup association. It associates LMI_Group and LMI_Identity, where LMI_Identity is associated by LMI_AssignedAccountIdentity with LMI_Account:
# Get users from root group
# 1) Get root group object
root_group = c.root.cimv2.LMI_Group.first_instance(key="Name", value="root")
# 2) Get LMI_Identity objects associated with root group
identities = root_group.associators(ResultClass="LMI_Identity", AssocClass="LMI_MemberOfGroup")
# 3) go through all identites, get LMI_Account associated with identity and print user name
# Note: associators returns a list, but there is just one LMI_Account
for i in identities:
print i.first_associator(ResultClass="LMI_Account").Name
For user creation we have to use LMI_AccountManagementService. There is CreateAccount method, which will create user with descired attributes:
# get computer system
cs = c.root.cimv2.Linux_ComputerSystem.first_instance()
# get service
lams = c.root.cimv2.LMI_AccountManagementService.first_instance()
# invoke method, print result
lams.CreateAccount(Name="lmishell-user", System=cs)
Similarly like creating user, creating groups are don in LMI_AccountManagementService, using CreateGroup method:
# get computer system
cs = c.root.cimv2.Linux_ComputerSystem.first_instance()
# get service
lams = c.root.cimv2.LMI_AccountManagementService.first_instance()
# invoke method, print result
print lams.CreateGroup(Name="lmishell-group", System=cs)
User deletion is done with DeleteUser method on the desired LMI_Account object.
# get the desired user
acci = c.root.cimv2.LMI_Account.first_instance(key="Name", value="tobedeleted")
# delete the user
acci.DeleteUser()
Note
Previous releases allowed to use DeleteInstance intrinsic method to delete LMI_Account. This method is now deprecated and will be removed from future releases of OpenLMI Account. The reason is that DeleteInstance cannot have parameters; it is equivalent to call DeleteAccount without specifying parameters.
Group deletion is done with DeleteInstance intrinsic method on the desired LMI_Group object:
# get the desired group
grp = c.root.cimv2.LMI_Group.first_instance(key="Name", value="tobedeleted")
# delete the group
grp.delete()
Warning
Due to a bug in libuser (library used), it is possible to delete user’s primary group if the group id is not the default one.
Adding user to group is done with CreateInstance intrinsic method on the LMI_MemberOfGroup class, which requires reference to LMI_Group and LMI_Identity:
# We will add root user to pegasus group
# get group pegasus
grp = c.root.cimv2.LMI_Group.first_instance(key="Name", value="pegasus")
# get user root
acc = c.root.cimv2.LMI_Account.first_instance(key="Name", value="root")
# get identity of root user
identity = acc.first_associator(ResultClass="LMI_Identity")
# create instance of LMI_MemberOfGroup with the above references
c.root.cimv2.LMI_MemberOfGroup.create_instance({"Member":identity.path, "Collection":grp.path})
Removing user from group is done with DeleteInstance intrinsic method on the desired LMI_MemberOfGroup object:
# We will remove root user from pegasus group
# get group pegasus
grp = c.root.cimv2.LMI_Group.first_instance(key="Name", value="pegasus")
# get user root
acc = c.root.cimv2.LMI_Account.first_instance(key="Name", value="root")
# get identity of root user
identity = acc.associators(ResultClass="LMI_Identity")[0]
# iterate through all LMI_MemberOfGroup associated with identity and remove the one with our group
for mog in identity.references(ResultClass="LMI_MemberOfGroup"):
if mog.Collection == grp.path:
mog.delete()
It is also possible to modify user details and it is done by ModifyInstance intrinsic method on the desired LMI_Account object:
# Change login shell of test user
acci = c.root.cimv2.LMI_Account.first_instance(key="Name", value="test")
acci.LoginShell = '/bin/sh'
# propagate changes
acci.push()
OpenLMI Account supports the following indications:
Both indications works only on the following classes:
See more below.
Client can be notified when instance of class has been created. It is done with LMI_AccountInstanceCreationIndication. The indication filter query must be in the following form: SELECT * FROM LMI_AccountInstanceCreationIndication WHERE SOURCEINSTANCE ISA class_name, where class_name is one of the allowed classes.
The following example creates filter, handler and subscription (lmi shell do it in one step), which will notify client when user is created:
# Notify when a user is created
c.root.interop.create_indication(
FilterCreationClassName="CIM_IndicationFilter",
FilterSystemCreationClassName="CIM_ComputerSystem",
FilterSourceNamespace="root/cimv2",
QueryLanguage="WQL",
Query='SELECT * FROM LMI_AccountInstanceCreationIndication WHERE SOURCEINSTANCE ISA LMI_Account',
Name="account_creation",
HandlerCreationClassName="CIM_IndicationHandlerCIMXML",
HandlerSystemCreationClassName="CIM_ComputerSystem",
Destination="http://192.168.122.1:5988" # this is the destination computer, where all the indications will be delivered
)
Client can be notified when instance is deleted. The same rules like in Creation Indication applies here:
# Notify when a user is deleted
c.root.interop.create_indication(
FilterCreationClassName="CIM_IndicationFilter",
FilterSystemCreationClassName="CIM_ComputerSystem",
FilterSourceNamespace="root/cimv2",
QueryLanguage="WQL",
Query='SELECT * FROM LMI_AccountInstanceDeletionIndication WHERE SOURCEINSTANCE ISA LMI_Account',
Name="account_creation",
HandlerCreationClassName="CIM_IndicationHandlerCIMXML",
HandlerSystemCreationClassName="CIM_ComputerSystem",
Destination="http://192.168.122.1:5988" # this is the destination computer, where all the indications will be delivered
)
Note
Both indications uses indication manager and polling.