nucleus 0.2.0 → 0.3.1

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 (72) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +3 -0
  3. data/CHANGELOG.md +9 -0
  4. data/README.md +43 -72
  5. data/lib/nucleus/adapter_resolver.rb +3 -3
  6. data/lib/nucleus/adapters/base_adapter.rb +109 -109
  7. data/lib/nucleus/adapters/v1/cloud_foundry_v2/application.rb +111 -111
  8. data/lib/nucleus/adapters/v1/cloud_foundry_v2/cloud_foundry_v2.rb +141 -141
  9. data/lib/nucleus/adapters/v1/cloud_foundry_v2/data.rb +97 -97
  10. data/lib/nucleus/adapters/v1/cloud_foundry_v2/domains.rb +5 -5
  11. data/lib/nucleus/adapters/v1/cloud_foundry_v2/lifecycle.rb +41 -41
  12. data/lib/nucleus/adapters/v1/cloud_foundry_v2/logs.rb +6 -6
  13. data/lib/nucleus/adapters/v1/cloud_foundry_v2/regions.rb +33 -33
  14. data/lib/nucleus/adapters/v1/cloud_foundry_v2/services.rb +6 -6
  15. data/lib/nucleus/adapters/v1/cloud_foundry_v2/vars.rb +80 -80
  16. data/lib/nucleus/adapters/v1/heroku/app_states.rb +57 -57
  17. data/lib/nucleus/adapters/v1/heroku/data.rb +78 -78
  18. data/lib/nucleus/adapters/v1/heroku/heroku.rb +146 -146
  19. data/lib/nucleus/adapters/v1/heroku/lifecycle.rb +51 -51
  20. data/lib/nucleus/adapters/v1/heroku/logs.rb +2 -2
  21. data/lib/nucleus/adapters/v1/heroku/regions.rb +42 -42
  22. data/lib/nucleus/adapters/v1/heroku/services.rb +168 -168
  23. data/lib/nucleus/adapters/v1/heroku/vars.rb +65 -65
  24. data/lib/nucleus/adapters/v1/openshift_v2/app_states.rb +68 -68
  25. data/lib/nucleus/adapters/v1/openshift_v2/application.rb +1 -1
  26. data/lib/nucleus/adapters/v1/openshift_v2/data.rb +96 -96
  27. data/lib/nucleus/adapters/v1/openshift_v2/lifecycle.rb +60 -60
  28. data/lib/nucleus/adapters/v1/openshift_v2/logs.rb +106 -106
  29. data/lib/nucleus/adapters/v1/openshift_v2/openshift_v2.rb +125 -125
  30. data/lib/nucleus/adapters/v1/openshift_v2/regions.rb +58 -58
  31. data/lib/nucleus/adapters/v1/openshift_v2/services.rb +173 -173
  32. data/lib/nucleus/adapters/v1/openshift_v2/vars.rb +49 -49
  33. data/lib/nucleus/adapters/v1/stub_adapter.rb +464 -464
  34. data/lib/nucleus/core/adapter_extensions/auth/auth_client.rb +44 -44
  35. data/lib/nucleus/core/adapter_extensions/auth/expiring_token_auth_client.rb +53 -53
  36. data/lib/nucleus/core/adapter_extensions/auth/http_basic_auth_client.rb +3 -3
  37. data/lib/nucleus/core/adapter_extensions/auth/o_auth2_auth_client.rb +95 -95
  38. data/lib/nucleus/core/adapter_extensions/auth/token_auth_client.rb +36 -36
  39. data/lib/nucleus/core/adapter_extensions/http_client.rb +5 -5
  40. data/lib/nucleus/core/common/files/archive_extractor.rb +1 -1
  41. data/lib/nucleus/core/common/files/archiver.rb +2 -2
  42. data/lib/nucleus/core/file_handling/file_manager.rb +64 -64
  43. data/lib/nucleus/core/file_handling/git_deployer.rb +133 -133
  44. data/lib/nucleus/core/import/adapter_configuration.rb +53 -53
  45. data/lib/nucleus/scripts/initialize_config_defaults.rb +26 -26
  46. data/lib/nucleus/version.rb +1 -1
  47. data/nucleus.gemspec +2 -2
  48. data/spec/integration/api/auth_spec.rb +3 -3
  49. data/spec/spec_helper.rb +98 -98
  50. data/spec/test_suites.rake +1 -1
  51. data/spec/unit/adapters/git_deployer_spec.rb +262 -262
  52. data/spec/unit/common/helpers/auth_helper_spec.rb +1 -1
  53. data/tasks/evaluation.rake +1 -1
  54. data/wiki/adapter_tests.md +0 -7
  55. data/wiki/implement_new_adapter.md +1 -1
  56. metadata +4 -20
  57. data/config/adapters/cloud_control.yml +0 -32
  58. data/lib/nucleus/adapters/v1/cloud_control/application.rb +0 -108
  59. data/lib/nucleus/adapters/v1/cloud_control/authentication.rb +0 -27
  60. data/lib/nucleus/adapters/v1/cloud_control/buildpacks.rb +0 -23
  61. data/lib/nucleus/adapters/v1/cloud_control/cloud_control.rb +0 -153
  62. data/lib/nucleus/adapters/v1/cloud_control/data.rb +0 -76
  63. data/lib/nucleus/adapters/v1/cloud_control/domains.rb +0 -68
  64. data/lib/nucleus/adapters/v1/cloud_control/lifecycle.rb +0 -27
  65. data/lib/nucleus/adapters/v1/cloud_control/log_poller.rb +0 -71
  66. data/lib/nucleus/adapters/v1/cloud_control/logs.rb +0 -103
  67. data/lib/nucleus/adapters/v1/cloud_control/regions.rb +0 -32
  68. data/lib/nucleus/adapters/v1/cloud_control/scaling.rb +0 -17
  69. data/lib/nucleus/adapters/v1/cloud_control/semantic_errors.rb +0 -31
  70. data/lib/nucleus/adapters/v1/cloud_control/services.rb +0 -162
  71. data/lib/nucleus/adapters/v1/cloud_control/token.rb +0 -17
  72. data/lib/nucleus/adapters/v1/cloud_control/vars.rb +0 -88
