vmware-vra 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,59 @@
1
+ #
2
+ # Author:: Chef Partner Engineering (<partnereng@chef.io>)
3
+ # Copyright:: Copyright (c) 2015 Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'spec_helper'
20
+
21
+ describe Vra::Requests do
22
+ let(:client) do
23
+ Vra::Client.new(username: 'user@corp.local',
24
+ password: 'password',
25
+ tenant: 'tenant',
26
+ base_url: 'https://vra.corp.local')
27
+ end
28
+
29
+ let(:requests) { Vra::Requests.new(client) }
30
+
31
+ describe '#all_resources' do
32
+ it 'calls the requests API endpoint' do
33
+ expect(client).to receive(:http_get_paginated_array!)
34
+ .with('/catalog-service/api/consumer/requests')
35
+ .and_return([])
36
+
37
+ requests.all_requests
38
+ end
39
+
40
+ it 'returns an array of request objects' do
41
+ allow(client).to receive(:http_get_paginated_array!)
42
+ .with('/catalog-service/api/consumer/requests')
43
+ .and_return([ { 'id' => '1' }, { 'id' => '2' } ])
44
+
45
+ items = requests.all_requests
46
+
47
+ expect(items[0]).to be_an_instance_of(Vra::Request)
48
+ expect(items[1]).to be_an_instance_of(Vra::Request)
49
+ end
50
+ end
51
+
52
+ describe '#by_id' do
53
+ it 'returns a request object' do
54
+ expect(Vra::Request).to receive(:new).with(client, '12345')
55
+
56
+ requests.by_id('12345')
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,245 @@
1
+ #
2
+ # Author:: Chef Partner Engineering (<partnereng@chef.io>)
3
+ # Copyright:: Copyright (c) 2015 Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'spec_helper'
20
+ require 'ffi_yajl'
21
+
22
+ describe Vra::Resource do
23
+ let(:client) do
24
+ Vra::Client.new(username: 'user@corp.local',
25
+ password: 'password',
26
+ tenant: 'tenant',
27
+ base_url: 'https://vra.corp.local')
28
+ end
29
+
30
+ let(:resource_id) { '31a7badc-6562-458d-84f3-ec58d74a6953' }
31
+ let(:vm_payload) do
32
+ FFI_Yajl::Parser.parse(File.read(File.join(File.dirname(__FILE__),
33
+ 'fixtures',
34
+ 'resource',
35
+ 'vm_resource.json')))
36
+ end
37
+
38
+ let(:vm_payload_no_ops) do
39
+ FFI_Yajl::Parser.parse(File.read(File.join(File.dirname(__FILE__),
40
+ 'fixtures',
41
+ 'resource',
42
+ 'vm_resource_no_operations.json')))
43
+ end
44
+
45
+ let(:non_vm_payload) do
46
+ FFI_Yajl::Parser.parse(File.read(File.join(File.dirname(__FILE__),
47
+ 'fixtures',
48
+ 'resource',
49
+ 'non_vm_resource.json')))
50
+ end
51
+
52
+ describe '#initialize' do
53
+ it 'raises an error if no ID or resource data have been provided' do
54
+ expect { Vra::Resource.new }.to raise_error(ArgumentError)
55
+ end
56
+
57
+ it 'raises an error if an ID and resource data have both been provided' do
58
+ expect { Vra::Resource.new(id: 123, data: 'foo') }.to raise_error(ArgumentError)
59
+ end
60
+
61
+ context 'when an ID is provided' do
62
+ it 'calls fetch_resource_data' do
63
+ resource = Vra::Resource.allocate
64
+ expect(resource).to receive(:fetch_resource_data)
65
+ resource.send(:initialize, client, id: resource_id)
66
+ end
67
+ end
68
+
69
+ context 'when resource data is provided' do
70
+ it 'populates the ID correctly' do
71
+ resource = Vra::Resource.new(client, data: vm_payload)
72
+ expect(resource.id).to eq resource_id
73
+ end
74
+ end
75
+ end
76
+
77
+ describe '#fetch_resource_data' do
78
+ it 'calls http_get! against the resources API endpoint' do
79
+ expect(client).to receive(:http_get!)
80
+ .with("/catalog-service/api/consumer/resources/#{resource_id}")
81
+ .and_return('')
82
+
83
+ Vra::Resource.new(client, id: resource_id)
84
+ end
85
+ end
86
+
87
+ context 'when a valid VM resource instance has been created' do
88
+ before(:each) do
89
+ @resource = Vra::Resource.new(client, data: vm_payload)
90
+ end
91
+
92
+ describe '#name' do
93
+ it 'returns the correct name' do
94
+ expect(@resource.name).to eq 'hol-dev-11'
95
+ end
96
+ end
97
+
98
+ describe '#status' do
99
+ it 'returns the correct status' do
100
+ expect(@resource.status).to eq 'ACTIVE'
101
+ end
102
+ end
103
+
104
+ describe '#vm?' do
105
+ it 'returns true for the VM resource we created' do
106
+ expect(@resource.vm?).to be true
107
+ end
108
+ end
109
+
110
+ describe '#tenant_id' do
111
+ it 'returns the correct tenant ID' do
112
+ expect(@resource.tenant_id).to eq 'vsphere.local'
113
+ end
114
+ end
115
+
116
+ describe '#tenant_name' do
117
+ it 'returns the correct tenant name' do
118
+ expect(@resource.tenant_name).to eq 'vsphere.local'
119
+ end
120
+ end
121
+
122
+ describe '#subtenant_id' do
123
+ it 'returns the correct subtenant ID' do
124
+ expect(@resource.subtenant_id).to eq '5327ddd3-1a4e-4663-9e9d-63db86ffc8af'
125
+ end
126
+ end
127
+
128
+ describe '#subtenant_name' do
129
+ it 'returns the correct subtenant name' do
130
+ expect(@resource.subtenant_name).to eq 'Rainpole Developers'
131
+ end
132
+ end
133
+
134
+ describe '#network_interfaces' do
135
+ it 'returns an array of 2 elements' do
136
+ expect(@resource.network_interfaces.size).to be 2
137
+ end
138
+
139
+ it 'contains the correct data' do
140
+ nic1, nic2 = @resource.network_interfaces
141
+
142
+ expect(nic1['NETWORK_NAME']).to eq 'VM Network'
143
+ expect(nic1['NETWORK_ADDRESS']).to eq '192.168.110.200'
144
+ expect(nic1['NETWORK_MAC_ADDRESS']).to eq '00:50:56:ae:95:3c'
145
+
146
+ expect(nic2['NETWORK_NAME']).to eq 'Management Network'
147
+ expect(nic2['NETWORK_ADDRESS']).to eq '192.168.220.200'
148
+ expect(nic2['NETWORK_MAC_ADDRESS']).to eq '00:50:56:ae:95:3d'
149
+ end
150
+ end
151
+
152
+ describe '#ip_addresses' do
153
+ it 'returns the correct IP addresses' do
154
+ expect(@resource.ip_addresses).to eq [ '192.168.110.200', '192.168.220.200' ]
155
+ end
156
+
157
+ it 'returns nil if there are no network interfaces' do
158
+ allow(@resource).to receive(:network_interfaces).and_return nil
159
+ expect(@resource.ip_addresses).to be_nil
160
+ end
161
+ end
162
+
163
+ describe '#actions' do
164
+ it 'does not call #fetch_resource_data' do
165
+ expect(@resource).not_to receive(:fetch_resource_data)
166
+ @resource.actions
167
+ end
168
+ end
169
+
170
+ describe '#action_id_by_name' do
171
+ it 'returns the correct action ID for the destroy action' do
172
+ expect(@resource.action_id_by_name('Destroy')).to eq 'ace8ba42-e724-48d8-9614-9b3a62b5a464'
173
+ end
174
+
175
+ it 'returns nil if there are no resource operations' do
176
+ allow(@resource).to receive(:actions).and_return nil
177
+ expect(@resource.action_id_by_name('Destroy')).to be_nil
178
+ end
179
+
180
+ it 'returns nil if there are actions, but none with the right name' do
181
+ allow(@resource).to receive(:actions).and_return([ { 'name' => 'some action' }, { 'name' => 'another action' } ])
182
+ expect(@resource.action_id_by_name('Destroy')).to be_nil
183
+ end
184
+ end
185
+
186
+ describe '#destroy' do
187
+ context 'when the destroy action is available' do
188
+ it 'calls gets the action ID and submits the request' do
189
+ expect(@resource).to receive(:action_id_by_name).with('Destroy').and_return('action-123')
190
+ expect(@resource).to receive(:submit_action_request).with('action-123')
191
+ @resource.destroy
192
+ end
193
+ end
194
+
195
+ context 'when the destroy action is not available' do
196
+ it 'raises an exception' do
197
+ allow(@resource).to receive(:action_id_by_name).and_return nil
198
+ expect { @resource.destroy }.to raise_error(Vra::Exception::NotFound)
199
+ end
200
+ end
201
+ end
202
+
203
+ describe '#submit_action_request' do
204
+ before do
205
+ allow(@resource).to receive(:action_request_payload).and_return({})
206
+ response = double('response', code: 200, headers: { location: '/requests/request-12345' })
207
+ allow(client).to receive(:http_post).with('/catalog-service/api/consumer/requests', '{}').and_return(response)
208
+ end
209
+
210
+ it 'calls http_post' do
211
+ expect(client).to receive(:http_post).with('/catalog-service/api/consumer/requests', '{}')
212
+
213
+ @resource.submit_action_request('action-123')
214
+ end
215
+
216
+ it 'returns a Vra::Request object' do
217
+ expect(@resource.submit_action_request('action-123')).to be_an_instance_of(Vra::Request)
218
+ end
219
+ end
220
+ end
221
+
222
+ context 'when a valid VM resource instance with no operations is created' do
223
+ before(:each) do
224
+ @resource = Vra::Resource.new(client, data: vm_payload_no_ops)
225
+ end
226
+
227
+ describe '#actions' do
228
+ it 'calls #fetch_resource_data' do
229
+ expect(@resource).to receive(:fetch_resource_data)
230
+ @resource.actions
231
+ end
232
+ end
233
+ end
234
+
235
+ context 'when a valid non-VM resource instance has been created' do
236
+ before(:each) do
237
+ @resource = Vra::Resource.new(client, data: non_vm_payload)
238
+ end
239
+
240
+ it 'returns nil for network_interfaces and ip_addresses' do
241
+ expect(@resource.network_interfaces).to be_nil
242
+ expect(@resource.ip_addresses).to be_nil
243
+ end
244
+ end
245
+ end
@@ -0,0 +1,59 @@
1
+ #
2
+ # Author:: Chef Partner Engineering (<partnereng@chef.io>)
3
+ # Copyright:: Copyright (c) 2015 Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'spec_helper'
20
+
21
+ describe Vra::Resources do
22
+ let(:client) do
23
+ Vra::Client.new(username: 'user@corp.local',
24
+ password: 'password',
25
+ tenant: 'tenant',
26
+ base_url: 'https://vra.corp.local')
27
+ end
28
+
29
+ let(:resources) { Vra::Resources.new(client) }
30
+
31
+ describe '#all_resources' do
32
+ it 'calls the resources API endpoint' do
33
+ expect(client).to receive(:http_get_paginated_array!)
34
+ .with('/catalog-service/api/consumer/resources')
35
+ .and_return([])
36
+
37
+ resources.all_resources
38
+ end
39
+
40
+ it 'returns an array of resource objects' do
41
+ allow(client).to receive(:http_get_paginated_array!)
42
+ .with('/catalog-service/api/consumer/resources')
43
+ .and_return([ { 'id' => '1' }, { 'id' => '2' } ])
44
+
45
+ items = resources.all_resources
46
+
47
+ expect(items[0]).to be_an_instance_of(Vra::Resource)
48
+ expect(items[1]).to be_an_instance_of(Vra::Resource)
49
+ end
50
+ end
51
+
52
+ describe '#by_id' do
53
+ it 'returns a resource object' do
54
+ expect(Vra::Resource).to receive(:new).with(client, id: '12345')
55
+
56
+ resources.by_id('12345')
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,22 @@
1
+ #
2
+ # Author:: Chef Partner Engineering (<partnereng@chef.io>)
3
+ # Copyright:: Copyright (c) 2015 Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'vra'
20
+ require 'webmock/rspec'
21
+
22
+ WebMock.disable_net_connect!(allow_localhost: true)
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'vra/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'vmware-vra'
8
+ spec.version = Vra::VERSION
9
+ spec.authors = ['Adam Leff']
10
+ spec.email = ['partnereng@chef.io']
11
+ spec.summary = 'Client gem for interacting with VMware vRealize Automation.'
12
+ spec.description = spec.summary
13
+ spec.homepage = ''
14
+ spec.license = 'Apache 2.0'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'rest-client', '~> 1.8'
22
+ spec.add_dependency 'ffi-yajl', '~> 2.2'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.7'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.0'
27
+ spec.add_development_dependency 'webmock', '~> 1.21'
28
+ spec.add_development_dependency 'pry', '~> 0.10'
29
+ end
metadata ADDED
@@ -0,0 +1,185 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vmware-vra
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.rc1
5
+ platform: ruby
6
+ authors:
7
+ - Adam Leff
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ffi-yajl
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.21'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.21'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.10'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.10'
111
+ description: Client gem for interacting with VMware vRealize Automation.
112
+ email:
113
+ - partnereng@chef.io
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rubocop.yml"
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - lib/vra.rb
124
+ - lib/vra/catalog.rb
125
+ - lib/vra/catalog_item.rb
126
+ - lib/vra/catalog_request.rb
127
+ - lib/vra/client.rb
128
+ - lib/vra/exceptions.rb
129
+ - lib/vra/request.rb
130
+ - lib/vra/request_parameters.rb
131
+ - lib/vra/requests.rb
132
+ - lib/vra/resource.rb
133
+ - lib/vra/resources.rb
134
+ - lib/vra/version.rb
135
+ - spec/catalog_item_spec.rb
136
+ - spec/catalog_request_spec.rb
137
+ - spec/catalog_spec.rb
138
+ - spec/client_spec.rb
139
+ - spec/fixtures/resource/non_vm_resource.json
140
+ - spec/fixtures/resource/vm_resource.json
141
+ - spec/fixtures/resource/vm_resource_no_operations.json
142
+ - spec/request_spec.rb
143
+ - spec/requests_spec.rb
144
+ - spec/resource_spec.rb
145
+ - spec/resources_spec.rb
146
+ - spec/spec_helper.rb
147
+ - vmware-vra.gemspec
148
+ homepage: ''
149
+ licenses:
150
+ - Apache 2.0
151
+ metadata: {}
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">"
164
+ - !ruby/object:Gem::Version
165
+ version: 1.3.1
166
+ requirements: []
167
+ rubyforge_project:
168
+ rubygems_version: 2.4.4
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: Client gem for interacting with VMware vRealize Automation.
172
+ test_files:
173
+ - spec/catalog_item_spec.rb
174
+ - spec/catalog_request_spec.rb
175
+ - spec/catalog_spec.rb
176
+ - spec/client_spec.rb
177
+ - spec/fixtures/resource/non_vm_resource.json
178
+ - spec/fixtures/resource/vm_resource.json
179
+ - spec/fixtures/resource/vm_resource_no_operations.json
180
+ - spec/request_spec.rb
181
+ - spec/requests_spec.rb
182
+ - spec/resource_spec.rb
183
+ - spec/resources_spec.rb
184
+ - spec/spec_helper.rb
185
+ has_rdoc: