def realise_modify_routes(self, allow_recreate: bool) -> None: defn: NetworkOptions = self.get_defn().config # workaround to get hashable types # TODO patch nixops StateDict getter for dicts (if appropriate) prev_routes = { RouteOptions(**x) for x in self._state.get("routes", ()) } final_routes = set(defn.routes) for route in prev_routes - final_routes: self.logger.log(f"deleting route for {route.gateway}") self.wait_on_action(self.get_client().networks.delete_route( network=Network(self.resource_id), route=NetworkRoute(route.destination, route.gateway), )) for route in final_routes - prev_routes: self.logger.log(f"adding route to {route.gateway}") self.wait_on_action(self.get_client().networks.add_route( network=Network(self.resource_id), route=NetworkRoute(route.destination, route.gateway), )) # Why must we insert as a list when the original type (tuple) is json encodable? # StateDict setter accepts inserting lists and dicts for legacy reasons. # TODO patch nixops to encode tuples (in addition, for backwards compat) with self.depl._db: self._state["routes"] = list(defn.routes)
def test_delete_route(self, hetzner_client, bound_network, generic_action): hetzner_client.request.return_value = generic_action route = NetworkRoute(destination="10.100.1.0/24", gateway="10.0.1.1") action = bound_network.delete_route(route) hetzner_client.request.assert_called_with(url="/networks/14/actions/delete_route", method="POST", json={"destination": "10.100.1.0/24", "gateway": "10.0.1.1"}) assert action.id == 1 assert action.progress == 0
def _create_route(self): route = NetworkRoute(destination=self.module.params.get("destination"), gateway=self.module.params.get('gateway')) if not self.module.check_mode: try: self.hcloud_network.add_route( route=route).wait_until_finished() except Exception as e: self.module.fail_json(msg=e.message) self._mark_as_changed() self._get_network() self._get_route()
def __init__(self, client, data, complete=True): subnets = data.get("subnets", []) if subnets is not None: subnets = [NetworkSubnet.from_dict(subnet) for subnet in subnets] data['subnets'] = subnets routes = data.get("routes", []) if routes is not None: routes = [NetworkRoute.from_dict(route) for route in routes] data['routes'] = routes from hcloud.servers.client import BoundServer servers = data.get("servers", []) if servers is not None: servers = [ BoundServer(client._client.servers, {"id": server}, complete=False) for server in servers ] data['servers'] = servers super(BoundNetwork, self).__init__(client, data, complete)
def network_route(self): return NetworkRoute(destination="10.100.1.0/24", gateway="10.0.1.1")