vmware-vra 2.7.1 → 3.1.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 (50) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -1
  3. data/CHANGELOG.md +22 -1
  4. data/README.md +81 -143
  5. data/Rakefile +1 -12
  6. data/lib/vra/catalog.rb +39 -8
  7. data/lib/vra/catalog_base.rb +62 -0
  8. data/lib/vra/catalog_item.rb +37 -71
  9. data/lib/vra/catalog_source.rb +116 -0
  10. data/lib/vra/catalog_type.rb +56 -0
  11. data/lib/vra/client.rb +72 -53
  12. data/lib/vra/deployment.rb +155 -0
  13. data/lib/vra/deployment_request.rb +125 -0
  14. data/lib/vra/{resources.rb → deployments.rb} +26 -17
  15. data/lib/vra/exceptions.rb +2 -2
  16. data/lib/vra/http.rb +16 -8
  17. data/lib/vra/request.rb +28 -36
  18. data/lib/vra/request_parameters.rb +12 -12
  19. data/lib/vra/resource.rb +33 -203
  20. data/lib/vra/version.rb +2 -2
  21. data/lib/vra.rb +15 -12
  22. data/spec/catalog_item_spec.rb +91 -216
  23. data/spec/catalog_source_spec.rb +178 -0
  24. data/spec/catalog_spec.rb +112 -72
  25. data/spec/catalog_type_spec.rb +114 -0
  26. data/spec/client_spec.rb +287 -228
  27. data/spec/deployment_request_spec.rb +231 -0
  28. data/spec/deployment_spec.rb +227 -0
  29. data/spec/deployments_spec.rb +80 -0
  30. data/spec/fixtures/resource/sample_catalog_item.json +18 -0
  31. data/spec/fixtures/resource/sample_catalog_item_2.json +18 -0
  32. data/spec/fixtures/resource/sample_catalog_source.json +20 -0
  33. data/spec/fixtures/resource/sample_catalog_type.json +49 -0
  34. data/spec/fixtures/resource/sample_dep_actions.json +58 -0
  35. data/spec/fixtures/resource/sample_dep_request.json +19 -0
  36. data/spec/fixtures/resource/sample_dep_resource.json +112 -0
  37. data/spec/fixtures/resource/sample_deployment.json +26 -0
  38. data/spec/fixtures/resource/sample_entitlements.json +25 -0
  39. data/spec/http_spec.rb +63 -61
  40. data/spec/request_spec.rb +62 -68
  41. data/spec/resource_spec.rb +71 -390
  42. data/spec/spec_helper.rb +10 -4
  43. data/vmware-vra.gemspec +2 -4
  44. metadata +42 -32
  45. data/.travis.yml +0 -14
  46. data/lib/vra/catalog_request.rb +0 -134
  47. data/lib/vra/requests.rb +0 -41
  48. data/spec/catalog_request_spec.rb +0 -268
  49. data/spec/requests_spec.rb +0 -60
  50. data/spec/resources_spec.rb +0 -71
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3e28bed631cab067e13738576e243460e12e5b4ab8421f984faf1261c5b70691
4
- data.tar.gz: 1be855a3503c4a15386c885accdd75a8f5fc5fc13eb84abc82c91cfe08250b6a
3
+ metadata.gz: 55b844fe4edf716d4c6668cfa5c13721caa0f36a4873aae4f2949509cf6bfc1c
4
+ data.tar.gz: 5a68b428aa38c0e30a8ba89790ccdb5fd9b5d233485497886cd4d290f4f528d0
5
5
  SHA512:
6
- metadata.gz: 300797ca6f9997718a76e9c86fd1ba9f898463a94ebc95dd8e626799e77bdd6e0cd0bec7a4290f5246c31dbebbed338c84941fc132fc71763b1d7bedd10c2b1d
7
- data.tar.gz: 3d8e098691e4edadafa5b5c9b6768b267ad92ff5736aed899c31ed0f62969714f743f2f8cd2b96f6a23cab6fa83609810cdfd71860c7aaaf980bd8a60d25f7c9
6
+ metadata.gz: b41f6cf8aca5fde7912e59937a569af9b0fb550877a07eb1302edd94b02a3caac478ca41474786fbf2293c2a96fc3334440b0eb72784a7b48c41090a401a9f99
7
+ data.tar.gz: 3dfa51c5e343ca7994f9240afa7b6b2f81de243bd88de0b8a9f19f9d189d571a4d95d4fe00628950a50673e8e15f4f2b849cf78456edc8423ff20aa512a84eb8
data/.gitignore CHANGED
@@ -15,4 +15,6 @@
15
15
  mkmf.log
