Class: AwsUtils::Ec2LsGrp

Inherits:
Ec2SecurityGroup show all
Defined in:
lib/awsutils/ec2lsgrp.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Ec2SecurityGroup

#assigned?, #connection, #current_groups, #exist?, #groups, #references

Constructor Details

#initializeEc2LsGrp

Returns a new instance of Ec2LsGrp.



73
74
75
76
# File 'lib/awsutils/ec2lsgrp.rb', line 73

def initialize
  @opts = parse_opts
  @search = ARGV.last
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



10
11
12
# File 'lib/awsutils/ec2lsgrp.rb', line 10

def opts
  @opts
end

#owner_idObject (readonly)

Returns the value of attribute owner_id.



10
11
12
# File 'lib/awsutils/ec2lsgrp.rb', line 10

def owner_id
  @owner_id
end

#searchObject (readonly)

Returns the value of attribute search.



10
11
12
# File 'lib/awsutils/ec2lsgrp.rb', line 10

def search
  @search
end

Instance Method Details

#group_details(g) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/awsutils/ec2lsgrp.rb', line 29

def group_details(g)
  @owner_id = g.owner_id

  msg_pair('ID', g.group_id)
  msg_pair('NAME', g.name)
  msg_pair('OWNER_ID', owner_id)
  msg_pair('DESCRIPTION', g.description)
  msg_pair('VPC_ID', g.vpc_id) if g.vpc_id

  perms_out('incoming', g.ip_permissions)
  perms_out('egress', g.ip_permissions_egress) if g.vpc_id
end

#msg_pair(key, value) ⇒ Object



12
13
14
# File 'lib/awsutils/ec2lsgrp.rb', line 12

def msg_pair(key, value)
  puts("#{key} #{value}")
end

#perms_out(direction, perms) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/awsutils/ec2lsgrp.rb', line 16

def perms_out(direction, perms)
  puts "#{direction.upcase} RULES"
  perms.to_enum.with_index(1) do |perm, index|
    print "  #{index} "
    print "groups: #{group_perm_string(perm['groups'])}; " if perm['groups'].count > 0
    print "ip_ranges: #{perm['ipRanges'].join(', ')}; " if perm['ipRanges'].count > 0
    print "ipProtocol: #{perm['ipProtocol']}; "
    print "fromPort: #{perm['fromPort']}; " if perm['fromPort']
    print "toPort: #{perm['toPort']}" if perm['toPort']
    print "\n"
  end
end


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/awsutils/ec2lsgrp.rb', line 57

def print_refs(refs)
  refs.each do |grp, data|
    print "#{grp} (#{data['groupId']}): "
    perm_strings = data['references'].map do |perm|
      if perm['ipProtocol'] == '-1'
        'all'
      else
        "#{perm['ipProtocol']}/#{perm['fromPort']}" +
          (perm['fromPort'] != perm['toPort'] ? "-#{perm['toPort']}" : '')
      end
    end
    print perm_strings.join ', '
    puts ''
  end
end

#runObject

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/awsutils/ec2lsgrp.rb', line 42

def run
  raise ArgumentError, 'Please specify a security group' unless search
  unless group_o = group(search) # rubocop:disable Lint/AssignmentInCondition
    raise GroupDoesNotExist
  end
  return group_details(group_o) unless opts[:list_refs]

  refs = references(group_o.group_id)
  if refs.empty?
    puts 'No references'
  else
    print_refs refs
  end
end