@@ -1,97 +1,97 @@
1
- module Nucleus
2
- module Adapters
3
- module V1
4
- class CloudFoundryV2 < Stub
5
- module Data
6
- # @see Stub#deploy
7
- def deploy(application_name_or_id, file, file_compression_format)
8
- # could be made async, too
9
- # resources: [] says that no previous data shall be reused, see also:
10
- # http://apidocs.cloudfoundry.org/202/apps/uploads_the_bits_for_an_app.html
11
-
12
- app_guid = app_guid(application_name_or_id)
13
- # deploy by guid
14
- # deploy_response = put("/v2/apps/#{app_guid}/bits", body: { resources: [], application: file.read },
15
- # headers: { 'Content-Type' => 'multipart/form-data; '\
16
- # 'boundary=nucleus-cloud-foundry-adapter-file-upload-boundary' })
17
-
18
- begin
19
- # convert all archives to .zip archives
20
- converted_file = ArchiveConverter.convert(file, file_compression_format, 'zip', true)
21
- unless converted_file.respond_to?(:path) && converted_file.respond_to?(:read)
22
- tmpfile = Tempfile.new(["nucleus-cf-deploy-upload-#{app_guid}", '.zip'])
23
- tmpfile.binmode
24
- tmpfile.write converted_file.read
25
- tmpfile.rewind
26
- converted_file = tmpfile
27
- end
28
-
29
- # TODO: this is only a temporary solution until excon supports multipart requests
30
- # See also: https://github.com/excon/excon/issues/353
31
- url = "#{@endpoint_url}/v2/apps/#{app_guid}/bits"
32
- request_body = { multipart: true, application: converted_file, async: false, resources: '[]' }
33
- begin
34
- RestClient::Request.execute(method: :put, url: url, payload: request_body,
35
- headers: headers, verify_ssl: @check_certificates)
36
- rescue RestClient::BadRequest => e
37
- raise Errors::AdapterRequestError, e.http_body
38
- end
39
- ensure
40
- if tmpfile
41
- tmpfile.close
42
- tmpfile.unlink # deletes this temporary file
43
- end
44
- end
45
- end
46
-
47
- # @see Stub#download
48
- def download(application_name_or_id, compression_format)
49
- app_guid = app_guid(application_name_or_id)
50
- # fail if there is no deployment
51
- unless deployed?(app_guid)
52
- fail Errors::SemanticAdapterRequestError, 'Application must be deployed before data can be downloaded'
53
- end
54
-
55
- download_response = get("/v2/apps/#{app_guid}/download", follow_redirects: false, expects: [200, 302])
56
- if download_response.status == 200
57
- data = download_response.body
58
- else
59
- download_location = download_response.headers[:Location]
60
- # if IBM f*cked with the download URL, fix the address
61
- download_location = download_location.gsub(/objectstorage.service.networklayer.com/, 'objectstorage.softlayer.net')
62
- # omit_default_port: https://github.com/excon/excon/issues/475
63
- data = Excon.new(download_location, omit_default_port: true).get.body
64
- end
65
-
66
- # write data to tmpfile so that it can be converted
67
- downloaded_application_archive = Tempfile.new(["nucleus-cf-deployment-download-#{app_guid}", '.zip'])
68
- downloaded_application_archive.binmode
69
- downloaded_application_archive.write StringIO.new(data).read
70
- downloaded_application_archive.rewind
71
-
72
- # convert from current format (which is always a zip archive) to the destination format
73
- ArchiveConverter.convert(downloaded_application_archive, 'zip', compression_format, false)
74
- ensure
75
- if downloaded_application_archive
76
- downloaded_application_archive.close
77
- downloaded_application_archive.unlink
78
- end
79
- end
80
-
81
- # @see Stub#rebuild
82
- def rebuild(application_name_or_id)
83
- app_guid = app_guid(application_name_or_id)
84
- # fail if there is no deployment
85
- unless deployed?(app_guid)
86
- fail Errors::SemanticAdapterRequestError, 'Application must be deployed before it can be rebuild'
87
- end
88
-
89
- # rebuild by name or id
90
- rebuild_response = post("/v2/apps/#{app_guid}/restage")
91
- to_nucleus_app(rebuild_response.body)
92
- end
93
- end
94
- end
95
- end
96
- end
97
- end
1
+ module Nucleus
2
+ module Adapters
3
+ module V1
4
+ class CloudFoundryV2 < Stub
5
+ module Data
6
+ # @see Stub#deploy
7
+ def deploy(application_name_or_id, file, file_compression_format)
8
+ # could be made async, too
9
+ # resources: [] says that no previous data shall be reused, see also:
10
+ # http://apidocs.cloudfoundry.org/202/apps/uploads_the_bits_for_an_app.html
11
+
12
+ app_guid = app_guid(application_name_or_id)
13
+ # deploy by guid
14
+ # deploy_response = put("/v2/apps/#{app_guid}/bits", body: { resources: [], application: file.read },
15
+ # headers: { 'Content-Type' => 'multipart/form-data; '\
16
+ # 'boundary=nucleus-cloud-foundry-adapter-file-upload-boundary' })
17
+
18
+ begin
19
+ # convert all archives to .zip archives
20
+ converted_file = ArchiveConverter.convert(file, file_compression_format, 'zip', true)
21
+ unless converted_file.respond_to?(:path) && converted_file.respond_to?(:read)
22
+ tmpfile = Tempfile.new(["nucleus-cf-deploy-upload-#{app_guid}", '.zip'])
23
+ tmpfile.binmode
24
+ tmpfile.write converted_file.read
25
+ tmpfile.rewind
26
+ converted_file = tmpfile
27
+ end
28
+
29
+ # TODO: this is only a temporary solution until excon supports multipart requests
30
+ # See also: https://github.com/excon/excon/issues/353
31
+ url = "#{@endpoint_url}/v2/apps/#{app_guid}/bits"
32
+ request_body = { multipart: true, application: converted_file, async: false, resources: '[]' }
33
+ begin
34
+ RestClient::Request.execute(method: :put, url: url, payload: request_body,
35
+ headers: headers, verify_ssl: @check_certificates)
36
+ rescue RestClient::BadRequest => e
37
+ raise Errors::AdapterRequestError, e.http_body
38
+ end
39
+ ensure
40
+ if tmpfile
41
+ tmpfile.close
42
+ tmpfile.unlink # deletes this temporary file
43
+ end
44
+ end
45
+ end
46
+
47
+ # @see Stub#download
48
+ def download(application_name_or_id, compression_format)
49
+ app_guid = app_guid(application_name_or_id)
50
+ # fail if there is no deployment
51
+ unless deployed?(app_guid)
52
+ raise Errors::SemanticAdapterRequestError, 'Application must be deployed before data can be downloaded'
53
+ end
54
+
55
+ download_response = get("/v2/apps/#{app_guid}/download", follow_redirects: false, expects: [200, 302])
56
+ if download_response.status == 200
57
+ data = download_response.body
58
+ else
59
+ download_location = download_response.headers[:Location]
60
+ # if IBM f*cked with the download URL, fix the address
61
+ download_location = download_location.gsub(/objectstorage.service.networklayer.com/, 'objectstorage.softlayer.net')
62
+ # omit_default_port: https://github.com/excon/excon/issues/475
63
+ data = Excon.new(download_location, omit_default_port: true).get.body
64
+ end
65
+
66
+ # write data to tmpfile so that it can be converted
67
+ downloaded_application_archive = Tempfile.new(["nucleus-cf-deployment-download-#{app_guid}", '.zip'])
68
+ downloaded_application_archive.binmode
69
+ downloaded_application_archive.write StringIO.new(data).read
70
+ downloaded_application_archive.rewind
71
+
72
+ # convert from current format (which is always a zip archive) to the destination format
73
+ ArchiveConverter.convert(downloaded_application_archive, 'zip', compression_format, false)
74
+ ensure
75
+ if downloaded_application_archive
76
+ downloaded_application_archive.close
77
+ downloaded_application_archive.unlink
78
+ end
79
+ end
80
+
81
+ # @see Stub#rebuild
82
+ def rebuild(application_name_or_id)
83
+ app_guid = app_guid(application_name_or_id)
84
+ # fail if there is no deployment
85
+ unless deployed?(app_guid)
86
+ raise Errors::SemanticAdapterRequestError, 'Application must be deployed before it can be rebuild'
87
+ end
88
+
89
+ # rebuild by name or id
90
+ rebuild_response = post("/v2/apps/#{app_guid}/restage")
91
+ to_nucleus_app(rebuild_response.body)
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -29,8 +29,8 @@ module Nucleus
29
29
  def create_domain(application_name_or_id, domain)
