Class: Terraspace::Terraform::Api::Workspace

Inherits:
Base
  • Object
show all
Extended by:
Memoist
Defined in:
lib/terraspace/terraform/api/workspace.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Logging

#logger

Methods included from Http::Concern

#http

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

#nameObject (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

#attributesObject



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

#createObject



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_updateObject



97
98
99
# File 'lib/terraspace/terraform/api/workspace.rb', line 97

def create_or_update
  exist? ? update : create
end

#destroyObject



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(options={})
  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 = options[:exit_on_fail].nil? ? true : options[: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

Returns:

  • (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

Returns:

  • (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_varsObject



31
32
33
# File 'lib/terraspace/terraform/api/workspace.rb', line 31

def set_env_vars
  Vars.new(@mod, details).run
end

#set_working_dirObject



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

#updateObject



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_payloadObject



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_directoryObject



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