squall 0.0.3 → 1.0.0

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 (100) hide show
  1. data/.gitignore +10 -0
  2. data/.rspec +2 -0
  3. data/.rvmrc +1 -0
  4. data/.travis.yml +7 -0
  5. data/Gemfile +3 -0
  6. data/Gemfile.lock +100 -0
  7. data/LICENSE +1 -1
  8. data/README.md +69 -0
  9. data/Rakefile +48 -42
  10. data/lib/squall/base.rb +96 -0
  11. data/lib/squall/config.rb +26 -0
  12. data/lib/squall/exception.rb +13 -0
  13. data/lib/squall/hypervisor.rb +46 -16
  14. data/lib/squall/ip_address.rb +13 -0
  15. data/lib/squall/network.rb +42 -0
  16. data/lib/squall/params.rb +50 -0
  17. data/lib/squall/role.rb +56 -0
  18. data/lib/squall/statistic.rb +10 -0
  19. data/lib/squall/template.rb +22 -0
  20. data/lib/squall/transaction.rb +16 -0
  21. data/lib/squall/user.rb +87 -0
  22. data/lib/squall/version.rb +3 -0
  23. data/lib/squall/virtual_machine.rb +169 -41
  24. data/lib/squall.rb +76 -20
  25. data/spec/params_spec.rb +195 -0
  26. data/spec/spec_helper.rb +47 -0
  27. data/spec/squall/base_spec.rb +135 -0
  28. data/spec/squall/config_spec.rb +44 -0
  29. data/spec/squall/hypervisor_spec.rb +143 -0
  30. data/spec/squall/ip_address_spec.rb +32 -0
  31. data/spec/squall/network_spec.rb +121 -0
  32. data/spec/squall/role_spec.rb +123 -0
  33. data/spec/squall/statistic_spec.rb +23 -0
  34. data/spec/squall/template_spec.rb +59 -0
  35. data/spec/squall/transaction_spec.rb +42 -0
  36. data/spec/squall/user_spec.rb +195 -0
  37. data/spec/squall/virtual_machine_spec.rb +471 -0
  38. data/spec/squall_spec.rb +67 -0
  39. data/spec/vcr_cassettes/hypervisor/create.yml +77 -0
  40. data/spec/vcr_cassettes/hypervisor/delete.yml +77 -0
  41. data/spec/vcr_cassettes/hypervisor/edit.yml +79 -0
  42. data/spec/vcr_cassettes/hypervisor/list.yml +40 -0
  43. data/spec/vcr_cassettes/hypervisor/reboot.yml +77 -0
  44. data/spec/vcr_cassettes/hypervisor/show.yml +77 -0
  45. data/spec/vcr_cassettes/ipaddress/list.yml +77 -0
  46. data/spec/vcr_cassettes/network/create.yml +196 -0
  47. data/spec/vcr_cassettes/network/delete.yml +77 -0
  48. data/spec/vcr_cassettes/network/edit.yml +233 -0
  49. data/spec/vcr_cassettes/network/list.yml +40 -0
  50. data/spec/vcr_cassettes/role/create.yml +77 -0
  51. data/spec/vcr_cassettes/role/delete.yml +77 -0
  52. data/spec/vcr_cassettes/role/edit.yml +139 -0
  53. data/spec/vcr_cassettes/role/list.yml +40 -0
  54. data/spec/vcr_cassettes/role/permissions.yml +40 -0
  55. data/spec/vcr_cassettes/role/show.yml +77 -0
  56. data/spec/vcr_cassettes/statistic/usage_statistics.yml +40 -0
  57. data/spec/vcr_cassettes/template/download.yml +40 -0
  58. data/spec/vcr_cassettes/template/list.yml +40 -0
  59. data/spec/vcr_cassettes/template/make_public.yml +77 -0
  60. data/spec/vcr_cassettes/transaction/list.yml +40 -0
  61. data/spec/vcr_cassettes/transaction/show.yml +77 -0
  62. data/spec/vcr_cassettes/user/activate.yml +40 -0
  63. data/spec/vcr_cassettes/user/create.yml +116 -0
  64. data/spec/vcr_cassettes/user/delete.yml +77 -0
  65. data/spec/vcr_cassettes/user/edit_role.yml +116 -0
  66. data/spec/vcr_cassettes/user/generate_api_key.yml +40 -0
  67. data/spec/vcr_cassettes/user/list.yml +40 -0
  68. data/spec/vcr_cassettes/user/show.yml +77 -0
  69. data/spec/vcr_cassettes/user/stats.yml +40 -0
  70. data/spec/vcr_cassettes/user/suspend.yml +40 -0
  71. data/spec/vcr_cassettes/user/virtual_machines.yml +79 -0
  72. data/spec/vcr_cassettes/virtual_machine/build.yml +77 -0
  73. data/spec/vcr_cassettes/virtual_machine/change_owner.yml +108 -0
  74. data/spec/vcr_cassettes/virtual_machine/change_password.yml +77 -0
  75. data/spec/vcr_cassettes/virtual_machine/create.yml +40 -0
  76. data/spec/vcr_cassettes/virtual_machine/delete.yml +77 -0
  77. data/spec/vcr_cassettes/virtual_machine/edit.yml +77 -0
  78. data/spec/vcr_cassettes/virtual_machine/list.yml +40 -0
  79. data/spec/vcr_cassettes/virtual_machine/migrate.yml +116 -0
  80. data/spec/vcr_cassettes/virtual_machine/reboot.yml +77 -0
  81. data/spec/vcr_cassettes/virtual_machine/resize.yml +77 -0
  82. data/spec/vcr_cassettes/virtual_machine/show.yml +77 -0
  83. data/spec/vcr_cassettes/virtual_machine/shutdown.yml +77 -0
  84. data/spec/vcr_cassettes/virtual_machine/startup.yml +77 -0
  85. data/spec/vcr_cassettes/virtual_machine/stop.yml +77 -0
  86. data/spec/vcr_cassettes/virtual_machine/suspend.yml +77 -0
  87. data/spec/vcr_cassettes/virtual_machine/unlock.yml +77 -0
  88. data/spec/vcr_cassettes/virtual_machine/unsuspend.yml +77 -0
  89. data/squall.gemspec +25 -62
  90. metadata +224 -42
  91. data/.document +0 -5
  92. data/README.rdoc +0 -17
  93. data/VERSION.yml +0 -5
  94. data/lib/squall/client.rb +0 -87
  95. data/test/fixtures/virtual_machines.json +0 -1
  96. data/test/fixtures/virtual_machines_1.json +0 -1
  97. data/test/helper.rb +0 -31
  98. data/test/test_client.rb +0 -152
  99. data/test/test_squall.rb +0 -12
  100. data/test/test_virtual_machine.rb +0 -98
