class Biosphere::TerraformGraph

Attributes

graph[RW]

Public Class Methods

new() click to toggle source
# File lib/biosphere/cli/terraformplanning.rb, line 161
def initialize()

end

Public Instance Methods

filter_tf_plan(plan) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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