Class: Bcome::Node::MetaDataLoader

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/objects/node/meta_data_loader.rb

Constant Summary collapse

META_DATA_FILE_PATH_PREFIX =
'bcome/metadata'.freeze

Instance Method Summary collapse

Constructor Details

#initializeMetaDataLoader

Returns a new instance of MetaDataLoader.



7
8
9
# File 'lib/objects/node/meta_data_loader.rb', line 7

def initialize
  @all_metadata_filenames = Dir["#{META_DATA_FILE_PATH_PREFIX}/*"]
end

Instance Method Details

#dataObject



15
16
17
# File 'lib/objects/node/meta_data_loader.rb', line 15

def data
  @data ||= do_load
end

#data_for_namespace(namespace) ⇒ Object



19
20
21
22
# File 'lib/objects/node/meta_data_loader.rb', line 19

def data_for_namespace(namespace)
  static_data = data[namespace.to_sym] ? data[namespace.to_sym] : {}
  static_data.merge(terraform_data_for_namespace(namespace))
end

#decryption_keyObject



11
12
13
# File 'lib/objects/node/meta_data_loader.rb', line 11

def decryption_key
  @decryption_key
end

#do_loadObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/objects/node/meta_data_loader.rb', line 59

def do_load
   = {}
  @all_metadata_filenames.each do |filepath|
    next if filepath =~ /-unenc/  # we only read from the encrypted, packed files. 

    begin
      filedata = load_file_data_for(filepath)
      .deep_merge!(filedata)
    rescue Psych::SyntaxError => e
      raise Bcome::Exception::InvalidMetaDataConfig, "Error: #{e.message}"
    end
  end
  return 
end

#load_file_data_for(filepath) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/objects/node/meta_data_loader.rb', line 48

def load_file_data_for(filepath)
 if filepath =~ /.enc/  # encrypted file contents 
    prompt_for_decryption_key unless decryption_key
    encrypted_contents = File.read(filepath)     
    decrypted_contents = encrypted_contents.decrypt(decryption_key) 
    return YAML.load(decrypted_contents)
  else # unencrypted
    return YAML.load_file(filepath)
  end
end

#prompt_for_decryption_keyObject



43
44
45
46
# File 'lib/objects/node/meta_data_loader.rb', line 43

def prompt_for_decryption_key
  print "\nEnter your decryption key: ".informational
  @decryption_key = STDIN.noecho(&:gets).chomp
end

#terraform_data_for_namespace(namespace) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/objects/node/meta_data_loader.rb', line 24

def terraform_data_for_namespace(namespace)
  ## TODO Not sure what was being smoked, but this only adds in data for the first module
  ## Until I can fix, we will: 

  parser = Bcome::Terraform::Parser.new(namespace)
  attributes = parser.attributes

  terraform_data = {}

  if attributes.keys.any?
    ## 1. Keep the old broken implementation
    terraform_data["terraform_attributes"] = attributes if attributes.keys.any?
    ## 2. But make all the data accessible
    terraform_data["tf_state"] = parser.state.config
  end

  terraform_data
end