data/lib/squall/client.rb DELETED
@@ -1,87 +0,0 @@
1
- module Squall
2
- class Client
3
-
4
- attr_reader :successful, :request, :response, :result, :message, :raw_response
5
-
6
- def initialize
7
- @default_options = {:accept => :json, :content_type => 'application/json'}
8
- @debug = false
9
- end
10
-
11
- def toggle_debug
12
- if @debug
13
- @debug = false
14
- RestClient.log = nil
15
- else
16
- @debug = true
17
- RestClient.log = STDERR
18
- end
19
- end
20
-
21
- def get(uri)
22
- RestClient.get("#{uri_with_auth}/#{uri}.json", @default_options) { |_response, _request, _result|
23
- handle_response _response, _request, _result
24
- }
25
- end
26
-
27
- def post(uri, params = {})
28
- RestClient.post("#{uri_with_auth}/#{uri}.json", params.to_json, @default_options) { |_response, _request, _result|
29
- handle_response _response, _request, _result
30
- }
31
- end
32
-
33
- def put(uri, params = {})
34
- RestClient.put("#{uri_with_auth}/#{uri}.json", params.to_json, @default_options) { |_response, _request, _result|
35
- handle_response _response, _request, _result
36
- }
37
- end
38
-
39
- def delete(uri)
40
- RestClient.delete("#{uri_with_auth}/#{uri}.json", @default_options) { |_response, _request, _result|
41
- handle_response _response, _request, _result
42
- }
43
- end
44
-
45
- private
46
-
47
- def required_options!(required, actual)
48
- required = required
49
- actual = actual.keys
50
- missing_keys = required - [actual].flatten
51
- raise(ArgumentError, "Missing key(s): #{missing_keys.join(", ")}") unless missing_keys.empty?
52
- end
53
-
54
- def valid_options!(accepted, actual)
55
- actual = actual.keys if actual.is_a?(Hash)
56
- unknown_keys = actual - accepted
57
- raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
58
- end
59
-
60
- def uri_with_auth
61
- uri = Squall.api_endpoint
62
- protocol = uri.port == 443 ? 'https' : 'http'
63
- "#{protocol}://#{Squall.api_user}:#{Squall.api_password}@#{uri.host}#{uri.path}"
64
- end
65
-
66
- def handle_response(_response, _request, _result)
67
- @request = _request
68
- @result = _result
69
- @response = _response
70
- @raw_response = _response
71
- @successful = (200..207).include?(_response.code)
72
-
73
- case _response.code
74
- when 404
75
- @message = "404 Not Found"
76
- when 403
77
- @message = "Action is not permitted for that account"
78
- when 422
79
- @message = "Request Failed: #{@response}"
80
- else
81
- @message = (_response.strip.empty? ? _response : JSON.parse(_response))
82
- _response.return!
83
- end
84
- @successful
85
- end
86
- end
87
- end
@@ -1 +0,0 @@
1
- [{"virtual_machine":{"label":"label1","cpus":2,"operating_system_distro":"rhel","created_at":"2010-11-16T17:37:23Z","template_id":9,"operating_system":"linux","cpu_shares":16,"updated_at":"2010-11-16T17:37:23Z","memory":512,"local_remote_access_port":null,"allowed_swap":true,"recovery_mode":null,"allow_resize_without_reboot":true,"xen_id":null,"id":39,"hypervisor_id":1,"total_disk_size":6,"ip_addresses":[],"user_id":5,"template_label":null,"hostname":"label1.example.com","booted":false,"monthly_bandwidth_used":0,"remote_access_password":null,"min_disk_size":5,"initial_root_password":"password1","identifier":"blahblah1","locked":false,"built":false}},{"virtual_machine":{"label":"label2","cpus":2,"operating_system_distro":"rhel","created_at":"2010-11-16T17:37:23Z","template_id":9,"operating_system":"linux","cpu_shares":16,"updated_at":"2010-11-16T17:37:23Z","memory":512,"local_remote_access_port":null,"allowed_swap":true,"recovery_mode":null,"allow_resize_without_reboot":true,"xen_id":null,"id":40,"hypervisor_id":1,"total_disk_size":6,"ip_addresses":[],"user_id":5,"template_label":null,"hostname":"label2.example.com","booted":false,"monthly_bandwidth_used":0,"remote_access_password":null,"min_disk_size":5,"initial_root_password":"password2","identifier":"blahblah2","locked":false,"built":false}}]
@@ -1 +0,0 @@
1
- {"virtual_machine":{"label":"label1","cpus":2,"operating_system_distro":"rhel","created_at":"2010-11-16T17:37:23Z","template_id":9,"operating_system":"linux","cpu_shares":16,"updated_at":"2010-11-16T17:37:23Z","memory":512,"local_remote_access_port":null,"allowed_swap":true,"recovery_mode":null,"allow_resize_without_reboot":true,"xen_id":null,"id":1,"hypervisor_id":1,"total_disk_size":6,"ip_addresses":[],"user_id":5,"template_label":null,"hostname":"label1.example.com","booted":false,"monthly_bandwidth_used":0,"remote_access_password":null,"min_disk_size":5,"initial_root_password":"password1","identifier":"woah","locked":false,"built":false}}
data/test/helper.rb DELETED
@@ -1,31 +0,0 @@
1
- require 'rubygems'
2
- require 'test/unit'
3
- require 'redgreen'
4
-
5
- $LOAD_PATH.unshift(File.dirname(__FILE__))
6
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
-
8
- require 'squall'
9
- require 'fakeweb'
10
-
11
- class Test::Unit::TestCase
12
-
13
- def setup
14
- FakeWeb.allow_net_connect = false
15
- FakeWeb.clean_registry
16
- Squall.config('user', 'stupidpass', 'http://example.com/onapp')
17
- end
18
-
19
- def uri_with_login
20
- "http://#{Squall.api_user}:#{Squall.api_password}@#{Squall.api_endpoint.host}#{Squall.api_endpoint.path}"
21
- end
22
-
23
- def stub_json_request(meth, uri, content = nil, code = 200)
24
- content = File.read(File.join(File.dirname(__FILE__), "fixtures/#{uri.gsub('/', '_')}.json")) if content.nil?
25
- fake_response = Net::HTTPOK.new('1.1', code, 'OK')
26
- fake_response['Content-Type'] = 'application/json'
27
- fake_response.instance_variable_set('@body', content)
28
- FakeWeb.register_uri(meth, "#{uri_with_login}/#{uri}.json", :content_type => 'application/json', :response => fake_response)
29
- end
30
-
31
- end
data/test/test_client.rb DELETED
@@ -1,152 +0,0 @@
1
- require File.dirname(__FILE__) + '/helper'
2
-
3
- class TestClient < Test::Unit::TestCase
4
-
5
- def setup
6
- super
7
- @client = Squall::Client.new
8
- assert_defaults
9
- end
10
-
11
- def test_init
12
- default_options = {:accept => :json, :content_type => 'application/json'}
13
- assert_equal default_options, @client.instance_variable_get('@default_options')
14
- assert_equal false, @client.instance_variable_get('@debug')
15
- assert_nil defined?(Squall::Client::URI_PREFIX)
16
- end
17
-
18
- def test_debug
19
- assert_nil RestClient.log
20
- assert_equal false, @client.instance_variable_get('@debug')
21
-
22
- @client.toggle_debug
23
- assert_equal true, @client.instance_variable_get('@debug')
24
- assert_equal STDERR, RestClient.log
25
-
26
- @client.toggle_debug
27
- assert_nil RestClient.log
28
- assert_equal false, @client.instance_variable_get('@debug')
29
- end
30
-
31
- def test_get
32
- stub_json_request(:get, 'test_get', '["test"]')
33
- @client.get('test_get')
34
-
35
- assert_equal :get, @client.request.method
36
- end
37
-
38
- def test_post
39
- stub_json_request(:post, 'test_post', '["test"]')
40
- @client.post('test_post')
41
- assert_equal :post, @client.request.method
42
- end
43
-
44
- def test_put
45
- stub_json_request(:put, 'test_put', '["test"]')
46
- @client.put('test_put')
47
- assert_equal :put, @client.request.method
48
- end
49
-
50
- def test_delete
51
- stub_json_request(:delete, 'test_delete', '["test"]')
52
- @client.delete('test_delete')
53
- assert_equal :delete, @client.request.method
54
- end
55
-
56
- def test_required_options
57
- assert_nothing_raised do
58
- required_options = [:one, :two, :three]
59
- actual_options = {:one => 1, :two => 2, :three => 3}
60
- @client.send(:required_options!, required_options, actual_options)
61
- end
62
-
63
- assert_nothing_raised do
64
- required_options = [:one, :two, :three]
65
- actual_options = {:one => 1, :two => 2, :three => 3, :four => 4}
66
- @client.send(:required_options!, required_options, actual_options)
67
- end
68
-
69
- e = assert_raises ArgumentError do
70
- required_options = [:one, :two, :three]
71
- actual_options = {:one => 1 }
72
- @client.send(:required_options!, required_options, actual_options)
73
- end
74
- assert_match /Missing key\(s\): two, three/, e.message
75
- end
76
-
77
- def test_valid_options
78
- assert_nothing_raised do
79
- accepted = [:one, :two]
80
- actual = {:one => 1, :two => 2}
81
- @client.send(:valid_options!, accepted, actual)
82
- end
83
-
84
- e = assert_raises ArgumentError do
85
- accepted = [:one, :two]
86
- actual = {:one => 1, :two => 2, :three => 3}
87
- @client.send(:valid_options!, accepted, actual)
88
- end
89
- assert_match /Unknown key\(s\): three/, e.message
90
- end
91
-
92
- def test_uri_with_auth
93
- assert_equal "http://user:stupidpass@example.com/onapp", @client.send(:uri_with_auth)
94
-
95
- Squall.config('user', 'stupidpass', 'example.com/onapp')
96
- assert_equal "http://user:stupidpass@example.com/onapp", @client.send(:uri_with_auth)
97
-
98
- Squall.config('user', 'stupidpass', 'https://example.com/onapp')
99
- assert_equal "https://user:stupidpass@example.com/onapp", @client.send(:uri_with_auth)
100
- end
101
-
102
- def test_handle_response_success
103
- stub_json_request(:get, 'handle_response_success', '["response"]')
104
- @client.get('handle_response_success')
105
-
106
- assert_instance_of RestClient::Request, @client.request
107
- assert_instance_of Net::HTTPOK, @client.result
108
-
109
- assert_equal true, @client.response.respond_to?(:code)
110
- assert_equal 200, @client.response.code
111
- assert_equal ["response"], @client.message
112
- assert_equal true, @client.successful
113
- assert_equal '["response"]', @client.response
114
- end
115
-
116
- def test_handle_response_error
117
- FakeWeb.register_uri(:get, "#{uri_with_login}/handle_response_error_404.json",
118
- :content_type => 'application/json', :body => '', :status => ["404", "Not Found"])
119
- @client.get('handle_response_error_404')
120
- assert_equal "404 Not Found", @client.message
121
- assert_equal false, @client.successful
122
-
123
- FakeWeb.register_uri(:get, "#{uri_with_login}/handle_response_error_403.json",
124
- :content_type => 'application/json', :body => '', :status => ["403", "Forbidden"])
125
- @client.get('handle_response_error_403')
126
- assert_equal "Action is not permitted for that account", @client.message
127
- assert_equal false, @client.successful
128
-
129
- FakeWeb.register_uri(:get, "#{uri_with_login}/handle_response_error_422.json",
130
- :content_type => 'application/json', :body => '422', :status => ["422", "Failed"])
131
- @client.get('handle_response_error_422')
132
- assert_equal "Request Failed: 422", @client.message
133
- assert_equal false, @client.successful
134
-
135
- FakeWeb.register_uri(:get, "#{uri_with_login}/handle_response_error_500.json",
136
- :content_type => 'application/json', :body => '', :status => ["500", "Failed"])
137
- e = assert_raises RestClient::InternalServerError do
138
- @client.get('handle_response_error_500')
139
- end
140
- assert_match /Internal Server Error/i, e.message
141
- assert_equal false, @client.successful
142
- end
143
-
144
- def assert_defaults
145
- assert_nil @client.response
146
- assert_nil @client.result
147
- assert_nil @client.response
148
- assert_nil @client.successful
149
- assert_nil @client.message
150
- end
151
-
152
- end
data/test/test_squall.rb DELETED
@@ -1,12 +0,0 @@
1
- require File.dirname(__FILE__) + '/helper'
2
-
3
- class TestSquall < Test::Unit::TestCase
4
-
5
- def test_config
6
- url = 'http://example.com/onapp'
7
- assert_equal 'user', Squall.api_user
8
- assert_equal 'stupidpass', Squall.api_password
9
- assert_not_nil Squall.api_endpoint
10
- assert_equal URI.parse(url), Squall.api_endpoint
11
- end
12
- end
@@ -1,98 +0,0 @@
1
- require File.dirname(__FILE__) + '/helper'
2
-
3
- class TestVirtualMachine < Test::Unit::TestCase
4
- def setup
5
- super
6
- @virtual = Squall::VirtualMachine.new
7
- end
8
-
9
- def test_uri_prefix
10
- assert_equal 'virtual_machines' , Squall::VirtualMachine::URI_PREFIX
11
- end
12
-
13
- def test_list
14
- stub_json_request(:get, 'virtual_machines')
15
- vms = @virtual.list
16
- assert_equal 2, vms.size
17
- assert_equal 'label1', vms[0]['label']
18
- assert_equal 'label2', vms[1]['label']
19
- assert_equal true, @virtual.successful
20
- assert_instance_of Array, vms
21
- end
22
-
23
- def test_show
24
- stub_json_request(:get, 'virtual_machines/1')
25
- show = @virtual.show(1)
26
- assert_instance_of Hash, show
27
- expected = {"monthly_bandwidth_used"=>0, "cpus"=>2, "label"=>"label1",
28
- "created_at"=>"2010-11-16T17:37:23Z", "operating_system_distro"=>"rhel",
29
- "cpu_shares"=>16, "operating_system"=>"linux", "template_id"=>9,
30
- "allowed_swap"=>true, "local_remote_access_port"=>nil, "memory"=>512,
31
- "updated_at"=>"2010-11-16T17:37:23Z", "allow_resize_without_reboot"=>true,
32
- "recovery_mode"=>nil, "hypervisor_id"=>1, "id"=>1, "xen_id"=>nil,
33
- "user_id"=>5, "booted"=>false, "hostname"=>"label1.example.com",
34
- "template_label"=>nil, "total_disk_size"=>6, "identifier"=>"woah",
35
- "initial_root_password"=>"password1", "min_disk_size"=>5, "remote_access_password"=>nil,
36
- "built"=>false, "locked"=>false, "ip_addresses"=>[]}
37
- assert_equal expected, show
38
- assert_equal true, @virtual.successful
39
- end
40
-
41
- def test_edit
42
- stub_json_request(:put, 'virtual_machines/1', '')
43
- edit = @virtual.edit(1, :cpus => 1)
44
- assert_equal true, edit
45
- assert_equal true, @virtual.successful
46
-
47
- e = assert_raises ArgumentError do
48
- @virtual.edit(1, :what => 1)
49
- end
50
- assert_match /Unknown key\(s\): what/, e.message
51
- end
52
-
53
- def test_create
54
- params = {:memory => 128, :cpus => 1, :label => 'test_create', :template_id => 1,
55
- :hypervisor_id => 1, :initial_root_password => 'hi', :hostname => 'test_create'}
56
-
57
- response = '{"virtual_machine":{"primary_network_id":"1", "cpus":"1", "label":"test_create",
58
- "cpu_shares":"10", "template_id":"1", "swap_disk_size":"1", "memory":"128",
59
- "required_virtual_machine_build":"1", "hypervisor_id":"1",
60
- "required_ip_address_assignment":"1", "rate_limit":"10",
61
- "primary_disk_size":"5", "hostname":"s",
62
- "initial_root_password":"hi"} }'
63
-
64
- stub_json_request(:post, 'virtual_machines', response)
65
-
66
- create = @virtual.create(params)
67
- assert_equal true, create
68
- assert_equal JSON.parse(response), @virtual.message
69
- end
70
-
71
- def test_destroy
72
- stub_json_request(:delete, 'virtual_machines/1', '')
73
- destroy = @virtual.destroy(1)
74
- assert_equal true, destroy
75
- end
76
-
77
- def test_reboot
78
- stub_json_request(:post, 'virtual_machines/1/reboot', '')
79
- reboot = @virtual.reboot(1)
80
- assert_equal true, reboot
81
- end
82
-
83
- def test_search
84
- stub_json_request(:get, 'virtual_machines')
85
- search = @virtual.search('label2')
86
- assert_equal 1, search.size
87
-
88
- search = @virtual.search('label2', :label)
89
- assert_equal 1, search.size
90
-
91
- search = @virtual.search('label', :label)
92
- assert_equal 2, search.size
93
-
94
- search = @virtual.search(1, :hypervisor_id)
95
- assert_equal 2, search.size
96
- end
97
-
98
- end