Sha256: 006f8e0b162b362aaea09b9ff6bb3d39173653a0485bbd73ba2d9677224dc3e4

Contents?: true

Size: 1.69 KB

Versions: 71

Compression:

Stored size: 1.69 KB

Contents

class Terraspace::Terraform::Api
  class Token
    include Terraspace::Util::Logging

    attr_reader :token
    def initialize
      @creds_path = "#{ENV['HOME']}/.terraform.d/credentials.tfrc.json"
      @hostname = hostname
    end

    def get
      @token = ENV['TERRAFORM_TOKEN']
      return @token if @token
      @token = load
      return @token if @token
      error_exit!
    end

    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

    # Internal note only way to get here is to bypass init. Example:
    #
    #     terraspace up demo --no-init
    #
    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

    def hostname
      ENV['TFC_HOST'] || Terraspace.config.tfc.hostname || 'app.terraform.io'
    end

    def hostname_configured?
      !!Terraspace.config.tfc.hostname
    end

    def self.get
      new.get
    end
  end
end

Version data entries

71 entries across 71 versions & 1 rubygems

Version Path
terraspace-2.2.17 lib/terraspace/terraform/api/token.rb
terraspace-2.2.16 lib/terraspace/terraform/api/token.rb
terraspace-2.2.15 lib/terraspace/terraform/api/token.rb
terraspace-2.2.14 lib/terraspace/terraform/api/token.rb
terraspace-2.2.13 lib/terraspace/terraform/api/token.rb
terraspace-2.2.12 lib/terraspace/terraform/api/token.rb
terraspace-2.2.11 lib/terraspace/terraform/api/token.rb
terraspace-2.2.10 lib/terraspace/terraform/api/token.rb
terraspace-2.2.9 lib/terraspace/terraform/api/token.rb
terraspace-2.2.8 lib/terraspace/terraform/api/token.rb
terraspace-2.2.7 lib/terraspace/terraform/api/token.rb
terraspace-2.2.6 lib/terraspace/terraform/api/token.rb
terraspace-2.2.5 lib/terraspace/terraform/api/token.rb
terraspace-2.2.4 lib/terraspace/terraform/api/token.rb
terraspace-2.2.3 lib/terraspace/terraform/api/token.rb
terraspace-2.2.2 lib/terraspace/terraform/api/token.rb
terraspace-2.2.1 lib/terraspace/terraform/api/token.rb
terraspace-2.2.0 lib/terraspace/terraform/api/token.rb
terraspace-2.1.7 lib/terraspace/terraform/api/token.rb
terraspace-2.1.6 lib/terraspace/terraform/api/token.rb