Source code for saltext.influxdb.states.influxdb_user
"""Manage InfluxDB 0.9-1.x users statefully... important:: You can optionally specify default connection parameters via the general :ref:`influxdb setup <influxdb-setup>`."""def__virtual__():if"influxdb.db_exists"in__salt__:return"influxdb_user"return(False,"influxdb module could not be loaded")
[docs]defpresent(name,passwd,admin=False,grants=None,**client_args):""" Ensure that given user is present. name Name of the user to manage passwd Password of the user admin : False Whether the user should have cluster administration privileges or not. grants Optional - Dict of database:privilege items associated with the user. Example: grants: foo_db: read bar_db: all **Example:** .. code-block:: yaml example user present in influxdb: influxdb_user.present: - name: example - passwd: somepassword - admin: False - grants: foo_db: read bar_db: all """create=Falseret={"name":name,"changes":{},"result":True,"comment":f"User {name} is present and up to date",}ifnot__salt__["influxdb.user_exists"](name,**client_args):create=Trueif__opts__["test"]:ret["comment"]=f"User {name} will be created"ret["result"]=Nonereturnretelse:ifnot__salt__["influxdb.create_user"](name,passwd,admin=admin,**client_args):ret["comment"]=f"Failed to create user {name}"ret["result"]=Falsereturnretelse:user=__salt__["influxdb.user_info"](name,**client_args)ifuser["admin"]!=admin:ifnot__opts__["test"]:ifadmin:__salt__["influxdb.grant_admin_privileges"](name,**client_args)else:__salt__["influxdb.revoke_admin_privileges"](name,**client_args)ifadmin!=__salt__["influxdb.user_info"](name,**client_args)["admin"]:ret["comment"]=f"Failed to set admin privilege to user {name}"ret["result"]=Falsereturnretret["changes"]["Admin privileges"]=adminifgrants:db_privileges=__salt__["influxdb.list_privileges"](name,**client_args)fordatabase,privilegeingrants.items():privilege=privilege.lower()ifprivilege!=db_privileges.get(database,privilege):ifnot__opts__["test"]:__salt__["influxdb.revoke_privilege"](database,"all",name,**client_args)deldb_privileges[database]ifdatabasenotindb_privileges:ret["changes"][f"Grant on database {database} to user {name}"]=privilegeifnot__opts__["test"]:__salt__["influxdb.grant_privilege"](database,privilege,name,**client_args)ifret["changes"]:ifcreate:ret["comment"]=f"Created user {name}"ret["changes"][name]="User created"else:if__opts__["test"]:ret["result"]=Noneret["comment"]=f"User {name} will be updated with the following changes:"fork,vinret["changes"].items():ret["comment"]+=f"\n{k} => {v}"ret["changes"]={}else:ret["comment"]=f"Updated user {name}"returnret
[docs]defabsent(name,**client_args):""" Ensure that given user is absent. name The name of the user to manage """ret={"name":name,"changes":{},"result":True,"comment":f"User {name} is not present",}if__salt__["influxdb.user_exists"](name,**client_args):if__opts__["test"]:ret["result"]=Noneret["comment"]=f"User {name} will be removed"returnretelse:if__salt__["influxdb.remove_user"](name,**client_args):ret["comment"]=f"Removed user {name}"ret["changes"][name]="removed"returnretelse:ret["comment"]=f"Failed to remove user {name}"ret["result"]=Falsereturnretreturnret