def __init__(self, name, config, path, block_type, attributes, id='', source='', encode=False): """ :param name: unique name given to the terraform block, for example: 'aws_vpc.example_name' :param config: the section in tf_definitions that belong to this block :param path: the file location of the block :param block_type: BlockType :param attributes: dictionary of the block's original attributes in the terraform file """ self.name = name self.config = deepcopy(config) self.module_dependency = "" self.module_dependency_num = "" self.path = path if path: self.path, module_dependency, num = remove_module_dependency_in_path(path) self.path = os.path.realpath(self.path) if module_dependency: self.module_dependency = module_dependency self.module_dependency_num = num self.block_type = block_type if attributes.get(RESOLVED_MODULE_ENTRY_NAME): del attributes[RESOLVED_MODULE_ENTRY_NAME] self.attributes = attributes self.id = id self.source = source self.changed_attributes = {} self.breadcrumbs = {} self.module_connections = {} self.source_module = set() attributes_to_add = self._extract_inner_attributes() self.attributes.update(attributes_to_add) self.encode = encode
def __init__(self, name: str, config: Dict[str, Any], path: str, block_type: BlockType, attributes: Dict[str, Any], id: str = "", source: str = "") -> None: """ :param name: unique name given to the terraform block, for example: 'aws_vpc.example_name' :param config: the section in tf_definitions that belong to this block :param path: the file location of the block :param block_type: BlockType :param attributes: dictionary of the block's original attributes in the terraform file """ super(TerraformBlock, self).__init__(name, config, path, block_type, attributes, id, source) self.module_dependency = "" self.module_dependency_num = "" if path: self.path, module_dependency, num = remove_module_dependency_in_path( path) self.path = os.path.realpath(self.path) if module_dependency: self.module_dependency = module_dependency self.module_dependency_num = num if attributes.get(RESOLVED_MODULE_ENTRY_NAME): del attributes[RESOLVED_MODULE_ENTRY_NAME] self.attributes = attributes self.module_connections: Dict[str, List[int]] = {} self.source_module: Set[int] = set()
def get_module_dependency_map(tf_definitions): """ :param tf_definitions, with paths in format 'dir/main.tf[module_dir/main.tf#0]' :return module_dependency_map: mapping between directories and the location of its module definition: {'dir': 'module_dir/main.tf'} :return tf_definitions: with paths in format 'dir/main.tf' """ module_dependency_map = {} copy_of_tf_definitions = {} dep_index_mapping = {} definitions_keys = list(tf_definitions.keys()) origin_keys = list( filter(lambda k: not k.endswith(']'), definitions_keys)) unevaluated_keys = list( filter(lambda k: k.endswith(']'), definitions_keys)) for file_path in origin_keys: dir_name = os.path.dirname(file_path) module_dependency_map[dir_name] = [[]] copy_of_tf_definitions[file_path] = deepcopy( tf_definitions[file_path]) next_level, unevaluated_keys = Parser.get_next_vertices( origin_keys, unevaluated_keys) while next_level: for file_path in next_level: path, module_dependency, module_dependency_num = remove_module_dependency_in_path( file_path) dir_name = os.path.dirname(path) current_deps = deepcopy( module_dependency_map[os.path.dirname(module_dependency)]) for dep in current_deps: dep.append(module_dependency) if dir_name not in module_dependency_map: module_dependency_map[dir_name] = current_deps elif current_deps not in module_dependency_map[dir_name]: module_dependency_map[dir_name] += current_deps copy_of_tf_definitions[path] = deepcopy( tf_definitions[file_path]) origin_keys.append(path) dep_index_mapping[path] = module_dependency_num next_level, unevaluated_keys = Parser.get_next_vertices( origin_keys, unevaluated_keys) for key, dep_trails in module_dependency_map.items(): hashes = set() deduped = [] for trail in dep_trails: hash = unify_dependency_path(trail) if hash in hashes: continue hashes.add(hash) deduped.append(trail) module_dependency_map[key] = deduped return module_dependency_map, copy_of_tf_definitions, dep_index_mapping