Skip to content

Commit

Permalink
pylxd/models: Adapt JSON parsing for older LXD versions
Browse files Browse the repository at this point in the history
Older LXD versions' APIs don't include:
The `created_at` field,
the `expires_at` field on /1.0/storage-pools/<pool>/volumes/custom/<volume_name>/snapshots,
The `content_type` on /1.0/storage-pools/<pool>/volumes/custom/<volume_name>/snapshots/<snapshot_name>.

Signed-off-by: hamistao <[email protected]>
  • Loading branch information
hamistao committed Jul 8, 2024
1 parent bea1c58 commit 4cb5c31
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions pylxd/models/storage_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,37 @@ 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]

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

# 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

# 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 volume.client.has_api_extension("storage_volumes_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.
if not snapshot_object.content_type:
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,16 +723,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

# 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 @@ -727,19 +749,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 4cb5c31

Please sign in to comment.