squall 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  ---
2
- :patch: 2
2
+ :patch: 3
3
3
  :build:
4
4
  :major: 0
5
5
  :minor: 0
data/lib/squall/client.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Squall
2
2
  class Client
3
3
 
4
- attr_reader :successful, :request, :response, :result, :message
4
+ attr_reader :successful, :request, :response, :result, :message, :raw_response
5
5
 
6
6
  def initialize
7
7
  @default_options = {:accept => :json, :content_type => 'application/json'}
@@ -52,7 +52,8 @@ module Squall
52
52
  end
53
53
 
54
54
  def valid_options!(accepted, actual)
55
- unknown_keys = actual.keys - accepted
55
+ actual = actual.keys if actual.is_a?(Hash)
56
+ unknown_keys = actual - accepted
56
57
  raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
57
58
  end
58
59
 
@@ -63,9 +64,10 @@ module Squall
63
64
  end
64
65
 
65
66
  def handle_response(_response, _request, _result)
66
- @request = _request
67
- @result = _result
68
- @response = _response
67
+ @request = _request
68
+ @result = _result
69
+ @response = _response
70
+ @raw_response = _response
69
71
  @successful = (200..207).include?(_response.code)
70
72
 
71
73
  case _response.code
@@ -1,7 +1,21 @@
1
1
  module Squall
2
2
  class VirtualMachine < Client
3
3
 
4
- URI_PREFIX = 'virtual_machines'
4
+ URI_PREFIX = 'virtual_machines'
5
+ VALID_PARAMS = [:primary_network_id,
6
+ :cpus,
7
+ :label,
8
+ :cpu_shares,
9
+ :template_id,
10
+ :swap_disk_size,
11
+ :memory,
12
+ :required_virtual_machine_build,
13
+ :hypervisor_id,
14
+ :required_ip_address_assignment,
15
+ :rate_limit,
16
+ :primary_disk_size,
17
+ :hostname,
18
+ :initial_root_password ]
5
19
 
6
20
  def list
7
21
  if get(URI_PREFIX)
@@ -16,28 +30,8 @@ module Squall
16
30
  end
17
31
 
18
32
  def edit(id, params = {})
19
- valid = [ :primary_network_id,
20
- :cpus,
21
- :label,
22
- :cpu_shares,
23
- :template_id,
24
- :swap_disk_size,
25
- :memory,
26
- :required_virtual_machine_build,
27
- :hypervisor_id,
28
- :required_ip_address_assignment,
29
- :rate_limit,
30
- :primary_disk_size,
31
- :hostname,
32
- :initial_root_password,
33
- :reboot ]
34
-
35
- valid_options!(valid, params)
36
-
37
- reboot_after = params.delete(:reboot)
38
- update_request = put("#{URI_PREFIX}/#{id}", { :virtual_machine => params })
39
- reboot(id) if reboot_after
40
- update_request
33
+ valid_options!(VALID_PARAMS, params)
34
+ put("#{URI_PREFIX}/#{id}", { :virtual_machine => params })
41
35
  end
42
36
 
43
37
  def create(params = {})
@@ -55,9 +49,10 @@ module Squall
55
49
  end
56
50
 
57
51
  def search(pattern, *fields)
58
- fields = [:label] if fields.nil?
52
+ fields = [:label] if fields.empty?
53
+ valid_options!(VALID_PARAMS, fields)
59
54
  list.select do |vm|
60
- fields.detect { |field| vm[field.to_s].match(/#{pattern}/) }
55
+ fields.detect { |field| vm.has_key?(field.to_s) && vm[field.to_s].to_s.match(/#{pattern}/) }
61
56
  end
62
57
  end
63
58
  end
data/squall.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{squall}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Justin Mazzi"]
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
28
28
  "lib/squall/virtual_machine.rb",
29
29
  "squall.gemspec",
30
30
  "test/fixtures/virtual_machines.json",
31
+ "test/fixtures/virtual_machines_1.json",
31
32
  "test/helper.rb",
32
33
  "test/test_client.rb",
33
34
  "test/test_squall.rb",
@@ -0,0 +1 @@
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 CHANGED
@@ -20,9 +20,9 @@ class Test::Unit::TestCase
20
20
  "http://#{Squall.api_user}:#{Squall.api_password}@#{Squall.api_endpoint.host}#{Squall.api_endpoint.path}"
21
21
  end
22
22
 
23
- def stub_json_request(meth, uri, content = nil)
24
- content = File.read(File.join(File.dirname(__FILE__), "fixtures/#{uri}.json")) if content.nil?
25
- fake_response = Net::HTTPOK.new('1.1', '200', 'OK')
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
26
  fake_response['Content-Type'] = 'application/json'
27
27
  fake_response.instance_variable_set('@body', content)
28
28
  FakeWeb.register_uri(meth, "#{uri_with_login}/#{uri}.json", :content_type => 'application/json', :response => fake_response)
@@ -16,6 +16,83 @@ class TestVirtualMachine < Test::Unit::TestCase
16
16
  assert_equal 2, vms.size
17
17
  assert_equal 'label1', vms[0]['label']
18
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
19
70
 
71
+ def test_destroy
72
+ stub_json_request(:delete, 'virtual_machines/1', '')
73
+ destroy = @virtual.destroy(1)
74
+ assert_equal true, destroy
20
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
+
21
98
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: squall
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Justin Mazzi
@@ -95,6 +95,7 @@ files:
95
95
  - lib/squall/virtual_machine.rb
96
96
  - squall.gemspec
97
97
  - test/fixtures/virtual_machines.json
98
+ - test/fixtures/virtual_machines_1.json
98
99
  - test/helper.rb
99
100
  - test/test_client.rb
100
101
  - test/test_squall.rb