|
1 | 1 | # -*- coding: utf-8 -*- |
2 | | -from hcloud.core.client import BoundModelBase |
| 2 | +from hcloud.actions.client import BoundAction |
| 3 | +from hcloud.core.client import BoundModelBase, ClientEntityBase |
3 | 4 |
|
4 | 5 | from hcloud.images.domain import Image |
5 | 6 |
|
6 | 7 |
|
7 | 8 | class BoundImage(BoundModelBase): |
8 | 9 | model = Image |
| 10 | + |
| 11 | + def __init__(self, client, data): |
| 12 | + from hcloud.servers.client import BoundServer |
| 13 | + created_from = data.get("created_from") |
| 14 | + if created_from is not None: |
| 15 | + data['created_from'] = BoundServer(client._client.servers, created_from, complete=False) |
| 16 | + bound_to = data.get("bound_to") |
| 17 | + if bound_to is not None: |
| 18 | + data['bound_to'] = BoundServer(client._client.servers, {"id": bound_to}, complete=False) |
| 19 | + |
| 20 | + super(BoundImage, self).__init__(client, data) |
| 21 | + |
| 22 | + def get_actions(self, sort=None): |
| 23 | + # type: (Optional[List[str]]) -> List[BoundAction] |
| 24 | + return self._client.get_actions(self, sort) |
| 25 | + |
| 26 | + def update(self, description=None, type=None, labels=None): |
| 27 | + # type: (Optional[str], Optional[Dict[str, str]]) -> BoundImage |
| 28 | + return self._client.update(self, description, type, labels) |
| 29 | + |
| 30 | + def delete(self): |
| 31 | + # type: () -> bool |
| 32 | + return self._client.delete(self) |
| 33 | + |
| 34 | + def change_protection(self, delete=None): |
| 35 | + # type: (Optional[bool]) -> BoundAction |
| 36 | + return self._client.change_protection(self, delete) |
| 37 | + |
| 38 | + |
| 39 | +class ImagesClient(ClientEntityBase): |
| 40 | + |
| 41 | + def get_actions(self, image, sort=None): |
| 42 | + # type: (Image, Optional[List[str]]) -> List[BoundAction] |
| 43 | + params = {} |
| 44 | + if sort is not None: |
| 45 | + params.update({"sort": sort}) |
| 46 | + response = self._client.request(url="/images/{image_id}/actions".format(image_id=image.id), method="GET", params=params) |
| 47 | + return [BoundAction(self._client.actions, action_data) for action_data in response['actions']] |
| 48 | + |
| 49 | + def get_by_id(self, id): |
| 50 | + # type: (int) -> BoundImage |
| 51 | + response = self._client.request(url="/images/{image_id}".format(image_id=id), method="GET") |
| 52 | + return BoundImage(self, response['image']) |
| 53 | + |
| 54 | + def get_all(self, name=None, label_selector=None, bound_to=None, type=None, sort=None): |
| 55 | + # type: (Optional[str], Optional[str], Optional[List[str]], Optional[List[str]],Optional[List[str]]) -> List[BoundImage] |
| 56 | + params = {} |
| 57 | + if name: |
| 58 | + params['name'] = name |
| 59 | + if label_selector: |
| 60 | + params['label_selector'] = label_selector |
| 61 | + if bound_to: |
| 62 | + params['bound_to'] = bound_to |
| 63 | + if type: |
| 64 | + params['type'] = type |
| 65 | + if sort: |
| 66 | + params['sort'] = sort |
| 67 | + |
| 68 | + response = self._client.request(url="/images", method="GET", params=params) |
| 69 | + return [BoundImage(self, image_data) for image_data in response['images']] |
| 70 | + |
| 71 | + def update(self, image, description=None, type=None, labels=None): |
| 72 | + # type:(Image, Optional[str], Optional[str], Optional[Dict[str, str]]) -> BoundImage |
| 73 | + data = {} |
| 74 | + if description is not None: |
| 75 | + data.update({"description": description}) |
| 76 | + if type is not None: |
| 77 | + data.update({"type": type}) |
| 78 | + if labels is not None: |
| 79 | + data.update({"labels": labels}) |
| 80 | + response = self._client.request(url="/images/{image_id}".format(image_id=image.id), method="PUT", json=data) |
| 81 | + return BoundImage(self, response['image']) |
| 82 | + |
| 83 | + def delete(self, image): |
| 84 | + # type: (Image) -> bool |
| 85 | + self._client.request(url="/images/{image_id}".format(image_id=image.id), method="DELETE") |
| 86 | + # Return allays true, because the API does not return an action for it. When an error occurs a HcloudAPIException will be raised |
| 87 | + return True |
| 88 | + |
| 89 | + def change_protection(self, image, delete=None): |
| 90 | + # type: (Image, Optional[bool], Optional[bool]) -> BoundAction |
| 91 | + data = {} |
| 92 | + if delete is not None: |
| 93 | + data.update({"delete": delete}) |
| 94 | + |
| 95 | + response = self._client.request(url="/images/{image_id}/actions/change_protection".format(image_id=image.id), method="POST", json=data) |
| 96 | + return BoundAction(self._client.actions, response['action']) |
0 commit comments