Exemple #1
0
    def exec_module(self, **kwargs: Any) -> Optional[dict]:
        """Entrypoint for instance info module"""

        instance = self._get_matching_instance()

        if instance is None:
            return self.fail('failed to get instance')

        self.results['instance'] = instance._raw_json
        self.results['configs'] = paginated_list_to_json(instance.configs)
        self.results['disks'] = paginated_list_to_json(instance.disks)

        return self.results
Exemple #2
0
    def _handle_absent(self) -> None:
        """Destroys the instance defined in kwargs"""
        label = self.module.params.get('label')

        self._instance = self._get_instance_by_label(label)

        if self._instance is not None:
            self.results['instance'] = self._instance._raw_json
            self.results['configs'] = paginated_list_to_json(
                self._instance.configs)
            self.results['disks'] = paginated_list_to_json(
                self._instance.disks)
            self.register_action('Deleted instance {0}'.format(label))
            self._instance.delete()
Exemple #3
0
    def _handle_configs(self) -> None:
        """Updates the configs defined in new_configs under this NodeBalancer"""

        new_configs = self.module.params.get('configs') or []
        remote_configs = set(self._node_balancer.configs)

        for config in new_configs:
            config_exists, remote_config = self._check_config_exists(remote_configs, config)

            if config_exists:
                if config.get('nodes') is not None:
                    self._handle_config_nodes(remote_config, config.get('nodes'))
                remote_configs.remove(remote_config)
                continue

            new_config = self._create_config_register(self._node_balancer, config)
            if config.get('nodes') is not None:
                self._handle_config_nodes(new_config, config.get('nodes'))

        # Remove remaining configs
        for config in remote_configs:
            self._delete_config_register(config)

        cast(list, self.results['configs']) \
            .extend(paginated_list_to_json(self._node_balancer.configs))
Exemple #4
0
    def _handle_domain_absent(self) -> None:
        domain_name: str = self.module.params.get('domain')

        self._domain = self._get_domain_by_name(domain_name)

        if self._domain is not None:
            self.results['domain'] = self._domain._raw_json
            self.results['records'] = paginated_list_to_json(
                self._domain.records)

            self._domain.delete()
            self.register_action('Deleted domain {0}'.format(domain_name))
Exemple #5
0
    def _handle_firewall_absent(self) -> None:
        """Destroys the Firewall"""
        label = self.module.params.get('label')

        self._firewall = self._get_firewall_by_label(label)

        if self._firewall is not None:
            self.results['firewall'] = self._firewall._raw_json
            self.results['devices'] = paginated_list_to_json(
                self._firewall.devices)
            self.register_action('Deleted Firewall {0}'.format(label))
            self._firewall.delete()
    def exec_module(self, **kwargs: dict) -> Optional[dict]:
        """Entrypoint for Firewall info module"""

        firewall = self._get_matching_firewall()

        if firewall is None:
            self.fail('failed to get firewall')

        self.results['firewall'] = firewall._raw_json
        self.results['devices'] = paginated_list_to_json(firewall.devices)

        return self.results
Exemple #7
0
    def exec_module(self, **kwargs: Any) -> Optional[dict]:
        """Entrypoint for domain info module"""

        domain = self._get_matching_domain(kwargs)

        if domain is None:
            self.fail('failed to get domain')

        self.results['domain'] = domain._raw_json
        self.results['records'] = paginated_list_to_json(domain.records)

        return self.results
Exemple #8
0
    def _handle_present(self) -> None:
        """Updates the instance defined in kwargs"""

        label = self.module.params.get('label')

        self._instance = self._get_instance_by_label(label)

        if self._instance is None:
            result = self._create_instance()
            self._instance = cast(Instance, result.get('instance'))
            self._root_pass = str(result.get('root_pass'))

            self.register_action('Created instance {0}'.format(label))
        else:
            self._update_instance()

        # Wait for Linode to not be busy if configs or disks need to be created
        # This eliminates the need for unnecessary polling
        disks = self.module.params.get('disks') or []
        configs = self.module.params.get('configs') or []

        if len(configs) > 0 or len(disks) > 0:
            self._wait_for_instance_status(
                self._instance, {'offline', 'running'},
                self.module.params.get('wait_timeout'))

            self._update_disks()
            self._update_configs()

        if self.module.params.get('booted') is not None:
            self._handle_instance_boot()

        self._instance._api_get()
        inst_result = self._instance._raw_json
        inst_result['root_pass'] = self._root_pass

        self.results['instance'] = inst_result
        self.results['configs'] = paginated_list_to_json(
            self._instance.configs)
        self.results['disks'] = paginated_list_to_json(self._instance.disks)
Exemple #9
0
    def exec_module(self, **kwargs: Any) -> Optional[dict]:
        """Entrypoint for domain record info module"""

        domain = self._get_domain_from_params()
        if domain is None:
            return self.fail('failed to get domain')

        records = self._get_records_from_params(domain)
        if records is None:
            return self.fail('failed to get records')

        self.results['record'] = paginated_list_to_json(records)

        return self.results
Exemple #10
0
    def _handle_firewall(self) -> None:
        """Updates the Firewall"""
        label = self.module.params.get('label')

        self._firewall = self._get_firewall_by_label(label)

        if self._firewall is None:
            self._firewall = self._create_firewall()
            self.register_action('Created Firewall {0}'.format(label))

        self._update_firewall()

        self._firewall._api_get()

        self.results['firewall'] = self._firewall._raw_json
        self.results['devices'] = paginated_list_to_json(
            self._firewall.devices)
Exemple #11
0
    def _handle_domain(self) -> None:
        params = self.module.params

        domain_name: str = params.get('domain')

        self._domain = self._get_domain_by_name(domain_name)

        # Create the domain if it does not already exist
        if self._domain is None:
            self._domain = self._create_domain()

        self._update_domain()

        # Force lazy-loading
        self._domain._api_get()

        self.results['domain'] = self._domain._raw_json
        self.results['records'] = paginated_list_to_json(self._domain.records)
    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))