|
| 1 | +from __future__ import print_function |
| 2 | +from __future__ import unicode_literals |
| 3 | +from __future__ import division |
| 4 | +from __future__ import absolute_import |
| 5 | + |
| 6 | +from upcloud_api import Network, Interface, Router |
| 7 | + |
| 8 | + |
| 9 | +class NetworkManager(object): |
| 10 | + """ |
| 11 | + Functions for managing networks. Intended to be used as a mixin for CloudManager. |
| 12 | + """ |
| 13 | + |
| 14 | + def get_networks(self, zone=None): |
| 15 | + """ |
| 16 | + Get a list of all networks. |
| 17 | + Zone can be passed to return networks in a specific zone |
| 18 | + """ |
| 19 | + url = '/network/?zone={0}'.format(zone) if zone else '/network/' |
| 20 | + res = self.get_request(url) |
| 21 | + return [Network(**network) for network in res['networks']['network']] |
| 22 | + |
| 23 | + def get_network(self, uuid): |
| 24 | + """ |
| 25 | + Retrieves the details of a specific network. |
| 26 | + """ |
| 27 | + url = '/network/{0}'.format(uuid) |
| 28 | + res = self.get_request(url) |
| 29 | + return Network(**res['network']) |
| 30 | + |
| 31 | + def create_network(self, name, zone, address, dhcp, family, router=None, dhcp_default_route=None, dhcp_dns=None, dhcp_bootfile_url=None, gateway=None): |
| 32 | + """ |
| 33 | + Creates a new SDN private network that cloud servers from the same zone can be attached to. |
| 34 | + """ |
| 35 | + url = '/network/' |
| 36 | + body = { |
| 37 | + 'network': { |
| 38 | + 'name': name, |
| 39 | + 'zone': zone, |
| 40 | + 'ip_networks': { |
| 41 | + 'ip_network': { |
| 42 | + 'address': address, |
| 43 | + 'dhcp': dhcp, |
| 44 | + 'family': family |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + if router: |
| 51 | + body['network']['router'] = router |
| 52 | + if dhcp_default_route: |
| 53 | + body['network']['ip_networks']['network']['dhcp_default_route'] = dhcp_default_route |
| 54 | + if dhcp_dns: |
| 55 | + body['network']['ip_networks']['network']['dhcp_dns'] = dhcp_dns |
| 56 | + if dhcp_bootfile_url: |
| 57 | + body['network']['ip_networks']['network']['dhcp_bootfile_url'] = dhcp_bootfile_url |
| 58 | + if gateway: |
| 59 | + body['network']['ip_networks']['network']['gateway'] = gateway |
| 60 | + res = self.post_request(url, body) |
| 61 | + return Network(**res['network']) |
| 62 | + |
| 63 | + def modify_network(self, uuid, dhcp, family, name=None, router=None, dhcp_default_route=None, dhcp_dns=None, dhcp_bootfile_url=None, gateway=None): |
| 64 | + """ |
| 65 | + Modifies the details of a specific SDN private network. The Utility and public networks cannot be modified. |
| 66 | + """ |
| 67 | + url = '/network/{}'.format(uuid) |
| 68 | + body = { |
| 69 | + 'network': { |
| 70 | + 'ip_networks': { |
| 71 | + 'ip_network': {'family': family} |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + if name: |
| 76 | + body['network']['name'] = name |
| 77 | + if dhcp: |
| 78 | + body['network']['ip_networks']['ip_network']['dhcp'] = dhcp |
| 79 | + if router: |
| 80 | + body['network']['router'] = router |
| 81 | + if dhcp_default_route: |
| 82 | + body['network']['ip_networks']['ip_network']['dhcp_default_route'] = dhcp_default_route |
| 83 | + if dhcp_dns: |
| 84 | + body['network']['ip_networks']['ip_network']['dhcp_dns'] = dhcp_dns |
| 85 | + if dhcp_bootfile_url: |
| 86 | + body['network']['ip_networks']['ip_network']['dhcp_bootfile_url'] = dhcp_bootfile_url |
| 87 | + if gateway: |
| 88 | + body['network']['ip_networks']['ip_network']['gateway'] = gateway |
| 89 | + res = self.put_request(url, body) |
| 90 | + return Network(**res['network']) |
| 91 | + |
| 92 | + def delete_network(self, uuid): |
| 93 | + """ |
| 94 | + Deletes an SDN private network. All attached cloud servers must first be detached before SDN private networks can be deleted. |
| 95 | + """ |
| 96 | + url = '/network/{0}'.format(uuid) |
| 97 | + res = self.delete_request(url) |
| 98 | + return res |
| 99 | + |
| 100 | + def get_server_networks(self, server): |
| 101 | + """ |
| 102 | + List all networks the specific cloud server is connected to. |
| 103 | + """ |
| 104 | + url = '/server/{0}/networking'.format(server) |
| 105 | + res = self.get_request(url) |
| 106 | + return [Interface(**interface) for interface in res['networking']['interfaces']['interface']] |
| 107 | + |
| 108 | + def create_network_interface(self, server, network, type, ip_addresses, index=None, source_ip_filtering=None, bootable=None): |
| 109 | + """ |
| 110 | + Creates a new network interface on the specific cloud server and attaches the specified SDN private network to the new interface. |
| 111 | + """ |
| 112 | + url = '/server/{0}/networking/interface'.format(server) |
| 113 | + body = {'interface': {'network': network, 'type': type, 'ip_addresses': {'ip_address': ip_addresses}}} |
| 114 | + if index: |
| 115 | + body['interface']['index'] = index |
| 116 | + if source_ip_filtering: |
| 117 | + body['interface']['source_ip_filtering'] = source_ip_filtering |
| 118 | + if bootable: |
| 119 | + body['interface']['bootable'] = bootable |
| 120 | + res = self.post_request(url, body) |
| 121 | + return Interface(**res['interface']) |
| 122 | + |
| 123 | + def modify_network_interface(self, server, index_in_path, index_in_body=None, ip_addresses=None, source_ip_filtering=None, bootable=None): |
| 124 | + """ |
| 125 | + Modifies the network interface at the selected index on the specific cloud server. |
| 126 | + """ |
| 127 | + url = '/server/{0}/networking/interface/{0}'.format(server, index_in_path) |
| 128 | + body = {'interface': {}} |
| 129 | + if index_in_body: |
| 130 | + body['interface']['index'] = index_in_body |
| 131 | + if ip_addresses: |
| 132 | + body['interface']['ip_addresses']['ip_address'] = ip_addresses |
| 133 | + if source_ip_filtering: |
| 134 | + body['interface']['source_ip_filtering'] = source_ip_filtering |
| 135 | + if bootable: |
| 136 | + body['interface']['bootable'] = bootable |
| 137 | + res = self.post_request(url, body) |
| 138 | + return Interface(**res['interface']) |
| 139 | + |
| 140 | + def delete_network_interface(self, server, index): |
| 141 | + """ |
| 142 | + Detaches an SDN private network from a cloud server by deleting the network interface at the selected index on the specific cloud server. |
| 143 | + """ |
| 144 | + url = '/server/{0}/networking/interface/{0}'.format(server, index_in_path) |
| 145 | + res = self.delete_request(url) |
| 146 | + return res |
| 147 | + |
| 148 | + def get_routers(self): |
| 149 | + """ |
| 150 | + Returns a list of all available routers associated with the current account. |
| 151 | + """ |
| 152 | + url = '/router' |
| 153 | + res = self.get_request(url) |
| 154 | + return [Router(**router) for router in res['routers']['router']] |
| 155 | + |
| 156 | + def get_router(self, uuid): |
| 157 | + """ |
| 158 | + Returns detailed information about a specific router. |
| 159 | + """ |
| 160 | + url = '/router/{0}'.format(uuid) |
| 161 | + res = self.get_request(url) |
| 162 | + return Router(**res['router']) |
| 163 | + |
| 164 | + def create_router(self, name): |
| 165 | + """ |
| 166 | + Creates a new router. |
| 167 | + """ |
| 168 | + url = '/router' |
| 169 | + body = {'router': {'name': name}} |
| 170 | + res = self.post_request(url, body) |
| 171 | + return Router(**res['router']) |
| 172 | + |
| 173 | + def modify_router(self, uuid, name): |
| 174 | + """ |
| 175 | + Modify an existing router. |
| 176 | + """ |
| 177 | + url = '/router/{0}'.format(uuid) |
| 178 | + body = {'router': {'name': name}} |
| 179 | + res = self.put_request(url, body) |
| 180 | + return Router(**res['router']) |
| 181 | + |
| 182 | + def delete_router(self, uuid): |
| 183 | + """ |
| 184 | + Delete an existing router. |
| 185 | + """ |
| 186 | + url = '/router/{0}'.format(uuid) |
| 187 | + res = self.delete_request(url) |
| 188 | + return res |
0 commit comments