Module: Sfn::Command::Graph::Provider::Terraform
- Defined in:
- lib/sfn/command/graph/terraform.rb
Defined Under Namespace
Classes: TerraformGraphProcessor
Instance Method Summary collapse
- #edge_detection(template, graph, name = "", resource_names = []) ⇒ Object
- #output_discovery(template, outputs, resource_name, parent_template, name = "") ⇒ Object
- #resource_dependencies(data, names) ⇒ Object
Instance Method Details
#edge_detection(template, graph, name = "", resource_names = []) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/sfn/command/graph/terraform.rb', line 123 def edge_detection(template, graph, name = "", resource_names = []) resources = (template.fetch("resources", {}) || {}) node_prefix = name resources.each do |resource_name, resource_data| node_name = [node_prefix, resource_name].join("__") if resource_data["type"] == "module" graph.subgraph << generate_graph( resource_data["properties"].delete("stack"), :name => resource_name, :type => resource_data["type"], :resource_names => resource_names, ) next else graph.node(node_name).attributes << graph.fillcolor(colorize(node_prefix.empty? ? config[:file] : node_prefix).inspect) graph.box3d << graph.node(node_name) end graph.filled << graph.node(node_name) graph.node(node_name).label "#{resource_name}\n<#{resource_data["type"]}>\n#{name}" resource_dependencies(resource_data, resource_names + resources.keys).each do |dep_name| if resources.keys.include?(dep_name) dep_name = [node_prefix, dep_name].join("__") end if config[:graph_style] == "creation" @root_graph.edge(dep_name, node_name) else @root_graph.edge(node_name, dep_name) end end end resource_names.concat resources.keys.map { |r_name| [node_prefix, r_name].join("__") } end |
#output_discovery(template, outputs, resource_name, parent_template, name = "") ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/sfn/command/graph/terraform.rb', line 64 def output_discovery(template, outputs, resource_name, parent_template, name = "") if template["resources"] template["resources"].keys.each do |r_name| r_info = template["resources"][r_name] if r_info["type"] == "module" output_discovery(r_info["properties"]["stack"], outputs, r_name, template, r_name) end end end if parent_template ui.debug "Pre-processing stack resource `#{resource_name}`" substack_parameters = Smash[ parent_template.fetch("resources", resource_name, "properties", "parameters", {}).map do |key, value| result = [key, value] if value.to_s.start_with?("${module.") output_key = value.sub("${module.", "").sub("}", "").sub(".", "__") ui.debug "Output key for check: #{output_key}" if outputs.key?(output_key) new_value = outputs[output_key] result = [key, new_value] ui.debug "Parameter for output swap `#{key}`: #{value} -> #{new_value}" end end result end ] ui.debug "Generated internal parameters for `#{resource_name}`: #{substack_parameters}" processor = TerraformGraphProcessor.new({}, :parameters => substack_parameters) template["resources"] = processor.dereference_processor( template["resources"], [] ) template["outputs"] = processor.dereference_processor( template["outputs"], [] ) derefed_outs = template["outputs"] || {} derefed_outs.each do |o_name, o_data| o_key = [name, o_name].join("__") val = o_data["value"] if val.start_with?("${") && val.scan(".").count == 2 val = val.split(".") val[1] = "#{name}__#{val[1]}" val = val.join(".") end outputs[o_key] = val end end outputs.dup.each do |key, value| if value.to_s.start_with?("${module.") output_key = value.to_s.sub("${module.", "").sub("}", "").sub(".", "__") if outputs.key?(output_key) outputs[key] = outputs[output_key] end end end end |
#resource_dependencies(data, names) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/sfn/command/graph/terraform.rb', line 156 def resource_dependencies(data, names) case data when String result = [] if data.start_with?("${") && data.scan(".").count >= 1 data = data.tr("${}", "") check_name = data.split(".")[1] if names.include?(check_name) result.push(check_name) end end result when Hash data.map do |key, value| if key == "depends_on" [value].flatten.compact.map do |dependson_name| dep_name = dependson_name.split(".").last dep_name if names.include?(dep_name) end else resource_dependencies(key, names) + resource_dependencies(value, names) end end.flatten.compact.uniq when Array data.map do |item| resource_dependencies(item, names) end.flatten.compact.uniq else [] end end |