Class: Profitbricks::DataCenter
- Defined in:
- lib/profitbricks/data_center.rb
Class Method Summary collapse
-
.all ⇒ Array <DataCenter>
Returns a list of all Virtual Data Centers created by the user, including ID, name and version number.
-
.create(options) ⇒ DataCenter
Creates and saves a new, empty Virtual Data Center.
-
.find(options = {}) ⇒ Object
Finds a Virtual Data Center.
Instance Method Summary collapse
-
#clear ⇒ Object
Removes all components from the current Virtual Data Center.
-
#create_load_balancer(options) ⇒ Object
Creates a Load Balancer in the current Virtual Data Center, automatically sets the :data_center_id.
-
#create_server(options) ⇒ Object
Creates a Server in the current Virtual Data Center, automatically sets the :data_center_id.
-
#create_storage(options) ⇒ Object
Creates a Storage in the current Virtual Data Center, automatically sets the :data_center_id.
-
#delete ⇒ Boolean
Deletes an empty Virtual Data Center.
-
#provisioned? ⇒ Boolean
Checks if the Data Center was successfully provisioned.
-
#rename(name) ⇒ DataCenter
(also: #name=)
Renames the Virtual Data Center.
-
#update_state ⇒ String
This is a lightweight function for pooling the current provisioning state of the Virtual Data Center.
-
#wait_for_provisioning ⇒ Object
Blocks until the Data Center is provisioned.
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
.all ⇒ Array <DataCenter>
Returns a list of all Virtual Data Centers created by the user, including ID, name and version number.
91 92 93 94 95 96 97 |
# File 'lib/profitbricks/data_center.rb', line 91 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.
105 106 107 108 109 110 111 |
# File 'lib/profitbricks/data_center.rb', line 105 def create() raise ArgumentError.new(":region has to be one of 'DEFAULT', 'NORTH_AMERICA', or 'EUROPE'") if [:region] and !['DEFAULT', 'EUROPE', 'NORTH_AMERICA'].include? [:region] xml = "<dataCenterName>#{[:name]}</dataCenterName>" xml += get_xml_and_update_attributes , [:region] response = Profitbricks.request :create_data_center, xml self.find(:id => response.to_hash[:create_data_center_response][:return][:data_center_id] ) end |
.find(options = {}) ⇒ Object
Finds a Virtual Data Center
117 118 119 120 121 122 123 124 125 |
# File 'lib/profitbricks/data_center.rb', line 117 def find( = {}) if [:name] dc = PB::DataCenter.all().select { |d| d.name == [:name] }.first [:id] = dc.id if dc end raise "Unable to locate the datacenter named '#{[:name]}'" unless [:id] response = Profitbricks.request :get_data_center, "<dataCenterId>#{[:id]}</dataCenterId>" PB::DataCenter.new(response.to_hash[:get_data_center_response][:return]) end |
Instance Method Details
#clear ⇒ Object
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, "<dataCenterId>#{self.id}</dataCenterId>" @provisioning_state = nil @servers = [] @storages = [] return self if response.to_hash[:clear_data_center_response][:return] end |
#create_load_balancer(options) ⇒ Object
Creates a Load Balancer in the current Virtual Data Center, automatically sets the :data_center_id
63 64 65 |
# File 'lib/profitbricks/data_center.rb', line 63 def create_load_balancer() LoadBalancer.create(.merge(:data_center_id => self.id)) end |
#create_server(options) ⇒ Object
Creates a Server in the current Virtual Data Center, automatically sets the :data_center_id
51 52 53 |
# File 'lib/profitbricks/data_center.rb', line 51 def create_server() Server.create(.merge(:data_center_id => self.id)) end |
#create_storage(options) ⇒ Object
Creates a Storage in the current Virtual Data Center, automatically sets the :data_center_id
57 58 59 |
# File 'lib/profitbricks/data_center.rb', line 57 def create_storage() Storage.create(.merge(:data_center_id => self.id)) end |
#delete ⇒ Boolean
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, "<dataCenterId>#{self.id}</dataCenterId>" response.to_hash[:delete_data_center_response][:return] ? true : false end |
#provisioned? ⇒ Boolean
Checks if the Data Center was successfully provisioned
70 71 72 73 74 75 76 77 78 |
# File 'lib/profitbricks/data_center.rb', line 70 def provisioned? self.update_state if @provisioning_state == 'AVAILABLE' self.reload true else false end end |
#rename(name) ⇒ DataCenter Also known as: name=
Renames the Virtual Data Center.
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_state ⇒ String
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.
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_provisioning ⇒ Object
Blocks until the Data Center is provisioned
81 82 83 84 85 |
# File 'lib/profitbricks/data_center.rb', line 81 def wait_for_provisioning while !self.provisioned? sleep Profitbricks::Config.polling_interval end end |