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)


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/awsutils/ec2sg.rb', line 53

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.tags['Name'] : server.id
  end.compact

  return false unless servers_using_group.length > 0
  print 'The following servers are still using this group: '
  puts servers_using_group.join(',')

  true
end

#connectionObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/awsutils/ec2sg.rb', line 6

def connection
  @connection ||= begin
    options = {}

    if ENV['AWS_ACCESS_KEY']
      options = {
        aws_access_key_id: ENV['AWS_ACCESS_KEY'],
        aws_secret_access_key: ENV['AWS_SECRET_KEY']
      }
    end

    Fog::Compute::AWS.new options
  end
end

#current_groupsObject



71
72
73
74
75
# File 'lib/awsutils/ec2sg.rb', line 71

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

#exist?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/awsutils/ec2sg.rb', line 67

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

#groupsObject



49
50
51
# File 'lib/awsutils/ec2sg.rb', line 49

def groups
  @groups ||= connection.security_groups
end

#references(search) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/awsutils/ec2sg.rb', line 21

def references(search)
  if search =~ /^sg-/
    search_id = search
  else
    search_id = groups.find { |g| g.name == search }.group_id
  end

  groups.each_with_object({}) do |grp, m|
    assoc_p = grp.ip_permissions.select do |ip_perm|
      !ip_perm['groups'].select { |src_grp|
        src_grp['groupName'] == search ||
          src_grp['groupId'] == search_id
      }.empty?
    end
    if assoc_p.empty?
      next
    else
      m[grp.name] = {
        'groupId' => grp.group_id,
        'ipPermissions' => assoc_p.map do |ap|
          ap.delete('ipRanges')
          ap
        end
      }
    end
  end
end