Class: Sgupdater::Updater

Inherits:
Object
  • Object
show all
Defined in:
lib/sgupdater/updater.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Updater

Returns a new instance of Updater.



8
9
10
11
12
13
14
15
# File 'lib/sgupdater/updater.rb', line 8

def initialize(options = {})
  @options = options.dup
  @options[:without_convert] = true
  @options[:format] = :json
  AWS.config(@options)
  @client = Piculet::Client.new(@options)
  @exported = @client.export(without_convert: true)
end

Instance Method Details

#add(from, to) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sgupdater/updater.rb', line 32

def add(from, to)
  @exported.each do |vpc, sgs|
    sgs.each do |sg, props|
      props[:ingress].each do |ing|
        ing[:ip_ranges].each do |cidr|
          if cidr == from
            ing[:ip_ranges] << to
            puts [vpc ? vpc : '(classic)', sg, props[:name], ing[:protocol], ing[:port_range], ing[:ip_ranges], ing[:groups]].join("\t")
          end
        end
      end
    end
  end
end

#replace(from, to) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sgupdater/updater.rb', line 17

def replace(from, to)
  @exported.each do |vpc, sgs|
    sgs.each do |sg, props|
      props[:ingress].each do |ing|
        ing[:ip_ranges].each_with_index do |cidr, i|
          if cidr == from
            ing[:ip_ranges][i] = to
            puts [vpc ? vpc : 'classic', sg, props[:name], ing[:protocol], ing[:port_range], ing[:ip_rages], ing[:groups], cidr].join("\t")
          end
        end
      end
    end
  end
end

#updateObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sgupdater/updater.rb', line 47

def update
  exported = JSON.pretty_generate(@exported)
  file = Tempfile.new('exported')
  begin
    file.puts exported
    file.rewind
    file.rewind
    updated = @client.apply(file)
  ensure
    file.close
    file.unlink
  end
  updated
end