Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions linode_api4/groups/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .database import *
from .domain import *
from .image import *
from .image_share_group import *
from .linode import *
from .lke import *
from .lke_tier import *
Expand Down
142 changes: 142 additions & 0 deletions linode_api4/groups/image_share_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
from typing import Optional

from linode_api4.groups import Group
from linode_api4.objects import (
ImageShareGroup,
ImageShareGroupImagesToAdd,
ImageShareGroupToken,
)
from linode_api4.objects.base import _flatten_request_body_recursive
from linode_api4.util import drop_null_keys


class ImageShareGroupAPIGroup(Group):
"""
Collections related to Private Image Sharing.

NOTE: Private Image Sharing features are in beta and may not be generally available.
"""

def __call__(self, *filters):
"""
Retrieves a list of Image Share Groups created by the user (producer).
You can filter this query to retrieve only Image Share Groups
relevant to a specific query, for example::

filtered_share_groups = client.sharegroups(
ImageShareGroup.label == "my-label")

API Documentation: https://techdocs.akamai.com/linode-api/reference/get-sharegroups

:param filters: Any number of filters to apply to this query.
See :doc:`Filtering Collections</linode_api4/objects/filtering>`
for more details on filtering.

:returns: A list of Image Share Groups.
:rtype: PaginatedList of ImageShareGroup
"""
return self.client._get_and_filter(ImageShareGroup, *filters)

def sharegroups_by_image_id(self, image_id: str):
"""
Retrieves a list of Image Share Groups that share a specific Private Image.

API Documentation: https://techdocs.akamai.com/linode-api/reference/get-images-sharegroups-image

:param image_id: The ID of the Image to query for.
:type image_id: str

:returns: A list of Image Share Groups sharing the specified Image.
:rtype: PaginatedList of ImageShareGroup
"""
return self.client._get_and_filter(
ImageShareGroup, endpoint="/images/{}/sharegroups".format(image_id)
)

def tokens(self, *filters):
"""
Retrieves a list of Image Share Group Tokens created by the user (consumer).
You can filter this query to retrieve only Image Share Group Tokens
relevant to a specific query, for example::

filtered_share_group_tokens = client.sharegroups.tokens(
ImageShareGroupToken.label == "my-label")

API Documentation: https://techdocs.akamai.com/linode-api/reference/get-user-tokens

:param filters: Any number of filters to apply to this query.
See :doc:`Filtering Collections</linode_api4/objects/filtering>`
for more details on filtering.

:returns: A list of Image Share Group Tokens.
:rtype: PaginatedList of ImageShareGroupToken
"""
return self.client._get_and_filter(ImageShareGroupToken, *filters)

def create_sharegroup(
self,
label: Optional[str] = None,
description: Optional[str] = None,
images: Optional[ImageShareGroupImagesToAdd] = None,
):
"""
Creates a new Image Share Group.

API Documentation: https://techdocs.akamai.com/linode-api/reference/post-sharegroups

:param label: The label for the resulting Image Share Group.
:type label: str
:param description: The description for the new Image Share Group.
:type description: str
:param images: A list of Images to share in the new Image Share Group, formatted in JSON.
:type images: Optional[ImageShareGroupImagesToAdd]

:returns: The new Image Share Group.
:rtype: ImageShareGroup
"""
params = {
"label": label,
"description": description,
}

if images:
params["images"] = images

result = self.client.post(
"/images/sharegroups",
data=_flatten_request_body_recursive(drop_null_keys(params)),
)

return ImageShareGroup(self.client, result["id"], result)

def create_token(
self, valid_for_sharegroup_uuid: str, label: Optional[str] = None
):
"""
Creates a new Image Share Group Token and returns the token value.

API Documentation: https://techdocs.akamai.com/linode-api/reference/post-sharegroup-tokens

:param valid_for_sharegroup_uuid: The UUID of the Image Share Group that this token will be valid for.
:type valid_for_sharegroup_uuid: Optional[str]
:param label: The label for the resulting Image Share Group Token.
:type label: str

:returns: The new Image Share Group Token object and the one-time use token itself.
:rtype: (ImageShareGroupToken, str)
"""
params = {"valid_for_sharegroup_uuid": valid_for_sharegroup_uuid}

if label:
params["label"] = label

result = self.client.post(
"/images/sharegroups/tokens",
data=_flatten_request_body_recursive(drop_null_keys(params)),
)

token_value = result.pop("token", None)
token_obj = ImageShareGroupToken(
self.client, result["token_uuid"], result
)
return token_obj, token_value
4 changes: 1 addition & 3 deletions linode_api4/groups/linode.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
NetworkInterface,
_expand_placement_group_assignment,
)
from linode_api4.objects.linode_interfaces import (
LinodeInterfaceOptions,
)
from linode_api4.objects.linode_interfaces import LinodeInterfaceOptions
from linode_api4.util import drop_null_keys


Expand Down
4 changes: 4 additions & 0 deletions linode_api4/linode_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
DatabaseGroup,
DomainGroup,
ImageGroup,
ImageShareGroupAPIGroup,
LinodeGroup,
LKEGroup,
LongviewGroup,
Expand Down Expand Up @@ -440,6 +441,9 @@ def __init__(
#: Access methods related to Images - See :any:`ImageGroup` for more information.
self.images = ImageGroup(self)

#: Access methods related to Image Share Groups - See :any:`ImageShareGroupAPIGroup` for more information.
self.sharegroups = ImageShareGroupAPIGroup(self)

#: Access methods related to VPCs - See :any:`VPCGroup` for more information.
self.vpcs = VPCGroup(self)

Expand Down
1 change: 1 addition & 0 deletions linode_api4/objects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@
from .placement import *
from .monitor import *
from .monitor_api import *
from .image_share_group import *
34 changes: 34 additions & 0 deletions linode_api4/objects/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,38 @@ class ImageRegion(JSONObject):
status: Optional[ReplicationStatus] = None


@dataclass
class ImageSharingSharedWith(JSONObject):
"""
Data representing who an Image has been shared with.
"""

sharegroup_count: Optional[int] = None
sharegroup_list_url: Optional[str] = None


@dataclass
class ImageSharingSharedBy(JSONObject):
"""
Data representing who shared an Image.
"""

sharegroup_id: Optional[int] = None
sharegroup_uuid: Optional[str] = None
sharegroup_label: Optional[str] = None
source_image_id: Optional[str] = None


@dataclass
class ImageSharing(JSONObject):
"""
The Image Sharing status of an Image.
"""

shared_with: Optional[ImageSharingSharedWith] = None
shared_by: Optional[ImageSharingSharedBy] = None


class Image(Base):
"""
An Image is something a Linode Instance or Disk can be deployed from.
Expand All @@ -51,6 +83,7 @@ class Image(Base):
"updated": Property(is_datetime=True),
"type": Property(),
"is_public": Property(),
"is_shared": Property(),
"vendor": Property(),
"size": Property(),
"deprecated": Property(),
Expand All @@ -60,6 +93,7 @@ class Image(Base):
"tags": Property(mutable=True, unordered=True),
"total_size": Property(),
"regions": Property(json_object=ImageRegion, unordered=True),
"image_sharing": Property(json_object=ImageSharing),
}

def replicate(self, regions: Union[List[str], List[Region]]):
Expand Down
Loading
Loading