profitbricks 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. data/.autotest +23 -0
  2. data/.gemtest +0 -0
  3. data/.rspec +3 -0
  4. data/CHANGELOG.md +6 -0
  5. data/Gemfile +8 -0
  6. data/Gemfile.lock +53 -0
  7. data/Manifest.txt +106 -0
  8. data/README.md +70 -0
  9. data/Rakefile +31 -0
  10. data/bin/profitbricks +3 -0
  11. data/examples/create_datacenter.rb +57 -0
  12. data/lib/profitbricks/config.rb +16 -0
  13. data/lib/profitbricks/data_center.rb +100 -0
  14. data/lib/profitbricks/extensions.rb +9 -0
  15. data/lib/profitbricks/image.rb +47 -0
  16. data/lib/profitbricks/ip_block.rb +49 -0
  17. data/lib/profitbricks/model.rb +102 -0
  18. data/lib/profitbricks/nic.rb +103 -0
  19. data/lib/profitbricks/profitbricks.rb +86 -0
  20. data/lib/profitbricks/server.rb +83 -0
  21. data/lib/profitbricks/storage.rb +84 -0
  22. data/lib/profitbricks.rb +15 -0
  23. data/profitbricks.gemspec +34 -0
  24. data/spec/fixtures/add_public_ip_to_nic/success.json +10 -0
  25. data/spec/fixtures/add_public_ip_to_nic/success.xml +1 -0
  26. data/spec/fixtures/clear_data_center/success.json +10 -0
  27. data/spec/fixtures/clear_data_center/success.xml +1 -0
  28. data/spec/fixtures/connect_storage_to_server/success.json +10 -0
  29. data/spec/fixtures/connect_storage_to_server/success.xml +1 -0
  30. data/spec/fixtures/create_data_center/success.json +10 -0
  31. data/spec/fixtures/create_data_center/success.xml +1 -0
  32. data/spec/fixtures/create_nic/success.json +11 -0
  33. data/spec/fixtures/create_nic/success.xml +1 -0
  34. data/spec/fixtures/create_server/minimal.json +11 -0
  35. data/spec/fixtures/create_server/minimal.xml +1 -0
  36. data/spec/fixtures/create_storage/success.json +11 -0
  37. data/spec/fixtures/create_storage/success.xml +1 -0
  38. data/spec/fixtures/delete_data_center/success.json +8 -0
  39. data/spec/fixtures/delete_data_center/success.xml +1 -0
  40. data/spec/fixtures/delete_nic/success.json +10 -0
  41. data/spec/fixtures/delete_nic/success.xml +1 -0
  42. data/spec/fixtures/delete_server/success.json +10 -0
  43. data/spec/fixtures/delete_server/success.xml +1 -0
  44. data/spec/fixtures/delete_storage/failture.json +16 -0
  45. data/spec/fixtures/delete_storage/failture.xml +1 -0
  46. data/spec/fixtures/delete_storage/success.json +10 -0
  47. data/spec/fixtures/delete_storage/success.xml +1 -0
  48. data/spec/fixtures/disconnect_storage_from_server/failture.json +16 -0
  49. data/spec/fixtures/disconnect_storage_from_server/failture.xml +1 -0
  50. data/spec/fixtures/disconnect_storage_from_server/success.json +10 -0
  51. data/spec/fixtures/disconnect_storage_from_server/success.xml +1 -0
  52. data/spec/fixtures/get_all_data_centers/empty.json +1 -0
  53. data/spec/fixtures/get_all_data_centers/empty.xml +1 -0
  54. data/spec/fixtures/get_all_data_centers/test_datacenter.json +10 -0
  55. data/spec/fixtures/get_all_data_centers/test_datacenter.xml +1 -0
  56. data/spec/fixtures/get_all_images/success.json +100 -0
  57. data/spec/fixtures/get_all_images/success.xml +1 -0
  58. data/spec/fixtures/get_all_public_ip_blocks/success.json +16 -0
  59. data/spec/fixtures/get_all_public_ip_blocks/success.xml +1 -0
  60. data/spec/fixtures/get_data_center/create.json +147 -0
  61. data/spec/fixtures/get_data_center/create.xml +1 -0
  62. data/spec/fixtures/get_data_center/two_servers_with_storage.json +147 -0
  63. data/spec/fixtures/get_data_center/two_servers_with_storage.xml +1 -0
  64. data/spec/fixtures/get_data_center_state/in_process.json +1 -0
  65. data/spec/fixtures/get_data_center_state/in_process.xml +1 -0
  66. data/spec/fixtures/get_data_center_state/success.json +1 -0
  67. data/spec/fixtures/get_data_center_state/success.xml +1 -0
  68. data/spec/fixtures/get_image/success.json +15 -0
  69. data/spec/fixtures/get_image/success.xml +1 -0
  70. data/spec/fixtures/get_nic/success.json +16 -0
  71. data/spec/fixtures/get_nic/success.xml +1 -0
  72. data/spec/fixtures/get_nic/two_ips.json +19 -0
  73. data/spec/fixtures/get_nic/two_ips.xml +1 -0
  74. data/spec/fixtures/get_server/after_create.json +17 -0
  75. data/spec/fixtures/get_server/after_create.xml +1 -0
  76. data/spec/fixtures/get_server/connected_storage.json +26 -0
  77. data/spec/fixtures/get_server/connected_storage.xml +1 -0
  78. data/spec/fixtures/get_storage/success.json +14 -0
  79. data/spec/fixtures/get_storage/success.xml +1 -0
  80. data/spec/fixtures/reboot_server/success.json +1 -0
  81. data/spec/fixtures/reboot_server/success.xml +1 -0
  82. data/spec/fixtures/release_public_ip_block/success.json +1 -0
  83. data/spec/fixtures/release_public_ip_block/success.xml +1 -0
  84. data/spec/fixtures/remove_public_ip_from_nic/success.json +10 -0
  85. data/spec/fixtures/remove_public_ip_from_nic/success.xml +1 -0
  86. data/spec/fixtures/reserve_public_ip_block/success.json +13 -0
  87. data/spec/fixtures/reserve_public_ip_block/success.xml +1 -0
  88. data/spec/fixtures/set_image_os_type/success.json +1 -0
  89. data/spec/fixtures/set_image_os_type/success.xml +1 -0
  90. data/spec/fixtures/set_internet_access/success.json +10 -0
  91. data/spec/fixtures/set_internet_access/success.xml +1 -0
  92. data/spec/fixtures/update_data_center/success.json +10 -0
  93. data/spec/fixtures/update_data_center/success.xml +1 -0
  94. data/spec/fixtures/update_nic/success.json +10 -0
  95. data/spec/fixtures/update_nic/success.xml +1 -0
  96. data/spec/fixtures/update_server/basic.json +10 -0
  97. data/spec/fixtures/update_server/basic.xml +1 -0
  98. data/spec/fixtures/update_storage/success.json +10 -0
  99. data/spec/fixtures/update_storage/success.xml +1 -0
  100. data/spec/profitbricks/data_center_spec.rb +95 -0
  101. data/spec/profitbricks/image_spec.rb +23 -0
  102. data/spec/profitbricks/ip_block_spec.rb +26 -0
  103. data/spec/profitbricks/model_spec.rb +68 -0
  104. data/spec/profitbricks/nic_spec.rb +55 -0
  105. data/spec/profitbricks/server_spec.rb +41 -0
  106. data/spec/profitbricks/storage_spec.rb +47 -0
  107. data/spec/spec_helper.rb +45 -0
  108. metadata +193 -0
