Source code for saltext.zabbix.states.zabbix_hostgroup
"""Management of Zabbix host groups.:codeauthor: Jiri Kotlin <jiri.kotlin@ultimum.io>"""
[docs]def__virtual__():""" Only make these states available if Zabbix module is available. """if"zabbix.hostgroup_create"in__salt__:returnTruereturn(False,"zabbix module could not be loaded")
[docs]defpresent(name,**kwargs):""" Ensures that the host group exists, eventually creates new host group. .. versionadded:: 2016.3.0 :param name: name of the host group :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring) :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring) :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring) .. code-block:: yaml create_testing_host_group: zabbix_hostgroup.present: - name: 'My hostgroup name' """connection_args={}if"_connection_user"inkwargs:connection_args["_connection_user"]=kwargs["_connection_user"]if"_connection_password"inkwargs:connection_args["_connection_password"]=kwargs["_connection_password"]if"_connection_url"inkwargs:connection_args["_connection_url"]=kwargs["_connection_url"]ret={"name":name,"changes":{},"result":False,"comment":""}# Comment and change messagescomment_hostgroup_created=f"Host group {name} created."comment_hostgroup_notcreated=f"Unable to create host group: {name}. "comment_hostgroup_exists=f"Host group {name} already exists."changes_hostgroup_created={name:{"old":f"Host group {name} does not exist.","new":f"Host group {name} created.",}}hostgroup_exists=__salt__["zabbix.hostgroup_exists"](name,**connection_args)# Dry run, test=true modeif__opts__["test"]:ifhostgroup_exists:ret["result"]=Trueret["comment"]=comment_hostgroup_existselse:ret["result"]=Noneret["comment"]=comment_hostgroup_createdret["changes"]=changes_hostgroup_createdreturnretifhostgroup_exists:ret["result"]=Trueret["comment"]=comment_hostgroup_existselse:hostgroup_create=__salt__["zabbix.hostgroup_create"](name,**connection_args)if"error"notinhostgroup_create:ret["result"]=Trueret["comment"]=comment_hostgroup_createdret["changes"]=changes_hostgroup_createdelse:ret["result"]=Falseret["comment"]=comment_hostgroup_notcreated+str(hostgroup_create["error"])returnret
[docs]defabsent(name,**kwargs):""" Ensures that the host group does not exist, eventually delete host group. .. versionadded:: 2016.3.0 :param name: name of the host group :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring) :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring) :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring) .. code-block:: yaml delete_testing_host_group: zabbix_hostgroup.absent: - name: 'My hostgroup name' """ret={"name":name,"changes":{},"result":False,"comment":""}# Comment and change messagescomment_hostgroup_deleted=f"Host group {name} deleted."comment_hostgroup_notdeleted=f"Unable to delete host group: {name}. "comment_hostgroup_notexists=f"Host group {name} does not exist."changes_hostgroup_deleted={name:{"old":f"Host group {name} exists.","new":f"Host group {name} deleted.",}}connection_args={}if"_connection_user"inkwargs:connection_args["_connection_user"]=kwargs["_connection_user"]if"_connection_password"inkwargs:connection_args["_connection_password"]=kwargs["_connection_password"]if"_connection_url"inkwargs:connection_args["_connection_url"]=kwargs["_connection_url"]hostgroup_exists=__salt__["zabbix.hostgroup_exists"](name,**connection_args)# Dry run, test=true modeif__opts__["test"]:ifnothostgroup_exists:ret["result"]=Trueret["comment"]=comment_hostgroup_notexistselse:ret["result"]=Noneret["comment"]=comment_hostgroup_deletedret["changes"]=changes_hostgroup_deletedreturnrethostgroup_get=__salt__["zabbix.hostgroup_get"](name,**connection_args)ifnothostgroup_get:ret["result"]=Trueret["comment"]=comment_hostgroup_notexistselse:try:groupid=hostgroup_get[0]["groupid"]hostgroup_delete=__salt__["zabbix.hostgroup_delete"](groupid,**connection_args)exceptKeyError:hostgroup_delete=Falseifhostgroup_deleteand"error"notinhostgroup_delete:ret["result"]=Trueret["comment"]=comment_hostgroup_deletedret["changes"]=changes_hostgroup_deletedelse:ret["result"]=Falseret["comment"]=comment_hostgroup_notdeleted+str(hostgroup_delete["error"])returnret