Skip to content

Commit

Permalink
pylxd/models: Check created_at API responses.
Browse files Browse the repository at this point in the history
This creates __parse_snapshot_json to avoid code repetition.

Signed-off-by: hamistao <[email protected]>
  • Loading branch information
hamistao committed Jul 8, 2024
1 parent 60f89a3 commit cf77a1b
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions pylxd/models/storage_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,36 @@ def api(self):
"""
return self.volume.api[self._endpoint][self.name]

@classmethod
def __parse_snaspshot_json(cls, volume, snapshot_json):
snapshot_object = cls(volume.client, volume=volume, **snapshot_json)

# Snapshot names are namespaced in LXD, as volume-name/snapshot-name.
# We hide that implementation detail.
snapshot_object.name = snapshot_object.name.split("/")[-1]

# Getting '0001-01-01T00:00:00Z' means that the volume does not have an expiration time set.
if snapshot_object.expires_at == "0001-01-01T00:00:00Z":
snapshot_object.expires_at = None

# If reponse does not include expires_at sync the object to get that attribute.
if not snapshot_json.get("expires_at"):
snapshot_object.sync()

# Overriding default value for when created_at is not present on the response due to using LXD 4.0.
# Also having created_at as "0001-01-01T00:00:00Z" means this information is not available.
# This could be because the information was lost or the snapshot was created using an older LXD version.
if (
not snapshot_json.get("created_at")
or snapshot_json["created_at"] == "0001-01-01T00:00:00Z"
):
snapshot_object.created_at = None

# This field is may empty so derive it from its volume.
snapshot_object.content_type = volume.content_type

return snapshot_object

@classmethod
def get(cls, volume, name):
"""Get a :class:`pylxd.models.StorageVolumeSnapshot` by its name.
Expand All @@ -692,19 +722,7 @@ def get(cls, volume, name):

response = volume.api.snapshots[name].get()

snapshot = cls(volume.client, volume=volume, **response.json()["metadata"])

# Getting '0001-01-01T00:00:00Z' means that the volume does not have an expiration time set.
if response.json()["metadata"]["expires_at"] == "0001-01-01T00:00:00Z":
snapshot.expires_at = None

# This field is normally empty so derive it from its volume.
snapshot.content_type = volume.content_type

# Snapshot names are namespaced in LXD, as volume-name/snapshot-name.
# We hide that implementation detail.
snapshot.name = snapshot.name.split("/")[-1]
return snapshot
return cls.__parse_snaspshot_json(volume, response.json()["metadata"])

@classmethod
def all(cls, volume, use_recursion=False):
Expand All @@ -730,19 +748,10 @@ def all(cls, volume, use_recursion=False):

if use_recursion:
# Using recursion so returning list of StorageVolumeSnapshot objects.
def parse_response_item(snapshot_json):
snapshot_object = cls(volume.client, volume=volume, **snapshot_json)
snapshot_object.content_type = volume.content_type
# Snapshot names are namespaced in LXD, as volume-name/snapshot-name.
# We hide that implementation detail.
snapshot_object.name = snapshot_object.name.split("/")[-1]

return snapshot_object

response = volume.api.snapshots.get(params={"recursion": 1})

return [
parse_response_item(snapshot)
cls.__parse_snaspshot_json(volume, snapshot)
for snapshot in response.json()["metadata"]
]

Expand Down

0 comments on commit cf77a1b

Please sign in to comment.