File: attach_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 (61 lines) | stat: -rw-r--r-- 2,660 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
module Fog
  module AWS
    class Compute
      class Real
        require 'fog/aws/parsers/compute/attach_network_interface'

        # Attach a network interface
        #
        # ==== Parameters
        # * networkInterfaceId<~String> - ID of the network interface to attach
        # * instanceId<~String>         - ID of the instance that will be attached to the network interface
        # * deviceIndex<~Integer>       - index of the device for the network interface attachment on the instance
        #
        # === Returns
        # * response<~Excon::Response>:
        # * body<~Hash>:
        # * 'requestId'<~String>    - Id of request
        # * 'attachmentId'<~String> - ID of the attachment
        #
        # {Amazon API Reference}[http://docs.amazonwebservices.com/AWSEC2/2012-03-01/APIReference/index.html?ApiReference-query-AttachNetworkInterface.html]
        def attach_network_interface(nic_id, instance_id, device_index)
          request(
            'Action' => 'AttachNetworkInterface',
            'NetworkInterfaceId' => nic_id,
            'InstanceId'         => instance_id,
            'DeviceIndex'        => device_index,
            :parser => Fog::Parsers::AWS::Compute::AttachNetworkInterface.new
          )
        end
      end

      class Mock
        def attach_network_interface(nic_id, instance_id, device_index)
          response = Excon::Response.new
          if ! self.data[:instances].find{ |i,i_conf|
            i_conf['instanceId'] == instance_id
          }
            raise Fog::AWS::Compute::NotFound.new("The instance ID '#{instance_id}' does not exist")
          elsif self.data[:network_interfaces].find{ |ni,ni_conf| ni_conf['attachment']['instanceId'] == instance_id && ni_conf['attachment']['deviceIndex'] == device_index }
            raise Fog::AWS::Compute::Error.new("InvalidParameterValue => Instance '#{instance_id}' already has an interface attached at device index '#{device_index}'.")
          elsif self.data[:network_interfaces][nic_id]
            attachment = self.data[:network_interfaces][nic_id]['attachment']
            attachment['attachmentId'] = Fog::AWS::Mock.request_id
            attachment['instanceId']   = instance_id
            attachment['deviceIndex']  = device_index

            response.status = 200
            response.body = {
              'requestId'    => Fog::AWS::Mock.request_id,
              'attachmentId' => attachment['attachmentId']
            }
          else
            raise Fog::AWS::Compute::NotFound.new("The network interface '#{nic_id}' does not exist")
          end

          response
        end
      end
    end
  end
end