|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +from linode_api4.groups import Group |
| 4 | +from linode_api4.objects import ( |
| 5 | + ImageShareGroup, |
| 6 | + ImageShareGroupImagesToAdd, |
| 7 | + ImageShareGroupToken, |
| 8 | +) |
| 9 | +from linode_api4.objects.base import _flatten_request_body_recursive |
| 10 | +from linode_api4.util import drop_null_keys |
| 11 | + |
| 12 | + |
| 13 | +class ImageShareGroupAPIGroup(Group): |
| 14 | + """ |
| 15 | + Collections related to Private Image Sharing. |
| 16 | +
|
| 17 | + NOTE: Private Image Sharing features are in beta and may not be generally available. |
| 18 | + """ |
| 19 | + |
| 20 | + def __call__(self, *filters): |
| 21 | + """ |
| 22 | + Retrieves a list of Image Share Groups created by the user (producer). |
| 23 | + You can filter this query to retrieve only Image Share Groups |
| 24 | + relevant to a specific query, for example:: |
| 25 | +
|
| 26 | + filtered_share_groups = client.sharegroups( |
| 27 | + ImageShareGroup.label == "my-label") |
| 28 | +
|
| 29 | + API Documentation: https://techdocs.akamai.com/linode-api/reference/get-sharegroups |
| 30 | +
|
| 31 | + :param filters: Any number of filters to apply to this query. |
| 32 | + See :doc:`Filtering Collections</linode_api4/objects/filtering>` |
| 33 | + for more details on filtering. |
| 34 | +
|
| 35 | + :returns: A list of Image Share Groups. |
| 36 | + :rtype: PaginatedList of ImageShareGroup |
| 37 | + """ |
| 38 | + return self.client._get_and_filter(ImageShareGroup, *filters) |
| 39 | + |
| 40 | + def sharegroups_by_image_id(self, image_id: str): |
| 41 | + """ |
| 42 | + Retrieves a list of Image Share Groups that share a specific Private Image. |
| 43 | +
|
| 44 | + API Documentation: https://techdocs.akamai.com/linode-api/reference/get-images-sharegroups-image |
| 45 | +
|
| 46 | + :param image_id: The ID of the Image to query for. |
| 47 | + :type image_id: str |
| 48 | +
|
| 49 | + :returns: A list of Image Share Groups sharing the specified Image. |
| 50 | + :rtype: PaginatedList of ImageShareGroup |
| 51 | + """ |
| 52 | + return self.client._get_and_filter( |
| 53 | + ImageShareGroup, endpoint="/images/{}/sharegroups".format(image_id) |
| 54 | + ) |
| 55 | + |
| 56 | + def tokens(self, *filters): |
| 57 | + """ |
| 58 | + Retrieves a list of Image Share Group Tokens created by the user (consumer). |
| 59 | + You can filter this query to retrieve only Image Share Group Tokens |
| 60 | + relevant to a specific query, for example:: |
| 61 | +
|
| 62 | + filtered_share_group_tokens = client.sharegroups.tokens( |
| 63 | + ImageShareGroupToken.label == "my-label") |
| 64 | +
|
| 65 | + API Documentation: https://techdocs.akamai.com/linode-api/reference/get-user-tokens |
| 66 | +
|
| 67 | + :param filters: Any number of filters to apply to this query. |
| 68 | + See :doc:`Filtering Collections</linode_api4/objects/filtering>` |
| 69 | + for more details on filtering. |
| 70 | +
|
| 71 | + :returns: A list of Image Share Group Tokens. |
| 72 | + :rtype: PaginatedList of ImageShareGroupToken |
| 73 | + """ |
| 74 | + return self.client._get_and_filter(ImageShareGroupToken, *filters) |
| 75 | + |
| 76 | + def create_sharegroup( |
| 77 | + self, |
| 78 | + label: Optional[str] = None, |
| 79 | + description: Optional[str] = None, |
| 80 | + images: Optional[ImageShareGroupImagesToAdd] = None, |
| 81 | + ): |
| 82 | + """ |
| 83 | + Creates a new Image Share Group. |
| 84 | +
|
| 85 | + API Documentation: https://techdocs.akamai.com/linode-api/reference/post-sharegroups |
| 86 | +
|
| 87 | + :param label: The label for the resulting Image Share Group. |
| 88 | + :type label: str |
| 89 | + :param description: The description for the new Image Share Group. |
| 90 | + :type description: str |
| 91 | + :param images: A list of Images to share in the new Image Share Group, formatted in JSON. |
| 92 | + :type images: Optional[ImageShareGroupImagesToAdd] |
| 93 | +
|
| 94 | + :returns: The new Image Share Group. |
| 95 | + :rtype: ImageShareGroup |
| 96 | + """ |
| 97 | + params = { |
| 98 | + "label": label, |
| 99 | + "description": description, |
| 100 | + } |
| 101 | + |
| 102 | + if images: |
| 103 | + params["images"] = images |
| 104 | + |
| 105 | + result = self.client.post( |
| 106 | + "/images/sharegroups", |
| 107 | + data=_flatten_request_body_recursive(drop_null_keys(params)), |
| 108 | + ) |
| 109 | + |
| 110 | + return ImageShareGroup(self.client, result["id"], result) |
| 111 | + |
| 112 | + def create_token( |
| 113 | + self, valid_for_sharegroup_uuid: str, label: Optional[str] = None |
| 114 | + ): |
| 115 | + """ |
| 116 | + Creates a new Image Share Group Token and returns the token value. |
| 117 | +
|
| 118 | + API Documentation: https://techdocs.akamai.com/linode-api/reference/post-sharegroup-tokens |
| 119 | +
|
| 120 | + :param valid_for_sharegroup_uuid: The UUID of the Image Share Group that this token will be valid for. |
| 121 | + :type valid_for_sharegroup_uuid: Optional[str] |
| 122 | + :param label: The label for the resulting Image Share Group Token. |
| 123 | + :type label: str |
| 124 | +
|
| 125 | + :returns: The new Image Share Group Token object and the one-time use token itself. |
| 126 | + :rtype: (ImageShareGroupToken, str) |
| 127 | + """ |
| 128 | + params = {"valid_for_sharegroup_uuid": valid_for_sharegroup_uuid} |
| 129 | + |
| 130 | + if label: |
| 131 | + params["label"] = label |
| 132 | + |
| 133 | + result = self.client.post( |
| 134 | + "/images/sharegroups/tokens", |
| 135 | + data=_flatten_request_body_recursive(drop_null_keys(params)), |
| 136 | + ) |
| 137 | + |
| 138 | + token_value = result.pop("token", None) |
| 139 | + token_obj = ImageShareGroupToken( |
| 140 | + self.client, result["token_uuid"], result |
| 141 | + ) |
| 142 | + return token_obj, token_value |
0 commit comments