vmware-vra 2.7.2 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -1
  3. data/CHANGELOG.md +5 -0
  4. data/README.md +79 -144
  5. data/Rakefile +0 -11
  6. data/lib/vra/catalog.rb +39 -8
  7. data/lib/vra/catalog_base.rb +62 -0
  8. data/lib/vra/catalog_item.rb +28 -74
  9. data/lib/vra/catalog_source.rb +116 -0
  10. data/lib/vra/catalog_type.rb +56 -0
  11. data/lib/vra/client.rb +62 -54
  12. data/lib/vra/deployment.rb +155 -0
  13. data/lib/vra/deployment_request.rb +117 -0
  14. data/lib/vra/{resources.rb → deployments.rb} +26 -17
  15. data/lib/vra/exceptions.rb +1 -1
  16. data/lib/vra/http.rb +11 -6
  17. data/lib/vra/request.rb +28 -36
  18. data/lib/vra/request_parameters.rb +12 -12
  19. data/lib/vra/resource.rb +32 -203
  20. data/lib/vra/version.rb +2 -2
  21. data/lib/vra.rb +15 -12
  22. data/spec/catalog_item_spec.rb +64 -222
  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 +271 -226
  27. data/spec/deployment_request_spec.rb +192 -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 +0 -1
  44. metadata +40 -30
  45. data/.travis.yml +0 -14
  46. data/lib/vra/catalog_request.rb +0 -137
  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: 748935ab6ce2c1922e5ffded2a7d2567709308e4c62bfe81d15d22b71a186508
4
- data.tar.gz: 95b4943dc8f6015055e4a7c142115a8f555072c79244c4a62fed2e0e1862c8ac
3
+ metadata.gz: d3671834fbc97510772f7d42d524f2775789c7ac0fe7f29c80dad8c64effe3b6
4
+ data.tar.gz: 394929c607677c9c2341e9a6060e9341810f2a8941111247e2fe1e1f71ce5afb
5
5
  SHA512:
6
- metadata.gz: 4b244e271a780513e497356114eff2ca721cf7fe2665d1fe6cbb10a500284b7702b53679a3c3aa19bd18727f6064ee6f2cb17fe38e5685e29716667b4d3cf25f
7
- data.tar.gz: 8f1bd967dc41e4ed9363ae6404c402bdbb4524689d05d86a37c56e1d86f31efa374018108c5ebeef582972d15151391a25c7856621f0eeca33331c84b3846c3c
6
+ metadata.gz: a04bbf0434372a58119367bb5f3d9f35c10fff450263e14f92fac6bc78082a74d0ca8fbca713b4ae7a1c0fdc1e1a40a953b321b21be32bfd0996db3cf107d776
7
+ data.tar.gz: 3f3646d1aa4fb7d15fd451fce246ab9ccb1e4828dfd4e13110abdb5ce552a3c4a158fb3ff8ab6a6139154af6fec3c57646566876ae4af2c6414bd6f5fb5284b7
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,10 @@
1
1
  # Change Log
2
2
 