16
16
  .direnv/
17
17
  .envrc
18
- .ruby-version
18
+ .ruby-version
19
+ .idea
20
+
data/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
1
1
  # Change Log
2
2
 
3
+ ## [3.1.0](https://github.com/chef-partners/vmware-vra-gem/tree/v3.1.0) (2022-01-30)
4
+ [Full Changelog](https://github.com/chef-partners/vmware-vra-gem/compare/v3.0.1...v3.1.0)
5
+
6
+ - Make the version param optional in deployment request api
7
+
8
+ ## [3.0.1](https://github.com/chef-partners/vmware-vra-gem/tree/v3.0.1) (2022-01-25)
9
+ [Full Changelog](https://github.com/chef-partners/vmware-vra-gem/compare/v3.0.0...v3.0.1)
10
+
11
+ - Fix access token workflow to work with VRA8
12
+
13
+ ## [3.0.0](https://github.com/chef-partners/vmware-vra-gem/tree/v3.0.0) (2022-01-18)
14
+ [Full Changelog](https://github.com/chef-partners/vmware-vra-gem/compare/v2.7.2...v3.0.0)
15
+
16
+ - Rewritten to support vRA 8. If you require support for 7 make sure to pin on the previous 2.7.2 release.
17
+
18
+ ## [2.7.2](https://github.com/chef-partners/vmware-vra-gem/tree/v2.7.2) (2020-09-09)
19
+ [Full Changelog](https://github.com/chef-partners/vmware-vra-gem/compare/v2.7.1...v2.7.2)
20
+
21
+ - Added an extra option to handle shirt size parameter
22
+ - Masking user credentials(password) in debug mode
23
+
3
24
  ## [2.7.1](https://github.com/chef-partners/vmware-vra-gem/tree/v2.7.1) (2019-05-28)
4
25
  [Full Changelog](https://github.com/chef-partners/vmware-vra-gem/compare/v2.7.0...v2.7.1)
5
26
 
@@ -298,4 +319,4 @@
298
319
 
299
320
 
300
321
 
301
- \* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
322
+ \* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
data/README.md CHANGED
@@ -1,6 +1,5 @@
1
1
  # VMware vRA Gem
2
2
  [![Gem Version](https://badge.fury.io/rb/vmware-vra.svg)](http://badge.fury.io/rb/vmware-vra)
3
- [![Build Status](https://travis-ci.org/chef-partners/vmware-vra-gem.svg?branch=master)](https://travis-ci.org/chef-partners/vmware-vra-gem)
4
3
 
5
4
  Client gem for interacting with VMware's vRealize Automation application.
6
5
 
@@ -16,6 +15,8 @@ to create Chef plugins for knife, test-kitchen, and provisioning.
16
15
 
17
16
  `2.0.0` version and forward will support vRA 7+.
18
17
 
18
+ `3.0.0` version and forward will support vRA 8+.
19
+
19
20
  ## Installation
20
21
 
21
22
  Add this line to your application's Gemfile:
@@ -44,165 +45,136 @@ require 'vra'
44
45
  Then, set up your client object. You will need to know your tenant ID from your vRA administrator.
45
46
 
46
47
  ```
47
- vra = Vra::Client.new(username: 'devmgr@corp.local', password: 'mypassword', tenant: 'mytenant', base_url: 'https://vra.corp.local', verify_ssl: true)
48
+ client = Vra::Client.new(username: 'devmgr@corp.local', password: 'mypassword', tenant: 'mytenant', base_url: 'https://vra.corp.local', verify_ssl: true)
48
49
  => #<Vra::Client:0x000000034c0df8 ... >
49
50
  ```
51
+ ### Catalog Types:
50
52
 
51
- To list all items in the catalog:
53
+ To list all the catalog types:
52
54
 
53
55
  ```
54
- vra.catalog.all_items
55
- => [{"@type"=>"CatalogItem", "id"=>"a9cd6148-6e0b-4a80-ac47-f5255c52b43d", "version"=>2, "name"=>"CentOS 6.6", "description"=>"Blueprint for deploying a CentOS Linux development server", ... }]
56
+ client.catalog.all_types
57
+ => [#<Vra::CatalogType:0x00007fcde6855370 @id="com.vmw.vro.workflow", @data={"id"=>"com.vmw.vro.workflow", ... ]
56
58
  ```
57
59
 
58
- To only list the items in the catalog for which you are entitled to request:
60
+ ### Catalog Sources:
61
+
62
+ To list all the catalog sources:
59
63
 
60
64
  ```
61
- vra.catalog.entitled_items
62
- => [{"@type"=>"ConsumerEntitledCatalogItem", "catalogItem"=>{"id"=>"d29efd6b-3cd6-4f8d-b1d8-da4ddd4e52b1", "version"=>2, "name"=>"WindowsServer2012", "description"=>"Windows Server 2012", ... }]
65
+ client.catalog.all_sources
66
+ [#<Vra::CatalogSource:0x00007fcde3948c30 @id="2f5b2d5c-6dc2-4ea7-b304-cd8fea5ede0f", @data= ...]
63
67
  ```
64
68
 
65
- When you are ready to request an item from the catalog, create a new catalog request object:
69
+ And to list the sources that are entitled only:
66
70
 
67
71
  ```
68
- catalog_request = vra.catalog.request('a9cd6148-6e0b-4a80-ac47-f5255c52b43d', cpus: 1, memory: 512, requested_for: 'devmgr@corp.local', lease_days: 30)
69
- => #<Vra::CatalogRequest:0x00000003477c20 ... >
72
+ client.catalog.entitled_sources(project_id)
73
+ => [#<Vra::CatalogSource:0x00007fcde2a28c00 @id="18102dc2-9e48-487a-93a8-aafab2ecc05 ...]
70
74
  ```
71
75
 
72
- To retrive catalog id from catalog name:
76
+ Creating a new source can be done as follows:
73
77
 
74
78
  ```
75
- vra.catalog.fetch_catalog_items('my_catalog_name')
76
- => #<Vra::CatalogRequest:0x00000004477c20 ... >
79
+ source = Vra::CatalogSource.create(client, name: 'New source', catalog_type_id: 'com.vmw.vro.workflow', project_id: project_id)
80
+ => #<Vra::CatalogSource:0x00007fad651f63b8 ... >
77
81
  ```
78
82
 
79
- vRA requires your sub-tenant (a.k.a. "business group") to be specified when requesting an item from the catalog. If the catalog item you are requesting is specifically created for a given business group, the gem will use that ID automatically without you needing to specify it.
83
+ ### Catalog Items
80
84
 
81
- An easier option has been provided to end user to provide a friendly sub-tenant name instead of sub-tenant id, and the driver would take care of retrieving the sub-tenant id for the corresponding sub-tenant name. Tenant name is also required along with sub-tenant name to retrieve sub-tenant id.
82
-
83
- To retrieve sub-tenant id from sub-tenant name:
85
+ To list all items in the catalog:
84
86
 
85
87
  ```
86
- vra.fetch_subtenant_items('my_tenant', 'my_subtenant_name')
88
+ client.catalog.all_items
89
+ => [#<Vra::CatalogItem:0x00007fe583863b28>, #<Vra::CatalogItem:0x00007fe583863ad8 ... ]
87
90
  ```
88
91
 
89
- However, if there is no sub-tenant ID or sub-tenant name available for us to use, you will receive an error when you submit:
92
+ To only list the items in the catalog for which you are entitled to request:
90
93
 
91
94
  ```
92
- request = catalog_request.submit
93
- ArgumentError: Unable to submit request, required param(s) missing => subtenant_id
94
- from /home/aleff/vmware-vra/lib/vra/catalog_request.rb:42:in `validate_params!'
95
- from /home/aleff/vmware-vra/lib/vra/catalog_request.rb:99:in `submit'
96
- from (irb):4
97
- from /opt/chefdk/embedded/bin/irb:11:in `<main>'
98
-
95
+ client.catalog.entitled_items(project_id)
96
+ => [#<Vra::CatalogItem:0x00007fe583863b28>, #<Vra::CatalogItem:0x00007fe583863ad8 ... ]
99
97
  ```
100
98
 
101
- In this case, you will need to supply the sub-tenant ID manually:
99
+ To retrive catalog id from catalog name:
102
100
 
103
101
  ```
104
- catalog_request.subtenant_id = '5327ddd3-1a4e-4663-9e9d-63db86ffc8af'
105
- => "5327ddd3-1a4e-4663-9e9d-63db86ffc8af"
102
+ client.catalog.fetch_catalog_items('centos')
103
+ =>
104
+ [#<Vra::CatalogItem:0x00007fb734110f60
105
+ @id="f2e8c6ee-dd00-32c4-94c7-0a50046cb2f3",
106
+ @data={
107
+ "name"=>"oe-centos-1633598756_bp",
108
+ ...>
109
+ ]
106
110
  ```
107
111
 
108
- If your catalog blueprint item requires additional parameters to successfully submit your request, you may add them:
112
+ ### Requesting Deployments
113
+ When you are ready to request a deployment using a catalog, create a new deployment object:
109
114
 
110
115
  ```
111
- catalog_request.set_parameter('my_parameter', 'string', 'my value')
116
+ request = client.catalog.request(
117
+ catalog_id,
118
+ image_mapping: 'VRA-nc-lnx-ce8.4-Docker',
119
+ flavor_mapping: 'Small',
120
+ name: 'CentOS VRA8 Test',
121
+ project_id: project_id,
122
+ version: '1'
123
+ )
124
+ =>
125
+ #<Vra::DeploymentRequest:0x00007fb7340b7438
126
+ ...
112
127
  ```
128
+ To request a deployment from a catalog item, you can use the above method with a project ID that has a cloud template version released to the project.
129
+
130
+ The ID of the catalog item from which you are requesting the deployment should be also included, and the version of the released cloud template. If the user doesn't specify a version explicitly, the latest version will be fetched automatically and will be used for the request.
131
+
132
+ Additionally, the name of the deployment should be specified and it should be unique.
133
+ The image mapping specifies the OS image for a VM and the flavor mapping specifies the CPU count and RAM of a VM.
113
134
 
114
- If you need to set a parameter on a child object in the blueprint, you can add them by using a ~:
135
+ If your catalog blueprint item requires additional parameters to successfully submit your request, you may add them:
115
136
 
116
137
  ```
117
- catalog_request.set_parameter('object~my_parameter', 'string', 'my value')
138
+ request.set_parameter('my_parameter', 'string', 'my value')
118
139
  ```
119
140
 
120
- ### Creating a request from a yaml or json payload
121
- Should you want to create a request ahead of time you can create the parameters up front by
122
- reading from a file or a hard coded payload you use every time. This is not required by any means but allows
123
- for some extra flexibility when using this request object directly. The only difference is that you can pass
124
- in the request parameters instead of having to set them after you create the object.
141
+ ### Managing the deployment
125
142
 
126
- Given a sample request object like the following you will want to read the yaml into an ruby object:
127
-
128
- ```yaml
129
- requestData:
130
- entries:
131
- key: provider-provisioningGroupId
132
- value:
133
- type: string
134
- value: 93992-3929392-32323828-832882394
135
- key: provider-datacenter
136
- type: string
137
- value: datacenter1
138
- key: provider-domain
139
- type: string
140
- value: chef.com
143
+ Now, submit your request! The client will return a new "Deployment" object you can use to query for status.
141
144
 
142
145
  ```
146
+ deployment = catalog_request.submit
147
+ => #<Vra::Deployment:0x000000027caea0 ... >
143
148
 
144
- And now use that data to create the Vra::RequestParameters to feed into the catalog request.
145
-
146
- ```ruby
147
- # read in the request data
148
- yaml_data = YAML,load(data)
149
- # create a parameters array, although this only works with VRA6, since VRA7 can have complex data
150
- parameters = yaml_data['requestData']['entries'].map {|item| [item['key'], item['value'].values].flatten }
151
- # We put the values in a array so we can easily explode the parameters using the splat operator later
152
- request_params = Vra::RequestParameters.new
153
- # loop through each parameter and setting each parameter
154
- parameters.each {|p| request_params.set(*p) # splat
155
- request_options = {
156
- cpus: 1,
157
- memory: 1024,
158
- requested_for: 'me@me.com',
159
- lease_days: 2,
160
- additional_params: request_params
161
- }
162
- # create the request
163
- catalog_request = vra.catalog.request(blueprint, request_options)
164
- ```
165
-
166
- Now, submit your request! The client will return a new "Request" object you can use to query for status.
167
-
168
- ```
169
- request = catalog_request.submit
170
- => #<Vra::Request:0x000000027caea0 ... >
171
-
172
- request.status
149
+ deployment.status
173
150
  => "IN_PROGRESS"
174
151
  ```
175
152
 
176
- You can easily refresh your request object to get the latest status:
153
+ You can refresh your deployment object to get the latest status:
177
154
 
178
155
  ```
179
- request.refresh && request.status
180
- => "SUCCESSFUL"
181
-
182
- request.completion_state
156
+ deployment.refresh && deployment.status
183
157
  => "SUCCESSFUL"
184
-
185
- request.completion_details
186
- => "Request succeeded. Created hol-dev-32."
187
158
  ```
188
159
 
189
- You can also save the request ID for later, and create a new request object at your leisure to follow-up on your request:
160
+ You can also save the deployment ID for later, and create a new deployment object at your leisure to follow-up on your deployment request:
190
161
 
191
162
  ```
192
- request.id
163
+ deployment.id
193
164
  => "aed22465-02db-481d-b55a-cefe216096a2"
194
165
 
195
- new_request = vra.requests.by_id('aed22465-02db-481d-b55a-cefe216096a2')
196
- => #<Vra::Request:0x0000000564ac30 ... >
166
+ new_deployment = client.deployments.by_id('aed22465-02db-481d-b55a-cefe216096a2')
167
+ => #<Vra::Deployment:0x0000000564ac30 ... >
197
168
 
198
- new_request.status
199
- => "SUCCESSFUL"
169
+ new_deployment.status
170
+ => "CREATE_SUCCESSFUL"
200
171
  ```
201
172
 
202
- When the request is successful, you can query the resources created as the result of your request. Assuming that the catalog item blueprint we requested only creates a single VM, we can get that resource and learn more information about it:
173
+ ### Deployment Resources
174
+ When the deployment request is successful, you can query the resources created as the result of your request. Assuming that the catalog item blueprint we requested only creates a single VM, we can get that resource and learn more information about it:
203
175
 
204
176
  ```
205
- resource = request.resources.first
177
+ resource = deployment.resources.first
206
178
  => #<Vra::Resource:0x00000006772e68 ... >
207
179
 
208
180
  resource.network_interfaces
@@ -215,23 +187,26 @@ resource.name
215
187
  => "hol-dev-32"
216
188
  ```
217
189
 
218
- And just like requests, you can save the resource ID and query it again later:
190
+ If you have the resource_id and the deployment object, you can fetch the resources details as follows
219
191
 
220
192
  ```
221
193
  resource.id
222
194
  => "331fd10b-f2a2-40ae-86bc-1255c1ee9a6d"
223
195
 
224
- new_resource = vra.resources.by_id('331fd10b-f2a2-40ae-86bc-1255c1ee9a6d')
196
+ new_resource = deployment.resource_by_id('331fd10b-f2a2-40ae-86bc-1255c1ee9a6d')
225
197
  => #<Vra::Resource:0x000000067c13b0 ... >
226
198
 
227
199
  new_resource.name
228
200
  => "hol-dev-32"
229
201
  ```
230
202
 
231
- When you no longer need the VM, you can destroy it, which returns another request object you can query for status:
203
+ ### Deleting a deployment from vRA
204
+
205
+ When you no longer need the VM, you can destroy the deployment which will delete all the associated resources as well.
206
+ The method will return a request object you can query for status:
232
207
 
233
208
  ```
234
- destroy_req = resource.destroy
209
+ destroy_req = deployment.destroy
235
210
  => #<Vra::Request:0x00000006ea90d8 ... >
236
211
 
237
212
  destroy_req.status
@@ -241,45 +216,8 @@ destroy_req.status
241
216
  You can also list all resources and requests you have permission to see with these methods:
242
217
 
243
218
  ```
244
- vra.resources.all_resources
245
- vra.requests.all_requests
246
- ```
247
-
248
- ### Download VRA catalog templates
249
- It can be quite useful to download the catalog templates from your VRA server for future playback or inspection. This
250
- can now be easily done with some helpful class methods.
251
-
252
- To get a json string representation of the catalog template you can use `Vra::CatalogItem.dump_template(client, catalog_id)`
253
-
254
- To dump the catalog template to a file instead of a string `Vra::CatalogItem.write_template(client, catalog_id)`. This will create a file like `windows2012.json`.
255
-
256
- If you just want to dump all the templates you can use `Vra::CatalogItem.dump_templates(client)`. This will create a directory named vra_templates
257
- with all the entitled templates for the current user.
258
-
259
- There are additional options you can provide to these methods in order to customize output file names and directories, so please see the source code in lib/vra/catalog_item.rb
260
-
261
- ### Supply custom VRA catalog template and create request
262
- If you previously had some custom templates already in JSON format you can now use those to create requests instead of setting
263
- a bunch of parameters. This can be especially useful when your request has complex parameters or you have a bunch of templates
264
- that you want to create unique requests for.
265
-
266
- To use you can do something like:
267
-
268
- ```ruby
269
- payload_file = '/tmp/windows_2012.json' # must be in json format
270
- cr = Vra::CatalogRequest.request_from_payload(client, payload_file)
271
- cr.submit
272
-
273
- ```
274
-
275
- If you already have a catalog request object you can still supply a custom payload by simply setting the template_payload property.
276
- Note this custom payload will be merged with the properties originally set when creating the catalog request object.
277
-
278
- ```ruby
279
- cr = Vra::CatalogRequest.new(id, opts)
280
- cr.template_payload = File.read('/tmp/windows_2012.json')
281
- cr.submit
282
-
219
+ deployment.resources
220
+ deployment.requests
283
221
  ```
284
222
 
285
223
  ### Pagination
@@ -314,7 +252,7 @@ $env:VRA_HTTP_TRACE=1
314
252
 
315
253
  Author:: Chef Partner Engineering (<partnereng@chef.io>)
316
254
 
317
- Copyright:: Copyright (c) 2015-2016 Chef Software, Inc.
255
+ Copyright:: Copyright (c) 2022 Chef Software, Inc.
318
256
 
319
257
  License:: Apache License, Version 2.0
320
258
 
data/Rakefile CHANGED
@@ -9,15 +9,4 @@ RuboCop::RakeTask.new do |task|
9
9
  task.options << "--display-cop-names"
10
10
  end
11
11
 
12
- begin
13
- require "github_changelog_generator/task"
14
-
15
- GitHubChangelogGenerator::RakeTask.new :changelog do |config|
16
- config.future_release = Vra::VERSION
17
- config.issues = true
18
- end
19
- rescue LoadError
20
- puts "github_changelog_generator is not available. gem install github_changelog_generator to generate changelogs"
21
- end
22
-
23
- task default: [ :spec, :rubocop ]
12
+ task default: %i{spec rubocop}
data/lib/vra/catalog.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
3
  # Author:: Chef Partner Engineering (<partnereng@chef.io>)
4
- # Copyright:: Copyright (c) 2015 Chef Software, Inc.
4
+ # Copyright:: Copyright (c) 2022 Chef Software, Inc.
5
5
  # License:: Apache License, Version 2.0
6
6
  #
7
7
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -25,22 +25,53 @@ module Vra
25
25
  @client = client
26
26
  end
27
27
 
28
+ def all_types
29
+ fetch_resources Vra::CatalogType
30
+ end
31
+
32
+ def all_sources
33
+ fetch_resources Vra::CatalogSource
34
+ end
35
+
28
36
  def all_items
29
- client.http_get_paginated_array!("/catalog-service/api/consumer/catalogItems")
30
- .map! { |x| Vra::CatalogItem.new(client, data: x) }
37
+ fetch_resources Vra::CatalogItem
38
+ end
39
+
40
+ def entitled_sources(project_id)
41
+ fetch_entitlements(project_id, 'CatalogSourceIdentifier')
31
42
  end
32
43
 
33
- def entitled_items
34
- client.http_get_paginated_array!("/catalog-service/api/consumer/entitledCatalogItems")
35
- .map! { |x| Vra::CatalogItem.new(client, data: x["catalogItem"]) }
44
+ def entitled_items(project_id)
45
+ fetch_entitlements(project_id, 'CatalogItemIdentifier')
36
46
  end
37
47
 
38
48
  def request(*args)
39
- Vra::CatalogRequest.new(@client, *args)
49
+ Vra::DeploymentRequest.new(@client, *args)
40
50
  end
41
51
 
42
52
  def fetch_catalog_items(catalog_name)
43
- client.http_get("/catalog-service/api/consumer/entitledCatalogItemViews?%24filter=name+eq+'#{catalog_name}'")
53
+ fetch_resources(
54
+ Vra::CatalogItem,
55
+ '/catalog/api/admin/items',
56
+ "search=#{catalog_name}"
57
+ )
58
+ end
59
+
60
+ private
61
+
62
+ def fetch_resources(klass, url = nil, filter = nil)
63
+ client
64
+ .http_get_paginated_array!(url || klass::INDEX_URL, filter)
65
+ .map! { |x| klass.new(client, data: x) }
66
+ end
67
+
68
+ def fetch_entitlements(project_id, type)
69
+ klass = type == 'CatalogSourceIdentifier' ? Vra::CatalogSource : Vra::CatalogItem
70
+
71
+ client
72
+ .get_parsed("/catalog/api/admin/entitlements?projectId=#{project_id}")
73
+ .select { |x| x['definition']['type'] == type }
74
+ .map! { |x| klass.new(client, data: x['definition']) }
44
75
  end
45
76
  end
46
77
  end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Author:: Ashique Saidalavi (<ashique.saidalavi@progress.com>)
4
+ # Copyright:: Copyright (c) 2022 Chef Software, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+ module Vra
20
+ # Base class with common methods
21
+ class CatalogBase
22
+ attr_reader :id
23
+
24
+ # @param client [Vra::Client] - a vra client object
25
+ # @param opts [Hash] - Contains the either id of the catalog or the data hash
26
+ # Either one of id or data hash is required, must not supply both
27
+ def initialize(client, opts)
28
+ @client = client
29
+ @id = opts[:id]
30
+ @data = opts[:data]
31
+ end
32
+
33
+ def entitle!(opts = {})
34
+ response = client.http_post(
35
+ "/catalog/api/admin/entitlements?project_id=#{project_id}",
36
+ FFI_Yajl::Encoder.encode(entitle_params(opts[:type])),
37
+ opts[:skip_auth] || false
38
+ )
39
+
40
+ FFI_Yajl::Parser.parse(response.body)
41
+ end
42
+
43
+ private
44
+
45
+ attr_reader :client, :data
46
+
47
+ def validate!
48
+ raise ArgumentError, 'must supply id or data hash' if @id.nil? && @data.nil?
49
+ raise ArgumentError, 'must supply id or data hash, not both' if !@id.nil? && !@data.nil?
50
+ end
51
+
52
+ def entitle_params(type)
53
+ {
54
+ 'projectId': project_id,
55
+ 'definition': {
56
+ 'type': type,
57
+ 'id': id
58
+ }
59
+ }
60
+ end
61
+ end
62
+ end