Class: Profitbricks::Nic

Inherits:
Model
  • Object
show all
Defined in:
lib/profitbricks/nic.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#attributes, belongs_to, get_xml_and_update_attributes, #get_xml_and_update_attributes, has_many, #reload

Constructor Details

#initialize(hash, parent = nil) ⇒ Nic

Returns a new instance of Nic.



6
7
8
9
# File 'lib/profitbricks/nic.rb', line 6

def initialize(hash, parent=nil)
  super(hash)
  @ips = [@ips] if @ips.class != Array
end

Class Method Details

.create(options = {}) ⇒ Nic

Creates a NIC on an existing virtual server.

The user can specify and assign local IPs manually to a NIC, which is connected to a Private LAN. Valid IP addresses for Private LANs are 10.0.0.0/8, 172.16.0.0/12 or 192.168.0.0/16. In a Public LAN, a random DHCP IP address is assigned to each connected NIC by default. This IP Address is automatically generated and will change eventually, e.g. during a server reboot or while disconnecting and reconnecting a LAN to the internet.

Parameters:

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

    parameters for the new NIC

Options Hash (options):

  • :server_id (Fixnum)

    Identifier of the target virtual server (required)

  • :lan_id (Fixnum)

    Identifier of the target LAN > 0 that is to be connected to the specified virtual server. If no LAN exists for such ID, a new LAN with the given ID will be created. (required)

  • :ip (String)

    Public/private IP address.

  • :name (String)

    Names the NIC

Returns:

  • (Nic)

    The created NIC



84
85
86
87
88
89
90
# File 'lib/profitbricks/nic.rb', line 84

def create(options = {})
  xml = "<arg0>"
  xml += get_xml_and_update_attributes options, [:server_id, :lan_id, :ip, :name]
  xml += "</arg0>"
  response = Profitbricks.request :create_nic, xml
  self.find(:id => response.to_hash[:create_nic_return][:return][:nic_id])
end

.find(options = {}) ⇒ Object

Returns information about the state and configuration of an existing NIC.

Parameters:

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

    currently just :id is supported

Options Hash (options):

  • :id (String)

    The id of the NIC to locate



96
97
98
99
100
# File 'lib/profitbricks/nic.rb', line 96

def find(options = {})
  raise "Unable to locate the Nic named '#{options[:name]}'" unless options[:id]
  response = Profitbricks.request :get_nic, "<nicId>#{options[:id]}</nicId>"
  PB::Nic.new(response.to_hash[:get_nic_response][:return])
end

Instance Method Details

#add_ip(ip) ⇒ Object

Adds an existing reserved public IP to a NIC. This operation is required, when dealing with reserved public IPs to ensure proper routing by the ProfitBricks cloud networking layer.

Parameters:



41
42
43
44
45
# File 'lib/profitbricks/nic.rb', line 41

def add_ip(ip)
  response = Profitbricks.request :add_public_ip_to_nic, "<nicId>#{self.id}</nicId><ip>#{ip}</ip>"
  @ips.push ip
  return true if response.to_hash[:add_public_ip_to_nic_response][:return]
end

#deleteBoolean

Deletes an existing NIC.

Returns:

  • (Boolean)

    true on success, false otherwise



59
60
61
62
# File 'lib/profitbricks/nic.rb', line 59

def delete
  response = Profitbricks.request :delete_nic, "<nicId>#{self.id}</nicId>"
  return true if response.to_hash[:delete_nic_response][:return]
end

#ipObject



64
65
66
67
68
69
70
# File 'lib/profitbricks/nic.rb', line 64

def ip
  if @ips.length <= 1
    @ips.first 
  else
    raise ArgumentError.new("This Nic has more then one IP assigned")
  end
end

#remove_ip(ip) ⇒ Object

Removes a reserved public IP from a NIC. This operation is required, when dealing with reserved public IPs to ensure proper routing by the ProfitBricks cloud networking layer.

Parameters:



50
51
52
53
54
# File 'lib/profitbricks/nic.rb', line 50

def remove_ip(ip)
  response = Profitbricks.request :remove_public_ip_from_nic, "<nicId>#{self.id}</nicId><ip>#{ip}</ip>"
  @ips.delete ip
  return true if response.to_hash[:remove_public_ip_from_nic_response][:return]
end

#set_internet_access=(value) ⇒ Boolean

Connects or disconnects an existing NIC to a public LAN to get internet access.

Parameters:

  • Internet (Boolean)

    access (trUe/false)

Returns:

  • (Boolean)

    true on success, false otherwise



15
16
17
18
19
# File 'lib/profitbricks/nic.rb', line 15

def set_internet_access=(value)
  xml = get_xml_and_update_attributes :data_center_id => self.data_center_id, :lan_id => self.lan_id, :land_id => self.lan_id, :internet_access => value
  response = Profitbricks.request :set_internet_access, xml
  return true if response.to_hash[:set_internet_access_response][:return]
end

#update(options = {}) ⇒ Boolean

Changes the settings of an existing NIC.

Parameters:

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

    parameters to update

Options Hash (options):

  • :server_id (Fixnum)

    Identifier of the target virtual server (required)

  • :lan_id (Fixnum)

    Identifier of the target LAN > 0 that is to be connected to the specified virtual server. If no LAN exists for such ID, a new LAN with the given ID will be created.

  • :ip (String)

    Public/private IP address.

  • :name (String)

    Names the NIC

Returns:

  • (Boolean)

    true on success, false otherwise



29
30
31
32
33
34
35
36
# File 'lib/profitbricks/nic.rb', line 29

def update(options = {})
  xml = "<arg0>"
  options.merge!(:nic_id => self.id)
  xml += get_xml_and_update_attributes options , [:nic_id, :lan_id, :server_id, :ip, :name]
  xml += "</arg0>"
  response = Profitbricks.request :update_nic, xml
  return true if response.to_hash[:update_nic_response][:return]
end