Class: Biosphere::TerraformGraph

Inherits:
Object
  • Object
show all
Defined in:
lib/biosphere/cli/terraformplanning.rb

Defined Under Namespace

Classes: Edge, Graph

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTerraformGraph

Returns a new instance of TerraformGraph.



161
162
163
# File 'lib/biosphere/cli/terraformplanning.rb', line 161

def initialize()

end

Instance Attribute Details

#graphObject

Returns the value of attribute graph.



9
10
11
# File 'lib/biosphere/cli/terraformplanning.rb', line 9

def graph
  @graph
end

Instance Method Details

#filter_tf_plan(plan) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/biosphere/cli/terraformplanning.rb', line 207

def filter_tf_plan(plan)

    blacklist = []
    plan.items.each do |item|
        if item[:action] == :not_picked
            begin
                blacklist << get_blacklist_by_dependency(item[:resource_name])
            rescue ArgumentError => e
                puts "Error: #{e}. item: #{item}"
            end
        end
    end

    blacklist.each do |blacklist_items|
        blacklist_items.each do |blacklist_path_item|
            plan.items.each do |item|
                if item[:resource_name] == blacklist_path_item && item[:action] != :not_picked
                    item[:action] = :not_picked
                    item[:reason] = "not selected as dependent on #{blacklist_items[blacklist_items.index(item[:resource_name])+1..-1].join(" -> ")}"
                end
            end
        end
    end
    return plan
end

#get_blacklist_by_dependency(item) ⇒ Object



202
203
204
205
# File 'lib/biosphere/cli/terraformplanning.rb', line 202

def get_blacklist_by_dependency(item)
    path = @graph.dijkstra("root", item)
    return path[:path]
end

#load(data) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/biosphere/cli/terraformplanning.rb', line 182

def load(data)

    @graph = Graph.new

    lines = data.split("\n")
    lines.each do |line|

        m = parse_line(line)
        if m
            if m[:type] == :node
                @graph.push_name m[:name]
            elsif m[:type] == :edge
                @graph.push_name m[:from]
                @graph.push_name m[:to]
                @graph.connect(m[:from], m[:to], 1)
            end
        end
    end
end

#parse_line(line) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/biosphere/cli/terraformplanning.rb', line 165

def parse_line(line)
    if (m = line.match(/"\[(.+?)\] (?<name>\S+?)(\((.+?)\))?" \[label/))
        return {
            :type => :node,
            :name => m[:name]
        }
    elsif (m = line.match(/"\[(.+?)\] (?<from>\S+?)(\s\((.+?)\)){0,1}" -> "\[(.+?)\] (?<to>\S+?)(\s\((.+?)\)){0,1}"/))
        return {
            :type => :edge,
            :from => m[:from],
            :to => m[:to]
        }
    else
        return nil
    end
end