Class: PropertyGenerator::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/generator/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_data, config, globals) ⇒ Service

Returns a new instance of Service.



8
9
10
11
12
13
14
15
16
# File 'lib/generator/service.rb', line 8

def initialize(service_data, config, globals)
  @service_data = service_data
  @environments = config.environments
  @globals = globals
  @environment_configs = config.environment_configs
  @configmapname = service_data['configname'].nil? ? nil : service_data['configname']
  set_additional_options
  set_service
end

Instance Attribute Details

#additional_optionsObject (readonly)

Returns the value of attribute additional_options.



6
7
8
# File 'lib/generator/service.rb', line 6

def additional_options
  @additional_options
end

#serviceObject

Returns the value of attribute service.



5
6
7
# File 'lib/generator/service.rb', line 5

def service
  @service
end

Instance Method Details

#configmap_nameObject



31
32
33
# File 'lib/generator/service.rb', line 31

def configmap_name
  @configmapname
end

#interpolateObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/generator/service.rb', line 35

def interpolate
  environments = @environments
  # read in config
  # interate through environment and substitute config for values for that environment
  environments.each do |env|
    # get the map of config for a env
    interpolations = @environment_configs[env]['interpolations']

    # Recursively interate through the properties for an environment and gsub the config
    # with defined interpolations.
    service_env = Marshal.load(Marshal.dump(@service[env]))
    interpolate_nested_properties(service_env, interpolations)
    @service[env] = service_env
  end
  service
end

#interpolate_nested_properties(service_env, interpolations) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/generator/service.rb', line 52

def interpolate_nested_properties(service_env, interpolations)
  interpolations.each do |matcher_key, matcher_value|
    service_env.each { |k, v| service_env[k] = v.gsub("{#{matcher_key}}", matcher_value) if v.class == String && v.include?("{#{matcher_key}}") }
    service_env.values.each do |v|
      interpolate_nested_properties(v, interpolations) if v.class == Hash
      v.each_with_index do |val, idx|
        v[idx] = val.gsub("{#{matcher_key}}", matcher_value) if val.class == String && val.include?("{#{matcher_key}}")
      end if v.class == Array
    end
  end
end

#merge_env_default(data, environments) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/generator/service.rb', line 64

def merge_env_default(data, environments)
  # creates a hash of the environments merged with the defaults
  # {service => {env1 =>  {properties},
  #             env2 => {properties}
  #           }
  # }
  output = {}
  default = data['default']

  environments.each do |env|
    default_clone = default.dup
    # if nil, use set to environments as nothing to merge env with
    data['environments'] ||= {}
    data['environments'][env] ||= {}
    environment_data = data['environments'][env].dup
    if data['encrypted']
      encrypted = data['encrypted'][env].dup unless data['encrypted'][env].nil?
      environment_data = data['environments'][env].deep_merge(encrypted) unless encrypted.nil?
    end
    if default_clone.nil?
      merged = environment_data
    else
      merged = default_clone.deep_merge(environment_data)
    end
    output[env] = merged
  end
  output
end

#merge_service_with_globals(globals_data, service_data, environments) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/generator/service.rb', line 93

def merge_service_with_globals(globals_data, service_data, environments)
  # service will now overwrite globals, merging will be done for each environment
  output = {}
  envs = environments
  envs.each do |env|
    globals_clone = globals_data.dup
    if globals_clone[env].nil? || globals_clone[env] == false
      merged = service_data[env]
    else
      merged = globals_clone[env].deep_merge(service_data[env])
    end
    output[env] = merged
  end
  output
end

#set_additional_optionsObject



23
24
25
26
27
28
29
# File 'lib/generator/service.rb', line 23

def set_additional_options
  @additional_options = {}
  @additional_options['configname'] = @service_data['configname'].nil? ? nil : @service_data['configname']
  @additional_options['stringdata'] = @service_data['stringdata'].nil? ? nil : @service_data['stringdata']
  @additional_options['configlabels'] = @service_data['configlabels'].nil? ? nil : @service_data['configlabels']
  @additional_options['secretlabels'] = @service_data['secretlabels'].nil? ? nil : @service_data['secretlabels']
end

#set_serviceObject



18
19
20
21
# File 'lib/generator/service.rb', line 18

def set_service
  service_data = merge_env_default(@service_data, @environments)
  @service = merge_service_with_globals(@globals, service_data, @environments)
end