data/.autotest ADDED
@@ -0,0 +1,23 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'autotest/restart'
4
+
5
+ # Autotest.add_hook :initialize do |at|
6
+ # at.extra_files << "../some/external/dependency.rb"
7
+ #
8
+ # at.libs << ":../some/external"
9
+ #
10
+ # at.add_exception 'vendor'
11
+ #
12
+ # at.add_mapping(/dependency.rb/) do |f, _|
13
+ # at.files_matching(/test_.*rb$/)
14
+ # end
15
+ #
16
+ # %w(TestA TestB).each do |klass|
17
+ # at.extra_class_map[klass] = "test/test_misc.rb"
18
+ # end
19
+ # end
20
+
21
+ # Autotest.add_hook :run_command do |at|
22
+ # system "rake build"
23
+ # end
data/.gemtest ADDED
File without changes
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format progress
3
+ --backtrace
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ === 1.0.0 / 2012-02-28
2
+
3
+ * 1 major enhancement
4
+
5
+ * Birthday!
6
+
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source :rubygems
2
+ gem 'savon'
3
+
4
+ group :test, :development do
5
+ gem 'rspec'
6
+ gem 'savon_spec'
7
+ gem 'simplecov'
8
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,53 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ akami (1.0.0)
5
+ gyoku (>= 0.4.0)
6
+ builder (3.0.0)
7
+ diff-lcs (1.1.3)
8
+ gyoku (0.4.4)
9
+ builder (>= 2.1.2)
10
+ httpi (0.9.6)
11
+ rack
12
+ metaclass (0.0.1)
13
+ mocha (0.10.5)
14
+ metaclass (~> 0.0.1)
15
+ multi_json (1.1.0)
16
+ nokogiri (1.5.0)
17
+ nori (1.1.0)
18
+ rack (1.4.1)
19
+ rspec (2.8.0)
20
+ rspec-core (~> 2.8.0)
21
+ rspec-expectations (~> 2.8.0)
22
+ rspec-mocks (~> 2.8.0)
23
+ rspec-core (2.8.0)
24
+ rspec-expectations (2.8.0)
25
+ diff-lcs (~> 1.1.2)
26
+ rspec-mocks (2.8.0)
27
+ savon (0.9.9)
28
+ akami (~> 1.0)
29
+ builder (>= 2.1.2)
30
+ gyoku (>= 0.4.0)
31
+ httpi (~> 0.9)
32
+ nokogiri (>= 1.4.0)
33
+ nori (~> 1.1)
34
+ wasabi (~> 2.1)
35
+ savon_spec (0.1.6)
36
+ mocha (>= 0.9.8)
37
+ rspec (>= 2.0.0)
38
+ savon (>= 0.8.0)
39
+ simplecov (0.6.1)
40
+ multi_json (~> 1.0)
41
+ simplecov-html (~> 0.5.3)
42
+ simplecov-html (0.5.3)
43
+ wasabi (2.1.0)
44
+ nokogiri (>= 1.4.0)
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ rspec
51
+ savon
52
+ savon_spec
53
+ simplecov
data/Manifest.txt ADDED
@@ -0,0 +1,106 @@
1
+ .autotest
2
+ .rspec
3
+ CHANGELOG.md
4
+ Gemfile
5
+ Gemfile.lock
6
+ Manifest.txt
7
+ README.md
8
+ Rakefile
9
+ bin/profitbricks
10
+ examples/create_datacenter.rb
11
+ lib/profitbricks.rb
12
+ lib/profitbricks/config.rb
13
+ lib/profitbricks/data_center.rb
14
+ lib/profitbricks/extensions.rb
15
+ lib/profitbricks/image.rb
16
+ lib/profitbricks/ip_block.rb
17
+ lib/profitbricks/model.rb
18
+ lib/profitbricks/nic.rb
19
+ lib/profitbricks/profitbricks.rb
20
+ lib/profitbricks/server.rb
21
+ lib/profitbricks/storage.rb
22
+ profitbricks.gemspec
23
+ spec/fixtures/add_public_ip_to_nic/success.json
24
+ spec/fixtures/add_public_ip_to_nic/success.xml
25
+ spec/fixtures/clear_data_center/success.json
26
+ spec/fixtures/clear_data_center/success.xml
27
+ spec/fixtures/connect_storage_to_server/success.json
28
+ spec/fixtures/connect_storage_to_server/success.xml
29
+ spec/fixtures/create_data_center/success.json
30
+ spec/fixtures/create_data_center/success.xml
31
+ spec/fixtures/create_nic/success.json
32
+ spec/fixtures/create_nic/success.xml
33
+ spec/fixtures/create_server/minimal.json
34
+ spec/fixtures/create_server/minimal.xml
35
+ spec/fixtures/create_storage/success.json
36
+ spec/fixtures/create_storage/success.xml
37
+ spec/fixtures/delete_data_center/success.json
38
+ spec/fixtures/delete_data_center/success.xml
39
+ spec/fixtures/delete_nic/success.json
40
+ spec/fixtures/delete_nic/success.xml
41
+ spec/fixtures/delete_server/success.json
42
+ spec/fixtures/delete_server/success.xml
43
+ spec/fixtures/delete_storage/failture.json
44
+ spec/fixtures/delete_storage/failture.xml
45
+ spec/fixtures/delete_storage/success.json
46
+ spec/fixtures/delete_storage/success.xml
47
+ spec/fixtures/disconnect_storage_from_server/failture.json
48
+ spec/fixtures/disconnect_storage_from_server/failture.xml
49
+ spec/fixtures/disconnect_storage_from_server/success.json
50
+ spec/fixtures/disconnect_storage_from_server/success.xml
51
+ spec/fixtures/get_all_data_centers/empty.json
52
+ spec/fixtures/get_all_data_centers/empty.xml
53
+ spec/fixtures/get_all_data_centers/test_datacenter.json
54
+ spec/fixtures/get_all_data_centers/test_datacenter.xml
55
+ spec/fixtures/get_all_images/success.json
56
+ spec/fixtures/get_all_images/success.xml
57
+ spec/fixtures/get_all_public_ip_blocks/success.json
58
+ spec/fixtures/get_all_public_ip_blocks/success.xml
59
+ spec/fixtures/get_data_center/create.json
60
+ spec/fixtures/get_data_center/create.xml
61
+ spec/fixtures/get_data_center/two_servers_with_storage.json
62
+ spec/fixtures/get_data_center/two_servers_with_storage.xml
63
+ spec/fixtures/get_data_center_state/in_process.json
64
+ spec/fixtures/get_data_center_state/in_process.xml
65
+ spec/fixtures/get_data_center_state/success.json
66
+ spec/fixtures/get_data_center_state/success.xml
67
+ spec/fixtures/get_image/success.json
68
+ spec/fixtures/get_image/success.xml
69
+ spec/fixtures/get_nic/success.json
70
+ spec/fixtures/get_nic/success.xml
71
+ spec/fixtures/get_nic/two_ips.json
72
+ spec/fixtures/get_nic/two_ips.xml
73
+ spec/fixtures/get_server/after_create.json
74
+ spec/fixtures/get_server/after_create.xml
75
+ spec/fixtures/get_server/connected_storage.json
76
+ spec/fixtures/get_server/connected_storage.xml
77
+ spec/fixtures/get_storage/success.json
78
+ spec/fixtures/get_storage/success.xml
79
+ spec/fixtures/reboot_server/success.json
80
+ spec/fixtures/reboot_server/success.xml
81
+ spec/fixtures/release_public_ip_block/success.json
82
+ spec/fixtures/release_public_ip_block/success.xml
83
+ spec/fixtures/remove_public_ip_from_nic/success.json
84
+ spec/fixtures/remove_public_ip_from_nic/success.xml
85
+ spec/fixtures/reserve_public_ip_block/success.json
86
+ spec/fixtures/reserve_public_ip_block/success.xml
87
+ spec/fixtures/set_image_os_type/success.json
88
+ spec/fixtures/set_image_os_type/success.xml
89
+ spec/fixtures/set_internet_access/success.json
90
+ spec/fixtures/set_internet_access/success.xml
91
+ spec/fixtures/update_data_center/success.json
92
+ spec/fixtures/update_data_center/success.xml
93
+ spec/fixtures/update_nic/success.json
94
+ spec/fixtures/update_nic/success.xml
95
+ spec/fixtures/update_server/basic.json
96
+ spec/fixtures/update_server/basic.xml
97
+ spec/fixtures/update_storage/success.json
98
+ spec/fixtures/update_storage/success.xml
99
+ spec/profitbricks/data_center_spec.rb
100
+ spec/profitbricks/image_spec.rb
101
+ spec/profitbricks/ip_block_spec.rb
102
+ spec/profitbricks/model_spec.rb
103
+ spec/profitbricks/nic_spec.rb
104
+ spec/profitbricks/server_spec.rb
105
+ spec/profitbricks/storage_spec.rb
106
+ spec/spec_helper.rb
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # Profitbricks
2
+
3
+ ## DESCRIPTION
4
+ A Ruby client for the ProfitBricks API.
5
+
6
+ BE AWARE: This software is in a very early state of development, the methods and responses will very likely change over time.
7
+
8
+ ## Dependencies
9
+ A Ruby interpreter.
10
+ Currently only tested on MRI 1.9.3, more implementations will follow once the tests pass on TravisCI
11
+
12
+
13
+ ## Installation
14
+ gem install profitbricks
15
+
16
+
17
+ ## Synopsis
18
+ require 'profitbricks'
19
+ Profitbricks.configure do |config|
20
+ username = "username"
21
+ password = "password"
22
+ end
23
+
24
+ Get a list of all your Datacenters
25
+
26
+ DataCenter.all
27
+
28
+ Create a new Datacenter
29
+
30
+ dc = DataCenter.create('Name')
31
+
32
+ Find a Datacenter by name
33
+
34
+ dc = DataCenter.find(:name => 'Name')
35
+
36
+ Create a new Server
37
+
38
+ dc.create_server(:cores => 1, :ram => 256, :name => 'Test Server')
39
+
40
+ or
41
+
42
+ Server.create(:cores => 1, :ram => 256, :name => 'Test Server')
43
+
44
+ Check out the examples directory for more detailed usage information.
45
+
46
+ ## License
47
+ This might also change!
48
+
49
+ (The MIT License)
50
+
51
+ Copyright (c) 2012 Dominik Sander
52
+
53
+ Permission is hereby granted, free of charge, to any person obtaining
54
+ a copy of this software and associated documentation files (the
55
+ 'Software'), to deal in the Software without restriction, including
56
+ without limitation the rights to use, copy, modify, merge, publish,
57
+ distribute, sublicense, and/or sell copies of the Software, and to
58
+ permit persons to whom the Software is furnished to do so, subject to
59
+ the following conditions:
60
+
61
+ The above copyright notice and this permission notice shall be
62
+ included in all copies or substantial portions of the Software.
63
+
64
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
65
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
66
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
67
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
68
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
69
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
70
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require "rspec/core/rake_task"
6
+
7
+ # Hoe.plugin :compiler
8
+ # Hoe.plugin :gem_prelude_sucks
9
+ # Hoe.plugin :inline
10
+ # Hoe.plugin :racc
11
+ # Hoe.plugin :rubyforge
12
+ Hoe.plugin :git
13
+ Hoe.plugin :gemspec
14
+ Hoe.plugin :bundler
15
+ Hoe.plugin :gemcutter
16
+ Hoe.plugins.delete :rubyforge
17
+
18
+ Hoe.spec 'profitbricks' do
19
+ developer('Dominik Sander', 'git@dsander.de')
20
+
21
+ self.readme_file = 'README.md'
22
+ self.history_file = 'CHANGELOG.md'
23
+ self.extra_deps << ["savon"]
24
+ end
25
+
26
+ task :prerelease => [:clobber, :check_manifest, :test]
27
+
28
+ RSpec::Core::RakeTask.new
29
+
30
+ task :default => :spec
31
+ task :test => :spec
data/bin/profitbricks ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ abort "you need to write me"
@@ -0,0 +1,57 @@
1
+ require 'pp'
2
+ require 'profitbricks'
3
+
4
+ Profitbricks.configure do |config|
5
+ config.username = "username"
6
+ config.password = "password"
7
+ end
8
+
9
+ # An example how to use the Profitbricks API
10
+ #
11
+ # We will create a small DataCenter with two Servers
12
+ # * one webserver connected to the internet
13
+ # * and a database server
14
+ #
15
+
16
+ dc = DataCenter.create('Example Project')
17
+ dc.wait_for_provisioning
18
+
19
+ debian = Image.find(:name => "profitbricks-debian-squeeze-EN-6.0.1a-amd64.img")
20
+ dc.wait_for_provisioning
21
+
22
+ hdd1 = Storage.create(:name => "Debian Webserver", :size => 10, :mount_image_id => debian.id, :data_center_id => dc.id)
23
+ dc.wait_for_provisioning
24
+
25
+ webserver = dc.create_server(:cores => 1, :ram => 256, :name => "Webserver", :boot_from_storage_id => hdd1.id, :lan_id => 2)
26
+ dc.wait_for_provisioning
27
+ webserver.reload
28
+
29
+ webserver_private_nic = webserver.nics.first
30
+ webserver_private_nic.update(:ip => "192.168.0.10")
31
+
32
+ ipblock = IpBlock.reserve(1)
33
+ ip = ipblock.ips.first
34
+
35
+ dc.wait_for_provisioning
36
+
37
+ public_nic = Nic.create(:server_id => webserver.id, :lan_id => 1, :ip => ip, :name => "Public")
38
+ dc.wait_for_provisioning
39
+
40
+ public_nic.set_internet_access = true
41
+ dc.wait_for_provisioning
42
+
43
+
44
+ hdd2 = Storage.create(:name => "Debian Database Server", :size => 10, :mount_image_id => debian.id, :data_center_id => dc.id)
45
+ dc.wait_for_provisioning
46
+
47
+ db_server = dc.create_server(:cores => 1, :ram => 256, :name => "Database Server", :lan_id => 2, :boot_from_storage_id => hdd2.id, :data_center_id => dc.id)
48
+ dc.wait_for_provisioning
49
+ db_server.reload
50
+
51
+ db_server_private_nic = db_server.nics.first
52
+ db_server_private_nic.update(:ip => "192.168.0.11")
53
+ dc.wait_for_provisioning
54
+
55
+ dc.reload
56
+
57
+ pp dc
@@ -0,0 +1,16 @@
1
+ module Profitbricks
2
+ class Config
3
+ class << self
4
+ # Your Profitbricks username (required)
5
+ attr_accessor :username
6
+ # Your Profitbricks password (required)
7
+ attr_accessor :password
8
+ # Disable namespacing the classes, set to false to avoid name conflicts, default: true
9
+ attr_accessor :global_classes
10
+ # Development only, saves SOAP responses on disk, default: false
11
+ attr_accessor :save_responses
12
+ # Set to true to enable Savons request/response logging, default: false
13
+ attr_accessor :log
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,100 @@
1
+ module Profitbricks
2
+ class DataCenter < Profitbricks::Model
3
+ has_many :servers
4
+ has_many :storages
5
+
6
+ # Deletes an empty Virtual Data Center. All components must be removed first.
7
+ # @return [Boolean] true on success, false otherwise
8
+ def delete
9
+ response = Profitbricks.request :delete_data_center, "<dataCenterId>#{self.id}</dataCenterId>"
10
+ response.to_hash[:delete_data_center_response][:return] ? true : false
11
+ end
12
+
13
+ # Removes all components from the current Virtual Data Center.
14
+ # @return The current Virtual Data Center
15
+ def clear
16
+ response = Profitbricks.request :clear_data_center, "<dataCenterId>#{self.id}</dataCenterId>"
17
+ @provisioning_state = nil
18
+ @servers = []
19
+ @storages = []
20
+ return self if response.to_hash[:clear_data_center_response][:return]
21
+ end
22
+
23
+ # Renames the Virtual Data Center.
24
+ #
25
+ # @param [String] Name
26
+ # @return [DataCenter] The renamed DataCenter
27
+ def rename(name)
28
+ response = Profitbricks.request :update_data_center,
29
+ "<arg0><dataCenterId>#{self.id}</dataCenterId><dataCenterName>#{name}</dataCenterName></arg0>"
30
+ if response.to_hash[:update_data_center_response][:return]
31
+ @name = name
32
+ end
33
+ self
34
+ end
35
+ alias_method :name=, :rename
36
+
37
+ # This is a lightweight function for pooling the current provisioning state of the Virtual Data
38
+ # Center. It is recommended to use this function for large Virtual Data Centers to query request
39
+ # results.
40
+ #
41
+ # @return [String] Provisioning State of the target Virtual Data Center (INACTIVE, INPROCESS, AVAILABLE, DELETED)
42
+ def update_state
43
+ response = Profitbricks.request :get_data_center_state, "<dataCenterId>#{self.id}</dataCenterId>"
44
+ @provisioning_state = response.to_hash[:get_data_center_state_response][:return]
45
+ self.provisioning_state
46
+ end
47
+
48
+ # Creates a Server in the current Virtual Data Center, automatically sets the :data_center_id
49
+ # @see Profitbricks::Server#create
50
+ def create_server(options)
51
+ Server.create(options.merge(:data_center_id => self.id))
52
+ end
53
+
54
+ def wait_for_provisioning
55
+ self.update_state
56
+ while @provisioning_state != 'AVAILABLE'
57
+ self.update_state
58
+ sleep 1
59
+ end
60
+ self.reload
61
+ end
62
+
63
+ class << self
64
+ # Returns a list of all Virtual Data Centers created by the user, including ID, name and version number.
65
+ #
66
+ # @return [Array <DataCenter>] Array of all available DataCenter
67
+ def all
68
+ resp = Profitbricks.request :get_all_data_centers
69
+ datacenters = resp.to_hash[:get_all_data_centers_response][:return]
70
+ [datacenters].flatten.compact.collect do |dc|
71
+ PB::DataCenter.find(:id => PB::DataCenter.new(dc).id)
72
+ end
73
+ end
74
+
75
+ # Creates and saves a new, empty Virtual Data Center.
76
+ #
77
+ # @param [String] Name of the Virtual Data Center (can not start with or contain (@, /, \\, |, ", '))
78
+ # @return [DataCenter] The newly created Virtual Data Center
79
+ def create(name)
80
+ response = Profitbricks.request :create_data_center, "<dataCenterName>#{name}</dataCenterName>"
81
+ self.find(:id => response.to_hash[:create_data_center_response][:return][:data_center_id] )
82
+ end
83
+
84
+ # Finds a Virtual Data Center
85
+ # @param [Hash] options either name or id of the Virtual Data Center
86
+ # @option options [String] :name The name of the Virtual Data Center
87
+ # @option options [String] :id The id of the Virtual Data Center
88
+ def find(options = {})
89
+ if options[:name]
90
+ dc = PB::DataCenter.all().select { |d| d.name == options[:name] }.first
91
+ options[:id] = dc.id if dc
92
+ end
93
+ raise "Unable to locate the datacenter named '#{options[:name]}'" unless options[:id]
94
+ response = Profitbricks.request :get_data_center, "<dataCenterId>#{options[:id]}</dataCenterId>"
95
+ PB::DataCenter.new(response.to_hash[:get_data_center_response][:return])
96
+ end
97
+ end
98
+
99
+ end
100
+ end