Class: Profitbricks::DataCenter

Inherits:
Model
  • Object
show all
Defined in:
lib/profitbricks/data_center.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, #initialize, #reload

Constructor Details

This class inherits a constructor from Profitbricks::Model

Class Method Details

.allArray <DataCenter>

Returns a list of all Virtual Data Centers created by the user, including ID, name and version number.

Returns:

  • (Array <DataCenter>)

    Array of all available DataCenter



68
69
70
71
72
73
74
# File 'lib/profitbricks/data_center.rb', line 68

def all
  resp = Profitbricks.request :get_all_data_centers
  datacenters = resp.to_hash[:get_all_data_centers_response][:return]
  [datacenters].flatten.compact.collect do |dc|
    PB::DataCenter.find(:id => PB::DataCenter.new(dc).id)
  end
end

.create(options) ⇒ DataCenter

Creates and saves a new, empty Virtual Data Center.

Parameters:

  • options (Hash)

Options Hash (options):

  • :name (String)

    Name of the Virtual Data Center (can not start with or contain (@, /, \, |, “, ‘))

Returns:

  • (DataCenter)

    The newly created Virtual Data Center



81
82
83
84
# File 'lib/profitbricks/data_center.rb', line 81

def create(options)
  response = Profitbricks.request :create_data_center, "<dataCenterName>#{options[:name]}</dataCenterName>"
  self.find(:id => response.to_hash[:create_data_center_response][:return][:data_center_id] )
end

.find(options = {}) ⇒ Object

Finds a Virtual Data Center

Parameters:

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

    either name or id of the Virtual Data Center

Options Hash (options):

  • :name (String)

    The name of the Virtual Data Center

  • :id (String)

    The id of the Virtual Data Center



90
91
92
93
94
95
96
97
98
# File 'lib/profitbricks/data_center.rb', line 90

def find(options = {})
  if options[:name]
    dc = PB::DataCenter.all().select { |d| d.name == options[:name] }.first
    options[:id] = dc.id if dc
  end
  raise "Unable to locate the datacenter named '#{options[:name]}'" unless options[:id]
  response = Profitbricks.request :get_data_center, "<dataCenterId>#{options[:id]}</dataCenterId>"
  PB::DataCenter.new(response.to_hash[:get_data_center_response][:return])
end

Instance Method Details

#clearObject

Removes all components from the current Virtual Data Center.

Returns:

  • The current Virtual Data Center



16
17
18
19
20
21
22
# File 'lib/profitbricks/data_center.rb', line 16

def clear
  response = Profitbricks.request :clear_data_center, "<dataCenterId>#{self.id}</dataCenterId>"
  @provisioning_state = nil
  @servers = []
  @storages = []
  return self if response.to_hash[:clear_data_center_response][:return]
end

#create_server(options) ⇒ Object

Creates a Server in the current Virtual Data Center, automatically sets the :data_center_id

See Also:

  • Server#create


51
52
53
# File 'lib/profitbricks/data_center.rb', line 51

def create_server(options)
  Server.create(options.merge(:data_center_id => self.id))
end

#deleteBoolean

Deletes an empty Virtual Data Center. All components must be removed first.

Returns:

  • (Boolean)

    true on success, false otherwise



9
10
11
12
# File 'lib/profitbricks/data_center.rb', line 9

def delete
  response = Profitbricks.request :delete_data_center, "<dataCenterId>#{self.id}</dataCenterId>"
  response.to_hash[:delete_data_center_response][:return] ? true : false
end

#rename(name) ⇒ DataCenter Also known as: name=

Renames the Virtual Data Center.

Parameters:

Returns:



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

def rename(name)
  response = Profitbricks.request :update_data_center,
              "<arg0><dataCenterId>#{self.id}</dataCenterId><dataCenterName>#{name}</dataCenterName></arg0>"
  if response.to_hash[:update_data_center_response][:return]
    @name = name
  end
  self
end

#update_stateString

This is a lightweight function for pooling the current provisioning state of the Virtual Data Center. It is recommended to use this function for large Virtual Data Centers to query request results.

Returns:

  • (String)

    Provisioning State of the target Virtual Data Center (INACTIVE, INPROCESS, AVAILABLE, DELETED)



43
44
45
46
47
# File 'lib/profitbricks/data_center.rb', line 43

def update_state
  response = Profitbricks.request :get_data_center_state, "<dataCenterId>#{self.id}</dataCenterId>"
  @provisioning_state = response.to_hash[:get_data_center_state_response][:return]
  self.provisioning_state
end

#wait_for_provisioningObject



55
56
57
58
59
60
61
62
# File 'lib/profitbricks/data_center.rb', line 55

def wait_for_provisioning
  self.update_state
  while @provisioning_state != 'AVAILABLE'
    self.update_state
    sleep 1
  end
  self.reload
end