30
30
  domains(application_name_or_id).each do |existing_domain|
31
31
  if existing_domain[:name] == domain[:name]
32
- fail Errors::SemanticAdapterRequestError,
33
- "Domain '#{domain[:name]}' is already assigned to the application"
32
+ raise Errors::SemanticAdapterRequestError,
33
+ "Domain '#{domain[:name]}' is already assigned to the application"
34
34
  end
35
35
  end
36
36
 
@@ -51,12 +51,12 @@ module Nucleus
51
51
  if delete_response.status == 400
52
52
  cf_error = delete_response.body[:code]
53
53
  if cf_error == 1002
54
- fail Errors::AdapterResourceNotFoundError, 'Domain not found. '\
54
+ raise Errors::AdapterResourceNotFoundError, 'Domain not found. '\
55
55
  'CF context specific: Route does not exist or is not assigned with this application'
56
56
  else
57
57
  # delete failed with 400, but not due to invalid domain
58
- fail Errors::AdapterRequestError,
59
- "#{delete_response.body[:description]} (#{cf_error} - #{delete_response.body[:error_code]})"
58
+ raise Errors::AdapterRequestError,
59
+ "#{delete_response.body[:description]} (#{cf_error} - #{delete_response.body[:error_code]})"
60
60
  end
61
61
  end
