Sha256: e61e16fda60bb6931f0e0b133489030158622284916970358e2acdaa888c1b33

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

module Terraspace::Terraform
  class Api
    extend Memoist
    include Client
    include Terraspace::Util::Logging

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

    # Docs: https://www.terraform.io/docs/cloud/api/workspaces.html
    def set_working_dir
      working_directory = @mod.cache_dir.sub("#{Terraspace.root}/", '')
      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

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

    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
    memoize :workspace

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
terraspace-0.2.1 lib/terraspace/terraform/api.rb
terraspace-0.2.0 lib/terraspace/terraform/api.rb