vcloud-core 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/.gitignore +2 -0
  2. data/Gemfile +9 -0
  3. data/LICENSE.txt +20 -0
  4. data/README.md +103 -0
  5. data/Rakefile +18 -0
  6. data/bin/vcloud-query +84 -0
  7. data/jenkins.sh +10 -0
  8. data/lib/vcloud/core.rb +23 -0
  9. data/lib/vcloud/core/compute_metadata.rb +13 -0
  10. data/lib/vcloud/core/edge_gateway.rb +46 -0
  11. data/lib/vcloud/core/entity.rb +23 -0
  12. data/lib/vcloud/core/metadata_helper.rb +29 -0
  13. data/lib/vcloud/core/org_vdc_network.rb +102 -0
  14. data/lib/vcloud/core/query.rb +142 -0
  15. data/lib/vcloud/core/vapp.rb +118 -0
  16. data/lib/vcloud/core/vapp_template.rb +39 -0
  17. data/lib/vcloud/core/vdc.rb +37 -0
  18. data/lib/vcloud/core/version.rb +5 -0
  19. data/lib/vcloud/core/vm.rb +162 -0
  20. data/lib/vcloud/fog.rb +5 -0
  21. data/lib/vcloud/fog/content_types.rb +20 -0
  22. data/lib/vcloud/fog/model_interface.rb +33 -0
  23. data/lib/vcloud/fog/relation.rb +8 -0
  24. data/lib/vcloud/fog/service_interface.rb +257 -0
  25. data/spec/spec_helper.rb +26 -0
  26. data/spec/support/stub_fog_interface.rb +59 -0
  27. data/spec/vcloud/core/edge_gateway_spec.rb +79 -0
  28. data/spec/vcloud/core/metadata_helper_spec.rb +89 -0
  29. data/spec/vcloud/core/org_vdc_network_spec.rb +257 -0
  30. data/spec/vcloud/core/query_spec.rb +111 -0
  31. data/spec/vcloud/core/vapp_spec.rb +173 -0
  32. data/spec/vcloud/core/vapp_template_spec.rb +77 -0
  33. data/spec/vcloud/core/vdc_spec.rb +68 -0
  34. data/spec/vcloud/core/vm_spec.rb +290 -0
  35. data/spec/vcloud/data/basic_preamble_test.erb +8 -0
  36. data/spec/vcloud/data/basic_preamble_test.erb.OUT +8 -0
  37. data/spec/vcloud/fog/fog_model_interface_spec.rb +25 -0
  38. data/spec/vcloud/fog/service_interface_spec.rb +30 -0
  39. data/vcloud-core.gemspec +30 -0
  40. metadata +198 -0
