class Terraspace::Terraform::Api::Token

Attributes

token[R]

Public Class Methods

get() click to toggle source
# File lib/terraspace/terraform/api/token.rb, line 61
def self.get
  new.get
end
new() click to toggle source
# File lib/terraspace/terraform/api/token.rb, line 6
def initialize
  @creds_path = "#{ENV['HOME']}/.terraform.d/credentials.tfrc.json"
  @hostname = hostname
end

Public Instance Methods

error_exit!() click to toggle source

Internal note only way to get here is to bypass init. Example:

terraspace up demo --no-init
# File lib/terraspace/terraform/api/token.rb, line 39
    def error_exit!
      login_hostname = @hostname if hostname_configured?
      logger.error "ERROR: Unable to not find a Terraform token. A Terraform token is needed for Terraspace to call the Terraform API.".color(:red)
      logger.error <<~EOL
        Here are some ways to provide the Terraform token:

            1. By running: terraform login #{login_hostname}
            2. With an env variable: export TERRAFORM_TOKEN=xxx

        Please configure a Terraform token and try again.
      EOL
      exit 1
    end
get() click to toggle source
# File lib/terraspace/terraform/api/token.rb, line 11
def get
  @token = ENV['TERRAFORM_TOKEN']
  return @token if @token
  @token = load
  return @token if @token
  error_exit!
end
hostname() click to toggle source
# File lib/terraspace/terraform/api/token.rb, line 53
def hostname
  ENV['TFC_HOST'] || Terraspace.config.tfc.hostname || 'app.terraform.io'
end
hostname_configured?() click to toggle source
# File lib/terraspace/terraform/api/token.rb, line 57
def hostname_configured?
  !!Terraspace.config.tfc.hostname
end
load() click to toggle source
# File lib/terraspace/terraform/api/token.rb, line 19
    def load
      return unless File.exist?(@creds_path)

      data = JSON.load(IO.read(@creds_path))
      @token = data.dig('credentials', @hostname, 'token')
      return @token if @token

      return unless hostname_configured?
      logger.error "You configured a cloud.hostname: #{@hostname}".color(:red)
      logger.error <<~EOL
        But it was not found into your #{@creds_path}
        Please double check it.
      EOL
      @token
    end