Ejemplo n.º 1
0
    def parse(self):
        '''
        Main method for initialising the parsing of the terraform plan json file

        :return: nothing
        '''
        self._version_check()
        self._identify_data_file()
        self._parse_resources()

        if self.file_type == 'plan':
            self._parse_variables()
            self._parse_configurations()

        cache_mounted_resources = self.cache.get(
            'mounted_resources') if self.parse_it else None
        cache_raw_resources = self.cache.get(
            'resources_raw') if self.parse_it else None
        cache_type_to_after_unknown_properties = self.cache.get(
            'type_to_after_unknown_properties') if self.parse_it else None

        if cache_mounted_resources and cache_raw_resources:
            # print('Read from cache, instead of re-mounting.')
            self.resources = cache_mounted_resources
            self.resources_raw = cache_raw_resources
            self.type_to_after_unknown_properties = cache_type_to_after_unknown_properties
        else:
            # print('Building cache for mounted resources at {}'.format(Defaults.cache_dir))
            self._mount_references()
            # metadata related calls
            self._add_action_status()
            self._add_module_call_source()

            self.resources = recursive_jsonify(self.resources)
            self.resources_raw = recursive_jsonify(self.resources_raw)
            self.type_to_after_unknown_properties = recursive_jsonify(
                self.type_to_after_unknown_properties)
            self.variables = recursive_jsonify(self.variables)
            self.data = recursive_jsonify(self.data)
            self.providers = recursive_jsonify(self.providers)

            if self.parse_it:
                self.cache.set('mounted_resources', self.resources)
                self.cache.set('resources_raw', self.resources_raw)
                self.cache.set('type_to_after_unknown_properties',
                               self.type_to_after_unknown_properties)

        self._distribute_providers()

        for _, resource in self.resources.items():
            self._expand_resource_tags(resource)
Ejemplo n.º 2
0
def i_have_name_section_configured(_step_obj,
                                   name,
                                   type_name='resource',
                                   _terraform_config=world):
    '''
    Finds given resource or variable by name and returns it. Skips the step (and further steps) if it is not found.

    :param _step_obj: Internal, step object for radish.
    :param name: String of the name of the resource_type or variable.
    :param type_name: String of the type, either resource(s) or variable(s)
    :param _terraform_config: Internal, terraform configuration.
    :return:
    '''
    assert (type_name in ['resource', 'resources',
                          'variable', 'variables',
                          'output', 'outputs',
                          'provider', 'providers',
                          'data', 'datas']), \
        '{} configuration type does not exist or not implemented yet. ' \
        'Use resource(s), provider(s), variable(s), output(s) or data(s) instead.'.format(type_name)

    if type_name.endswith('s'):
        type_name = type_name[:-1]

    # Process the tags
    _step_obj = look_for_bdd_tags(_step_obj)

    if name in ('a resource', 'any resource', 'resources'):
        _step_obj.context.type = type_name
        _step_obj.context.name = name
        _step_obj.context.stash = recursive_jsonify([
            obj for key, obj in
            _terraform_config.config.terraform.resources_raw.items()
        ])
        _step_obj.context.addresses = get_resource_address_list_from_stash(
            _step_obj.context.stash)
        _step_obj.context.property_name = type_name
        return True

    elif name in ('an output', 'any output', 'outputs'):
        _step_obj.context.type = 'output'
        _step_obj.context.name = name
        _step_obj.context.stash = recursive_jsonify([
            obj for key, obj in _terraform_config.config.terraform.
            configuration['outputs'].items()
        ])
        _step_obj.context.addresses = get_resource_address_list_from_stash(
            _terraform_config.config.terraform.configuration['outputs'])
        _step_obj.context.property_name = 'output'
        return True

    elif name in ('a variable', 'any variable', 'variables'):
        _step_obj.context.type = 'variable'
        _step_obj.context.name = name
        _step_obj.context.stash = recursive_jsonify([
            obj for key, obj in _terraform_config.config.terraform.
            configuration['variables'].items()
        ])
        _step_obj.context.addresses = 'variable'
        _step_obj.context.property_name = 'variable'
        return True

    elif name.startswith('resource that supports'):
        filter_property = re.match(r'resource that supports (.*)',
                                   name).group(1)

        resource_types_supports_tags = find_root_by_key(
            _terraform_config.config.terraform.resources_raw,
            filter_property,
            return_key='type')
        resource_list = []
        for resource_type in resource_types_supports_tags:
            # Issue-168: Mounted resources causes problem on recursive searching for resources that supports tags
            #            We are removing all mounted resources here for future steps, since we don't need them for
            #            tags checking.
            found_resources = remove_mounted_resources(
                _terraform_config.config.terraform.find_resources_by_type(
                    resource_type))
            found_resources = transform_asg_style_tags(found_resources)
            resource_list.extend(found_resources)

        if resource_list:
            _step_obj.context.type = type_name
            _step_obj.context.name = name
            _step_obj.context.stash = recursive_jsonify(resource_list)
            _step_obj.context.addresses = get_resource_address_list_from_stash(
                resource_list)
            _step_obj.context.property_name = type_name
            return True

    elif type_name == 'resource':
        name = convert_resource_type(name)
        resource_list = _terraform_config.config.terraform.find_resources_by_type(
            name)

        if resource_list:
            _step_obj.context.type = type_name
            _step_obj.context.name = name
            _step_obj.context.stash = recursive_jsonify(resource_list)
            _step_obj.context.addresses = get_resource_address_list_from_stash(
                resource_list)
            _step_obj.context.property_name = type_name
            return True

    elif type_name == 'variable':
        found_variable = _terraform_config.config.terraform.variables.get(
            name, None)

        if found_variable:
            _step_obj.context.type = type_name
            _step_obj.context.name = name
            _step_obj.context.stash = recursive_jsonify(found_variable)
            _step_obj.context.addresses = name
            _step_obj.context.property_name = type_name
            return True

    elif type_name == 'output':
        found_output = _terraform_config.config.terraform.outputs.get(
            name, None)

        if found_output:
            _step_obj.context.type = type_name
            _step_obj.context.name = name
            _step_obj.context.stash = recursive_jsonify(found_output)
            _step_obj.context.addresses = name
            _step_obj.context.property_name = type_name
            return True

    elif type_name == 'provider':
        found_provider = _terraform_config.config.terraform.get_providers_from_configuration(
            name)

        if found_provider:
            _step_obj.context.type = type_name
            _step_obj.context.name = name
            _step_obj.context.stash = recursive_jsonify(found_provider)
            _step_obj.context.addresses = name
            _step_obj.context.address = name
            _step_obj.context.property_name = type_name
            return True

    elif type_name == 'data':
        name = convert_resource_type(name)
        data_list = _terraform_config.config.terraform.find_data_by_type(name)

        if data_list:
            _step_obj.context.type = type_name
            _step_obj.context.name = name
            _step_obj.context.stash = recursive_jsonify(data_list)
            _step_obj.context.addresses = name
            _step_obj.context.address = name
            _step_obj.context.property_name = type_name
            return True

    skip_step(_step_obj, name)