Class: AWSEdges::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/aws-edges/config.rb

Constant Summary collapse

@@valid_keys =
["name", "sources", "cluster", "label", "edges", 
"from", "from_shape", "from_color", 
"to", "to_shape", "to_color" ,"rotate", "save_as"]
@@valid_sources =
["VPC","EC2","RDS","Redshift","Subnet","IAM"]
@@valid_prefixes =
@@valid_sources

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



9
10
11
12
13
# File 'lib/aws-edges/config.rb', line 9

def initialize
  @node_types = @@valid_sources.map{|s| 
    eval "AWSEdges::#{s}.supported_fields.map{|f| s.downcase + '_' + f}"
  }.flatten
end

Instance Method Details

#msg_and_exit(msg) ⇒ Object



15
16
17
18
# File 'lib/aws-edges/config.rb', line 15

def msg_and_exit(msg)
  puts msg
  exit 1
end

#parse(config_file) ⇒ Object

Attempt to parse the json/yaml config and run through validation



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/aws-edges/config.rb', line 139

def parse(config_file)
  begin
    contents = File.read(config_file)
  rescue Exception => e
    msg_and_exit("Failed to read config: #{e.message}")
  end

  begin
    config = YAML.load(contents)
  rescue Exception => e
    config = JSON.parse(contents)
  rescue Exception => e
    msg_and_exit("Failed to parse config: #{e.message}")
  end

  # run through validation checks
  validate_keys(config)
  validate_colors(config)
  validate_shapes(config)
  validate_sources(config['sources'])
  validate_nodes(config)
  validate_name(config['name'])

  return config
end

#validate_colors(hashed_data) ⇒ Object

Verify that the colors defined are supported by the underlying Graph gem



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/aws-edges/config.rb', line 73

def validate_colors(hashed_data)
  hashed_data.each do |k,v|
    if k == "edges"
      v.each{|e|
        if e["from_color"]
          test_color(e["from_color"])
        end

        if e["to_color"]
          test_color(e["to_color"])
        end
      }
    end
  end
end

#validate_keys(hashed_data) ⇒ Object

Validate that the ‘keys’ are valid in the config



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/aws-edges/config.rb', line 117

def validate_keys(hashed_data)
  keys = []
  hashed_data.each do |k,v|
    unless k == "edges"
      keys.push(k)
      keys.push(validate_keys(v)) if v.class == Hash 
    else
      v.each{|e|
        keys.push(validate_keys(e)) 
      }
    end
  end
  keys.flatten.uniq.each do |u|
    unless @@valid_keys.include?(u)
      msg_and_exit("Invalid key found in config: #{u}")
    end
  end
end

#validate_name(graph_name) ⇒ Object

Verify that a graph name is specified in the config



35
36
37
# File 'lib/aws-edges/config.rb', line 35

def validate_name(graph_name)
  msg_and_exit("No graph name specified in the config") if graph_name.nil?
end

#validate_nodes(hashed_data) ⇒ Object

Confirm that from and to edges are specified properly and are not a many-to-many relationship



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/aws-edges/config.rb', line 42

def validate_nodes(hashed_data)
  nodes = []
  hashed_data.each do |k,v|
    if k == "edges"
      v.each{|e| 
        if e["from"].include?('-') and e["to"].include?('-')
          msg_and_exit("Error: from many to many edges detected in config:\n (#{e['from']}) -> (#{e['to']})")
        end
        nodes.push e["from"]
        nodes.push e["to"]
      } 
    end
    nodes.push(validate_nodes(v)) if v.class == Hash
  end
  nodes.flatten.uniq.each do |u|
    unless @node_types.include?(u)
      msg_and_exit("Invalid edge node specified: #{u}")
    end
  end
end

#validate_shapes(hashed_data) ⇒ Object

Verify that the shapes defined are supported by the underlying Graph gem



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/aws-edges/config.rb', line 99

def validate_shapes(hashed_data)
  hashed_data.each do |k,v|
    if k == "edges"
      v.each{|e|
        if e["from_shape"]
          test_shape(e["from_shape"])
        end

        if e["to_shape"]
          test_shape(e["to_shape"])
        end
      }
    end
  end
end

#validate_sources(sources) ⇒ Object

Validate the source section of the config file



23
24
25
26
27
28
29
30
# File 'lib/aws-edges/config.rb', line 23

def validate_sources(sources)
  msg_and_exit("No sources specified in the config") if sources.nil?
  sources.each do |source|
    unless @@valid_sources.map{|i| i.downcase}.include?(source)
      msg_and_exit("Invalid source detected in config: #{source}")
    end
  end
end