@@ -0,0 +1,173 @@
1
+ require 'spec_helper'
2
+
3
+ module Vcloud
4
+ module Core
5
+ describe Vapp do
6
+ before(:each) do
7
+ @vapp_id = 'vapp-12345678-1234-1234-1234-000000111111'
8
+ @mock_fog_interface = StubFogInterface.new
9
+ Vcloud::Fog::ServiceInterface.stub(:new).and_return(@mock_fog_interface)
10
+ end
11
+
12
+ context "Class public interface" do
13
+ it { Vapp.should respond_to(:instantiate) }
14
+ it { Vapp.should respond_to(:get_by_name) }
15
+ it { Vapp.should respond_to(:get_metadata) }
16
+ end
17
+
18
+ context "Instance public interface" do
19
+ subject { Vapp.new(@vapp_id) }
20
+ it { should respond_to(:id) }
21
+ it { should respond_to(:vcloud_attributes) }
22
+ it { should respond_to(:name) }
23
+ it { should respond_to(:href) }
24
+ it { should respond_to(:vdc_id) }
25
+ it { should respond_to(:fog_vms) }
26
+ it { should respond_to(:networks) }
27
+ it { should respond_to(:power_on) }
28
+ end
29
+
30
+ context "#initialize" do
31
+
32
+ it "should be constructable from just an id reference" do
33
+ obj = Vapp.new(@vapp_id)
34
+ expect(obj.class).to be(Vcloud::Core::Vapp)
35
+ end
36
+
37
+ it "should store the id specified" do
38
+ obj = Vapp.new(@vapp_id)
39
+ expect(obj.id) == @vapp_id
40
+ end
41
+
42
+ it "should raise error if id is not in correct format" do
43
+ bogus_id = '12314124-ede5-4d07-bad5-000000111111'
44
+ expect{ Vapp.new(bogus_id) }.to raise_error("vapp id : #{bogus_id} is not in correct format" )
45
+ end
46
+
47
+ end
48
+
49
+ context "#get_by_name" do
50
+
51
+ it "should return a Vapp object if name exists" do
52
+ q_results = [
53
+ { :name => 'vapp-test-1', :href => @vapp_id }
54
+ ]
55
+ mock_query = double(:query, :get_all_results => q_results)
56
+ Vcloud::Query.should_receive(:new).with('vApp', :filter => "name==vapp-test-1").and_return(mock_query)
57
+ obj = Vapp.get_by_name('vapp-test-1')
58
+ expect(obj.class).to be(Vcloud::Core::Vapp)
59
+ end
60
+
61
+ it "should raise an error if no vApp with that name exists" do
62
+ q_results = [ ]
63
+ mock_query = double(:query, :get_all_results => q_results)
64
+ Vcloud::Query.should_receive(:new).with('vApp', :filter => "name==vapp-test-1").and_return(mock_query)
65
+ expect{ Vapp.get_by_name('vapp-test-1') }.to raise_exception(RuntimeError)
66
+ end
67
+
68
+ it "should raise an error if multiple vApps with that name exists (NB: prescribes unique vApp names!)" do
69
+ q_results = [
70
+ { :name => 'vapp-test-1', :href => @vapp_id },
71
+ { :name => 'vapp-test-1', :href => '/bogus' },
72
+ ]
73
+ mock_query = double(:query, :get_all_results => q_results)
74
+ Vcloud::Query.should_receive(:new).with('vApp', :filter => "name==vapp-test-1").and_return(mock_query)
75
+ expect{ Vapp.get_by_name('vapp-test-1') }.to raise_exception(RuntimeError)
76
+ end
77
+
78
+ end
79
+
80
+ context "attributes" do
81
+ before(:each) {
82
+ @stub_attrs = {
83
+ :name => 'Webserver vapp-1',
84
+ :href => "https://api.vcd.portal.skyscapecloud.com/api/vApp/#{@vapp_id}",
85
+ :Link => [{
86
+ :rel => 'up',
87
+ :type => 'application/vnd.vmware.vcloud.vdc+xml',
88
+ :href => 'https://api.vcloud-director.example.com/api/vdc/074aea1e-a5e9-4dd1-a028-40db8c98d237'
89
+ }],
90
+ :Children => {:Vm => [{:href => '/vm-123aea1e-a5e9-4dd1-a028-40db8c98d237'}]}
91
+ }
92
+ StubFogInterface.any_instance.stub(:get_vapp).and_return(@stub_attrs)
93
+ @vapp = Vapp.new(@vapp_id)
94
+ }
95
+ it { @vapp.name.should == 'Webserver vapp-1' }
96
+
97
+ context "id" do
98
+ it "should extract id correctly" do
99
+ @vapp.id.should == @vapp_id
100
+ end
101
+ end
102
+
103
+ context "vapp should have parent vdc" do
104
+ it "should load parent vdc id from fog attributes" do
105
+ @vapp.vdc_id.should == '074aea1e-a5e9-4dd1-a028-40db8c98d237'
106
+ end
107
+
108
+ it "should raise error if vapp without parent vdc found" do
109
+ @stub_attrs[:Link] = []
110
+ lambda { @vapp.vdc_id }.should raise_error('a vapp without parent vdc found')
111
+ end
112
+ end
113
+
114
+ it "should return vms" do
115
+ @vapp.fog_vms.count.should == 1
116
+ @vapp.fog_vms.first[:href].should == '/vm-123aea1e-a5e9-4dd1-a028-40db8c98d237'
117
+ end
118
+ end
119
+
120
+ context "power on" do
121
+ context "successful power on" do
122
+ before(:each) do
123
+ @fog_vapp_body = {
124
+ :name => "Webserver vapp-1",
125
+ :href => "https://api.vcloud-director.example.com/api/vApp/vapp-63d3be58-2d5c-477d-8410-267e7c3c4a02",
126
+ :Link => [{
127
+ :rel => "up",
128
+ :type => "application/vnd.vmware.vcloud.vdc+xml",
129
+ :href => "https://api.vcloud-director.example.com/api/vdc/074aea1e-a5e9-4dd1-a028-40db8c98d237"
130
+ }]
131
+ }
132
+ end
133
+
134
+ it "should power on a vapp that is not powered on" do
135
+ vapp = Vapp.new(@vapp_id)
136
+ @mock_fog_interface.should_receive(:get_vapp).twice().and_return({:status => 8})
137
+ @mock_fog_interface.should_receive(:power_on_vapp).with(vapp.id)
138
+ state = vapp.power_on
139
+ expect(state) == true
140
+ end
141
+
142
+ it "should not power on a vapp that is already powered on, but should return true" do
143
+ vapp = Vapp.new(@vapp_id)
144
+ @mock_fog_interface.should_receive(:get_vapp).and_return({:status => 4})
145
+ @mock_fog_interface.should_not_receive(:power_on_vapp)
146
+ state = vapp.power_on
147
+ expect(state) == true
148
+ end
149
+ end
150
+
151
+ end
152
+
153
+ context "#get_by_name_and_vdc_name" do
154
+
155
+ it "should return nil if fog returns nil" do
156
+ StubFogInterface.any_instance.stub(:get_vapp_by_name_and_vdc_name)
157
+ .with('vapp_name', 'vdc_name').and_return(nil)
158
+ Vapp.get_by_name_and_vdc_name('vapp_name', 'vdc_name').should == nil
159
+ end
160
+
161
+ it "should return vapp instance if found" do
162
+ vcloud_attr_vapp = { :href => "/#{@vapp_id}" }
163
+ StubFogInterface.any_instance.stub(:get_vapp_by_name_and_vdc_name)
164
+ .with('vapp_name', 'vdc_name').and_return(vcloud_attr_vapp)
165
+ Vapp.get_by_name_and_vdc_name('vapp_name', 'vdc_name').class.should == Core::Vapp
166
+ end
167
+
168
+ end
169
+
170
+ end
171
+ end
172
+ end
173
+
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ module Vcloud
4
+ module Core
5
+ describe VappTemplate do
6
+
7
+ before(:each) do
8
+ @id = 'vappTemplate-12345678-1234-1234-1234-000000234121'
9
+ @mock_fog_interface = StubFogInterface.new
10
+ Vcloud::Fog::ServiceInterface.stub(:new).and_return(@mock_fog_interface)
11
+ @test_config = {
12
+ :catalog => 'test_catalog',
13
+ :catalog_item => 'test_template'
14
+ }
15
+ end
16
+
17
+ context "Class public interface" do
18
+ it { VappTemplate.should respond_to(:get) }
19
+ end
20
+
21
+ context "Instance public interface" do
22
+ subject { VappTemplate.new(@id) }
23
+ it { should respond_to(:id) }
24
+ it { should respond_to(:vcloud_attributes) }
25
+ it { should respond_to(:name) }
26
+ it { should respond_to(:href) }
27
+ end
28
+
29
+ context "#initialize" do
30
+
31
+ it "should be constructable from just an id reference" do
32
+ obj = VappTemplate.new(@id)
33
+ expect(obj.class).to be(Vcloud::Core::VappTemplate)
34
+ end
35
+
36
+ it "should store the id specified" do
37
+ obj = VappTemplate.new(@id)
38
+ expect(obj.id) == @id
39
+ end
40
+
41
+ it "should raise error if id is not in correct format" do
42
+ bogus_id = '12314124-ede5-4d07-bad5-000000111111'
43
+ expect{ VappTemplate.new(bogus_id) }.to raise_error("vappTemplate id : #{bogus_id} is not in correct format" )
44
+ end
45
+
46
+ end
47
+
48
+
49
+ context '#get' do
50
+
51
+ it 'should raise a RuntimeError if there is no template' do
52
+ @mock_fog_interface.should_receive(:template).
53
+ with('test_catalog', 'test_template').and_return(nil)
54
+ expect { VappTemplate.get('test_catalog', 'test_template') }.
55
+ to raise_error('Could not find template vApp')
56
+ end
57
+
58
+
59
+ it 'should return a valid template object if it exists' do
60
+ test_catalog_item_entity = {
61
+ :href => "/vappTemplate-12345678-90ab-cdef-0123-4567890abcde"
62
+ }
63
+ @mock_fog_interface.should_receive(:template).
64
+ with('test_catalog', 'test_template').
65
+ and_return(test_catalog_item_entity)
66
+ Vcloud::Fog::ServiceInterface.should_receive(:new).
67
+ and_return(@mock_fog_interface)
68
+
69
+ test_template = VappTemplate.get('test_catalog', 'test_template')
70
+ test_template.id.should == 'vappTemplate-12345678-90ab-cdef-0123-4567890abcde'
71
+ end
72
+
73
+ end
74
+
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+
3
+ module Vcloud
4
+ module Core
5
+ describe Vdc do
6
+
7
+ before(:each) do
8
+ @vdc_id = '12345678-1234-1234-1234-000000111232'
9
+ @mock_fog_interface = StubFogInterface.new
10
+ Vcloud::Fog::ServiceInterface.stub(:new).and_return(@mock_fog_interface)
11
+ end
12
+
13
+ context "Class public interface" do
14
+ it { Vdc.should respond_to(:get_by_name) }
15
+ end
16
+
17
+ context "Instance public interface" do
18
+ subject { Vdc.new(@vdc_id) }
19
+ it { should respond_to(:id) }
20
+ it { should respond_to(:name) }
21
+ it { should respond_to(:href) }
22
+ end
23
+
24
+ context "#initialize" do
25
+
26
+ it "should be constructable from just an id reference" do
27
+ obj = Vdc.new(@vdc_id)
28
+ expect(obj.class).to be(Vcloud::Core::Vdc)
29
+ end
30
+
31
+ it "should store the id specified" do
32
+ obj = Vdc.new(@vdc_id)
33
+ expect(obj.id) == @vdc_id
34
+ end
35
+
36
+ it "should raise error if id is not in correct format" do
37
+ bogus_id = '123123-bogus-id-123445'
38
+ expect{ Vdc.new(bogus_id) }.to raise_error("vdc id : #{bogus_id} is not in correct format" )
39
+ end
40
+
41
+ end
42
+
43
+ context "#get_by_name" do
44
+
45
+ it "should return a Vdc object if name exists" do
46
+ q_results = [
47
+ { :name => 'vdc-test-1', :href => @vdc_id }
48
+ ]
49
+ mock_query = double(:query, :get_all_results => q_results)
50
+ Vcloud::Query.should_receive(:new).with('orgVdc', :filter => "name==vdc-test-1").and_return(mock_query)
51
+ obj = Vdc.get_by_name('vdc-test-1')
52
+ expect(obj.class).to be(Vcloud::Core::Vdc)
53
+ end
54
+
55
+ it "should raise an error if no vDC with that name exists" do
56
+ q_results = [ ]
57
+ mock_query = double(:query, :get_all_results => q_results)
58
+ Vcloud::Query.should_receive(:new).with('orgVdc', :filter => "name==vdc-test-1").and_return(mock_query)
59
+ expect{ Vdc.get_by_name('vdc-test-1') }.to raise_exception(RuntimeError, "vDc vdc-test-1 not found")
60
+ end
61
+
62
+ end
63
+
64
+ end
65
+
66
+ end
67
+
68
+ end
@@ -0,0 +1,290 @@
1
+ require 'spec_helper'
2
+
3
+ module Vcloud
4
+ module Core
5
+ describe Vm do
6
+
7
+ before(:each) do
8
+ @vm_id = 'vm-1234'
9
+ @vapp_id = 'vapp-4321'
10
+ @vapp_name = 'test-vapp-1'
11
+ @vm_name = 'test-vm-1'
12
+ @data_dir = File.join(File.dirname(__FILE__), "../data")
13
+ @mock_vm_memory_size = 1024
14
+ @mock_metadata = {
15
+ :foo => "bar",
16
+ :false_thing => false,
17
+ :true_thing => true,
18
+ :number => 53,
19
+ :zero => 0,
20
+ }
21
+ @mock_vm_cpu_count = 1
22
+ @fog_interface = StubFogInterface.new
23
+ @mock_vapp = double(:vappm, :name => @vapp_name, :id => @vapp_id)
24
+ Vcloud::Fog::ServiceInterface.stub(:new).and_return(@fog_interface)
25
+ @fog_interface.stub(:get_vapp).with(@vm_id).and_return({
26
+ :name => "#{@vm_name}",
27
+ :href => "vm-href/#{@vm_id}",
28
+ :'ovf:VirtualHardwareSection' => {
29
+ :'ovf:Item' => [
30
+ {
31
+ :'rasd:ResourceType' => '4',
32
+ :'rasd:VirtualQuantity' => "#{@mock_vm_memory_size}",
33
+ },
34
+ {
35
+ :'rasd:ResourceType' => '3',
36
+ :'rasd:VirtualQuantity' => "#{@mock_vm_cpu_count}",
37
+ }
38
+ ]
39
+ }
40
+ })
41
+ @vm = Vm.new(@vm_id, @mock_vapp)
42
+ end
43
+
44
+ context "Class public interface" do
45
+ end
46
+
47
+ context "Instance public interface" do
48
+ subject { Vm.new(@vm_id, @mock_vapp) }
49
+ it { should respond_to(:id) }
50
+ it { should respond_to(:vcloud_attributes) }
51
+ it { should respond_to(:name) }
52
+ it { should respond_to(:href) }
53
+ it { should respond_to(:vapp_name) }
54
+ it { should respond_to(:update_name) }
55
+ it { should respond_to(:update_cpu_count) }
56
+ it { should respond_to(:update_metadata) }
57
+ it { should respond_to(:update_storage_profile) }
58
+ it { should respond_to(:add_extra_disks) }
59
+ it { should respond_to(:configure_network_interfaces) }
60
+ it { should respond_to(:configure_guest_customization_section) }
61
+ it { should respond_to(:generate_preamble) }
62
+ end
63
+
64
+ context "#initialize" do
65
+
66
+ it "should be constructable from just an id reference & Vapp object" do
67
+ obj = Vm.new(@vm_id, @mock_vapp)
68
+ expect(obj.class).to be(Vcloud::Core::Vm)
69
+ end
70
+
71
+ it "should store the id specified" do
72
+ obj = Vm.new(@vm_id, @mock_vapp)
73
+ expect(obj.id) == @vm_id
74
+ end
75
+
76
+ it "should raise error if id is not in correct format" do
77
+ bogus_id = '12314124-ede5-4d07-bad5-000000111111'
78
+ expect{ Vm.new(bogus_id, @mock_vapp) }.to raise_error("vm id : #{bogus_id} is not in correct format" )
79
+ end
80
+
81
+ end
82
+
83
+ context "update memory in VM" do
84
+ it "should not allow memory size < 64MB" do
85
+ @fog_interface.should_not_receive(:put_memory)
86
+ @vm.update_memory_size_in_mb(63)
87
+ end
88
+ it "should not update memory if is size has not changed" do
89
+ @fog_interface.should_not_receive(:put_memory)
90
+ @vm.update_memory_size_in_mb(@mock_vm_memory_size)
91
+ end
92
+ it "should gracefully handle a nil memory size" do
93
+ @fog_interface.should_not_receive(:put_memory)
94
+ @vm.update_memory_size_in_mb(nil)
95
+ end
96
+ it "should set memory size 64MB" do
97
+ @fog_interface.should_receive(:put_memory).with(@vm_id, 64)
98
+ @vm.update_memory_size_in_mb(64)
99
+ end
100
+ it "should set memory size 4096MB" do
101
+ @fog_interface.should_receive(:put_memory).with(@vm_id, 4096)
102
+ @vm.update_memory_size_in_mb(4096)
103
+ end
104
+ end
105
+
106
+ context "update the number of cpus in vm" do
107
+ it "should gracefully handle nil cpu count" do
108
+ @fog_interface.should_not_receive(:put_cpu)
109
+ @vm.update_cpu_count(nil)
110
+ end
111
+ it "should not update cpu if is count has not changed" do
112
+ @fog_interface.should_not_receive(:put_cpu)
113
+ @vm.update_cpu_count(@mock_vm_cpu_count)
114
+ end
115
+ it "should not allow a zero cpu count" do
116
+ @fog_interface.should_not_receive(:put_cpu)
117
+ @vm.update_cpu_count(0)
118
+ end
119
+ it "should update cpu count in input is ok" do
120
+ @fog_interface.should_receive(:put_cpu).with(@vm_id, 2)
121
+ @vm.update_cpu_count(2)
122
+ end
123
+ end
124
+
125
+ context '#generate_preamble' do
126
+ it "should interpolate vars hash into template" do
127
+ vars = {:message => 'hello world'}
128
+ erbfile = "#{@data_dir}/basic_preamble_test.erb"
129
+ expected_output = File.read("#{erbfile}.OUT")
130
+ @vm.generate_preamble(erbfile, nil, vars).should == expected_output
131
+ end
132
+
133
+ it "should interpolate vars hash and post-process template" do
134
+ vars = {:message => 'hello world'}
135
+ erbfile = "#{@data_dir}/basic_preamble_test.erb"
136
+ expected_output = File.read("#{erbfile}.OUT")
137
+ @vm.generate_preamble(erbfile, '/bin/cat', vars).should == expected_output
138
+ end
139
+ end
140
+
141
+ context "update metadata" do
142
+ it "should handle empty metadata hash" do
143
+ @fog_interface.should_not_receive(:put_vapp_metadata_value)
144
+ @vm.update_metadata(nil)
145
+ end
146
+ it "should handle metadata of multiple types" do
147
+ @fog_interface.should_receive(:put_vapp_metadata_value).with(@vm_id, :foo, 'bar')
148
+ @fog_interface.should_receive(:put_vapp_metadata_value).with(@vm_id, :false_thing, false)
149
+ @fog_interface.should_receive(:put_vapp_metadata_value).with(@vm_id, :true_thing, true)
150
+ @fog_interface.should_receive(:put_vapp_metadata_value).with(@vm_id, :number, 53)
151
+ @fog_interface.should_receive(:put_vapp_metadata_value).with(@vm_id, :zero, 0)
152
+ @fog_interface.should_receive(:put_vapp_metadata_value).with(@vapp_id, :foo, 'bar')
153
+ @fog_interface.should_receive(:put_vapp_metadata_value).with(@vapp_id, :false_thing, false)
154
+ @fog_interface.should_receive(:put_vapp_metadata_value).with(@vapp_id, :true_thing, true)
155
+ @fog_interface.should_receive(:put_vapp_metadata_value).with(@vapp_id, :number, 53)
156
+ @fog_interface.should_receive(:put_vapp_metadata_value).with(@vapp_id, :zero, 0)
157
+ @vm.update_metadata(@mock_metadata)
158
+ end
159
+ end
160
+
161
+ context "configure vm network interfaces" do
162
+ it "should configure single nic without an IP" do
163
+ network_config = [{:name => 'Default'}]
164
+ @fog_interface.should_receive(:put_network_connection_system_section_vapp).with(@vm_id, {
165
+ :PrimaryNetworkConnectionIndex => 0,
166
+ :NetworkConnection => [
167
+ {
168
+ :network => 'Default',
169
+ :needsCustomization => true,
170
+ :NetworkConnectionIndex => 0,
171
+ :IsConnected => true,
172
+ :IpAddressAllocationMode => "DHCP"
173
+ }
174
+ ]})
175
+ @vm.configure_network_interfaces(network_config)
176
+ end
177
+
178
+ it "should configure single nic" do
179
+ network_config = [{:name => 'Default', :ip_address => '192.168.1.1'}]
180
+ @fog_interface.should_receive(:put_network_connection_system_section_vapp).with(@vm_id, {
181
+ :PrimaryNetworkConnectionIndex => 0,
182
+ :NetworkConnection => [
183
+ {
184
+ :network => 'Default',
185
+ :needsCustomization => true,
186
+ :NetworkConnectionIndex => 0,
187
+ :IsConnected => true,
188
+ :IpAddress => "192.168.1.1",
189
+ :IpAddressAllocationMode => "MANUAL"
190
+ }
191
+ ]})
192
+ @vm.configure_network_interfaces(network_config)
193
+ end
194
+
195
+ it "should configure multiple nics" do
196
+ network_config = [
197
+ {:name => 'Default', :ip_address => '192.168.1.1'},
198
+ {:name => 'Monitoring', :ip_address => '192.168.2.1'}
199
+ ]
200
+
201
+ @fog_interface.should_receive(:put_network_connection_system_section_vapp).with(@vm_id, {
202
+ :PrimaryNetworkConnectionIndex => 0,
203
+ :NetworkConnection => [
204
+ {
205
+ :network => 'Default',
206
+ :needsCustomization => true,
207
+ :NetworkConnectionIndex => 0,
208
+ :IsConnected => true,
209
+ :IpAddress => "192.168.1.1",
210
+ :IpAddressAllocationMode => "MANUAL"
211
+ },
212
+ {
213
+ :network => 'Monitoring',
214
+ :needsCustomization => true,
215
+ :NetworkConnectionIndex => 1,
216
+ :IsConnected => true,
217
+ :IpAddress => "192.168.2.1",
218
+ :IpAddressAllocationMode => "MANUAL"
219
+ },
220
+ ]})
221
+ @vm.configure_network_interfaces(network_config)
222
+ end
223
+
224
+ it "should configure no nics" do
225
+ network_config = nil
226
+ @fog_interface.should_not_receive(:put_network_connection_system_section_vapp)
227
+ @vm.configure_network_interfaces(network_config)
228
+ end
229
+
230
+ end
231
+
232
+ context "update storage profiles" do
233
+ it "should update the storage profile" do
234
+ storage_profile = 'storage_profile_name'
235
+ vdc_results = [
236
+ { :vdcName => 'vdc-test-1' }
237
+ ]
238
+ mock_vdc_query = double(:query, :get_all_results => vdc_results)
239
+
240
+ storage_profile_results = [
241
+ { :href => 'test-href' }
242
+ ]
243
+ mock_sp_query = double(:query, :get_all_results => storage_profile_results)
244
+
245
+ Vcloud::Query.should_receive(:new).with('vApp', :filter => "name==#{@vapp_name}").and_return(mock_vdc_query)
246
+ Vcloud::Query.should_receive(:new).with('orgVdcStorageProfile', :filter => "name==storage_profile_name;vdcName==vdc-test-1").and_return(mock_sp_query)
247
+
248
+ generated_storage_profile = { name: 'storage_profile_name', href: 'test-href' }
249
+ @fog_interface.should_receive(:put_vm).with(@vm_id, @vm_name, { :StorageProfile => generated_storage_profile} ).and_return(true)
250
+ @vm.update_storage_profile(storage_profile).should == true
251
+ end
252
+
253
+ it "should raise an error if storage profile is not found" do
254
+ storage_profile = 'storage_profile_name'
255
+ vdc_results = [
256
+ { :vdcName => 'vdc-test-1' }
257
+ ]
258
+ mock_vdc_query = double(:query, :get_all_results => vdc_results)
259
+
260
+ storage_profile_results = []
261
+ mock_sp_query = double(:query, :get_all_results => storage_profile_results)
262
+
263
+ Vcloud::Query.should_receive(:new).with('vApp', :filter => "name==#{@vapp_name}").and_return(mock_vdc_query)
264
+ Vcloud::Query.should_receive(:new).with('orgVdcStorageProfile', :filter => "name==storage_profile_name;vdcName==vdc-test-1").and_return(mock_sp_query)
265
+
266
+ expect{ @vm.update_storage_profile(storage_profile) }.to raise_error("storage profile not found" )
267
+ end
268
+
269
+ it "should raise an error if storage profile id is in unexpected format" do
270
+ storage_profile = 'storage_profile_name'
271
+ vdc_results = [
272
+ { :vdcName => 'vdc-test-1' }
273
+ ]
274
+ mock_vdc_query = double(:query, :get_all_results => vdc_results)
275
+
276
+ storage_profile_results = [ { :id => 'test-href' }]
277
+ mock_sp_query = double(:query, :get_all_results => storage_profile_results)
278
+
279
+ Vcloud::Query.should_receive(:new).with('vApp', :filter => "name==#{@vapp_name}").and_return(mock_vdc_query)
280
+ Vcloud::Query.should_receive(:new).with('orgVdcStorageProfile', :filter => "name==storage_profile_name;vdcName==vdc-test-1").and_return(mock_sp_query)
281
+
282
+ expect{ @vm.update_storage_profile(storage_profile) }.to raise_error("storage profile not found" )
283
+ end
284
+
285
+ end
286
+
287
+ end
288
+ end
289
+ end
290
+