Skip to content

Commit

Permalink
Remove __utils__ usage from salt/{modules,states}/{zfs,zpool}.py
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Algarvio <palgarvio@vmware.com>
  • Loading branch information
s0undt3ch committed Jun 6, 2023
1 parent 95beaef commit 44edd0a
Show file tree
Hide file tree
Showing 12 changed files with 532 additions and 748 deletions.
95 changes: 48 additions & 47 deletions salt/modules/zfs.py

Large diffs are not rendered by default.

115 changes: 58 additions & 57 deletions salt/modules/zpool.py

Large diffs are not rendered by default.

115 changes: 58 additions & 57 deletions salt/states/zfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import logging
from datetime import datetime

import salt.utils.zfs
from salt.utils.odict import OrderedDict

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -118,7 +119,7 @@ def _absent(name, dataset_type, force=False, recursive=False):
ret["comment"] = mod_res["error"]
else:
## NOTE: no dataset found with name of the dataset_type
ret["comment"] = "{} {} is absent".format(dataset_type, name)
ret["comment"] = f"{dataset_type} {name} is absent"

return ret

Expand All @@ -140,12 +141,12 @@ def filesystem_absent(name, force=False, recursive=False):
destroying the volume specified by ``name``. This module is dataset type sensitive.
"""
if not __utils__["zfs.is_dataset"](name):
if not salt.utils.zfs.is_dataset(name):
ret = {
"name": name,
"changes": {},
"result": False,
"comment": "invalid dataset name: {}".format(name),
"comment": f"invalid dataset name: {name}",
}
else:
ret = _absent(name, "filesystem", force, recursive)
Expand All @@ -169,12 +170,12 @@ def volume_absent(name, force=False, recursive=False):
destroying the filesystem specified by ``name``. This module is dataset type sensitive.
"""
if not __utils__["zfs.is_dataset"](name):
if not salt.utils.zfs.is_dataset(name):
ret = {
"name": name,
"changes": {},
"result": False,
"comment": "invalid dataset name: {}".format(name),
"comment": f"invalid dataset name: {name}",
}
else:
ret = _absent(name, "volume", force, recursive)
Expand All @@ -193,12 +194,12 @@ def snapshot_absent(name, force=False, recursive=False):
also destroy all the child datasets (zfs destroy -r)
"""
if not __utils__["zfs.is_snapshot"](name):
if not salt.utils.zfs.is_snapshot(name):
ret = {
"name": name,
"changes": {},
"result": False,
"comment": "invalid snapshot name: {}".format(name),
"comment": f"invalid snapshot name: {name}",
}
else:
ret = _absent(name, "snapshot", force, recursive)
Expand All @@ -217,12 +218,12 @@ def bookmark_absent(name, force=False, recursive=False):
also destroy all the child datasets (zfs destroy -r)
"""
if not __utils__["zfs.is_bookmark"](name):
if not salt.utils.zfs.is_bookmark(name):
ret = {
"name": name,
"changes": {},
"result": False,
"comment": "invalid bookmark name: {}".format(name),
"comment": f"invalid bookmark name: {name}",
}
else:
ret = _absent(name, "bookmark", force, recursive)
Expand All @@ -248,18 +249,18 @@ def hold_absent(name, snapshot, recursive=False):
log.debug("zfs.hold_absent::%s::config::recursive = %s", name, recursive)

## check we have a snapshot/tag name
if not __utils__["zfs.is_snapshot"](snapshot):
if not salt.utils.zfs.is_snapshot(snapshot):
ret["result"] = False
ret["comment"] = "invalid snapshot name: {}".format(snapshot)
ret["comment"] = f"invalid snapshot name: {snapshot}"
return ret

if (
__utils__["zfs.is_snapshot"](name)
or __utils__["zfs.is_bookmark"](name)
salt.utils.zfs.is_snapshot(name)
or salt.utils.zfs.is_bookmark(name)
or name == "error"
):
ret["result"] = False
ret["comment"] = "invalid tag name: {}".format(name)
ret["comment"] = f"invalid tag name: {name}"
return ret

## release hold if required
Expand Down Expand Up @@ -317,18 +318,18 @@ def hold_present(name, snapshot, recursive=False):
log.debug("zfs.hold_present::%s::config::recursive = %s", name, recursive)

## check we have a snapshot/tag name
if not __utils__["zfs.is_snapshot"](snapshot):
if not salt.utils.zfs.is_snapshot(snapshot):
ret["result"] = False
ret["comment"] = "invalid snapshot name: {}".format(snapshot)
ret["comment"] = f"invalid snapshot name: {snapshot}"
return ret

if (
__utils__["zfs.is_snapshot"](name)
or __utils__["zfs.is_bookmark"](name)
salt.utils.zfs.is_snapshot(name)
or salt.utils.zfs.is_bookmark(name)
or name == "error"
):
ret["result"] = False
ret["comment"] = "invalid tag name: {}".format(name)
ret["comment"] = f"invalid tag name: {name}"
return ret

