Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created the ability to tag partitions #240

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions maas/client/viscera/partitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

__all__ = ["Partition", "Partitions"]

from typing import Iterable
from . import check, Object, ObjectField, ObjectFieldRelated, ObjectSet, ObjectType
from .nodes import Node
from .block_devices import BlockDevice
Expand Down Expand Up @@ -53,6 +54,7 @@ class Partition(Object, metaclass=PartitionType):
used_for = ObjectField.Checked("used_for", check(str), readonly=True)

filesystem = ObjectFieldRelated("filesystem", "Filesystem", readonly=True)
tags = ObjectField.Checked("tags", check(list), check(list))

def __repr__(self):
return super(Partition, self).__repr__(fields={"path", "size"})
Expand Down Expand Up @@ -108,6 +110,25 @@ async def umount(self):
id=self.id,
)
)
async def save(self):
"""Save this partition."""
old_tags = list(self._orig_data["tags"])
new_tags = list(self.tags)
self._changed_data.pop("tags", None)
for tag_name in new_tags:
if tag_name not in old_tags:
await self._handler.add_tag(
system_id=self.block_device.node.system_id, device_id=self.block_device.id, id=self.id, tag=tag_name
)
else:
old_tags.remove(tag_name)
for tag_name in old_tags:
await self._handler.remove_tag(
system_id=self.block_device.node.system_id, device_id=self.block_device.id, id=self.id, tag=tag_name
)
self._orig_data["tags"] = new_tags
self._data["tags"] = list(new_tags)



class PartitionsType(ObjectType):
Expand All @@ -132,14 +153,22 @@ async def read(cls, node, block_device):
data = await cls._handler.read(system_id=system_id, device_id=block_device)
return cls(cls._object(item) for item in data)

async def create(cls, block_device: BlockDevice, size: int):
async def create(
cls,
block_device: BlockDevice,
*,
size: int,
tags: Iterable[str] = None
):
"""
Create a partition on a block device.

:param block_device: BlockDevice to create the paritition on.
:type block_device: `BlockDevice`
:param size: The size of the partition in bytes.
:type size: `int`
:param tags: List of tags to add to the partition.
:type tags: sequence of `str`
"""
params = {}
if isinstance(block_device, BlockDevice):
Expand All @@ -154,7 +183,11 @@ async def create(cls, block_device: BlockDevice, size: int):
if not size:
raise ValueError("size must be provided and greater than zero.")
params["size"] = size
return cls._object(await cls._handler.create(**params))
partition = cls._object(await cls._handler.create(**params))
if tags:
partition.tags = tags
await partition.save()
return partition


class Partitions(ObjectSet, metaclass=PartitionsType):
Expand Down