Ejemplo n.º 1
0
def parse_tf_plan(tf_plan_file: str) -> Tuple[Optional[Dict[str, Dict[str, Any]]], Optional[List[Tuple[int, str]]]]:
    """
    :type tf_plan_file: str - path to plan file
    :rtype: tf_definition dictionary
    """
    tf_defintions: Dict[str, Dict[str, Any]] = {}
    tf_defintions[tf_plan_file] = {}
    tf_defintions[tf_plan_file]["resource"] = []
    template, template_lines = parse(tf_plan_file)
    if not template:
        return None, None
    for resource in template.get("planned_values", {}).get("root_module", {}).get("resources", []):
        conf = next(
            (
                x
                for x in template.get("configuration", {}).get("root_module", {}).get("resources", [])
                if x["type"] == resource["type"] and x["name"] == resource["name"]
            ),
            None,
        )
        resource_block, prepared = _prepare_resource_block(resource, conf)
        if prepared is True:
            tf_defintions[tf_plan_file]["resource"].append(resource_block)
    child_modules = template.get("planned_values", {}).get("root_module", {}).get("child_modules", [])
    # Terraform supports modules within modules so we need to search
    # in nested modules to find all resource blocks
    resource_blocks = _find_child_modules(child_modules)
    for resource in resource_blocks:
        tf_defintions[tf_plan_file]["resource"].append(resource)
    return tf_defintions, template_lines
Ejemplo n.º 2
0
def parse_tf_plan(tf_plan_file):
    """

    :type tf_plan_file: str - path to plan file
    :rtype: tf_definition dictionary
    """
    tf_defintions = {}
    tf_defintions[tf_plan_file] = {}
    tf_defintions[tf_plan_file]['resource'] = []
    template, template_lines = parse(tf_plan_file)
    if not template:
        return None, None
    for resource in template.get('planned_values',
                                 {}).get("root_module",
                                         {}).get("resources", []):
        resource_block = {}
        resource_block[resource['type']] = {}
        resource_block[resource['type']][resource.get(
            'name', "default")] = _hclify(resource['values'])
        tf_defintions[tf_plan_file]['resource'].append(resource_block)
    for child_module in template.get('planned_values',
                                     {}).get("root_module",
                                             {}).get("child_modules", []):
        for resource in child_module.get("resources", []):
            resource_block = {}
            resource_block[resource['type']] = {}
            if 'values' in resource:  # modules block does not have 'values' section in plan file
                resource_block[resource['type']][resource.get(
                    'name', "default")] = _hclify(resource['values'])
                tf_defintions[tf_plan_file]['resource'].append(resource_block)

    return tf_defintions, template_lines
Ejemplo n.º 3
0
def parse_tf_plan(tf_plan_file):
    """
    :type tf_plan_file: str - path to plan file
    :rtype: tf_definition dictionary
    """
    tf_defintions = {}
    tf_defintions[tf_plan_file] = {}
    tf_defintions[tf_plan_file]['resource'] = []
    template, template_lines = parse(tf_plan_file)
    if not template:
        return None, None
    for resource in template.get('planned_values',
                                 {}).get("root_module",
                                         {}).get("resources", []):
        resource_block, prepared = _prepare_resource_block(resource)
        if prepared is True:
            tf_defintions[tf_plan_file]['resource'].append(resource_block)
    child_modules = template.get('planned_values',
                                 {}).get("root_module",
                                         {}).get("child_modules", [])
    # Terraform supports modules within modules so we need to search
    # in nested modules to find all resource blocks
    resource_blocks = _find_child_modules(child_modules)
    for resource in resource_blocks:
        tf_defintions[tf_plan_file]['resource'].append(resource)
    return tf_defintions, template_lines
Ejemplo n.º 4
0
def parse_tf_plan(tf_plan_file):
    """

    :type tf_plan_file: str - path to plan file
    :rtype: tf_definition dictionary
    """
    tf_defintions = {}
    tf_defintions[tf_plan_file] = {}
    tf_defintions[tf_plan_file]['resource'] = []
    template, template_lines = parse(tf_plan_file)
    for resource in template.get('planned_values', {}).get("root_module", {}).get("resources", []):
        resource_block = {}
        resource_block[resource['type']] = {}
        resource_block[resource['type']][resource.get('name', "default")] = _hclify(resource['values'])
        tf_defintions[tf_plan_file]['resource'].append(resource_block)

    return tf_defintions, template_lines