Module: PropertyGenerator

Included in:
Generator
Defined in:
lib/linter/linter.rb,
lib/linter/report.rb,
lib/helpers/helpers.rb,
lib/generator/config.rb,
lib/generator/globals.rb,
lib/generator/service.rb,
lib/generator/generator.rb,
lib/linter/config_linter.rb,
lib/linter/globals_linter.rb,
lib/linter/services_linter.rb

Defined Under Namespace

Classes: Config, ConfigLinter, Generator, Globals, GlobalsLinter, Linter, Report, Service, ServicesLinter

Class Method Summary collapse

Class Method Details

.config_enforcer(environment_configs) ⇒ Object

Force users to specify VPCs for all environments if specified for one environment. This allows us to skip having conditional logic downstream in CPS requests



95
96
97
98
99
100
101
102
103
104
# File 'lib/helpers/helpers.rb', line 95

def config_enforcer(environment_configs)
  a_vpc_exists = false
  environment_configs.each do |environment, config|
    if config.key?('vpc')
      a_vpc_exists = true
    elsif a_vpc_exists
      raise('If you are using VPCs then a VPC must be specified for all environments in the environment_configs')
    end
  end
end

.get_list_of_files(path, ignore_list) ⇒ Object



16
17
18
19
20
# File 'lib/helpers/helpers.rb', line 16

def get_list_of_files(path, ignore_list)
  # Returns a list of files in given path
  # Ignores files in a given ignore list
  Dir.glob("#{path}/**/*").select { |e| File.file?(e) unless ignore_list.include?(e.split('/')[-1]) }
end

.invalid_paths(path, ignore_list) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/helpers/helpers.rb', line 36

def invalid_paths(path, ignore_list)
  invalid_paths = []
  list_of_file_paths = get_list_of_files(path, ignore_list)
  list_of_file_paths.each do |file_path|
    begin
      YAML.load_file(file_path)
    rescue StandardError
      invalid_paths << file_path
    end
  end
  invalid_paths
end

.read_services(path) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/helpers/helpers.rb', line 49

def read_services(path)
  Dir.glob("#{path}/services/*.{yaml,yml}").each_with_object({}) do |file, acc|
    name = File.basename(file)[/(?<service>.*)\.ya?ml$/, :service]
    path = File.absolute_path(file)
    acc[name] = path
  end
end

.sync(region, account, bucket, file, file_region) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/helpers/helpers.rb', line 84

def sync(region, , bucket, file, file_region)
  s3 = Aws::S3::Resource.new(region: region)
  filename = file.split('/').last
  puts "Destination: #{}/#{file_region}/#{filename}"
  puts "Uploading: #{file}"
  obj = s3.bucket(bucket).object("#{}/#{file_region}/#{filename}")
  obj.upload_file(file)
end

.test_runner(object, test_list) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/helpers/helpers.rb', line 8

def test_runner(object, test_list)
  results = {}
  test_list.each do |test|
    results[test] = object.send(test)
  end
  results
end

.valid_paths(path) ⇒ Object



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

def valid_paths(path)
  valid_paths = []
  list_of_file_paths = get_list_of_files(path, [])
  list_of_file_paths.each do |file_path|
    begin
      YAML.load_file(file_path)
      valid_paths << file_path
    rescue StandardError
      next
    end
  end
  valid_paths
end

.writer(service_name, finalized, configs, output_path, additional_options) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/helpers/helpers.rb', line 57

def writer(service_name, finalized, configs, output_path, additional_options)
  output = []
  envs = configs.environments
  environmental_configs = configs.environment_configs
  envs.each do |env|
     = environmental_configs[env]['account']
    region = environmental_configs[env]['region']
    hash = { 'properties' => finalized[env] }
    ['configname', 'stringdata', 'configlabels', 'secretlabels'].each do |setting|
      hash[setting] = additional_options[setting] unless additional_options[setting].nil?
    end
    json = JSON.pretty_generate(hash)
    # IF users are specifing a vpc then we will drop property files under the dir that corresponds to the vpc
    if environmental_configs[env].key?('vpc') && !environmental_configs[env]['vpc'].nil?
      vpc_dir = "#{output_path}/#{}/#{region}/#{environmental_configs[env]['vpc']}"
      FileUtils.mkdir_p("#{vpc_dir}/") unless Dir.exist?(vpc_dir)
      File.write("#{output_path}/#{}/#{region}/#{environmental_configs[env]['vpc']}/#{service_name}.json", json)
      output << "#{output_path}/#{}/#{region}/#{environmental_configs[env]['vpc']}/#{service_name}.json"
    else
      FileUtils.mkdir_p("#{output_path}/#{}/#{region}/") unless Dir.exist?("#{output_path}/#{}/#{region}/")
      File.write("#{output_path}/#{}/#{region}/#{service_name}.json", json)
      output << "#{output_path}/#{}/#{region}/#{service_name}.json"
    end
  end
  output
end