class Terraspace::Terraform::Api::Workspace

Attributes

name[R]

Public Class Methods

new(mod, organization, name) click to toggle source
# File lib/terraspace/terraform/api/workspace.rb, line 6
def initialize(mod, organization, name)
  @mod, @organization, @name = mod, organization, name
end

Public Instance Methods

attributes() click to toggle source
# 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() click to toggle source

Docs: www.terraform.io/docs/cloud/api/workspaces.html

# File lib/terraspace/terraform/api/workspace.rb, line 62
def create
  payload = upsert_payload
  http.post("organizations/#{@organization}/workspaces", payload)
end
create_or_update() click to toggle source
# File lib/terraspace/terraform/api/workspace.rb, line 97
def create_or_update
  exist? ? update : create
end
destroy() click to toggle source
# 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={}) click to toggle source
# 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?() click to toggle source
# File lib/terraspace/terraform/api/workspace.rb, line 101
def exist?
  !!details(exit_on_fail: false)
end
not_found_error?(payload) click to toggle source
# 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() click to toggle source
# File lib/terraspace/terraform/api/workspace.rb, line 31
def set_env_vars
  Vars.new(@mod, details).run
end
set_working_dir() click to toggle source

Docs: www.terraform.io/docs/cloud/api/workspaces.html

# 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() click to toggle source
# 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() click to toggle source
# File lib/terraspace/terraform/api/workspace.rb, line 73
def upsert_payload
  {
    data: {
      attributes: attributes,
      type: "workspaces"
    }
  }
end
working_directory() click to toggle source
# 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