62
62
 
@@ -1,41 +1,41 @@
1
- module Nucleus
2
- module Adapters
3
- module V1
4
- class CloudFoundryV2 < Stub
5
- module Lifecycle
6
- # @see Stub#start
7
- def start(application_name_or_id)
8
- app_guid = app_guid(application_name_or_id)
9
- # fail if there is no deployment
10
- unless deployed?(app_guid)
11
- fail Errors::SemanticAdapterRequestError, 'Application must be deployed before it can be started'
12
- end
13
-
14
- # start by name or id
15
- start_response = put("/v2/apps/#{app_guid}", body: { state: 'STARTED' })
16
- to_nucleus_app(start_response.body)
17
- end
18
-
19
- # @see Stub#stop
20
- def stop(application_name_or_id)
21
- app_guid = app_guid(application_name_or_id)
22
- # fail if there is no deployment
23
- unless deployed?(app_guid)
24
- fail Errors::SemanticAdapterRequestError, 'Application must be deployed before it can be stopped'
25
- end
26
-
27
- # stop by name or id
28
- stop_response = put("/v2/apps/#{app_guid}", body: { state: 'STOPPED' })
29
- to_nucleus_app(stop_response.body)
30
- end
31
-
32
- # @see Stub#restart
33
- def restart(application_name_or_id)
34
- stop(application_name_or_id)
35
- start(application_name_or_id)
36
- end
37
- end
38
- end
39
- end
40
- end
41
- end
1
+ module Nucleus
2
+ module Adapters
3
+ module V1
4
+ class CloudFoundryV2 < Stub
5
+ module Lifecycle
6
+ # @see Stub#start
7
+ def start(application_name_or_id)
8
+ app_guid = app_guid(application_name_or_id)
9
+ # fail if there is no deployment
10
+ unless deployed?(app_guid)
11
+ raise Errors::SemanticAdapterRequestError, 'Application must be deployed before it can be started'
12
+ end
13
+
14
+ # start by name or id
15
+ start_response = put("/v2/apps/#{app_guid}", body: { state: 'STARTED' })
16
+ to_nucleus_app(start_response.body)
17
+ end
18
+
19
+ # @see Stub#stop
20
+ def stop(application_name_or_id)
21
+ app_guid = app_guid(application_name_or_id)
22
+ # fail if there is no deployment
23
+ unless deployed?(app_guid)
24
+ raise Errors::SemanticAdapterRequestError, 'Application must be deployed before it can be stopped'
25
+ end
26
+
27
+ # stop by name or id
28
+ stop_response = put("/v2/apps/#{app_guid}", body: { state: 'STOPPED' })
29
+ to_nucleus_app(stop_response.body)
30
+ end
31
+
32
+ # @see Stub#restart
33
+ def restart(application_name_or_id)
34
+ stop(application_name_or_id)
35
+ start(application_name_or_id)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -117,8 +117,8 @@ module Nucleus
117
117
  filter = nil
