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, has_many, #initialize, #reload

Constructor Details

This class inherits a constructor from Profitbricks::Model

Class Method Details

+ (Array <DataCenter>) all

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



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

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

+ (DataCenter) create(options)

Creates and saves a new, empty Virtual Data Center.

Options Hash (options):

  • :name (String)

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

  • :region (String)

    Select region to create the data center (NORTH_AMERICA, EUROPE, DEFAULT). If DEFAULT or empty, the Virtual Data Center will be created in the default region of the user

Raises:

  • (ArgumentError)


103
104
105
106
107
108
# File 'lib/profitbricks/data_center.rb', line 103

def create(options)
  raise ArgumentError.new(":region has to be one of 'DEFAULT', 'NORTH_AMERICA', or 'EUROPE'") if options[:region] and !['DEFAULT', 'EUROPE', 'NORTH_AMERICA'].include? options[:region]
  options[:data_center_name] = options.delete :name
  response = Profitbricks.request :create_data_center, options
  self.find(:id => response[:data_center_id] )
end

+ (Object) find(options = {})

Finds a Virtual Data Center

Options Hash (options):

  • :name (String)

    The name of the Virtual Data Center

  • :id (String)

    The id of the Virtual Data Center



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/profitbricks/data_center.rb', line 114

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]
  options[:data_center_id]   = options.delete :id
  options.delete :name
  response = Profitbricks.request :get_data_center, options
  PB::DataCenter.new(response)
end

Instance Method Details

- (Object) clear

Removes all components from 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, data_center_id: self.id
  @provisioning_state = nil
  @servers = []
  @storages = []
  return self if response
end

- (Object) create_load_balancer(options)

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

See Also:

  • LoadBalancer#create


62
63
64
# File 'lib/profitbricks/data_center.rb', line 62

def create_load_balancer(options)
  LoadBalancer.create(options.merge(data_center_id: self.id))
end

- (Object) create_server(options)

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

See Also:

  • Server#create


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

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

- (Object) create_storage(options)

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

See Also:

  • Storage#create


56
57
58
# File 'lib/profitbricks/data_center.rb', line 56

def create_storage(options)
  Storage.create(options.merge(data_center_id: self.id))
end

- (Boolean) delete

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



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

def delete
  response = Profitbricks.request :delete_data_center, data_center_id: self.id
  response ? true : false
end

- (Boolean) provisioned?

Checks if the Data Center was successfully provisioned



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

def provisioned?
  self.update_state
  if @provisioning_state == 'AVAILABLE'
    self.reload
    true
  else
    false
  end
end

- (DataCenter) rename(name) Also known as: name=

Renames the Virtual Data Center.



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

def rename(name)
  response = Profitbricks.request :update_data_center, data_center_id: self.id, data_center_name: name
  if response
    @name = name
  end
  self
end

- (String) update_state

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.



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

def update_state
  response = Profitbricks.request :get_data_center_state, data_center_id: self.id
  @provisioning_state = response
  self.provisioning_state
end

- (Object) wait_for_provisioning

Blocks until the Data Center is provisioned



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

def wait_for_provisioning
  while !self.provisioned?
    sleep Profitbricks::Config.polling_interval
  end
end