Class: AwsUtils::Ec2SecurityGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/awsutils/ec2sg.rb

Instance Method Summary collapse

Instance Method Details

#assigned?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/awsutils/ec2sg.rb', line 49

def assigned?
  servers_using_group = connection.servers.map do |server|
    next unless server.state != 'terminated' &&
                server.groups.include?(@opts[:security_group])

    server.tags['Name'] || server.id
  end.compact

  return false unless servers_using_group.empty?

  print 'The following servers are still using this group: '
  puts servers_using_group.join(',')

  true
end

#connectionObject



5
6
7
# File 'lib/awsutils/ec2sg.rb', line 5

def connection
  @connection ||= Fog::Compute::AWS.new
end

#current_groupsObject



69
70
71
72
73
# File 'lib/awsutils/ec2sg.rb', line 69

def current_groups
  @current_groups ||= begin
    connection.security_groups.map { |g| [g.name, g.group_id] }.flatten.uniq
  end
end

#exist?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/awsutils/ec2sg.rb', line 65

def exist?
  current_groups.include?(@opts[:security_group])
end

#groupsObject



45
46
47
# File 'lib/awsutils/ec2sg.rb', line 45

def groups
  @groups ||= connection.security_groups
end

#references(search_string) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/awsutils/ec2sg.rb', line 9

def references(search_string)
  search =
    if search_string =~ /^sg-/
      {
        id: search_string,
        name: groups.find { |gr| gr.group_id == search_string }.name
      }
    else
      {
        id: groups.find { |gr| gr.name == search_string }.group_id,
        name: search_string
      }
    end

  groups.each_with_object({}) do |grp, m|
    permission_references = grp.ip_permissions.select do |ip_perm|
      ip_perm['groups'].find do |pair|
        pair['groupId'] == search[:id] ||
          pair['groupName'] == search[:name]
      end
    end

    next if permission_references.empty?

    m[grp.name] = { 'groupId' => grp.group_id }
    m[grp.name]['references'] = permission_references.map do |pr|
      {
        'groupId' => grp.group_id,
        'ipProtocol' => pr['ipProtocol'],
        'fromPort' => pr['fromPort'],
        'toPort' => pr['toPort']
      }
    end
  end
end