118
118
  else
119
119
  # invalid log requests --> 404
120
- fail Errors::AdapterResourceNotFoundError,
121
- "Invalid log file '#{log_id}', not available for application '#{app_guid}'"
120
+ raise Errors::AdapterResourceNotFoundError,
121
+ "Invalid log file '#{log_id}', not available for application '#{app_guid}'"
122
122
  end
123
123
  filter
124
124
  end
@@ -146,8 +146,8 @@ module Nucleus
146
146
  log_files = get("/v2/apps/#{app_guid}/instances/0/files/#{file_path}",
147
147
  follow_redirects: false, expects: expected_statuses, headers: headers_to_use)
148
148
  if log_files.status == 400 || log_files.status == 404
149
- fail Errors::AdapterResourceNotFoundError,
150
- "Invalid log file: '#{file_path}' not available for application '#{app_guid}'"
149
+ raise Errors::AdapterResourceNotFoundError,
150
+ "Invalid log file: '#{file_path}' not available for application '#{app_guid}'"
151
151
  end
152
152
  return log_files.body if log_files.status == 200
153
153
 
@@ -162,8 +162,8 @@ module Nucleus
162
162
  downloaded_logfile_response = connection.request(method: :get, expects: expected_statuses)
163
163
 
164
164
  if downloaded_logfile_response.status == 404
