File: subnet.rb

package info (click to toggle)
ruby-aws-sdk 1.67.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,840 kB
  • sloc: ruby: 28,436; makefile: 7
file content (161 lines) | stat: -rw-r--r-- 5,068 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# 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

    # @attr_reader [String] vpc_id
    #
    # @attr_reader [Symbol] state
    #
    # @attr_reader [String] cidr_block
    #
    # @attr_reader [Integer] available_ip_address_count
    #
    # @attr_reader [String] availability_zone_name
    #
    class Subnet < Resource

      include TaggedItem

      def initialize subnet_id, options = {}
        @subnet_id = subnet_id
        super
      end

      # @return [String]
      attr_reader :subnet_id

      alias_method :id, :subnet_id

      attribute :vpc_id, :static => true

      attribute :state, :to_sym => true

      attribute :cidr_block, :static => true

      attribute :available_ip_address_count, :integer => true, :static => true

      attribute :availability_zone_name,
        :from => :availability_zone, :static => true

      populates_from(:create_subnet) do |resp|
        resp.subnet if resp.subnet.subnet_id == subnet_id
      end

      populates_from(:describe_subnets) do |resp|
        resp.subnet_set.find{|s| s.subnet_id == subnet_id }
      end

      # @return [VPC] Returns the VPC this subnet belongs to.
      def vpc
        VPC.new(vpc_id, :config => config)
      end

      # @return [NetworkACL] Returns the network ACL currently assigned
      #   to this subnet.
      def network_acl
        network_acl_association.network_acl
      end

      # Replaces the currently assigned network ACL with the passed one.
      # @param [NetworkACL,String] network_acl A {NetworkACL} or network
      #   ACL id to assign to this subnet.
      def network_acl= network_acl
        network_acl_association.replace_network_acl(network_acl)
      end

      # @return [NetworkACL::Association] Returns the association between
      #   this subnet and its network ACL.
      def network_acl_association
        associations = AWS.memoize { vpc.network_acls.map(&:associations) }.flatten
        associations.find{|a| a.subnet == self }
      end

      # @return [RouteTable] Returns the route table currently associated
      #   with this subnet.
      def route_table
        route_table_association.route_table
      end

      # Sets the route table for this subnet.  If there is already a
      # route table associated with this subnet, that association
      # is replaced.
      #
      # @param [RouteTable,String] route_table A {RouteTable} object or
      #   a route table id string.
      #
      # @return [RouteTable::Association]
      #
      def set_route_table route_table

        unless route_table.is_a?(RouteTable)
          route_table = RouteTable.new(route_table, :config => config)
        end

        client_opts = {}
        client_opts[:route_table_id] = route_table.id

        assoc = route_table_association

        if assoc.main?
          client_opts[:subnet_id] = subnet_id
          response = client.associate_route_table(client_opts)
          association_id = response.association_id
        else
          client_opts[:association_id] = assoc.association_id
          resp = client.replace_route_table_association(client_opts)
          association_id = resp.new_association_id
        end

        RouteTable::Association.new(route_table, association_id, subnet_id)

      end
      alias_method :route_table=, :set_route_table

      # @return [RouteTable::Association] Returns the association between
      #   this subnet and its route table.
      def route_table_association
        assocs = AWS.memoize { vpc.route_tables.map(&:associations) }.flatten
        assocs.find{|a| a.subnet == self } || assocs.find{|a| a.main? }
      end

      # See also {#availability_zone_name}.
      # @return [AvailabilityZone]
      def availability_zone
        AvailabilityZone.new(availability_zone_name, :config => config)
      end

      # @return [InstanceCollection] Returns a filtered collection of
      #   instances launched in this subnet.
      def instances
        InstanceCollection.new(:config => config).filter('subnet-id', id)
      end

      # @return [NetworkInterfaceCollection] Returns a collection that
      #   represents all of the network interfaces for this subnet.
      def network_interfaces
        NetworkInterfaceCollection.new(:config => config).filter('subnet-id', id)
      end

      # Deletes the current subnet.  The subnet must be empty before it can
      # be deleted.
      # @return [nil]
      def delete
        client.delete_subnet(:subnet_id => subnet_id)
        nil
      end

    end
  end
end