File: create_network_interface.rb

package info (click to toggle)
ruby-fog-aws 3.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,816 kB
  • sloc: ruby: 68,587; makefile: 6
file content (134 lines) | stat: -rw-r--r-- 6,547 bytes parent folder | download | duplicates (2)
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
module Fog
  module AWS
    class Compute
      class Real
        require 'fog/aws/parsers/compute/create_network_interface'

        # Creates a network interface
        #
        # ==== Parameters
        # * subnetId<~String> - The ID of the subnet to associate with the network interface
        # * options<~Hash>:
        #   * PrivateIpAddress<~String> - The private IP address of the network interface
        #   * Description<~String>      - The description of the network interface
        #   * GroupSet<~Array>          - The security group IDs for use by the network interface
        #
        # === Returns
        # * response<~Excon::Response>:
        # * body<~Hash>:
        # * 'requestId'<~String>          - Id of request
        # * 'networkInterface'<~Hash>     - The created network interface
        # *   'networkInterfaceId'<~String> - The ID of the network interface
        # *   'subnetId'<~String>           - The ID of the subnet
        # *   'vpcId'<~String>              - The ID of the VPC
        # *   'availabilityZone'<~String>   - The availability zone
        # *   'description'<~String>        - The description
        # *   'ownerId'<~String>            - The ID of the person who created the interface
        # *   'requesterId'<~String>        - The ID ot teh entity requesting this interface
        # *   'requesterManaged'<~String>   -
        # *   'status'<~String>             - "available" or "in-use"
        # *   'macAddress'<~String>         -
        # *   'privateIpAddress'<~String>   - IP address of the interface within the subnet
        # *   'privateDnsName'<~String>     - The private DNS name
        # *   'sourceDestCheck'<~Boolean>   - Flag indicating whether traffic to or from the instance is validated
        # *   'groupSet'<~Hash>             - Associated security groups
        # *     'key'<~String>              - ID of associated group
        # *     'value'<~String>            - Name of associated group
        # *   'attachment'<~Hash>:          - Describes the way this nic is attached
        # *     'attachmentID'<~String>
        # *     'instanceID'<~String>
        # *   'association'<~Hash>:         - Describes an eventual instance association
        # *     'attachmentID'<~String>     - ID of the network interface attachment
        # *     'instanceID'<~String>       - ID of the instance attached to the network interface
        # *     'publicIp'<~String>         - Address of the Elastic IP address bound to the network interface
        # *     'ipOwnerId'<~String>        - ID of the Elastic IP address owner
        # *   'tagSet'<~Array>:             - Tags assigned to the resource.
        # *     'key'<~String>              - Tag's key
        # *     'value'<~String>            - Tag's value
        #
        # {Amazon API Reference}[http://docs.amazonwebservices.com/AWSEC2/2012-03-01/APIReference/ApiReference-query-CreateNetworkInterface.html]
        def create_network_interface(subnetId, options = {})
          if security_groups = options.delete('GroupSet')
            options.merge!(Fog::AWS.indexed_param('SecurityGroupId', [*security_groups]))
          end
          request({
            'Action'     => 'CreateNetworkInterface',
            'SubnetId'   => subnetId,
            :parser      => Fog::Parsers::AWS::Compute::CreateNetworkInterface.new
          }.merge!(options))
        end
      end

      class Mock
        def create_network_interface(subnetId, options = {})
          response = Excon::Response.new
          if subnetId
            subnet = self.data[:subnets].find{ |s| s['subnetId'] == subnetId }
            if subnet.nil?
              raise Fog::AWS::Compute::Error.new("Unknown subnet '#{subnetId}' specified")
            else
              id = Fog::AWS::Mock.network_interface_id
              cidr_block = IPAddress.parse(subnet['cidrBlock'])

              groups = {}
              if options['GroupSet']
                options['GroupSet'].each do |group_id|
                  group_obj = self.data[:security_groups][group_id]
                  if group_obj.nil?
                    raise Fog::AWS::Compute::Error.new("Unknown security group '#{group_id}' specified")
                  end
                  groups[group_id] = group_obj['groupName']
                end
              end

              if options['PrivateIpAddress'].nil?
                # Here we try to act like a DHCP server and pick the first
                # available IP (not including the first in the cidr block,
                # which is typically reserved for the gateway).
                cidr_block.each_host do |p_ip|
                  unless self.data[:network_interfaces].map{ |ni, ni_conf| ni_conf['privateIpAddress'] }.include?p_ip.to_s ||
                    cidr_block.first == p_ip
                    options['PrivateIpAddress'] = p_ip.to_s
                    break
                  end
                end
              elsif self.data[:network_interfaces].map{ |ni,ni_conf| ni_conf['privateIpAddress'] }.include?options['PrivateIpAddress']
                raise Fog::AWS::Compute::Error.new('InUse => The specified address is already in use.')
              end

              data = {
                'networkInterfaceId' => id,
                'subnetId'           => subnetId,
                'vpcId'              => 'mock-vpc-id',
                'availabilityZone'   => 'mock-zone',
                'description'        => options['Description'],
                'ownerId'            => '',
                'requesterManaged'   => 'false',
                'status'             => 'available',
                'macAddress'         => '00:11:22:33:44:55',
                'privateIpAddress'   => options['PrivateIpAddress'],
                'sourceDestCheck'    => true,
                'groupSet'           => groups,
                'attachment'         => {},
                'association'        => {},
                'tagSet'             => {}
              }
              self.data[:network_interfaces][id] = data
              response.body = {
                'requestId'        => Fog::AWS::Mock.request_id,
                'networkInterface' => data
              }
              response
            end
          else
            response.status = 400
            response.body = {
              'Code'    => 'InvalidParameterValue',
              'Message' => "Invalid value '' for subnetId"
            }
          end
        end
      end
    end
  end
end