Class: AWS::EC2::NetworkInterface

Inherits:
Resource
  • Object
show all
Includes:
TaggedItem
Defined in:
lib/aws/ec2/network_interface.rb,
lib/aws/ec2/network_interface/attachment.rb

Overview

Represents a network interface in EC2.

Defined Under Namespace

Classes: Attachment

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TaggedItem

#add_tag, #clear_tags, #tags

Constructor Details

#initialize(network_interface_id, options = {}) ⇒ NetworkInterface

Returns a new instance of NetworkInterface.



24
25
26
27
# File 'lib/aws/ec2/network_interface.rb', line 24

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

Instance Attribute Details

#network_interface_idString (readonly) Also known as: id

Returns:

  • (String)


30
31
32
# File 'lib/aws/ec2/network_interface.rb', line 30

def network_interface_id
  @network_interface_id
end

Instance Method Details

#attach(instance, options = {}) ⇒ nil

Parameters:

  • instance (Instance, String)

    The instance to attach this network interface to, may be an Instance object or an instance id string.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :device_index (Integer) — default: 1

    The index of the device for the network interface attachment on the instance. Defaults to 1.

Returns:

  • (nil)


126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/aws/ec2/network_interface.rb', line 126

def attach instance, options = {}

  instance_id = instance.is_a?(Instance) ? instance.instance_id : instance

  client_opts = {}
  client_opts[:network_interface_id] = network_interface_id
  client_opts[:instance_id] = instance_id
  client_opts[:device_index] = options[:device_index] || 1

  client.attach_network_interface(client_opts)

  nil

end

#attachmentObject



110
111
112
113
114
# File 'lib/aws/ec2/network_interface.rb', line 110

def attachment
  if details = attachment_details
    Attachment.new(self, details)
  end
end

#deletenil

Deletes this network interface.

Returns:

  • (nil)


155
156
157
158
159
160
# File 'lib/aws/ec2/network_interface.rb', line 155

def delete
  client_opts = {}
  client_opts[:network_interface_id] = network_interface_id
  client.delete_network_interface(client_opts)
  nil
end

#detach(options = {}) ⇒ nil

Detaches this network interface.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :force (Boolean) — default: false

    Set true to force a detachment.

Returns:

  • (nil)


145
146
147
148
149
150
151
# File 'lib/aws/ec2/network_interface.rb', line 145

def detach options = {}
  if attachment = self.attachment
    attachment.detach(options)
  else
    raise 'unable to detach network interface, no attachment present'
  end
end

#exists?Boolean

Returns true if this network interface exists.

Returns:

  • (Boolean)

    Returns true if this network interface exists.



163
164
165
166
167
168
169
170
# File 'lib/aws/ec2/network_interface.rb', line 163

def exists?
  begin
    get_resource
    true
  rescue Errors::InvalidNetworkInterfaceID::NotFound
    false
  end
end

#instanceInstance?

Returns the instance this network interface is attached to. If it has not been attached, then nil is returned.

Returns:

  • (Instance, nil)

    Returns the instance this network interface is attached to. If it has not been attached, then nil is returned.



104
105
106
107
108
# File 'lib/aws/ec2/network_interface.rb', line 104

def instance
  if attachment = self.attachment
    attachment.instance
  end
end

#security_groupsArray<SecurityGroup>

Returns:



81
82
83
84
85
# File 'lib/aws/ec2/network_interface.rb', line 81

def security_groups
  groups.collect do |g|
    SecurityGroup.new(g.group_id, :name => g.group_name, :config => config)
  end
end

#set_security_groups(*groups) ⇒ nil Also known as: security_groups=

Parameters:

  • groups (Array<SecurityGroup>, Array<String>)

    A list of security groups objects or security group ids. This replaces the security group set on this network interface.

Returns:

  • (nil)


93
94
95
96
97
98
# File 'lib/aws/ec2/network_interface.rb', line 93

def set_security_groups *groups
  self.groups = [groups].flatten.collect do |g|
    g.is_a?(SecurityGroup) ? g.security_group_id : g
  end
  nil
end

#subnetSubnet

Returns the Subnet this network interface belongs to.

Returns:

  • (Subnet)

    Returns the Subnet this network interface belongs to.



76
77
78
# File 'lib/aws/ec2/network_interface.rb', line 76

def subnet
  Subnet.new(subnet_id, :vpc_id => vpc_id, :config => config)
end

#vpcVPC

Returns the VPC this network interface belongs to.

Returns:

  • (VPC)

    Returns the VPC this network interface belongs to.



70
71
72
# File 'lib/aws/ec2/network_interface.rb', line 70

def vpc
  VPC.new(vpc_id, :config => config)
end