Class: Jets::Dotenv::Var

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
AwsServices, Util::Logging
Defined in:
lib/jets/dotenv/var.rb

Constant Summary collapse

SSM_VARIABLE_REGEXP =
/^SSM:(.*)/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Logging

#log

Methods included from AwsServices

#apigateway, #aws_options, #cfn, #codebuild, #dynamodb, #lambda_client, #logs, #s3, #s3_resource, #sns, #sqs, #ssm, #sts, #wafv2

Methods included from AwsServices::StackStatus

#output_value, #stack_exists?

Methods included from AwsServices::GlobalMemoist

included, #reset_cache!

Constructor Details

#initialize(raw_key, raw_value) ⇒ Var

Returns a new instance of Var.



8
9
10
# File 'lib/jets/dotenv/var.rb', line 8

def initialize(raw_key, raw_value)
  @raw_key, @raw_value = raw_key, raw_value
end

Instance Attribute Details

#raw_keyObject (readonly)

Returns the value of attribute raw_key.



7
8
9
# File 'lib/jets/dotenv/var.rb', line 7

def raw_key
  @raw_key
end

#raw_valueObject (readonly)

Returns the value of attribute raw_value.



7
8
9
# File 'lib/jets/dotenv/var.rb', line 7

def raw_value
  @raw_value
end

Instance Method Details

#nameObject



12
13
14
# File 'lib/jets/dotenv/var.rb', line 12

def name
  @raw_key
end

#ssm?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/jets/dotenv/var.rb', line 56

def ssm?
  @raw_value&.start_with?("SSM")
end

#ssm_missing?Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/jets/dotenv/var.rb', line 51

def ssm_missing?
  value # trigger memoization
  !!@ssm_missing
end

#ssm_nameObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jets/dotenv/var.rb', line 22

def ssm_name
  if @raw_value == "SSM"
    # "/#{Jets.project.name}/#{Jets.env}/#{@raw_key}"
    Convention.new.ssm_name(@raw_key)
  else
    value = @raw_value.sub(/SSM:/i, "")
    if value.start_with?("/")
      value
    else
      # "/#{Jets.project.name}/#{Jets.env}/#{value}"
      Convention.new.ssm_name(value)
    end
  end
end

#ssm_valueObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jets/dotenv/var.rb', line 37

def ssm_value
  return "fake-ssm-value" if ENV["JETS_NO_INTERNET"]

  name = ssm_name
  resp = ssm.get_parameter(name: name, with_decryption: true)
  resp.parameter.value
rescue Aws::SSM::Errors::ParameterNotFound
  @ssm_missing = true
  nil
rescue Aws::SSM::Errors::ValidationException
  puts "ERROR: Invalid SSM parameter name: #{name.inspect}".color(:red)
  raise
end

#valueObject



16
17
18
# File 'lib/jets/dotenv/var.rb', line 16

def value
  ssm? ? ssm_value : @raw_value
end