Esempio n. 1
0
    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)
Esempio n. 2
0
    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()
Esempio n. 4
0
 def network_route(self):
     return NetworkRoute(destination="10.100.1.0/24", gateway="10.0.1.1")