Class: Terraspace::Terraform::Api

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Client, Util::Logging
Defined in:
lib/terraspace/terraform/api.rb,
lib/terraspace/terraform/api/var.rb,
lib/terraspace/terraform/api/http.rb,
lib/terraspace/terraform/api/vars.rb,
lib/terraspace/terraform/api/client.rb

Defined Under Namespace

Modules: Client Classes: Http, Var, Vars

Instance Method Summary collapse

Methods included from Util::Logging

#logger

Methods included from Client

#http

Constructor Details

#initialize(mod, remote) ⇒ Api

Returns a new instance of Api.



7
8
9
10
11
# File 'lib/terraspace/terraform/api.rb', line 7

def initialize(mod, remote)
  @mod = mod
  @organization = remote['organization']
  @workspace_name = remote['workspaces']['name']
end

Instance Method Details

#destroy_workspaceObject



53
54
55
56
# File 'lib/terraspace/terraform/api.rb', line 53

def destroy_workspace
  # resp payload from delete operation is nil
  http.delete("/organizations/#{@organization}/workspaces/#{@workspace_name}")
end

#set_env_varsObject



34
35
36
# File 'lib/terraspace/terraform/api.rb', line 34

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

#set_working_dirObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/terraspace/terraform/api.rb', line 14

def set_working_dir
  return if working_directory == workspace['attributes']['working-directory']

  payload = {
    data: {
      attributes: {
        "working-directory": working_directory
      },
      type: "workspaces"
    }
  }
  http.patch("organizations/#{@organization}/workspaces/#{@workspace_name}", payload)
end

#working_directoryObject



28
29
30
31
32
# File 'lib/terraspace/terraform/api.rb', line 28

def working_directory
  cache_dir = @mod.cache_dir.sub("#{Terraspace.root}/", '')
  relative_root = Terraspace.config.cloud.relative_root # prepended to TFC Working Directory
  relative_root ? "#{relative_root}/#{cache_dir}" : cache_dir
end

#workspace(options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/terraspace/terraform/api.rb', line 38

def workspace(options={})
  payload = http.get("organizations/#{@organization}/workspaces/#{@workspace_name}")
  # Note only way to get here is to bypass init. Example:
  #
  #     terraspace up demo --no-init
  #
  unless payload || options[:exit_on_fail] == false
    logger.error "ERROR: Unable to find the workspace. The workspace may not exist. Or the Terraform token may be invalid. Please double check your Terraform token.".color(:red)
    exit 1
  end
  return unless payload
  payload['data']
end