Class: Sgupdater::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli_options = {}, aws_configuration = {}) ⇒ Client

Returns a new instance of Client.



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

def initialize(cli_options = {}, aws_configuration = {})
  @cli_options = cli_options
  @logger ||= Logger.new STDOUT

  aws_configuration[:logger] = Logger.new STDOUT if @cli_options[:verbose]

  @ec2 = Aws::EC2::Resource.new aws_configuration
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/sgupdater/client.rb', line 6

def logger
  @logger
end

Instance Method Details

#get(cidr = ) ⇒ Object



17
18
19
# File 'lib/sgupdater/client.rb', line 17

def get(cidr = @cli_options[:from_cidr])
  security_groups_with_cidr(cidr)
end

#put_perms(sg, cidr) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/sgupdater/client.rb', line 35

def put_perms(sg, cidr)
  sg.ip_permissions.each do |perm|
    perm.ip_ranges.select {|ip| ip.values.include? cidr}.each do
      puts [sg.vpc_id, sg.group_id, sg.group_name, perm.from_port, perm.to_port, cidr].join("\t")
    end
  end
end

#security_groups_with_cidr(cidr) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/sgupdater/client.rb', line 27

def security_groups_with_cidr(cidr)
  @ec2.security_groups(
    filters: [
      {name: 'ip-permission.cidr', values: [cidr]}
    ]
  )
end

#updateObject



21
22
23
24
25
# File 'lib/sgupdater/client.rb', line 21

def update
  updater = Sgupdater::Updater.new(@cli_options)
  updater.replace(@cli_options[:from_cidr], @cli_options[:to_cidr])
  updater.update
end