Class: Terraspace::Terraform::Api::Workspace
- Extended by:
- Memoist
- Defined in:
- lib/terraspace/terraform/api/workspace.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #attributes ⇒ Object
- #create ⇒ Object
- #create_or_update ⇒ Object
- #destroy ⇒ Object
- #details(options = {}) ⇒ Object
- #exist? ⇒ Boolean
-
#initialize(mod, organization, name) ⇒ Workspace
constructor
A new instance of Workspace.
- #not_found_error?(payload) ⇒ Boolean
- #set_env_vars ⇒ Object
- #set_working_dir ⇒ Object
- #update ⇒ Object
- #upsert_payload ⇒ Object
- #working_directory ⇒ Object
Methods included from Util::Logging
Methods included from Http::Concern
Constructor Details
#initialize(mod, organization, name) ⇒ Workspace
Returns a new instance of Workspace.
6 7 8 |
# File 'lib/terraspace/terraform/api/workspace.rb', line 6 def initialize(mod, organization, name) @mod, @organization, @name = mod, organization, name end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/terraspace/terraform/api/workspace.rb', line 5 def name @name end |
Instance Method Details
#attributes ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/terraspace/terraform/api/workspace.rb', line 82 def attributes attrs = { name: @name } config = Terraspace.config.tfc.workspace.attrs attrs.merge!(config) # Default: run on all changes since app/modules can affect app/stacks if config['vcs-repo'] && config['file-triggers-enabled'].nil? attrs['file-triggers-enabled'.to_sym] = false end token = ENV['TS_CLOUD_OAUTH_TOKEN'] if config['vcs-repo'] && !config.dig('vcs-repo', 'oauth-token-id') && token attrs['vcs-repo'.to_sym]['oauth-token-id'.to_sym] ||= token end attrs end |
#create ⇒ Object
62 63 64 65 |
# File 'lib/terraspace/terraform/api/workspace.rb', line 62 def create payload = upsert_payload http.post("organizations/#{@organization}/workspaces", payload) end |
#create_or_update ⇒ Object
97 98 99 |
# File 'lib/terraspace/terraform/api/workspace.rb', line 97 def create_or_update exist? ? update : create end |
#destroy ⇒ Object
56 57 58 59 |
# File 'lib/terraspace/terraform/api/workspace.rb', line 56 def destroy # response payload from delete operation is nil http.delete("/organizations/#{@organization}/workspaces/#{@name}") end |
#details(options = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/terraspace/terraform/api/workspace.rb', line 35 def details(={}) payload = http.get("organizations/#{@organization}/workspaces/#{@name}") # Note only way to get here is to bypass init. Example: # # terraspace up demo --no-init # exit_on_fail = [:exit_on_fail].nil? ? true : [:exit_on_fail] if exit_on_fail && not_found_error?(payload) logger.error "ERROR: Unable to find the workspace: #{@name}. The workspace may not exist. Or the Terraform token may be invalid. Please double check your Terraform token.".color(:red) exit 1 end payload['data'] if payload end |
#exist? ⇒ Boolean
101 102 103 |
# File 'lib/terraspace/terraform/api/workspace.rb', line 101 def exist? !!details(exit_on_fail: false) end |
#not_found_error?(payload) ⇒ Boolean
50 51 52 53 54 |
# File 'lib/terraspace/terraform/api/workspace.rb', line 50 def not_found_error?(payload) return true unless payload return false unless payload.key?('errors') payload['errors'][0]['status'] == '404' end |
#set_env_vars ⇒ Object
31 32 33 |
# File 'lib/terraspace/terraform/api/workspace.rb', line 31 def set_env_vars Vars.new(@mod, details).run end |
#set_working_dir ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/terraspace/terraform/api/workspace.rb', line 11 def set_working_dir return if working_directory == details['attributes']['working-directory'] payload = { data: { attributes: { "working-directory": working_directory }, type: "workspaces" } } http.patch("organizations/#{@organization}/workspaces/#{@name}", payload) end |
#update ⇒ Object
67 68 69 70 71 |
# File 'lib/terraspace/terraform/api/workspace.rb', line 67 def update payload = upsert_payload http.patch("organizations/#{@organization}/workspaces/#{@name}", payload) self.flush_cache end |
#upsert_payload ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/terraspace/terraform/api/workspace.rb', line 73 def upsert_payload { data: { attributes: attributes, type: "workspaces" } } end |
#working_directory ⇒ Object
25 26 27 28 29 |
# File 'lib/terraspace/terraform/api/workspace.rb', line 25 def working_directory cache_dir = @mod.cache_dir.sub("#{Terraspace.root}/", '') prefix = Terraspace.config.tfc.working_dir_prefix # prepended to TFC Working Directory prefix ? "#{prefix}/#{cache_dir}" : cache_dir end |