File: security_group_collection.rb

package info (click to toggle)
ruby-aws-sdk 1.67.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,840 kB
  • sloc: ruby: 28,436; makefile: 7
file content (133 lines) | stat: -rw-r--r-- 4,797 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Copyright 2011-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

module AWS
  class EC2

    # Represents all EC2 security groups in an AWS account.
    class SecurityGroupCollection < Collection

      include TaggedCollection

      # Creates a new
      # @param [String] name The name of the security group to create.
      # @param [Hash] options
      # @option options [String] :description An informal description
      #   of this security group.  Accepts alphanumeric characters, spaces,
      #   dashes, and underscores. If left blank the description will be set
      #   to the name.
      #
      # @option options [VPC,String] :vpc (nil) A VPC or VPC id string to
      #   create the security group in.  When specified a VPC security
      #   group is created.
      #
      # @return [SecurityGroup]
      #
      def create name, options = {}

        description = options[:description] || name

        create_opts = {}
        create_opts[:group_name] = name
        create_opts[:description] = description

        vpc_id = options[:vpc]
        vpc_id ||= options[:vpc_id] # for backwards compatability
        vpc_id ||= filter_value_for('vpc-id')
        vpc_id = vpc_id.id if vpc_id.is_a?(VPC)

        create_opts[:vpc_id] = vpc_id if vpc_id

        response = client.create_security_group(create_opts)

        SecurityGroup.new(response.group_id, {
          :name => name,
          :description => description,
          :vpc_id => create_opts[:vpc_id],
          :config => config })

      end

      # @param [String] group_id The group id of a security group.
      # @return [SecurityGroup] The group with the given id.
      def [] group_id
        SecurityGroup.new(group_id, :config => config)
      end

      # Specify one or more criteria to filter security groups by.
      # A subsequent call to #each will limit the security groups returned
      # by the set of filters.
      #
      # If you supply multiple values to #filter then these values are
      # treated as an OR condition.  To return security groups named
      # 'test' or 'fake':
      #
      #     security_groups.filter('group-name', 'test', 'fake')
      #
      # If you want to and conditions together you need to chain calls to
      # filter.  To limit security groups to those with a name like
      # 'test' and like 'ruby':
      #
      #     security_groups.
      #       filter('group-name', '*test*').
      #       filter('group-name', '*ruby*').each do |group|
      #       #...
      #     end
      #
      # Note that * matches one or more characters and ? matches any one
      # character.
      #
      # ### Valid Filters
      #
      # * description - Description of the security group.
      # * group-id - ID of the security group.
      # * group-name - Name of the security group.
      # * ip-permission.cidr - CIDR range that has been granted the
      #   permission.
      # * ip-permission.from-port - Start of port range for the TCP and UDP
      #    protocols, or an ICMP type number.
      # * ip-permission.group-name - Name of security group that has been
      #   granted the permission.
      # * ip-permission.protocol - IP protocol for the permission. Valid
      #   values include 'tcp', 'udp', 'icmp' or a protocol number.
      # * ip-permission.to-port - End of port range for the TCP and UDP
      #   protocols, or an ICMP code.
      # * ip-permission.user-id - ID of AWS account that has been granted
      #   the permission.
      # * owner-id - AWS account ID of the owner of the security group.
      # * tag-key - Key of a tag assigned to the security group.
      # * tag-value - Value of a tag assigned to the security group.
      #
      # @return [SecurityGroupCollection] A new collection that represents
      #   a subset of the security groups associated with this account.
      #
      # @yield [group]
      # @yieldparam [SecurityGroup] group
      # @return [nil]
      def each &block

        response = filtered_request(:describe_security_groups)
        response.security_group_info.each do |info|

          group = SecurityGroup.new_from(:describe_security_groups, info,
            info.group_id, :config => config)

          yield(group)

        end
        nil
      end

    end
  end
end