Class: AwsUtils::Ec2DeleteSecurityGroup

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

Instance Method Summary collapse

Methods inherited from Ec2SecurityGroup

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

Constructor Details

#initialize(args) ⇒ Ec2DeleteSecurityGroup

Returns a new instance of Ec2DeleteSecurityGroup.



56
57
58
# File 'lib/awsutils/ec2delsg.rb', line 56

def initialize(args)
  @opts = Ec2SecurityGroup.parse_opts(args)
end

Instance Method Details

#delete_group_refsObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/awsutils/ec2delsg.rb', line 45

def delete_group_refs
  references.each do |ref|
    puts 'Removing rule: ' + ref.inspect

    connection.revoke_security_group_ingress(
      ref['group_name'],
      ref['options']
    )
  end
end

#nameObject



60
61
62
# File 'lib/awsutils/ec2delsg.rb', line 60

def name
  @opts[:security_group]
end

#referencesObject



7
8
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/ec2delsg.rb', line 7

def references
  @references ||= begin
    references = []

    connection.security_groups.each do |group|
      group.ip_permissions.each do |ip_perm|
        ip_perm['groups'].each do |src_grp|
          next unless src_grp['groupName'] == @opts[:security_group]

          options = {
            'IpPermissions' => [
              {
                'FromPort' => ip_perm['fromPort'],
                'Groups' => [
                  {
                    'GroupName' => @opts[:security_group],
                    'UserId' => @opts[:owner_group_id]
                  }
                ],
                'IpProtocol' => ip_perm['ipProtocol'],
                'IpRanges' => [],
                'ToPort' => ip_perm['toPort']
              }
            ]
          }

          references << {
            'group_name' => group.name,
            'options' => options
          }
        end
      end
    end

    references
  end
end

#runObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/awsutils/ec2delsg.rb', line 64

def run
  unless exist?
    puts 'Specified group does not exist.'
    exit 1
  end

  if assigned?
    puts 'Group is still assigned to one or more instances.'
    exit 1
  end

  delete_group_refs

  puts "Deleting group #{@opts[:security_group]}."
  connection.delete_security_group(nil,
                                   connection.security_groups.get(@opts[:security_group]).group_id)
end