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.



66
67
68
# File 'lib/awsutils/ec2delsg.rb', line 66

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

Instance Method Details

#delete_group_refsObject

def references



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

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



70
71
72
# File 'lib/awsutils/ec2delsg.rb', line 70

def name
  @opts[:security_group]
end

#referencesObject



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
44
45
46
47
48
49
# File 'lib/awsutils/ec2delsg.rb', line 9

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|
          if 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 # if src_grp["groupName"] == @opts[:security_group]
        end # ip_perm["groups"].each do |src_grp|
      end # group.ip_permissions.each do |ip_perm|
    end # connection.security_groups.each do |group|

    references
  end # @references ||= begin
end

#runObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/awsutils/ec2delsg.rb', line 74

def run

  if ! 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