165
- fail Errors::AdapterResourceNotFoundError,
166
- "Invalid log file: '#{file_path}' not available for application '#{app_guid}'"
165
+ raise Errors::AdapterResourceNotFoundError,
166
+ "Invalid log file: '#{file_path}' not available for application '#{app_guid}'"
167
167
  end
168
168
  downloaded_logfile_response.body
169
169
  end
@@ -1,33 +1,33 @@
1
- module Nucleus
2
- module Adapters
3
- module V1
4
- class CloudFoundryV2 < Stub
5
- module Regions
6
- # @see Stub#regions
7
- def regions
8
- [default_region]
9
- end
10
-
11
- # @see Stub#region
12
- def region(region_name)
13
- unless region_name.casecmp('default') == 0
14
- fail Errors::AdapterResourceNotFoundError, "Region '#{region_name}' does not exist at the endpoint"
15
- end
16
- default_region
17
- end
18
-
19
- private
20
-
21
- def default_region
22
- {
23
- id: 'default',
24
- description: 'Default region, Cloud Foundry does not support multi regions yet.',
25
- created_at: Time.at(0).to_datetime,
26
- updated_at: Time.at(0).to_datetime
27
- }
28
- end
29
- end
30
- end
31
- end
32
- end
33
- end
1
+ module Nucleus
2
+ module Adapters
3
+ module V1
4
+ class CloudFoundryV2 < Stub
5
+ module Regions
6
+ # @see Stub#regions
7
+ def regions
8
+ [default_region]
9
+ end
10
+
11
+ # @see Stub#region
12
+ def region(region_name)
13
+ unless region_name.casecmp('default') == 0
14
+ raise Errors::AdapterResourceNotFoundError, "Region '#{region_name}' does not exist at the endpoint"
15
+ end
16
+ default_region
17
+ end
18
+
19
+ private
20
+
21
+ def default_region
22
+ {
23
+ id: 'default',
24
+ description: 'Default region, Cloud Foundry does not support multi regions yet.',
25
+ created_at: Time.at(0).to_datetime,
26
+ updated_at: Time.at(0).to_datetime
27
+ }
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -46,8 +46,8 @@ module Nucleus
46
46
  service_guid = service_guid(service_id_or_name)
47
47
  cf_binding = binding(app_guid, service_guid)
48
48
  # make sure there is a binding
49
- fail Errors::AdapterResourceNotFoundError,
50
- "No such service '#{service_id_or_name}' for application '#{application_name_or_id}'" unless cf_binding
49
+ raise Errors::AdapterResourceNotFoundError,
50
+ "No such service '#{service_id_or_name}' for application '#{application_name_or_id}'" unless cf_binding
51
51
  to_nucleus_installed_service(cf_binding)
52
52
  end
53
53
 
@@ -138,8 +138,8 @@ module Nucleus
138
138
  services = get('/v2/services').body[:resources]
139
139
  # find a match and use the service's guid
140
140
  service_match = services.find { |service| service[:entity][:label] == service_id_or_name }
141
- fail error_class,
142
- "Invalid service: Could not find service with name '#{service_id_or_name}'" unless service_match
141
+ raise error_class,
142
+ "Invalid service: Could not find service with name '#{service_id_or_name}'" unless service_match
143
143
  service_match[:metadata][:guid]
144
144
  end
145
145
 
@@ -159,8 +159,8 @@ module Nucleus
159
159
  plans = get("/v2/services/#{service_id}/service_plans").body[:resources]
160
160
  # find a match and use the plan's guid
161
161
  plan_match = plans.find { |plan| plan[:entity][:name] == plan_name_or_id }
162
- fail error_class,
163
- "Invalid plan: No such plan '#{plan_name_or_id}' for service '#{service_id}'" unless plan_match
162
+ raise error_class,
163
+ "Invalid plan: No such plan '#{plan_name_or_id}' for service '#{service_id}'" unless plan_match
164
164
  plan_match[:metadata][:guid]