3
+ ## [3.0.0](https://github.com/chef-partners/vmware-vra-gem/tree/v3.0.0) (2022-01-18)
4
+ [Full Changelog](https://github.com/chef-partners/vmware-vra-gem/compare/v2.7.2...v3.0.0)
5
+
6
+ - Rewritten to support vRA 8. If you require support for 7 make sure to pin on the previous 2.7.2 release.
7
+
3
8
  ## [2.7.2](https://github.com/chef-partners/vmware-vra-gem/tree/v2.7.2) (2020-09-09)
4
9
  [Full Changelog](https://github.com/chef-partners/vmware-vra-gem/compare/v2.7.1...v2.7.2)
5
10
 
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,166 +45,134 @@ 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
+ 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.
130
+ Additionally, the name of the deployment should be specified and it should be unique.
131
+ The image mapping specifies the OS image for a VM and the flavor mapping specifies the CPU count and RAM of a VM.
113
132
 
114
- If you need to set a parameter on a child object in the blueprint, you can add them by using a ~:
133
+ If your catalog blueprint item requires additional parameters to successfully submit your request, you may add them:
115
134
 
116
135
  ```
117
- catalog_request.set_parameter('object~my_parameter', 'string', 'my value')
136
+ request.set_parameter('my_parameter', 'string', 'my value')
118
137
  ```
119
138
 
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.
139
+ ### Managing the deployment
125
140
 
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
141
+ Now, submit your request! The client will return a new "Deployment" object you can use to query for status.
141
142
 
142
143
  ```
144
+ deployment = catalog_request.submit
145
+ => #<Vra::Deployment:0x000000027caea0 ... >
143
146
 
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
- In the above option instead of cpus and memory, shirt_size can be used as well if the blueprint has shirt size option enabled. e.g. of shirt size can be like value.small, value.medium etc,
166
-
167
- Now, submit your request! The client will return a new "Request" object you can use to query for status.
168
-
169
- ```
170
- request = catalog_request.submit
171
- => #<Vra::Request:0x000000027caea0 ... >
172
-
173
- request.status
147
+ deployment.status
174
148
  => "IN_PROGRESS"
175
149
  ```
176
150
 
177
- You can easily refresh your request object to get the latest status:
151
+ You can refresh your deployment object to get the latest status:
178
152
 
179
153
  ```
180
- request.refresh && request.status
181
- => "SUCCESSFUL"
182
-
183
- request.completion_state
154
+ deployment.refresh && deployment.status
184
155
  => "SUCCESSFUL"
185
-
186
- request.completion_details
187
- => "Request succeeded. Created hol-dev-32."
188
156
  ```
189
157
 
190
- You can also save the request ID for later, and create a new request object at your leisure to follow-up on your request:
158
+ 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:
191
159
 
192
160
  ```
193
- request.id
161
+ deployment.id
194
162
  => "aed22465-02db-481d-b55a-cefe216096a2"
195
163
 
196
- new_request = vra.requests.by_id('aed22465-02db-481d-b55a-cefe216096a2')
197
- => #<Vra::Request:0x0000000564ac30 ... >
164
+ new_deployment = client.deployments.by_id('aed22465-02db-481d-b55a-cefe216096a2')
165
+ => #<Vra::Deployment:0x0000000564ac30 ... >
198
166
 
199
- new_request.status
200
- => "SUCCESSFUL"
167
+ new_deployment.status
168
+ => "CREATE_SUCCESSFUL"
201
169
  ```
202
170
 
203
- 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:
171
+ ### Deployment Resources
172
+ 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:
204
173
 
205
174
  ```
206
- resource = request.resources.first
175
+ resource = deployment.resources.first
207
176
  => #<Vra::Resource:0x00000006772e68 ... >
208
177
 
209
178
  resource.network_interfaces
@@ -216,23 +185,26 @@ resource.name
216
185
  => "hol-dev-32"
217
186
  ```
218
187
 
219
- And just like requests, you can save the resource ID and query it again later:
188
+ If you have the resource_id and the deployment object, you can fetch the resources details as follows
220
189
 
221
190
  ```
222
191
  resource.id
223
192
  => "331fd10b-f2a2-40ae-86bc-1255c1ee9a6d"
224
193
 
225
- new_resource = vra.resources.by_id('331fd10b-f2a2-40ae-86bc-1255c1ee9a6d')
194
+ new_resource = deployment.resource_by_id('331fd10b-f2a2-40ae-86bc-1255c1ee9a6d')
226
195
  => #<Vra::Resource:0x000000067c13b0 ... >
227
196
 
228
197
  new_resource.name
229
198
  => "hol-dev-32"
230
199
  ```
231
200
 
232
- When you no longer need the VM, you can destroy it, which returns another request object you can query for status:
201
+ ### Deleting a deployment from vRA
202
+
203
+ When you no longer need the VM, you can destroy the deployment which will delete all the associated resources as well.
204
+ The method will return a request object you can query for status:
233
205
 
234
206
  ```
235
- destroy_req = resource.destroy
207
+ destroy_req = deployment.destroy
236
208
  => #<Vra::Request:0x00000006ea90d8 ... >
237
209
 
238
210
  destroy_req.status
@@ -242,45 +214,8 @@ destroy_req.status
242
214
  You can also list all resources and requests you have permission to see with these methods:
243
215
 
244
216
  ```
245
- vra.resources.all_resources
246
- vra.requests.all_requests
247
- ```
248
-
249
- ### Download VRA catalog templates
250
- It can be quite useful to download the catalog templates from your VRA server for future playback or inspection. This
251
- can now be easily done with some helpful class methods.
252
-
253
- To get a json string representation of the catalog template you can use `Vra::CatalogItem.dump_template(client, catalog_id)`
254
-
255
- 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`.
256
-
257
- 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
258
- with all the entitled templates for the current user.
259
-
260
- 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
261
-
262
- ### Supply custom VRA catalog template and create request
263
- If you previously had some custom templates already in JSON format you can now use those to create requests instead of setting
264
- a bunch of parameters. This can be especially useful when your request has complex parameters or you have a bunch of templates
265
- that you want to create unique requests for.
266
-
267
- To use you can do something like:
268
-
269
- ```ruby
270
- payload_file = '/tmp/windows_2012.json' # must be in json format
271
- cr = Vra::CatalogRequest.request_from_payload(client, payload_file)
272
- cr.submit
273
-
274
- ```
275
-
276
- If you already have a catalog request object you can still supply a custom payload by simply setting the template_payload property.
277
- Note this custom payload will be merged with the properties originally set when creating the catalog request object.
278
-
279
- ```ruby
280
- cr = Vra::CatalogRequest.new(id, opts)
281
- cr.template_payload = File.read('/tmp/windows_2012.json')
282
- cr.submit
283
-
217
+ deployment.resources
218
+ deployment.requests
284
219
  ```
285
220
 
286
221
  ### Pagination
@@ -315,7 +250,7 @@ $env:VRA_HTTP_TRACE=1
315
250
 
316
251
  Author:: Chef Partner Engineering (<partnereng@chef.io>)
317
252
 
318
- Copyright:: Copyright (c) 2015-2016 Chef Software, Inc.
253
+ Copyright:: Copyright (c) 2022 Chef Software, Inc.
319
254
 
320
255
  License:: Apache License, Version 2.0
321
256
 
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
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
@@ -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");
@@ -21,110 +21,64 @@ require "ffi_yajl" unless defined?(FFI_Yajl)
21
21
  require "vra/catalog"
22
22
 
23
23
  module Vra
24
- class CatalogItem
25
- attr_reader :id, :client
26
- def initialize(client, opts)
27
- @client = client
28
- @id = opts[:id]
29
- @catalog_item_data = opts[:data]
24
+ # Class that represents the Catalog Item
25
+ class CatalogItem < Vra::CatalogBase
26
+ INDEX_URL = '/catalog/api/admin/items'
30
27
 
31
- if @id.nil? && @catalog_item_data.nil?
32
- raise ArgumentError, "must supply an id or a catalog item data hash"
33
- end
28
+ attr_reader :project_id
34
29
 
35
- if !@id.nil? && !@catalog_item_data.nil?
36
- raise ArgumentError, "must supply an id OR a catalog item data hash, not both"
37
- end
30
+ def initialize(client, opts = {})
31
+ super
32
+ @project_id = opts[:project_id]
33
+ validate!
38
34
 
39
- if @catalog_item_data.nil?
35
+ if @data.nil?
40
36
  fetch_catalog_item
41
37
  else
42
- @id = @catalog_item_data["id"]
38
+ @id = @data['id']
43
39
  end
44
40
  end
45
41
 
46
42
  def fetch_catalog_item
47
- @catalog_item_data = client.get_parsed("/catalog-service/api/consumer/catalogItems/#{id}")
43
+ @data = client.get_parsed("/catalog/api/admin/items/#{id}")
48
44
  rescue Vra::Exception::HTTPNotFound
49
45
  raise Vra::Exception::NotFound, "catalog ID #{id} does not exist"
50
46
  end
51
47
 
52
48
  def name
53
- @catalog_item_data["name"]
49
+ data['name']
54
50
  end
55
51
 
56
52
  def description
57
- @catalog_item_data["description"]
58
- end
59
-
60
- def status
61
- @catalog_item_data["status"]
53
+ data['description']
62
54
  end
63
55
 
64
- def organization
65
- return {} if @catalog_item_data["organization"].nil?
66
-
67
- @catalog_item_data["organization"]
56
+ def source_id
57
+ data['sourceId']
68
58
  end
69
59
 
70
- def tenant_id
71
- organization["tenantRef"]
60
+ def source_name
61
+ data['sourceName']
72
62
  end
73
63
 
74
- def tenant_name
75
- organization["tenantLabel"]
64
+ def source
65
+ @source ||= Vra::CatalogSource.new(client, id: source_id)
76
66
  end
77
67
 
78
- def subtenant_id
79
- organization["subtenantRef"]
68
+ def type
69
+ @type ||= Vra::CatalogType.new(client, data: data['type'])
80
70
  end
81
71
 
82
- def subtenant_name
83
- organization["subtenantLabel"]
72
+ def icon_id
73
+ data['iconId']
84
74
  end
85
75
 
86
- def blueprint_id
87
- @catalog_item_data["providerBinding"]["bindingId"]
76
+ def entitle!(opts = {})
77
+ super(opts.merge(type: 'CatalogItemIdentifier'))
88
78
  end
89
79
 
90
- # @param [String] - the id of the catalog item
91
- # @param [Vra::Client] - a vra client object
92
- # @return [String] - returns a json string of the catalog template
93
- def self.dump_template(client, id)
94
- response = client.http_get("/catalog-service/api/consumer/entitledCatalogItems/#{id}/requests/template")
95
- response.body
96
- end
97
-
98
- # @param client [Vra::Client] - a vra client object
99
- # @param id [String] - the id of the catalog item
100
- # @param filename [String] - the name of the file you want to output the template to
101
- # if left blank, will default to the id of the item
102
- # @note outputs the catalog template to a file in serialized format
103
- def self.write_template(client, id, filename = nil)
104
- filename ||= "#{id}.json"
105
- begin
106
- contents = dump_template(client, id)
107
- data = JSON.parse(contents)
108
- pretty_contents = JSON.pretty_generate(data)
109
- File.write(filename, pretty_contents)
110
- filename
111
- rescue Vra::Exception::HTTPError => e
112
- raise e
113
- end
114
- end
115
-
116
- # @param [Vra::Client] - a vra client object
117
- # @param [String] - the directory path to write the files to
118
- # @param [Boolean] - set to true if you wish the file name to be the id of the catalog item
119
- # @return [Array[String]] - a array of all the files that were generated
120
- def self.dump_templates(client, dir_name = "vra_templates", use_id = false)
121
- FileUtils.mkdir_p(dir_name) unless File.exist?(dir_name)
122
- client.catalog.entitled_items.map do |c|
123
- id = use_id ? c.id : c.name.tr(" ", "_")
124
- filename = File.join(dir_name, "#{id}.json").downcase
125
- write_template(client, c.id, filename)
126
- filename
127
- end
80
+ def self.entitle!(client, id)
81
+ new(client, id: id).entitle!
128
82
  end
129
83
  end
130
84
  end