Class: Cfer::Auster::Config

Inherits:
Object
  • Object
show all
Extended by:
Logging::Mixin
Includes:
Logging::Mixin
Defined in:
lib/cfer/auster/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging::Mixin

logger

Constructor Details

#initialize(name:, aws_region:, data:) ⇒ Config

Returns a new instance of Config.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cfer/auster/config.rb', line 13

def initialize(name:, aws_region:, data:)
  raise "name must be a String" unless name.is_a?(String)
  raise "aws_region must be a String" unless aws_region.is_a?(String)
  raise "data must be a Hash" unless data.is_a?(Hash)

  @name = name.dup.freeze
  @aws_region = aws_region.dup.freeze
  @data = data.deep_symbolize_keys

  @data[:PlanID] = @name
  @data[:AWSRegion] = @aws_region
end

Instance Attribute Details

#aws_regionObject (readonly)

Returns the value of attribute aws_region.



10
11
12
# File 'lib/cfer/auster/config.rb', line 10

def aws_region
  @aws_region
end

#dataObject (readonly)

Returns the value of attribute data.



11
12
13
# File 'lib/cfer/auster/config.rb', line 11

def data
  @data
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/cfer/auster/config.rb', line 9

def name
  @name
end

Class Method Details

.from_file(name:, aws_region:, data_file:, schema_file:) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cfer/auster/config.rb', line 41

def from_file(name:, aws_region:, data_file:, schema_file:)
  logger.debug "Loading config set from #{data_file}"
  schema = schema_file.nil? ? nil : Kwalify::Yaml.load_file(schema_file)
  validator = schema.nil? ? nil : Kwalify::Validator.new(schema)

  parser = Kwalify::Yaml::Parser.new(validator)

  data = parser.parse_file(data_file)
  errors = parser.errors()

  if errors && !errors.empty?
    # TODO: make a better error to raise that can encapsulate these validation failures.
    msg = "Schema validation failed for #{data_file}."

    logger.error "Schema validation failed for #{data_file}."
    errors.each do |e|
      logger.error "#{e.linenum}:#{e.column} [#{e.path}] #{e.message}"
    end

    raise msg
  end

  Config.new(name: name, aws_region: aws_region, data: data)
end

Instance Method Details

#env_vars_for_shellObject



30
31
32
33
34
35
36
# File 'lib/cfer/auster/config.rb', line 30

def env_vars_for_shell
  {
    "PLAN_ID" => name,
    "AWS_REGION" => aws_region,
    "AWS_DEFAULT_REGION" => aws_region
  }
end

#full_nameObject



26
27
28
# File 'lib/cfer/auster/config.rb', line 26

def full_name
  "#{aws_region}/#{name}"
end