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, 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.
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 |
.create(options) ⇒ DataCenter
Creates and saves a new, empty Virtual Data Center.
103 104 105 106 107 108 |
# File 'lib/profitbricks/data_center.rb', line 103 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] [:data_center_name] = .delete :name response = Profitbricks.request :create_data_center, self.find(:id => response[:data_center_id] ) end |
.find(options = {}) ⇒ Object
Finds a Virtual Data Center
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/profitbricks/data_center.rb', line 114 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] [:data_center_id] = .delete :id .delete :name response = Profitbricks.request :get_data_center, PB::DataCenter.new(response) 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, data_center_id: self.id @provisioning_state = nil @servers = [] @storages = [] return self if response end |
#create_load_balancer(options) ⇒ Object
Creates a Load Balancer in the current Virtual Data Center, automatically sets the :data_center_id
62 63 64 |
# File 'lib/profitbricks/data_center.rb', line 62 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
50 51 52 |
# File 'lib/profitbricks/data_center.rb', line 50 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
56 57 58 |
# File 'lib/profitbricks/data_center.rb', line 56 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, data_center_id: self.id response ? true : false end |
#provisioned? ⇒ Boolean
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 |
#rename(name) ⇒ DataCenter 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 |
#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.
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 |
#wait_for_provisioning ⇒ Object
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 |