Class: Terraspace::Cloud::Cost

Inherits:
Base show all
Defined in:
lib/terraspace/cloud/cost.rb,
lib/terraspace/cloud/cost/infracost.rb

Defined Under Namespace

Classes: Infracost

Instance Method Summary collapse

Methods inherited from Base

#cancelled?, #changes?, #clean_cache2_stage, #cloud_upload, #initialize, #record?, #sh, #stage_attrs, #success_status

Methods included from Util::Pretty

#pretty_path, #pretty_time

Methods included from Util::Sure

#sure?

Methods included from Util::Logging

#logger

Methods included from Context

#setup_context

Methods included from Api::Validate

#validate

Methods included from Api::Concern

#api, #cloud_stack_name, #expander, #region

Methods included from Api::Concern::Errors

#error_message, #errors?

Methods inherited from Terraspace::CLI::Base

#initialize

Constructor Details

This class inherits a constructor from Terraspace::Cloud::Base

Instance Method Details

#build(stream) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/terraspace/cloud/cost.rb', line 32

def build(stream)
  success = nil
  clean_cache2_stage
  download_previous_cost(stream)
  # .terraspace-cache/dev/stacks/demo
  Dir.chdir(@mod.cache_dir) do
    logger.info "Running cost estimate..."
    success = provider.run
  end
  success
end

#cani?(exit_on_error: true) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/terraspace/cloud/cost.rb', line 61

def cani?(exit_on_error: true)
  return true unless Terraspace.cloud?
  api.create_cost(cani: 1, exit_on_error: exit_on_error)
end

#cost_attrsObject

different from stage_attrs



25
26
27
28
29
30
# File 'lib/terraspace/cloud/cost.rb', line 25

def cost_attrs
  {
    provider: provider.name, # IE: infracost
    provider_version: provider.version,
  }
end

#create(uid:, stream:) ⇒ Object

uid of plan or update



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/terraspace/cloud/cost.rb', line 4

def create(uid:, stream:)
  return unless Terraspace.cloud?
  return unless Terraspace.config.cloud.cost.enabled

  cani?
  success = build(stream)
  return unless success # in case infracost not installed

  upload = cloud_upload.create(type: "cost", stream_id: stream['data']['id'])
  api.create_cost(
    upload_id: upload['data']['id'],
    stack_uid: upload['data']['attributes']['stack_id'], # use stack_uid since stack_id is friendly url name
    uid: uid,
    cost: cost_attrs,
  )
rescue Terraspace::NetworkError => e
  logger.warn "WARN: #{e.class} #{e.message}"
  logger.warn "WARN: Unable to save data to Terraspace cloud"
end

#download_previous_cost(stream) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/terraspace/cloud/cost.rb', line 44

def download_previous_cost(stream)
  stack_id = stream['data']['attributes']['stack_id'] # stack-SHWx8fW5FbDKg3QK
  cost = api.get_previous_cost(stack_uid: stack_id)
  download_url = cost['data']['attributes']['download_url'] # could be nil
  return unless download_url
  uri = URI(download_url)
  json = Net::HTTP.get(uri) # => JSON String
  dest = "#{@mod.cache_dir}/.terraspace-cache/.cache2/cost/cost_prev.json"
  FileUtils.mkdir_p(File.dirname(dest))
  IO.write(dest, json)
end

#providerObject



56
57
58
# File 'lib/terraspace/cloud/cost.rb', line 56

def provider
  Terraspace::Cloud::Cost::Infracost.new(cloud_stack_name: cloud_stack_name) # only provider currently supported
end