vmware-vra 2.1.0 → 2.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +5 -14
- data/CHANGELOG.md +8 -1
- data/Gemfile +1 -1
- data/README.md +1 -1
- data/Rakefile +8 -5
- data/lib/vra.rb +11 -11
- data/lib/vra/catalog.rb +3 -3
- data/lib/vra/catalog_item.rb +14 -14
- data/lib/vra/catalog_request.rb +9 -9
- data/lib/vra/client.rb +29 -27
- data/lib/vra/exceptions.rb +5 -5
- data/lib/vra/http.rb +9 -9
- data/lib/vra/request.rb +6 -6
- data/lib/vra/request_parameters.rb +7 -7
- data/lib/vra/requests.rb +2 -2
- data/lib/vra/resource.rb +69 -69
- data/lib/vra/resources.rb +1 -1
- data/lib/vra/version.rb +1 -1
- data/spec/catalog_item_spec.rb +61 -61
- data/spec/catalog_request_spec.rb +61 -61
- data/spec/catalog_spec.rb +58 -58
- data/spec/client_spec.rb +187 -187
- data/spec/http_spec.rb +59 -59
- data/spec/request_spec.rb +43 -43
- data/spec/requests_spec.rb +15 -15
- data/spec/resource_spec.rb +163 -163
- data/spec/resources_spec.rb +15 -15
- data/spec/spec_helper.rb +2 -2
- data/vmware-vra.gemspec +18 -18
- metadata +18 -17
@@ -17,143 +17,143 @@
|
|
17
17
|
# limitations under the License.
|
18
18
|
#
|
19
19
|
|
20
|
-
require
|
20
|
+
require "spec_helper"
|
21
21
|
|
22
22
|
describe Vra::CatalogRequest do
|
23
23
|
before(:each) do
|
24
|
-
catalog_item = double(
|
25
|
-
allow(catalog_item).to receive(:blueprint_id).and_return(
|
26
|
-
allow(catalog_item).to receive(:tenant_id).and_return(
|
27
|
-
allow(catalog_item).to receive(:subtenant_id).and_return(
|
24
|
+
catalog_item = double("catalog_item")
|
25
|
+
allow(catalog_item).to receive(:blueprint_id).and_return("catalog_blueprint")
|
26
|
+
allow(catalog_item).to receive(:tenant_id).and_return("catalog_tenant")
|
27
|
+
allow(catalog_item).to receive(:subtenant_id).and_return("catalog_subtenant")
|
28
28
|
allow(Vra::CatalogItem).to receive(:new).and_return(catalog_item)
|
29
29
|
end
|
30
30
|
|
31
31
|
let(:client) do
|
32
|
-
Vra::Client.new(username:
|
33
|
-
password:
|
34
|
-
tenant:
|
35
|
-
base_url:
|
32
|
+
Vra::Client.new(username: "user@corp.local",
|
33
|
+
password: "password",
|
34
|
+
tenant: "tenant",
|
35
|
+
base_url: "https://vra.corp.local")
|
36
36
|
end
|
37
37
|
|
38
|
-
context
|
38
|
+
context "when no subtenant ID is provided" do
|
39
39
|
let(:request) do
|
40
|
-
client.catalog.request(
|
40
|
+
client.catalog.request("catalog-12345",
|
41
41
|
cpus: 2,
|
42
42
|
memory: 1024,
|
43
43
|
lease_days: 15,
|
44
|
-
requested_for:
|
45
|
-
notes:
|
44
|
+
requested_for: "tester@corp.local",
|
45
|
+
notes: "test notes")
|
46
46
|
end
|
47
47
|
|
48
|
-
it
|
49
|
-
expect(request.subtenant_id).to eq
|
48
|
+
it "uses the subtenant ID from the catalog item" do
|
49
|
+
expect(request.subtenant_id).to eq "catalog_subtenant"
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
|
-
context
|
53
|
+
context "when subtenant is provided, and all shared tests" do
|
54
54
|
let(:request) do
|
55
|
-
client.catalog.request(
|
55
|
+
client.catalog.request("catalog-12345",
|
56
56
|
cpus: 2,
|
57
57
|
memory: 1024,
|
58
58
|
lease_days: 15,
|
59
|
-
requested_for:
|
60
|
-
notes:
|
61
|
-
subtenant_id:
|
59
|
+
requested_for: "tester@corp.local",
|
60
|
+
notes: "test notes",
|
61
|
+
subtenant_id: "user_subtenant")
|
62
62
|
end
|
63
63
|
|
64
|
-
describe
|
65
|
-
it
|
66
|
-
expect(request.catalog_id).to eq
|
64
|
+
describe "#initialize" do
|
65
|
+
it "sets the appropriate instance vars" do
|
66
|
+
expect(request.catalog_id).to eq "catalog-12345"
|
67
67
|
expect(request.cpus).to eq 2
|
68
68
|
expect(request.memory).to eq 1024
|
69
69
|
expect(request.lease_days).to eq 15
|
70
|
-
expect(request.requested_for).to eq
|
71
|
-
expect(request.notes).to eq
|
72
|
-
expect(request.subtenant_id).to eq
|
70
|
+
expect(request.requested_for).to eq "tester@corp.local"
|
71
|
+
expect(request.notes).to eq "test notes"
|
72
|
+
expect(request.subtenant_id).to eq "user_subtenant"
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
|
-
describe
|
77
|
-
context
|
78
|
-
it
|
76
|
+
describe "#validate_params!" do
|
77
|
+
context "when all required params are provided" do
|
78
|
+
it "does not raise an exception" do
|
79
79
|
expect { request.validate_params! }.to_not raise_error
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
83
|
-
context
|
84
|
-
it
|
83
|
+
context "when a required parameter is not provided" do
|
84
|
+
it "raises an exception" do
|
85
85
|
request.cpus = nil
|
86
86
|
expect { request.validate_params! }.to raise_error(ArgumentError)
|
87
87
|
end
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
91
|
-
describe
|
92
|
-
it
|
93
|
-
request.set_parameter(
|
94
|
-
request.set_parameter(
|
91
|
+
describe "#merge_payload" do
|
92
|
+
it "properly handles additional parameters" do
|
93
|
+
request.set_parameter("param1", "string", "my string")
|
94
|
+
request.set_parameter("param2", "integer", "2468")
|
95
95
|
|
96
|
-
template = File.read(
|
96
|
+
template = File.read("spec/fixtures/resource/catalog_request.json")
|
97
97
|
payload = JSON.parse(request.merge_payload(template))
|
98
|
-
param1 = payload[
|
99
|
-
param2 = payload[
|
98
|
+
param1 = payload["data"]["my_blueprint"]["data"]["param1"]
|
99
|
+
param2 = payload["data"]["my_blueprint"]["data"]["param2"]
|
100
100
|
|
101
101
|
expect(param1).to be_a(String)
|
102
102
|
expect(param2).to be_a(String)
|
103
|
-
expect(param1).to eq
|
104
|
-
expect(param2).to eq
|
103
|
+
expect(param1).to eq "my string"
|
104
|
+
expect(param2).to eq "2468"
|
105
105
|
end
|
106
106
|
end
|
107
107
|
|
108
|
-
describe
|
108
|
+
describe "#submit" do
|
109
109
|
before do
|
110
110
|
allow(request).to receive(:request_payload).and_return({})
|
111
|
-
response = double(
|
112
|
-
allow(client).to receive(:http_post).with(
|
111
|
+
response = double("response", location: "/requests/request-12345")
|
112
|
+
allow(client).to receive(:http_post).with("/catalog-service/api/consumer/requests", "{}").and_return(response)
|
113
113
|
end
|
114
114
|
|
115
|
-
it
|
116
|
-
skip
|
117
|
-
expect(client).to receive(:http_post).with(
|
115
|
+
it "calls http_post" do
|
116
|
+
skip "broken and needs to be updated per changes -JJ 2017-04-14"
|
117
|
+
expect(client).to receive(:http_post).with("/catalog-service/api/consumer/requests", "{}")
|
118
118
|
|
119
119
|
request.submit
|
120
120
|
end
|
121
121
|
|
122
|
-
it
|
123
|
-
skip
|
122
|
+
it "returns a Vra::Request object" do
|
123
|
+
skip "broken and needs to be updated per changes -JJ 2017-04-14"
|
124
124
|
expect(request.submit).to be_an_instance_of(Vra::Request)
|
125
125
|
end
|
126
126
|
end
|
127
127
|
end
|
128
128
|
|
129
129
|
let(:client_without_ssl) do
|
130
|
-
Vra::Client.new(username:
|
131
|
-
password:
|
132
|
-
tenant:
|
133
|
-
base_url:
|
130
|
+
Vra::Client.new(username: "user@corp.local",
|
131
|
+
password: "password",
|
132
|
+
tenant: "tenant",
|
133
|
+
base_url: "https://vra.corp.local",
|
134
134
|
verify_ssl: false)
|
135
135
|
end
|
136
136
|
|
137
|
-
context
|
137
|
+
context "when ssl is not verified by the client" do
|
138
138
|
let(:request) do
|
139
|
-
client_without_ssl.catalog.request(
|
139
|
+
client_without_ssl.catalog.request("catalog-12345",
|
140
140
|
cpus: 2,
|
141
141
|
memory: 1024,
|
142
142
|
lease_days: 15,
|
143
|
-
requested_for:
|
144
|
-
notes:
|
145
|
-
subtenant_id:
|
143
|
+
requested_for: "tester@corp.local",
|
144
|
+
notes: "test notes",
|
145
|
+
subtenant_id: "user_subtenant")
|
146
146
|
end
|
147
147
|
|
148
148
|
describe do
|
149
|
-
it
|
150
|
-
skip
|
149
|
+
it "passes verify_false to Vra::Http" do
|
150
|
+
skip "broken and needs to be updated per changes -JJ 2017-04-14"
|
151
151
|
allow(request.client).to receive(:authorized?).and_return(true)
|
152
|
-
expect(request.client.instance_variable_get(
|
152
|
+
expect(request.client.instance_variable_get("@verify_ssl")).to eq false
|
153
153
|
|
154
154
|
expect(Vra::Http).to receive(:execute).and_wrap_original do |_http, *args|
|
155
155
|
expect(*args).to include(verify_ssl: false)
|
156
|
-
double(location:
|
156
|
+
double(location: "auth/request_id")
|
157
157
|
end
|
158
158
|
|
159
159
|
request.submit
|
data/spec/catalog_spec.rb
CHANGED
@@ -17,78 +17,78 @@
|
|
17
17
|
# limitations under the License.
|
18
18
|
#
|
19
19
|
|
20
|
-
require
|
20
|
+
require "spec_helper"
|
21
21
|
|
22
22
|
describe Vra::Catalog do
|
23
23
|
let(:client) do
|
24
|
-
Vra::Client.new(username:
|
25
|
-
password:
|
26
|
-
tenant:
|
27
|
-
base_url:
|
24
|
+
Vra::Client.new(username: "user@corp.local",
|
25
|
+
password: "password",
|
26
|
+
tenant: "tenant",
|
27
|
+
base_url: "https://vra.corp.local")
|
28
28
|
end
|
29
29
|
|
30
30
|
let(:catalog_item) do
|
31
31
|
{
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
32
|
+
"@type" => "CatalogItem",
|
33
|
+
"id" => "a9cd6148-6e0b-4a80-ac47-f5255c52b43d",
|
34
|
+
"version" => 2,
|
35
|
+
"name" => "CentOS 6.6",
|
36
|
+
"description" => "Blueprint for deploying a CentOS Linux development server",
|
37
|
+
"status" => "PUBLISHED",
|
38
|
+
"statusName" => "Published",
|
39
|
+
"organization" => {
|
40
|
+
"tenantRef" => "vsphere.local",
|
41
|
+
"tenantLabel" => "vsphere.local",
|
42
|
+
"subtenantRef" => nil,
|
43
|
+
"subtenantLabel" => nil,
|
44
|
+
},
|
45
|
+
"providerBinding" => {
|
46
|
+
"bindingId" => "33af5413-4f20-4b3b-8268-32edad434dfb",
|
47
|
+
"providerRef" => {
|
48
|
+
"id" => "c3b2bc30-47b0-454f-b57d-df02a7356fe6",
|
49
|
+
"label" => "iaas-service",
|
50
|
+
},
|
44
51
|
},
|
45
|
-
'providerBinding' => {
|
46
|
-
'bindingId' => '33af5413-4f20-4b3b-8268-32edad434dfb',
|
47
|
-
'providerRef' => {
|
48
|
-
'id' => 'c3b2bc30-47b0-454f-b57d-df02a7356fe6',
|
49
|
-
'label' => 'iaas-service'
|
50
|
-
}
|
51
|
-
}
|
52
52
|
}
|
53
53
|
end
|
54
54
|
|
55
55
|
let(:entitled_catalog_item) do
|
56
56
|
{
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
57
|
+
"@type" => "ConsumerEntitledCatalogItem",
|
58
|
+
"catalogItem" => {
|
59
|
+
"id" => "d29efd6b-3cd6-4f8d-b1d8-da4ddd4e52b1",
|
60
|
+
"version" => 2,
|
61
|
+
"name" => "WindowsServer2012",
|
62
|
+
"description" => "Windows Server 2012 with the latest updates and patches.",
|
63
|
+
"status" => "PUBLISHED",
|
64
|
+
"statusName" => "Published",
|
65
|
+
"organization" => {
|
66
|
+
"tenantRef" => "vsphere.local",
|
67
|
+
"tenantLabel" => "vsphere.local",
|
68
|
+
"subtenantRef" => nil,
|
69
|
+
"subtenantLabel" => nil,
|
70
70
|
},
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
}
|
77
|
-
}
|
78
|
-
}
|
71
|
+
"providerBinding" => {
|
72
|
+
"bindingId" => "59fd02a1-acca-4918-9d3d-2298d310caef",
|
73
|
+
"providerRef" => {
|
74
|
+
"id" => "c3b2bc30-47b0-454f-b57d-df02a7356fe6",
|
75
|
+
"label" => "iaas-service",
|
76
|
+
},
|
77
|
+
},
|
78
|
+
},
|
79
79
|
}
|
80
80
|
end
|
81
81
|
|
82
|
-
describe
|
83
|
-
it
|
84
|
-
expect(client).to receive(:http_get_paginated_array!).with(
|
82
|
+
describe "#all_items" do
|
83
|
+
it "calls the catalogItems endpoint" do
|
84
|
+
expect(client).to receive(:http_get_paginated_array!).with("/catalog-service/api/consumer/catalogItems")
|
85
85
|
.and_return([ catalog_item ])
|
86
86
|
|
87
87
|
client.catalog.all_items
|
88
88
|
end
|
89
89
|
|
90
|
-
it
|
91
|
-
allow(client).to receive(:http_get_paginated_array!).with(
|
90
|
+
it "returns a Vra::CatalogItem object" do
|
91
|
+
allow(client).to receive(:http_get_paginated_array!).with("/catalog-service/api/consumer/catalogItems")
|
92
92
|
.and_return([ catalog_item ])
|
93
93
|
|
94
94
|
items = client.catalog.all_items
|
@@ -97,16 +97,16 @@ describe Vra::Catalog do
|
|
97
97
|
end
|
98
98
|
end
|
99
99
|
|
100
|
-
describe
|
101
|
-
it
|
102
|
-
expect(client).to receive(:http_get_paginated_array!).with(
|
100
|
+
describe "#entitled_items" do
|
101
|
+
it "calls the entitledCatalogItems endpoint" do
|
102
|
+
expect(client).to receive(:http_get_paginated_array!).with("/catalog-service/api/consumer/entitledCatalogItems")
|
103
103
|
.and_return([ entitled_catalog_item ])
|
104
104
|
|
105
105
|
client.catalog.entitled_items
|
106
106
|
end
|
107
107
|
|
108
|
-
it
|
109
|
-
allow(client).to receive(:http_get_paginated_array!).with(
|
108
|
+
it "returns a Vra::CatalogItem object" do
|
109
|
+
allow(client).to receive(:http_get_paginated_array!).with("/catalog-service/api/consumer/entitledCatalogItems")
|
110
110
|
.and_return([ entitled_catalog_item ])
|
111
111
|
|
112
112
|
items = client.catalog.entitled_items
|
@@ -115,10 +115,10 @@ describe Vra::Catalog do
|
|
115
115
|
end
|
116
116
|
end
|
117
117
|
|
118
|
-
describe
|
119
|
-
it
|
118
|
+
describe "#request" do
|
119
|
+
it "returns a new Vra::CatalogRequest object" do
|
120
120
|
allow(Vra::CatalogItem).to receive(:new)
|
121
|
-
request = client.catalog.request(
|
121
|
+
request = client.catalog.request("blueprint-1", cpus: 2)
|
122
122
|
expect(request).to be_an_instance_of(Vra::CatalogRequest)
|
123
123
|
end
|
124
124
|
end
|
data/spec/client_spec.rb
CHANGED
@@ -17,65 +17,65 @@
|
|
17
17
|
# limitations under the License.
|
18
18
|
#
|
19
19
|
|
20
|
-
require
|
20
|
+
require "spec_helper"
|
21
21
|
|
22
22
|
describe Vra::Client do
|
23
23
|
let(:client) do
|
24
|
-
Vra::Client.new(username:
|
25
|
-
password:
|
26
|
-
tenant:
|
27
|
-
base_url:
|
24
|
+
Vra::Client.new(username: "user@corp.local",
|
25
|
+
password: "password",
|
26
|
+
tenant: "tenant",
|
27
|
+
base_url: "https://vra.corp.local")
|
28
28
|
end
|
29
29
|
|
30
|
-
describe
|
31
|
-
it
|
30
|
+
describe "#initialize" do
|
31
|
+
it "calls validate_client_options!" do
|
32
32
|
client = Vra::Client.allocate
|
33
33
|
expect(client).to receive(:validate_client_options!)
|
34
|
-
client.send(:initialize, username:
|
35
|
-
password:
|
36
|
-
tenant:
|
37
|
-
base_url:
|
34
|
+
client.send(:initialize, username: "user@corp.local",
|
35
|
+
password: "password",
|
36
|
+
tenant: "tenant",
|
37
|
+
base_url: "https://vra.corp.local")
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
-
describe
|
42
|
-
it
|
43
|
-
expect(client.bearer_token_request_body[
|
41
|
+
describe "#bearer_token_request_body" do
|
42
|
+
it "gets the correct password from the PasswordMasker object" do
|
43
|
+
expect(client.bearer_token_request_body["password"]).to eq("password")
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
-
describe
|
48
|
-
context
|
49
|
-
it
|
50
|
-
client.bearer_token =
|
51
|
-
expect(client.request_headers.key?(
|
47
|
+
describe "#request_headers" do
|
48
|
+
context "when bearer token exists" do
|
49
|
+
it "has an Authorization header" do
|
50
|
+
client.bearer_token = "12345"
|
51
|
+
expect(client.request_headers.key?("Authorization")).to be true
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
|
-
context
|
56
|
-
it
|
57
|
-
expect(client.request_headers.key?(
|
55
|
+
context "when bearer token does not exist" do
|
56
|
+
it "has an Authorization header" do
|
57
|
+
expect(client.request_headers.key?("Authorization")).to be false
|
58
58
|
end
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
|
-
describe
|
63
|
-
context
|
64
|
-
it
|
62
|
+
describe "#authorize!" do
|
63
|
+
context "when a token is not authorized or no token exists" do
|
64
|
+
it "generates a token successfully" do
|
65
65
|
allow(client).to receive(:authorized?).twice.and_return(false, true)
|
66
66
|
expect(client).to receive(:generate_bearer_token)
|
67
67
|
|
68
68
|
client.authorize!
|
69
69
|
end
|
70
70
|
|
71
|
-
it
|
71
|
+
it "raises an exception if token generation fails" do
|
72
72
|
allow(client).to receive(:authorized?).and_return(false)
|
73
73
|
allow(client).to receive(:generate_bearer_token).and_raise(Vra::Exception::Unauthorized)
|
74
74
|
|
75
75
|
expect { client.authorize! }.to raise_error(Vra::Exception::Unauthorized)
|
76
76
|
end
|
77
77
|
|
78
|
-
it
|
78
|
+
it "raises an exception if a generated token is unauthorized" do
|
79
79
|
allow(client).to receive(:authorized).twice.and_return(false, false)
|
80
80
|
allow(client).to receive(:generate_bearer_token)
|
81
81
|
|
@@ -83,8 +83,8 @@ describe Vra::Client do
|
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
86
|
-
context
|
87
|
-
it
|
86
|
+
context "when a token is authorized" do
|
87
|
+
it "does not generate a new token" do
|
88
88
|
allow(client).to receive(:authorized?).and_return(true)
|
89
89
|
expect(client).to_not receive(:generate_bearer_token)
|
90
90
|
|
@@ -93,27 +93,27 @@ describe Vra::Client do
|
|
93
93
|
end
|
94
94
|
end
|
95
95
|
|
96
|
-
describe
|
97
|
-
context
|
98
|
-
it
|
96
|
+
describe "#authorized?" do
|
97
|
+
context "when token does not exist" do
|
98
|
+
it "returns false" do
|
99
99
|
expect(client.authorized?).to be false
|
100
100
|
end
|
101
101
|
end
|
102
102
|
|
103
|
-
context
|
103
|
+
context "when token exists" do
|
104
104
|
before(:each) do
|
105
|
-
client.bearer_token =
|
105
|
+
client.bearer_token = "12345"
|
106
106
|
end
|
107
107
|
|
108
|
-
it
|
109
|
-
response = double(
|
108
|
+
it "returns true if the token validates successfully" do
|
109
|
+
response = double("response", success_no_content?: true, code: 204)
|
110
110
|
allow(Vra::Http).to receive(:execute).and_return(response)
|
111
111
|
|
112
112
|
expect(client.authorized?).to be true
|
113
113
|
end
|
114
114
|
|
115
|
-
it
|
116
|
-
response = double(
|
115
|
+
it "returns false if the token validates unsuccessfully" do
|
116
|
+
response = double("response", success_no_content?: false, code: 500)
|
117
117
|
allow(Vra::Http).to receive(:execute).and_return(response)
|
118
118
|
|
119
119
|
expect(client.authorized?).to be false
|
@@ -121,18 +121,18 @@ describe Vra::Client do
|
|
121
121
|
end
|
122
122
|
end
|
123
123
|
|
124
|
-
describe
|
124
|
+
describe "#generate_bearer_token" do
|
125
125
|
payload = {
|
126
|
-
|
127
|
-
|
128
|
-
|
126
|
+
"username" => "user@corp.local",
|
127
|
+
"password" => "password",
|
128
|
+
"tenant" => "tenant",
|
129
129
|
}.to_json
|
130
130
|
|
131
|
-
it
|
132
|
-
response = double(
|
131
|
+
it "posts to the tokens API endpoint" do
|
132
|
+
response = double("response", code: 200, body: '{"id":"12345"}', success_ok?: true)
|
133
133
|
expect(Vra::Http).to receive(:execute)
|
134
134
|
.with(method: :post,
|
135
|
-
url: client.full_url(
|
135
|
+
url: client.full_url("/identity/api/tokens"),
|
136
136
|
payload: payload,
|
137
137
|
headers: anything,
|
138
138
|
verify_ssl: true)
|
@@ -141,20 +141,20 @@ describe Vra::Client do
|
|
141
141
|
client.generate_bearer_token
|
142
142
|
end
|
143
143
|
|
144
|
-
context
|
145
|
-
it
|
146
|
-
response = double(
|
144
|
+
context "when token is generated successfully" do
|
145
|
+
it "sets the token" do
|
146
|
+
response = double("response", code: 200, body: '{"id":"12345"}', success_ok?: true)
|
147
147
|
allow(Vra::Http).to receive(:execute).and_return(response)
|
148
148
|
|
149
149
|
client.generate_bearer_token
|
150
150
|
|
151
|
-
expect(client.bearer_token).to eq
|
151
|
+
expect(client.bearer_token).to eq "12345"
|
152
152
|
end
|
153
153
|
end
|
154
154
|
|
155
|
-
context
|
156
|
-
it
|
157
|
-
response = double(
|
155
|
+
context "when token is not generated successfully" do
|
156
|
+
it "raises an exception" do
|
157
|
+
response = double("response", code: 500, body: "error string", success_ok?: false)
|
158
158
|
allow(Vra::Http).to receive(:execute).and_return(response)
|
159
159
|
|
160
160
|
expect { client.generate_bearer_token }.to raise_error(Vra::Exception::Unauthorized)
|
@@ -162,38 +162,38 @@ describe Vra::Client do
|
|
162
162
|
end
|
163
163
|
end
|
164
164
|
|
165
|
-
describe
|
166
|
-
it
|
167
|
-
expect(client.full_url(
|
165
|
+
describe "#full_url" do
|
166
|
+
it "returns a properly formatted url" do
|
167
|
+
expect(client.full_url("/mypath")).to eq "https://vra.corp.local/mypath"
|
168
168
|
end
|
169
169
|
end
|
170
170
|
|
171
|
-
describe
|
172
|
-
context
|
173
|
-
it
|
174
|
-
response = double(
|
171
|
+
describe "#http_head" do
|
172
|
+
context "when skip_auth is nil" do
|
173
|
+
it "authorizes before proceeding" do
|
174
|
+
response = double("response")
|
175
175
|
allow(Vra::Http).to receive(:execute).and_return(response)
|
176
176
|
expect(client).to receive(:authorize!)
|
177
177
|
|
178
|
-
client.http_head(
|
178
|
+
client.http_head("/test")
|
179
179
|
end
|
180
180
|
end
|
181
181
|
|
182
|
-
context
|
183
|
-
it
|
184
|
-
response = double(
|
182
|
+
context "when skip_auth is not nil" do
|
183
|
+
it "does not authorize before proceeding" do
|
184
|
+
response = double("response")
|
185
185
|
allow(Vra::Http).to receive(:execute).and_return(response)
|
186
186
|
expect(client).to_not receive(:authorize!)
|
187
187
|
|
188
|
-
client.http_head(
|
188
|
+
client.http_head("/test", :skip_auth)
|
189
189
|
end
|
190
190
|
end
|
191
191
|
|
192
|
-
it
|
193
|
-
response = double(
|
194
|
-
path =
|
195
|
-
full_url =
|
196
|
-
headers = {
|
192
|
+
it "calls Vra::Http.execute" do
|
193
|
+
response = double("response")
|
194
|
+
path = "/test"
|
195
|
+
full_url = "https://vra.corp.local/test"
|
196
|
+
headers = { "Accept" => "application/json", "Content-Type" => "application/json" }
|
197
197
|
verify_ssl = true
|
198
198
|
|
199
199
|
allow(client).to receive(:authorize!)
|
@@ -206,41 +206,41 @@ describe Vra::Client do
|
|
206
206
|
client.http_head(path)
|
207
207
|
end
|
208
208
|
|
209
|
-
it
|
209
|
+
it "raises an HTTPNotFound on a 404 error" do
|
210
210
|
allow(client).to receive(:authorize!)
|
211
211
|
allow(Vra::Http).to receive(:execute)
|
212
|
-
.and_raise(Vra::Http::Error.new(
|
212
|
+
.and_raise(Vra::Http::Error.new("message", 404, "Not Found"))
|
213
213
|
|
214
|
-
expect { client.http_head(
|
214
|
+
expect { client.http_head("/404") }.to raise_error(Vra::Exception::HTTPNotFound)
|
215
215
|
end
|
216
216
|
end
|
217
217
|
|
218
|
-
describe
|
219
|
-
context
|
220
|
-
it
|
221
|
-
response = double(
|
218
|
+
describe "#http_get" do
|
219
|
+
context "when skip_auth is nil" do
|
220
|
+
it "authorizes before proceeding" do
|
221
|
+
response = double("response")
|
222
222
|
allow(Vra::Http).to receive(:execute).and_return(response)
|
223
223
|
expect(client).to receive(:authorize!)
|
224
224
|
|
225
|
-
client.http_get(
|
225
|
+
client.http_get("/test")
|
226
226
|
end
|
227
227
|
end
|
228
228
|
|
229
|
-
context
|
230
|
-
it
|
231
|
-
response = double(
|
229
|
+
context "when skip_auth is not nil" do
|
230
|
+
it "does not authorize before proceeding" do
|
231
|
+
response = double("response")
|
232
232
|
allow(Vra::Http).to receive(:execute).and_return(response)
|
233
233
|
expect(client).to_not receive(:authorize!)
|
234
234
|
|
235
|
-
client.http_get(
|
235
|
+
client.http_get("/test", :skip_auth)
|
236
236
|
end
|
237
237
|
end
|
238
238
|
|
239
|
-
it
|
240
|
-
response = double(
|
241
|
-
path =
|
242
|
-
full_url =
|
243
|
-
headers = {
|
239
|
+
it "calls Vra::Http.execute" do
|
240
|
+
response = double("response")
|
241
|
+
path = "/test"
|
242
|
+
full_url = "https://vra.corp.local/test"
|
243
|
+
headers = { "Accept" => "application/json", "Content-Type" => "application/json" }
|
244
244
|
verify_ssl = true
|
245
245
|
|
246
246
|
allow(client).to receive(:authorize!)
|
@@ -253,93 +253,93 @@ describe Vra::Client do
|
|
253
253
|
client.http_get(path)
|
254
254
|
end
|
255
255
|
|
256
|
-
it
|
256
|
+
it "raises an HTTPNotFound on a 404 error" do
|
257
257
|
allow(client).to receive(:authorize!)
|
258
258
|
allow(Vra::Http).to receive(:execute)
|
259
|
-
.and_raise(Vra::Http::Error.new(
|
259
|
+
.and_raise(Vra::Http::Error.new("message", 404, "Not Found"))
|
260
260
|
|
261
|
-
expect { client.http_get(
|
261
|
+
expect { client.http_get("/404") }.to raise_error(Vra::Exception::HTTPNotFound)
|
262
262
|
end
|
263
263
|
end
|
264
264
|
|
265
|
-
describe
|
266
|
-
it
|
265
|
+
describe "#http_get_paginated_array!" do
|
266
|
+
it "allows a limit override" do
|
267
267
|
client.page_size = 10
|
268
268
|
expect(client).to receive(:get_parsed)
|
269
|
-
.with(
|
270
|
-
.and_return(
|
269
|
+
.with("/test?limit=10&page=1")
|
270
|
+
.and_return("content" => [], "metadata" => { "totalPages" => 1 })
|
271
271
|
|
272
|
-
client.http_get_paginated_array!(
|
272
|
+
client.http_get_paginated_array!("/test")
|
273
273
|
end
|
274
274
|
|
275
|
-
it
|
275
|
+
it "only calls get_parsed once when total pages is 0 (no items)" do
|
276
276
|
expect(client).to receive(:get_parsed)
|
277
277
|
.once
|
278
|
-
.with(
|
279
|
-
.and_return(
|
278
|
+
.with("/test?limit=20&page=1")
|
279
|
+
.and_return("content" => [], "metadata" => { "totalPages" => 0 })
|
280
280
|
|
281
|
-
client.http_get_paginated_array!(
|
281
|
+
client.http_get_paginated_array!("/test")
|
282
282
|
end
|
283
283
|
|
284
|
-
it
|
284
|
+
it "only calls get_parsed once when total pages is 1" do
|
285
285
|
expect(client).to receive(:get_parsed)
|
286
286
|
.once
|
287
|
-
.with(
|
288
|
-
.and_return(
|
287
|
+
.with("/test?limit=20&page=1")
|
288
|
+
.and_return("content" => [], "metadata" => { "totalPages" => 1 })
|
289
289
|
|
290
|
-
client.http_get_paginated_array!(
|
290
|
+
client.http_get_paginated_array!("/test")
|
291
291
|
end
|
292
292
|
|
293
|
-
it
|
293
|
+
it "calls get_parsed 3 times if there are 3 pages of response" do
|
294
294
|
expect(client).to receive(:get_parsed)
|
295
|
-
.with(
|
296
|
-
.and_return(
|
295
|
+
.with("/test?limit=20&page=1")
|
296
|
+
.and_return("content" => [], "metadata" => { "totalPages" => 3 })
|
297
297
|
expect(client).to receive(:get_parsed)
|
298
|
-
.with(
|
299
|
-
.and_return(
|
298
|
+
.with("/test?limit=20&page=2")
|
299
|
+
.and_return("content" => [], "metadata" => { "totalPages" => 3 })
|
300
300
|
expect(client).to receive(:get_parsed)
|
301
|
-
.with(
|
302
|
-
.and_return(
|
301
|
+
.with("/test?limit=20&page=3")
|
302
|
+
.and_return("content" => [], "metadata" => { "totalPages" => 3 })
|
303
303
|
|
304
|
-
client.http_get_paginated_array!(
|
304
|
+
client.http_get_paginated_array!("/test")
|
305
305
|
end
|
306
306
|
|
307
|
-
it
|
307
|
+
it "raises an exception if duplicate items are returned by the API" do
|
308
308
|
allow(client).to receive(:get_parsed)
|
309
|
-
.with(
|
310
|
-
.and_return(
|
309
|
+
.with("/test?limit=20&page=1")
|
310
|
+
.and_return("content" => [ 1, 2, 3, 1 ], "metadata" => { "totalPages" => 1 })
|
311
311
|
|
312
|
-
expect { client.http_get_paginated_array!(
|
312
|
+
expect { client.http_get_paginated_array!("/test") }.to raise_error(Vra::Exception::DuplicateItemsDetected)
|
313
313
|
end
|
314
314
|
end
|
315
315
|
|
316
|
-
describe
|
317
|
-
context
|
318
|
-
it
|
319
|
-
response = double(
|
316
|
+
describe "#http_post" do
|
317
|
+
context "when skip_auth is nil" do
|
318
|
+
it "authorizes before proceeding" do
|
319
|
+
response = double("response")
|
320
320
|
allow(Vra::Http).to receive(:execute).and_return(response)
|
321
321
|
expect(client).to receive(:authorize!)
|
322
322
|
|
323
|
-
client.http_post(
|
323
|
+
client.http_post("/test", "some payload")
|
324
324
|
end
|
325
325
|
end
|
326
326
|
|
327
|
-
context
|
328
|
-
it
|
329
|
-
response = double(
|
327
|
+
context "when skip_auth is not nil" do
|
328
|
+
it "does not authorize before proceeding" do
|
329
|
+
response = double("response")
|
330
330
|
allow(Vra::Http).to receive(:execute).and_return(response)
|
331
331
|
expect(client).to_not receive(:authorize!)
|
332
332
|
|
333
|
-
client.http_post(
|
333
|
+
client.http_post("/test", "some payload", :skip_auth)
|
334
334
|
end
|
335
335
|
end
|
336
336
|
|
337
|
-
it
|
338
|
-
response = double(
|
339
|
-
path =
|
340
|
-
full_url =
|
341
|
-
headers = {
|
342
|
-
payload =
|
337
|
+
it "calls Vra::Http.execute" do
|
338
|
+
response = double("response")
|
339
|
+
path = "/test"
|
340
|
+
full_url = "https://vra.corp.local/test"
|
341
|
+
headers = { "Accept" => "application/json", "Content-Type" => "application/json" }
|
342
|
+
payload = "some payload"
|
343
343
|
verify_ssl = true
|
344
344
|
|
345
345
|
allow(client).to receive(:authorize!)
|
@@ -353,12 +353,12 @@ describe Vra::Client do
|
|
353
353
|
client.http_post(path, payload)
|
354
354
|
end
|
355
355
|
|
356
|
-
context
|
356
|
+
context "when not verifying ssl" do
|
357
357
|
let(:unverified_client) do
|
358
|
-
Vra::Client.new(username:
|
359
|
-
password:
|
360
|
-
tenant:
|
361
|
-
base_url:
|
358
|
+
Vra::Client.new(username: "user@corp.local",
|
359
|
+
password: "password",
|
360
|
+
tenant: "tenant",
|
361
|
+
base_url: "https://vra.corp.local",
|
362
362
|
verify_ssl: false)
|
363
363
|
end
|
364
364
|
|
@@ -366,130 +366,130 @@ describe Vra::Client do
|
|
366
366
|
allow(unverified_client).to receive(:authorized?).and_return(true)
|
367
367
|
end
|
368
368
|
|
369
|
-
it
|
369
|
+
it "configures Net::HTTP with VERIFY_NONE" do
|
370
370
|
allow(Net::HTTP).to receive(:start).and_wrap_original do |_http, *args|
|
371
371
|
expect(args.last).to include(verify_mode: OpenSSL::SSL::VERIFY_NONE)
|
372
|
-
double(
|
372
|
+
double("response", final?: true, success?: true)
|
373
373
|
end
|
374
374
|
|
375
|
-
unverified_client.http_post(
|
375
|
+
unverified_client.http_post("/path", "payload")
|
376
376
|
|
377
377
|
[:head, :get].each do |method|
|
378
|
-
unverified_client.http_fetch(method,
|
378
|
+
unverified_client.http_fetch(method, "/test", true)
|
379
379
|
end
|
380
380
|
end
|
381
381
|
end
|
382
382
|
|
383
|
-
it
|
383
|
+
it "calls raise_http_exception upon error" do
|
384
384
|
allow(client).to receive(:authorize!)
|
385
385
|
allow(Vra::Http).to receive(:execute).and_raise(StandardError)
|
386
386
|
expect(client).to receive(:raise_http_exception)
|
387
387
|
|
388
|
-
client.http_post(
|
388
|
+
client.http_post("/404", "test payload")
|
389
389
|
end
|
390
390
|
end
|
391
391
|
|
392
|
-
describe
|
393
|
-
it
|
394
|
-
response = double(
|
395
|
-
allow(client).to receive(:http_post).with(
|
392
|
+
describe "#http_post!" do
|
393
|
+
it "returns the response body" do
|
394
|
+
response = double("response", body: "body text")
|
395
|
+
allow(client).to receive(:http_post).with("/test", "test payload").and_return(response)
|
396
396
|
|
397
|
-
expect(client.http_post!(
|
397
|
+
expect(client.http_post!("/test", "test payload")).to eq "body text"
|
398
398
|
end
|
399
399
|
end
|
400
400
|
|
401
|
-
describe
|
402
|
-
context
|
401
|
+
describe "#raise_http_exception" do
|
402
|
+
context "when a 404 is received" do
|
403
403
|
let(:exception) do
|
404
|
-
double(
|
404
|
+
double("RestClient::ResourceNotFound",
|
405
405
|
http_code: 404,
|
406
|
-
message:
|
407
|
-
response:
|
406
|
+
message: "Not Found",
|
407
|
+
response: "404 Not Found")
|
408
408
|
end
|
409
409
|
|
410
|
-
it
|
411
|
-
expect { client.raise_http_exception(exception,
|
410
|
+
it "raises a Vra::Exception::HTTPNotFound exception" do
|
411
|
+
expect { client.raise_http_exception(exception, "/test") }.to raise_error(Vra::Exception::HTTPNotFound)
|
412
412
|
end
|
413
413
|
end
|
414
414
|
|
415
|
-
context
|
415
|
+
context "when an unspecified http error is received" do
|
416
416
|
let(:exception) do
|
417
|
-
double(
|
417
|
+
double("RestClient::BadRequest",
|
418
418
|
http_code: 400,
|
419
|
-
message:
|
420
|
-
response:
|
419
|
+
message: "Bad Request",
|
420
|
+
response: "400 Bad Request")
|
421
421
|
end
|
422
422
|
|
423
|
-
it
|
424
|
-
expect { client.raise_http_exception(exception,
|
423
|
+
it "raises a Vra::Exception::HTTPError exception" do
|
424
|
+
expect { client.raise_http_exception(exception, "/test") }.to raise_error(Vra::Exception::HTTPError)
|
425
425
|
end
|
426
426
|
end
|
427
427
|
end
|
428
428
|
|
429
|
-
describe
|
430
|
-
context
|
431
|
-
it
|
429
|
+
describe "#validate_client_options!" do
|
430
|
+
context "when all required options are supplied" do
|
431
|
+
it "does not raise an exception" do
|
432
432
|
expect { client.validate_client_options! }.not_to raise_error
|
433
433
|
end
|
434
434
|
end
|
435
435
|
|
436
|
-
context
|
436
|
+
context "when username is missing" do
|
437
437
|
let(:client) do
|
438
|
-
Vra::Client.new(password:
|
439
|
-
tenant:
|
440
|
-
base_url:
|
438
|
+
Vra::Client.new(password: "password",
|
439
|
+
tenant: "tenant",
|
440
|
+
base_url: "https://vra.corp.local")
|
441
441
|
end
|
442
442
|
|
443
|
-
it
|
443
|
+
it "raises an exception" do
|
444
444
|
expect { client.validate_client_options! }.to raise_error(ArgumentError)
|
445
445
|
end
|
446
446
|
end
|
447
447
|
|
448
|
-
context
|
448
|
+
context "when password is missing" do
|
449
449
|
let(:client) do
|
450
|
-
Vra::Client.new(username:
|
451
|
-
tenant:
|
452
|
-
base_url:
|
450
|
+
Vra::Client.new(username: "username",
|
451
|
+
tenant: "tenant",
|
452
|
+
base_url: "https://vra.corp.local")
|
453
453
|
end
|
454
454
|
|
455
|
-
it
|
455
|
+
it "raises an exception" do
|
456
456
|
expect { client.validate_client_options! }.to raise_error(ArgumentError)
|
457
457
|
end
|
458
458
|
end
|
459
459
|
|
460
|
-
context
|
460
|
+
context "when tenant is missing" do
|
461
461
|
let(:client) do
|
462
|
-
Vra::Client.new(username:
|
463
|
-
password:
|
464
|
-
base_url:
|
462
|
+
Vra::Client.new(username: "username",
|
463
|
+
password: "password",
|
464
|
+
base_url: "https://vra.corp.local")
|
465
465
|
end
|
466
466
|
|
467
|
-
it
|
467
|
+
it "raises an exception" do
|
468
468
|
expect { client.validate_client_options! }.to raise_error(ArgumentError)
|
469
469
|
end
|
470
470
|
end
|
471
471
|
|
472
|
-
context
|
472
|
+
context "when base URL is missing" do
|
473
473
|
let(:client) do
|
474
|
-
Vra::Client.new(username:
|
475
|
-
password:
|
476
|
-
tenant:
|
474
|
+
Vra::Client.new(username: "username",
|
475
|
+
password: "password",
|
476
|
+
tenant: "tenant")
|
477
477
|
end
|
478
478
|
|
479
|
-
it
|
479
|
+
it "raises an exception" do
|
480
480
|
expect { client.validate_client_options! }.to raise_error(ArgumentError)
|
481
481
|
end
|
482
482
|
end
|
483
483
|
|
484
|
-
context
|
484
|
+
context "when base URL is not a valid HTTP URL" do
|
485
485
|
let(:client) do
|
486
|
-
Vra::Client.new(username:
|
487
|
-
password:
|
488
|
-
tenant:
|
489
|
-
base_url:
|
486
|
+
Vra::Client.new(username: "username",
|
487
|
+
password: "password",
|
488
|
+
tenant: "tenant",
|
489
|
+
base_url: "something-that-is-not-a-HTTP-URI")
|
490
490
|
end
|
491
491
|
|
492
|
-
it
|
492
|
+
it "raises an exception" do
|
493
493
|
expect { client.validate_client_options! }.to raise_error(ArgumentError)
|
494
494
|
end
|
495
495
|
end
|