## place hold if required
Expand All @@ -349,9 +350,9 @@ def hold_present(name, snapshot, recursive=False):
ret["result"] = mod_res["held"]
if ret["result"]:
ret["changes"] = OrderedDict([(snapshot, OrderedDict([(name, "held")]))])
ret["comment"] = "hold {} added to {}".format(name, snapshot)
ret["comment"] = f"hold {name} added to {snapshot}"
else:
ret["comment"] = "failed to add hold {} to {}".format(name, snapshot)
ret["comment"] = f"failed to add hold {name} to {snapshot}"
if "error" in mod_res:
ret["comment"] = mod_res["error"]

Expand Down Expand Up @@ -410,10 +411,10 @@ def _dataset_present(
## ensure properties are zfs values
if properties is None:
properties = {}
properties = __utils__["zfs.from_auto_dict"](properties)
properties = salt.utils.zfs.from_auto_dict(properties)
if volume_size:
## NOTE: add volsize to properties
volume_size = __utils__["zfs.from_size"](volume_size)
volume_size = salt.utils.zfs.from_size(volume_size)
properties.update({"volsize": volume_size})
# The sorted isn't necessary for proper behavior, but it helps for the unit
# tests.
Expand All @@ -438,21 +439,21 @@ def _dataset_present(
)

## check we have valid filesystem name/volume name/clone snapshot
if not __utils__["zfs.is_dataset"](name):
if not salt.utils.zfs.is_dataset(name):
ret["result"] = False
ret["comment"] = "invalid dataset name: {}".format(name)
ret["comment"] = f"invalid dataset name: {name}"
return ret

if cloned_from and not __utils__["zfs.is_snapshot"](cloned_from):
if cloned_from and not salt.utils.zfs.is_snapshot(cloned_from):
ret["result"] = False
ret["comment"] = "{} is not a snapshot".format(cloned_from)
ret["comment"] = f"{cloned_from} is not a snapshot"
return ret

## ensure dataset is in correct state
## NOTE: update the dataset
exists = __salt__["zfs.exists"](name, **{"type": dataset_type})
if exists and len(properties) == 0:
ret["comment"] = "{} {} is uptodate".format(dataset_type, name)
ret["comment"] = f"{dataset_type} {name} is uptodate"
elif exists and len(properties) > 0:
## NOTE: fetch current volume properties
properties_current = __salt__["zfs.get"](
Expand Down Expand Up @@ -500,11 +501,11 @@ def _dataset_present(

## NOTE: update comment
if ret["result"] and name in ret["changes"]:
ret["comment"] = "{} {} was updated".format(dataset_type, name)
ret["comment"] = f"{dataset_type} {name} was updated"
elif ret["result"]:
ret["comment"] = "{} {} is uptodate".format(dataset_type, name)
ret["comment"] = f"{dataset_type} {name} is uptodate"
else:
ret["comment"] = "{} {} failed to be updated".format(dataset_type, name)
ret["comment"] = f"{dataset_type} {name} failed to be updated"

## NOTE: create or clone the dataset
elif not exists:
Expand All @@ -521,7 +522,7 @@ def _dataset_present(
mod_res = __salt__["zfs.clone"](
cloned_from,
name,
**{"create_parent": create_parent, "properties": properties}
**{"create_parent": create_parent, "properties": properties},
)
else:
## NOTE: create the dataset
Expand All @@ -532,7 +533,7 @@ def _dataset_present(
"properties": properties,
"volume_size": volume_size,
"sparse": sparse,
}
},
)

ret["result"] = mod_res[mod_res_action]
Expand Down Expand Up @@ -656,9 +657,9 @@ def bookmark_present(name, snapshot):
log.debug("zfs.bookmark_present::%s::config::snapshot = %s", name, snapshot)

## check we have valid snapshot/bookmark name
if not __utils__["zfs.is_snapshot"](snapshot):
if not salt.utils.zfs.is_snapshot(snapshot):
ret["result"] = False
ret["comment"] = "invalid snapshot name: {}".format(name)
ret["comment"] = f"invalid snapshot name: {name}"
return ret

if "#" not in name and "/" not in name:
Expand All @@ -668,9 +669,9 @@ def bookmark_present(name, snapshot):
name = "{}#{}".format(snapshot[: snapshot.index("@")], name)
ret["name"] = name

if not __utils__["zfs.is_bookmark"](name):
if not salt.utils.zfs.is_bookmark(name):
ret["result"] = False
ret["comment"] = "invalid bookmark name: {}".format(name)
ret["comment"] = f"invalid bookmark name: {name}"
return ret

## ensure bookmark exists
Expand All @@ -684,9 +685,9 @@ def bookmark_present(name, snapshot):
ret["result"] = mod_res["bookmarked"]
if ret["result"]:
ret["changes"][name] = snapshot
ret["comment"] = "{} bookmarked as {}".format(snapshot, name)
ret["comment"] = f"{snapshot} bookmarked as {name}"
else:
ret["comment"] = "failed to bookmark {}".format(snapshot)
ret["comment"] = f"failed to bookmark {snapshot}"
if "error" in mod_res:
ret["comment"] = mod_res["error"]
else:
Expand Down Expand Up @@ -719,12 +720,12 @@ def snapshot_present(name, recursive=False, properties=None):

## ensure properties are zfs values
if properties:
properties = __utils__["zfs.from_auto_dict"](properties)
properties = salt.utils.zfs.from_auto_dict(properties)

## check we have valid snapshot name
if not __utils__["zfs.is_snapshot"](name):
if not salt.utils.zfs.is_snapshot(name):
ret["result"] = False
ret["comment"] = "invalid snapshot name: {}".format(name)
ret["comment"] = f"invalid snapshot name: {name}"
return ret

## ensure snapshot exits
Expand All @@ -742,9 +743,9 @@ def snapshot_present(name, recursive=False, properties=None):
ret["changes"][name] = "snapshotted"
if properties:
ret["changes"][name] = properties
ret["comment"] = "snapshot {} was created".format(name)
ret["comment"] = f"snapshot {name} was created"
else:
ret["comment"] = "failed to create snapshot {}".format(name)
ret["comment"] = f"failed to create snapshot {name}"
if "error" in mod_res:
ret["comment"] = mod_res["error"]
else:
Expand All @@ -770,16 +771,16 @@ def promoted(name):
ret = {"name": name, "changes": {}, "result": True, "comment": ""}

## check we if we have a valid dataset name
if not __utils__["zfs.is_dataset"](name):
if not salt.utils.zfs.is_dataset(name):
ret["result"] = False
ret["comment"] = "invalid dataset name: {}".format(name)
ret["comment"] = f"invalid dataset name: {name}"
return ret

## ensure dataset is the primary instance
if not __salt__["zfs.exists"](name, **{"type": "filesystem,volume"}):
## NOTE: we don't have a dataset
ret["result"] = False
ret["comment"] = "dataset {} does not exist".format(name)
ret["comment"] = f"dataset {name} does not exist"
else:
## NOTE: check if we have a blank origin (-)
if (
Expand All @@ -789,7 +790,7 @@ def promoted(name):
== "-"
):
## NOTE: we're already promoted
ret["comment"] = "{} already promoted".format(name)
ret["comment"] = f"{name} already promoted"
else:
## NOTE: promote dataset
if not __opts__["test"]:
Expand All @@ -800,9 +801,9 @@ def promoted(name):
ret["result"] = mod_res["promoted"]
if ret["result"]:
ret["changes"][name] = "promoted"
ret["comment"] = "{} promoted".format(name)
ret["comment"] = f"{name} promoted"
else:
ret["comment"] = "failed to promote {}".format(name)
ret["comment"] = f"failed to promote {name}"
if "error" in mod_res:
ret["comment"] = mod_res["error"]

Expand Down Expand Up @@ -833,7 +834,7 @@ def _schedule_snapshot_retrieve(dataset, prefix, snapshots):
snap_name = snap[snap.index("@") + 1 :]

## NOTE: we only want snapshots matching our prefix
if not snap_name.startswith("{}-".format(prefix)):
if not snap_name.startswith(f"{prefix}-"):
continue

## NOTE: retrieve the holds for this snapshot
Expand Down Expand Up @@ -886,7 +887,7 @@ def _schedule_snapshot_prepare(dataset, prefix, snapshots):
## NOTE: extract datetime from snapshot name
timestamp = datetime.strptime(
snapshots[hold][-1],
"{}@{}-%Y%m%d_%H%M%S".format(dataset, prefix),
f"{dataset}@{prefix}-%Y%m%d_%H%M%S",
).replace(second=0, microsecond=0)

## NOTE: compare current timestamp to timestamp from snapshot
Expand Down Expand Up @@ -952,17 +953,17 @@ def scheduled_snapshot(name, prefix, recursive=True, schedule=None):

## strict configuration validation
## NOTE: we need a valid dataset
if not __utils__["zfs.is_dataset"](name):
if not salt.utils.zfs.is_dataset(name):
ret["result"] = False
ret["comment"] = "invalid dataset name: {}".format(name)
ret["comment"] = f"invalid dataset name: {name}"

if not __salt__["zfs.exists"](name, **{"type": "filesystem,volume"}):
ret["comment"] = "dataset {} does not exist".format(name)
ret["comment"] = f"dataset {name} does not exist"
ret["result"] = False

## NOTE: prefix must be 4 or longer
if not prefix or len(prefix) < 4:
ret["comment"] = "prefix ({}) must be at least 4 long".format(prefix)
ret["comment"] = f"prefix ({prefix}) must be at least 4 long"
ret["result"] = False

## NOTE: validate schedule
Expand Down Expand Up @@ -1015,7 +1016,7 @@ def scheduled_snapshot(name, prefix, recursive=True, schedule=None):

if not mod_res["snapshotted"]:
ret["result"] = False
ret["comment"] = "error creating snapshot ({})".format(snapshot_name)
ret["comment"] = f"error creating snapshot ({snapshot_name})"
else:
## NOTE: create holds (if we have a snapshot)
for hold in snapshot_holds:
Expand Down
Loading

0 comments on commit 44edd0a

Please sign in to comment.