Class: Terraspace::Terraform::Api::Var
- Inherits:
-
Object
- Object
- Terraspace::Terraform::Api::Var
- Extended by:
- Memoist
- Includes:
- Http::Concern, Util::Logging
- Defined in:
- lib/terraspace/terraform/api/var.rb
Constant Summary collapse
- @@current_vars_resp =
nil
Instance Method Summary collapse
- #category ⇒ Object
- #create ⇒ Object
- #creating_message ⇒ Object
- #current_var_resp ⇒ Object
- #current_vars_resp ⇒ Object
- #exist? ⇒ Boolean
-
#initialize(workspace, attrs = {}) ⇒ Var
constructor
workspace: details from the api response.
- #overwrite? ⇒ Boolean
- #payload(id = nil) ⇒ Object
- #sync ⇒ Object
- #update ⇒ Object
- #updating_message ⇒ Object
- #variable_id(key) ⇒ Object
- #vars ⇒ Object
Methods included from Util::Logging
Methods included from Http::Concern
Constructor Details
#initialize(workspace, attrs = {}) ⇒ Var
workspace: details from the api response
8 9 10 11 |
# File 'lib/terraspace/terraform/api/var.rb', line 8 def initialize(workspace, attrs={}) @workspace, @attrs = workspace, attrs @workspace_id = @workspace['id'] end |
Instance Method Details
#category ⇒ Object
77 78 79 |
# File 'lib/terraspace/terraform/api/var.rb', line 77 def category @attrs['category'] || 'terraform' # default category when not set is terraform end |
#create ⇒ Object
41 42 43 44 |
# File 'lib/terraspace/terraform/api/var.rb', line 41 def create http.post("workspaces/#{@workspace_id}/vars", payload) end |
#creating_message ⇒ Object
60 61 62 63 |
# File 'lib/terraspace/terraform/api/var.rb', line 60 def return unless %w[all create].include?(vars.) logger.info "Creating Terraform Cloud #{category} variable: #{@attrs['key']}" end |
#current_var_resp ⇒ Object
69 70 71 72 73 74 75 |
# 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 ⇒ Object
82 83 84 |
# File 'lib/terraspace/terraform/api/var.rb', line 82 def current_vars_resp @@current_vars_resp ||= http.get("workspaces/#{@workspace_id}/vars") end |
#exist? ⇒ Boolean
65 66 67 |
# File 'lib/terraspace/terraform/api/var.rb', line 65 def exist? !!current_var_resp end |
#overwrite? ⇒ Boolean
25 26 27 28 29 30 31 |
# 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) ⇒ Object
46 47 48 49 50 51 52 53 |
# 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 ⇒ Object
13 14 15 |
# File 'lib/terraspace/terraform/api/var.rb', line 13 def sync exist? ? update : create end |
#update ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/terraspace/terraform/api/var.rb', line 17 def update return unless overwrite? variable_id = variable_id(@attrs['key']) payload = payload(variable_id) http.patch("workspaces/#{@workspace_id}/vars/#{variable_id}", payload) end |
#updating_message ⇒ Object
55 56 57 58 |
# File 'lib/terraspace/terraform/api/var.rb', line 55 def return unless %w[all update].include?(vars.) logger.info "Updating Terraform Cloud #{category} variable: #{@attrs['key']}" end |
#variable_id(key) ⇒ Object
37 38 39 |
# File 'lib/terraspace/terraform/api/var.rb', line 37 def variable_id(key) current_var_resp['id'] end |
#vars ⇒ Object
33 34 35 |
# File 'lib/terraspace/terraform/api/var.rb', line 33 def vars Terraspace.config.tfc.vars end |