|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from hcloud.actions.client import BoundAction |
| 3 | +from hcloud.core.client import BoundModelBase, ClientEntityBase |
| 4 | +from hcloud.core.domain import add_meta_to_result |
| 5 | + |
| 6 | +from hcloud.floating_ips.domain import FloatingIP, CreateFloatingIPResponse |
| 7 | +from hcloud.locations.client import BoundLocation |
| 8 | + |
| 9 | + |
| 10 | +class BoundFloatingIP(BoundModelBase): |
| 11 | + model = FloatingIP |
| 12 | + |
| 13 | + def __init__(self, client, data): |
| 14 | + from hcloud.servers.client import BoundServer |
| 15 | + server = data.get("server") |
| 16 | + if server is not None: |
| 17 | + data['server'] = BoundServer(client._client.servers, {"id": server}, complete=False) |
| 18 | + |
| 19 | + home_location = data.get("home_location") |
| 20 | + if home_location is not None: |
| 21 | + data['home_location'] = BoundLocation(client._client.locations, home_location) |
| 22 | + |
| 23 | + super(BoundFloatingIP, self).__init__(client, data) |
| 24 | + |
| 25 | + def get_actions_list(self, sort=None, page=None, per_page=None): |
| 26 | + # type: (Optional[List[str]], Optional[int], Optional[int]) -> PageResult[BoundAction, Meta] |
| 27 | + return self._client.get_actions_list(self, sort, page, per_page) |
| 28 | + |
| 29 | + def get_actions(self, sort=None): |
| 30 | + # type: (Optional[List[str]]) -> List[BoundAction] |
| 31 | + return self._client.get_actions(self, sort) |
| 32 | + |
| 33 | + def update(self, description=None, labels=None): |
| 34 | + # type: (Optional[str], Optional[Dict[str, str]]) -> BoundFloatingIP |
| 35 | + return self._client.update(self, description, labels) |
| 36 | + |
| 37 | + def delete(self): |
| 38 | + # type: () -> bool |
| 39 | + return self._client.delete(self) |
| 40 | + |
| 41 | + def change_protection(self, delete=None): |
| 42 | + # type: (Optional[bool]) -> BoundAction |
| 43 | + return self._client.change_protection(self, delete) |
| 44 | + |
| 45 | + def assign(self, server): |
| 46 | + # type: (Server) -> BoundAction |
| 47 | + return self._client.assign(self, server) |
| 48 | + |
| 49 | + def unassign(self): |
| 50 | + # type: () -> BoundAction |
| 51 | + return self._client.unassign(self) |
| 52 | + |
| 53 | + def change_dns_ptr(self, ip, dns_ptr): |
| 54 | + # type: (str, str) -> BoundAction |
| 55 | + return self._client.change_dns_ptr(self, ip, dns_ptr) |
| 56 | + |
| 57 | + |
| 58 | +class FloatingIPsClient(ClientEntityBase): |
| 59 | + results_list_attribute_name = 'floating_ips' |
| 60 | + |
| 61 | + def get_actions_list(self, |
| 62 | + floating_ip, # type: FloatingIP |
| 63 | + sort=None, # type: Optional[List[str]] |
| 64 | + page=None, # type: Optional[int] |
| 65 | + per_page=None # type: Optional[int] |
| 66 | + ): |
| 67 | + # type: (...) -> PageResults[List[BoundAction], Meta] |
| 68 | + params = {} |
| 69 | + if sort is not None: |
| 70 | + params["sort"] = sort |
| 71 | + if page is not None: |
| 72 | + params["page"] = page |
| 73 | + if per_page is not None: |
| 74 | + params["per_page"] = per_page |
| 75 | + response = self._client.request(url="/floating_ips/{floating_ip_id}/actions".format(floating_ip_id=floating_ip.id), method="GET", params=params) |
| 76 | + actions = [BoundAction(self._client.actions, action_data) for action_data in response['actions']] |
| 77 | + return add_meta_to_result(actions, response, 'actions') |
| 78 | + |
| 79 | + def get_actions(self, |
| 80 | + floating_ip, # type: FloatingIP |
| 81 | + sort=None, # type: Optional[List[str]] |
| 82 | + ): |
| 83 | + # type: (...) -> List[BoundAction] |
| 84 | + return super(FloatingIPsClient, self).get_actions(floating_ip, sort=sort) |
| 85 | + |
| 86 | + def get_by_id(self, id): |
| 87 | + # type: (int) -> BoundFloatingIP |
| 88 | + response = self._client.request(url="/floating_ips/{floating_ip_id}".format(floating_ip_id=id), method="GET") |
| 89 | + return BoundFloatingIP(self, response['floating_ip']) |
| 90 | + |
| 91 | + def get_list(self, |
| 92 | + label_selector=None, # type: Optional[str] |
| 93 | + page=None, # type: Optional[int] |
| 94 | + per_page=None # type: Optional[int] |
| 95 | + ): |
| 96 | + # type: (...) -> PageResults[List[BoundFloatingIP]] |
| 97 | + params = {} |
| 98 | + |
| 99 | + if label_selector: |
| 100 | + params['label_selector'] = label_selector |
| 101 | + if page: |
| 102 | + params['page'] = page |
| 103 | + if per_page: |
| 104 | + params['per_page'] = per_page |
| 105 | + |
| 106 | + response = self._client.request(url="/floating_ips", method="GET", params=params) |
| 107 | + floating_ips = [BoundFloatingIP(self, floating_ip_data) for floating_ip_data in response['floating_ips']] |
| 108 | + |
| 109 | + return self.add_meta_to_result(floating_ips, response) |
| 110 | + |
| 111 | + def get_all(self, label_selector=None): |
| 112 | + # type: (Optional[str]) -> List[BoundFloatingIP] |
| 113 | + return super(FloatingIPsClient, self).get_all(label_selector=label_selector) |
| 114 | + |
| 115 | + def create(self, |
| 116 | + type, # type: str |
| 117 | + description=None, # type: Optional[str] |
| 118 | + labels=None, # type: Optional[str] |
| 119 | + home_location=None, # type: Optional[Location] |
| 120 | + server=None, # type: Optional[Server] |
| 121 | + ): |
| 122 | + # type: (...) -> CreateFloatingIPResponse |
| 123 | + |
| 124 | + data = { |
| 125 | + 'type': type |
| 126 | + } |
| 127 | + if description is not None: |
| 128 | + data['description'] = description |
| 129 | + if labels is not None: |
| 130 | + data['labels'] = labels |
| 131 | + if home_location is not None: |
| 132 | + data['home_location'] = home_location.id_or_name |
| 133 | + if server is not None: |
| 134 | + data['server'] = server.id |
| 135 | + |
| 136 | + response = self._client.request(url="/floating_ips", json=data, method="POST") |
| 137 | + |
| 138 | + result = CreateFloatingIPResponse( |
| 139 | + floating_ip=BoundFloatingIP(self, response['floating_ip']), |
| 140 | + action=BoundAction(self._client.actions, response['action']) |
| 141 | + ) |
| 142 | + return result |
| 143 | + |
| 144 | + def update(self, floating_ip, description=None, labels=None): |
| 145 | + # type: (FloatingIP, Optional[str], Optional[Dict[str, str]]) -> BoundFloatingIP |
| 146 | + data = {} |
| 147 | + if description is not None: |
| 148 | + data['description'] = description |
| 149 | + if labels is not None: |
| 150 | + data['labels'] = labels |
| 151 | + |
| 152 | + response = self._client.request(url="/floating_ips/{floating_ip_id}".format(floating_ip_id=floating_ip.id), method="PUT", json=data) |
| 153 | + return BoundFloatingIP(self, response['floating_ip']) |
| 154 | + |
| 155 | + def delete(self, floating_ip): |
| 156 | + # type: (FloatingIP) -> bool |
| 157 | + self._client.request(url="/floating_ips/{floating_ip_id}".format(floating_ip_id=floating_ip.id), method="DELETE") |
| 158 | + # Return allays true, because the API does not return an action for it. When an error occurs a HcloudAPIException will be raised |
| 159 | + return True |
| 160 | + |
| 161 | + def change_protection(self, floating_ip, delete=None): |
| 162 | + # type: (FloatingIP, Optional[bool], Optional[bool]) -> BoundAction |
| 163 | + data = {} |
| 164 | + if delete is not None: |
| 165 | + data.update({"delete": delete}) |
| 166 | + |
| 167 | + response = self._client.request(url="/floating_ips/{floating_ip_id}/actions/change_protection".format(floating_ip_id=floating_ip.id), method="POST", json=data) |
| 168 | + return BoundAction(self._client.actions, response['action']) |
| 169 | + |
| 170 | + def assign(self, floating_ip, server): |
| 171 | + # type: (FloatingIP, Server) -> BoundAction |
| 172 | + response = self._client.request(url="/floating_ips/{floating_ip_id}/actions/assign".format(floating_ip_id=floating_ip.id), method="POST", json={"server": server.id}) |
| 173 | + return BoundAction(self._client.actions, response['action']) |
| 174 | + |
| 175 | + def unassign(self, floating_ip): |
| 176 | + # type: (FloatingIP) -> BoundAction |
| 177 | + response = self._client.request(url="/floating_ips/{floating_ip_id}/actions/unassign".format(floating_ip_id=floating_ip.id), method="POST") |
| 178 | + return BoundAction(self._client.actions, response['action']) |
| 179 | + |
| 180 | + def change_dns_ptr(self, floating_ip, ip, dns_ptr): |
| 181 | + # type: (FloatingIP, str, str) -> BoundAction |
| 182 | + response = self._client.request(url="/floating_ips/{floating_ip_id}/actions/change_dns_ptr".format(floating_ip_id=floating_ip.id), method="POST", json={"ip": ip, "dns_ptr": dns_ptr}) |
| 183 | + return BoundAction(self._client.actions, response['action']) |
0 commit comments