Exemplo n.º 1
0
    def _handle_config_nodes(self, config: NodeBalancerConfig, new_nodes: List[dict]) -> None:
        """Updates the NodeBalancer nodes defined in new_nodes within the given config"""

        node_map = {}
        nodes = config.nodes

        for node in nodes:
            node._api_get()
            node_map[node.label] = node

        for node in new_nodes:
            node_label = node.get('label')

            if node_label in node_map:
                node_match, remote_node_match = dict_select_matching(
                    filter_null_values(node), node_map[node_label]._raw_json)

                if node_match == remote_node_match:
                    del node_map[node_label]
                    continue

                self._delete_node_register(node_map[node_label])

            self._create_node_register(config, node)

        for node in node_map.values():
            self._delete_node_register(node)
Exemplo n.º 2
0
    def __handle_configs(self, **kwargs):
        """Updates the configs defined in kwargs under the instance's NodeBalancer"""

        configs = kwargs.get('configs')
        configs = configs or []

        remote_configs = copy.deepcopy(self._node_balancer.configs)

        # Remove unspecified configs
        for remote_config in remote_configs:
            should_delete = True

            for config in configs:
                config_match, remote_config_match = dict_select_matching(
                    config, remote_config._raw_json)

                if config_match == remote_config_match:
                    should_delete = False
                    break

            if should_delete:
                self.register_action('Deleted Config {}'.format(remote_config.id))
                remote_config.delete()

        # Create specified configs that do not exist
        for config in configs:
            config_current = None
            exists = False

            for remote_config in remote_configs:
                config_match, remote_config_match = dict_select_matching(
                    config, remote_config._raw_json)

                if config_match == remote_config_match:
                    exists = True
                    config_current = remote_config

            if not exists:
                config_current = self.create_config(self._node_balancer, **config)
                self.register_action('Created Config {}'.format(config_current.id))

            self.__handle_config_nodes(config_current, config.get('nodes'))

        self.results['configs'].extend(paginated_list_to_json(self._node_balancer.configs))
Exemplo n.º 3
0
    def __handle_config_nodes(self, config, new_nodes):
        """Updates the NodeBalancer nodes defined in new_nodes within the given config"""

        new_nodes = new_nodes or []

        nodes = config.nodes

        # We have to do this to fix lazy loading
        for node in nodes:
            node._api_get()

        # Remove unspecified nodes
        for remote_node in nodes:
            should_delete = True

            for node in new_nodes:
                node_match, remote_node_match = dict_select_matching(node, remote_node._raw_json)

                if node_match == remote_node_match:
                    should_delete = False
                    break

            if should_delete:
                self.register_action('Deleted Node {}'.format(remote_node.label))
                remote_node.delete()

        # Create specified nodes that do not exist
        for node in new_nodes:
            exists = False
            current_node = None

            for remote_node in nodes:
                node_match, remote_node_match = dict_select_matching(node, remote_node._raw_json)

                if node_match == remote_node_match:
                    exists = True
                    current_node = remote_node

            if not exists:
                current_node = self.create_node(config, **node)
                self.register_action('Created Node {}'.format(current_node.label))

            self.results['nodes'].append(current_node._raw_json)
Exemplo n.º 4
0
    def _check_config_exists(target: Set[NodeBalancerConfig], config: dict) \
            -> Tuple[bool, Optional[NodeBalancerConfig]]:
        """Returns whether a config exists in the target set"""

        for remote_config in target:
            config_match, remote_config_match = \
                dict_select_matching(filter_null_values(config), remote_config._raw_json)

            if config_match == remote_config_match:
                return True, remote_config

        return False, None