class Terraspace::Terraform::Api::Var

Public Class Methods

new(workspace, attrs={}) click to toggle source

workspace: details from the api response

# File lib/terraspace/terraform/api/var.rb, line 8
def initialize(workspace, attrs={})
  @workspace, @attrs = workspace, attrs
  @workspace_id = @workspace['id']
end

Public Instance Methods

category() click to toggle source
# File lib/terraspace/terraform/api/var.rb, line 77
def category
  @attrs['category'] || 'terraform' # default category when not set is terraform
end
create() click to toggle source
# File lib/terraspace/terraform/api/var.rb, line 41
def create
  creating_message
  http.post("workspaces/#{@workspace_id}/vars", payload)
end
creating_message() click to toggle source
# File lib/terraspace/terraform/api/var.rb, line 60
def creating_message
  return unless %w[all create].include?(vars.show_message)
  logger.info "Creating Terraform Cloud #{category} variable: #{@attrs['key']}"
end
current_var_resp() click to toggle source
# File lib/terraspace/terraform/api/var.rb, line 69
def current_var_resp
  current_vars_resp['data'].find do |item|
    attributes = item['attributes']
    attributes['key'] == @attrs['key'] &&
    attributes['category'] == category
  end
end
current_vars_resp() click to toggle source
# File lib/terraspace/terraform/api/var.rb, line 82
def current_vars_resp
  @@current_vars_resp ||= http.get("workspaces/#{@workspace_id}/vars")
end
exist?() click to toggle source
# File lib/terraspace/terraform/api/var.rb, line 65
def exist?
  !!current_var_resp
end
overwrite?() click to toggle source
# File lib/terraspace/terraform/api/var.rb, line 25
def overwrite?
  if @attrs['sensitive']
    vars.overwrite_sensitive
  else
    vars.overwrite
  end
end
payload(id=nil) click to toggle source
# File lib/terraspace/terraform/api/var.rb, line 46
def payload(id=nil)
  data = {
    type: "vars",
    attributes: @attrs
  }
  data[:id] = id if id
  { data: data }
end
sync() click to toggle source
# File lib/terraspace/terraform/api/var.rb, line 13
def sync
  exist? ? update : create
end
update() click to toggle source
# File lib/terraspace/terraform/api/var.rb, line 17
def update
  return unless overwrite?
  updating_message
  variable_id = variable_id(@attrs['key'])
  payload = payload(variable_id)
  http.patch("workspaces/#{@workspace_id}/vars/#{variable_id}", payload)
end
updating_message() click to toggle source
# File lib/terraspace/terraform/api/var.rb, line 55
def updating_message
  return unless %w[all update].include?(vars.show_message)
  logger.info "Updating Terraform Cloud #{category} variable: #{@attrs['key']}"
end
variable_id(key) click to toggle source
# File lib/terraspace/terraform/api/var.rb, line 37
def variable_id(key)
  current_var_resp['id']
end
vars() click to toggle source
# File lib/terraspace/terraform/api/var.rb, line 33
def vars
  Terraspace.config.tfc.vars
end