165
165
  end
166
166
 
@@ -1,80 +1,80 @@
1
- module Nucleus
2
- module Adapters
3
- module V1
4
- class CloudFoundryV2 < Stub
5
- module Vars
6
- # @see Stub#env_vars
7
- def env_vars(application_name_or_id)
8
- app_guid = app_guid(application_name_or_id)
9
- app_vars = get("/v2/apps/#{app_guid}/env").body[:environment_json]
10
- formatted_vars = []
11
- app_vars.each do |key, value|
12
- formatted_vars.push(id: key, key: key, value: value)
13
- end
14
- formatted_vars
15
- end
16
-
17
- # @see Stub#env_var
18
- def env_var(application_name_or_id, env_var_key)
19
- app_guid = app_guid(application_name_or_id)
20
- all_vars = get("/v2/apps/#{app_guid}/env").body[:environment_json]
21
- fail Errors::AdapterResourceNotFoundError,
22
- "Env. var key '#{env_var_key}' does not exist" unless env_var?(app_guid, env_var_key, all_vars)
23
- { id: env_var_key, key: env_var_key, value: all_vars[env_var_key.to_sym] }
24
- end
25
-
26
- # @see Stub#create_env_var
27
- def create_env_var(application_name_or_id, env_var)
28
- app_guid = app_guid(application_name_or_id)
29
- fail Errors::SemanticAdapterRequestError,
30
- "Env. var key '#{env_var[:key]}' already taken" if env_var?(app_guid, env_var[:key])
31
- set_var(app_guid, env_var[:key].to_sym, env_var[:value])
32
- end
33
-
34
- # @see Stub#update_env_var
35
- def update_env_var(application_name_or_id, env_var_key, env_var)
36
- app_guid = app_guid(application_name_or_id)
37
- fail Errors::AdapterResourceNotFoundError,
38
- "Env. var key '#{env_var_key}' does not exist" unless env_var?(app_guid, env_var_key)
39
- set_var(app_guid, env_var_key.to_sym, env_var[:value])
40
- end
41
-
42
- # @see Stub#delete_env_var
43
- def delete_env_var(application_name_or_id, env_var_key)
44
- app_guid = app_guid(application_name_or_id)
45
- fail Errors::AdapterResourceNotFoundError,
46
- "Env. var key '#{env_var_key}' does not exist" unless env_var?(app_guid, env_var_key)
47
- request_body = get("/v2/apps/#{app_guid}/env").body[:environment_json]
48
- request_body.delete(env_var_key.to_sym)
49
- put("/v2/apps/#{app_guid}", body: { environment_json: request_body }).body[:entity][:environment_json]
50
- end
51
-
52
- private
53
-
54
- # Set the variable value (create or update) and include all already existing variables
55
- # to protect them from being deleted.
56
- # @param [String] app_guid GUID of the CF application
57
- # @param [Symbol] key variable key name
58
- # @param [String, Integer, Float, Double] value value to apply to the variable
59
- # @return [Hash] Nucleus representation of the modified variable
60
- def set_var(app_guid, key, value)
61
- request_body = get("/v2/apps/#{app_guid}/env").body[:environment_json]
62
- request_body[key] = value
63
- vars = put("/v2/apps/#{app_guid}", body: { environment_json: request_body }).body[:entity]
64
- { id: key, key: key, value: vars[:environment_json][key] }
65
- end
66
-
67
- # Checks if a variable with the env_var_key already exists.
68
- # @param [String] app_guid GUID of the CF application
69
- # @param [Symbol] env_var_key key name that shall be checked for existence
70
- # @param [Hash] all_vars collection of currently existing variables for the application
71
- # @return [Boolean] true if there is a variable with the env_var_key, otherwise false
72
- def env_var?(app_guid, env_var_key, all_vars = nil)
73
- all_vars = get("/v2/apps/#{app_guid}/env").body[:environment_json] if all_vars.nil?
74
- all_vars.key? env_var_key.to_sym
75
- end
76
- end
77
- end
78
- end
79
- end
80
- end
1
+ module Nucleus
2
+ module Adapters
3
+ module V1
4
+ class CloudFoundryV2 < Stub
5
+ module Vars
6
+ # @see Stub#env_vars
7
+ def env_vars(application_name_or_id)
8
+ app_guid = app_guid(application_name_or_id)
9
+ app_vars = get("/v2/apps/#{app_guid}/env").body[:environment_json]
10
+ formatted_vars = []
11
+ app_vars.each do |key, value|
12
+ formatted_vars.push(id: key, key: key, value: value)
13
+ end
14
+ formatted_vars
15
+ end
16
+
17
+ # @see Stub#env_var
18
+ def env_var(application_name_or_id, env_var_key)
19
+ app_guid = app_guid(application_name_or_id)
20
+ all_vars = get("/v2/apps/#{app_guid}/env").body[:environment_json]
21
+ raise Errors::AdapterResourceNotFoundError,
22
+ "Env. var key '#{env_var_key}' does not exist" unless env_var?(app_guid, env_var_key, all_vars)
23
+ { id: env_var_key, key: env_var_key, value: all_vars[env_var_key.to_sym] }
24
+ end
25
+
26
+ # @see Stub#create_env_var
27
+ def create_env_var(application_name_or_id, env_var)
28
+ app_guid = app_guid(application_name_or_id)
29
+ raise Errors::SemanticAdapterRequestError,
30
+ "Env. var key '#{env_var[:key]}' already taken" if env_var?(app_guid, env_var[:key])
31
+ set_var(app_guid, env_var[:key].to_sym, env_var[:value])
32
+ end
33
+
34
+ # @see Stub#update_env_var
35
+ def update_env_var(application_name_or_id, env_var_key, env_var)
36
+ app_guid = app_guid(application_name_or_id)
37
+ raise Errors::AdapterResourceNotFoundError,
38
+ "Env. var key '#{env_var_key}' does not exist" unless env_var?(app_guid, env_var_key)
39
+ set_var(app_guid, env_var_key.to_sym, env_var[:value])
40
+ end
41
+
42
+ # @see Stub#delete_env_var
43
+ def delete_env_var(application_name_or_id, env_var_key)
44
+ app_guid = app_guid(application_name_or_id)
45
+ raise Errors::AdapterResourceNotFoundError,
46
+ "Env. var key '#{env_var_key}' does not exist" unless env_var?(app_guid, env_var_key)
47
+ request_body = get("/v2/apps/#{app_guid}/env").body[:environment_json]
48
+ request_body.delete(env_var_key.to_sym)
49
+ put("/v2/apps/#{app_guid}", body: { environment_json: request_body }).body[:entity][:environment_json]
50
+ end
51
+
52
+ private
53
+
54
+ # Set the variable value (create or update) and include all already existing variables
55
+ # to protect them from being deleted.
56
+ # @param [String] app_guid GUID of the CF application
57
+ # @param [Symbol] key variable key name
58
+ # @param [String, Integer, Float, Double] value value to apply to the variable
59
+ # @return [Hash] Nucleus representation of the modified variable
60
+ def set_var(app_guid, key, value)
61
+ request_body = get("/v2/apps/#{app_guid}/env").body[:environment_json]
62
+ request_body[key] = value
63
+ vars = put("/v2/apps/#{app_guid}", body: { environment_json: request_body }).body[:entity]
64
+ { id: key, key: key, value: vars[:environment_json][key] }
65
+ end
66
+
67
+ # Checks if a variable with the env_var_key already exists.
68
+ # @param [String] app_guid GUID of the CF application
69
+ # @param [Symbol] env_var_key key name that shall be checked for existence
70
+ # @param [Hash] all_vars collection of currently existing variables for the application
71
+ # @return [Boolean] true if there is a variable with the env_var_key, otherwise false
72
+ def env_var?(app_guid, env_var_key, all_vars = nil)
73
+ all_vars = get("/v2/apps/#{app_guid}/env").body[:environment_json] if all_vars.nil?
74
+ all_vars.key? env_var_key.to_sym
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end