Source code for saltext.influxdb.states.influxdb_retention_policy
"""Manage InfluxDB 0.9-1.x retention policies 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_retention_policy"return(False,"influxdb module could not be loaded")
[docs]defconvert_duration(duration):""" Convert the a duration string into XXhYYmZZs format duration Duration to convert Returns: duration_string String representation of duration in XXhYYmZZs format """# durations must be specified in days, weeks or hoursifduration.endswith("h"):hours=int(duration.split("h"))elifduration.endswith("d"):days=duration.split("d")hours=int(days[0])*24elifduration.endswith("w"):weeks=duration.split("w")hours=int(weeks[0])*24*7else:raiseValueError(f"Invalid duration: {duration}")duration_string=str(hours)+"h0m0s"returnduration_string
[docs]defpresent(name,database,duration="7d",replication=1,default=False,**client_args):""" Ensure that given retention policy is present. name Name of the retention policy to create. database Database to create retention policy on. """ret={"name":name,"changes":{},"result":True,"comment":f"retention policy {name} is already present",}ifnot__salt__["influxdb.retention_policy_exists"](name=name,database=database,**client_args):if__opts__["test"]:ret["result"]=Noneret["comment"]=f" {name} is absent and will be created"returnretif__salt__["influxdb.create_retention_policy"](database,name,duration,replication,default,**client_args):ret["comment"]=f"retention policy {name} has been created"ret["changes"][name]="Present"returnretelse:ret["comment"]=f"Failed to create retention policy {name}"ret["result"]=Falsereturnretelse:current_policy=__salt__["influxdb.get_retention_policy"](database=database,name=name,**client_args)update_policy=Falseifcurrent_policy["duration"]!=convert_duration(duration):update_policy=Trueret["changes"]["duration"]="Retention changed from {} to {}.".format(current_policy["duration"],duration)ifcurrent_policy["replicaN"]!=replication:update_policy=Trueret["changes"]["replication"]="Replication changed from {} to {}.".format(current_policy["replicaN"],replication)ifcurrent_policy["default"]!=default:update_policy=Trueret["changes"]["default"]="Default changed from {} to {}.".format(current_policy["default"],default)ifupdate_policy:if__opts__["test"]:ret["result"]=Noneret["comment"]=f" {name} is present and set to be changed"returnretelse:if__salt__["influxdb.alter_retention_policy"](database,name,duration,replication,default,**client_args):ret["comment"]=f"retention policy {name} has been changed"returnretelse:ret["comment"]=f"Failed to update retention policy {name}"ret["result"]=Falsereturnretreturnret
[docs]defabsent(name,database,**client_args):""" Ensure that given retention policy is absent. name Name of the retention policy to remove. database Name of the database that the retention policy was defined on. """ret={"name":name,"changes":{},"result":True,"comment":f"retention policy {name} is not present",}if__salt__["influxdb.retention_policy_exists"](database,name,**client_args):if__opts__["test"]:ret["result"]=Noneret["comment"]=f"retention policy {name} is present and needs to be removed"returnretif__salt__["influxdb.drop_retention_policy"](database,name,**client_args):ret["comment"]=f"retention policy {name} has been removed"ret["changes"][name]="Absent"returnretelse:ret["comment"]=f"Failed to remove retention policy {name}"ret["result"]=Falsereturnretreturnret