Class: Cfhighlander::Dsl::Subcomponent

Inherits:
DslBase
  • Object
show all
Defined in:
lib/cfhighlander.dsl.subcomponent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from DslBase

#AWSAccountId, #AWSNoValue, #AWSNotificationARNs, #AWSPartition, #AWSStackName, #AWSStackRegion, #AWSURLSuffix, #FindInMap, #FnAnd, #FnBase64, #FnCidr, #FnEquals, #FnFindInMap, #FnGetAZs, #FnGetAtt, #FnIf, #FnImportValue, #FnJoin, #FnNot, #FnOr, #FnSelect, #FnSplit, #FnSub, #GetAtt, #Ref, #cfmap, #cfout

Constructor Details

#initialize(parent, name, template, param_values, component_sources = [], config = {}, export_config = {}, conditional = false, condition = nil, enabled = true, dependson = [], inline = false, distribution_format = 'yaml') ⇒ Subcomponent

Returns a new instance of Subcomponent.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 41

def initialize(parent,
    name,
    template,
    param_values,
    component_sources = [],
    config = {},
    export_config = {},
    conditional = false,
    condition = nil,
    enabled = true,
    dependson = [],
    inline = false,
    distribution_format = 'yaml')

  @parent = parent
  @config = config
  @export_config = export_config
  @component_sources = component_sources
  @conditional = conditional
  @condition = condition
  @dependson = [*dependson]
  @inlined = inline
  
  template_name = template
  template_version = 'latest'
  if template.include?('@') and not (template.start_with? 'git')
    template_name = template.split('@')[0]
    template_version = template.split('@')[1]
  end

  @template = template_name
  @template_version = template_version
  @name = name
  @cfn_name = @name.gsub('-', '').gsub('_', '').gsub(' ', '')
  @param_values = param_values

  # distribution settings
  @distribution_format = distribution_format
  # by default components located at same location as master stack
  @distribution_location = '.'
  build_distribution_url

  # load component
  factory = Cfhighlander::Factory::ComponentFactory.new(@component_sources, parent.template_dir)
  @component_loaded = factory.loadComponentFromTemplate(
      @template,
      @template_version,
      @name
  )
  @component_loaded.config.extend @config

  @parameters = []

  # add condition to parent if conditonal component
  if @conditional
    condition = "Enable#{@cfn_name}" if @condition.nil?
    @condition = condition
    parent_condition_defined = @parent.conditions.find {|c| c.name == @condition}
    unless parent_condition_defined
      @parent.Condition(condition, CfnDsl::Fn.new('Equals', [
          CfnDsl::RefDefinition.new(condition),
          'true'
      ]).to_json)
      @parent.Parameters do
        ComponentParam condition, enabled.to_s, allowedValues: %w(true false)
      end
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

for all the message received, try and forward them to load component dsl



187
188
189
190
191
192
193
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 187

def method_missing(method, *args, &block)
  child_dsl = @component_loaded.highlander_dsl
  if child_dsl.respond_to? method
    # child_dsl.method
    child_dsl.send method, *args, &block
  end
end

Instance Attribute Details

#cfn_nameObject (readonly)

Returns the value of attribute cfn_name.



31
32
33
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 31

def cfn_name
  @cfn_name
end

#component_config_overrideObject

Returns the value of attribute component_config_override.



21
22
23
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 21

def component_config_override
  @component_config_override
end

#component_loadedObject

Returns the value of attribute component_loaded.



21
22
23
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 21

def component_loaded
  @component_loaded
end

#conditionObject (readonly)

Returns the value of attribute condition.



31
32
33
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 31

def condition
  @condition
end

#conditionalObject (readonly)

Returns the value of attribute conditional.



31
32
33
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 31

def conditional
  @conditional
end

#dependsonObject (readonly)

Returns the value of attribute dependson.



31
32
33
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 31

def dependson
  @dependson
end

#distribution_formatObject

Returns the value of attribute distribution_format.



21
22
23
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 21

def distribution_format
  @distribution_format
end

#distribution_locationObject

Returns the value of attribute distribution_location.



21
22
23
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 21

def distribution_location
  @distribution_location
end

#distribution_urlObject

Returns the value of attribute distribution_url.



21
22
23
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 21

def distribution_url
  @distribution_url
end

#export_configObject

Returns the value of attribute export_config.



21
22
23
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 21

def export_config
  @export_config
end

#inlinedObject (readonly)

Returns the value of attribute inlined.



31
32
33
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 31

def inlined
  @inlined
end

#nameObject (readonly)

Returns the value of attribute name.



31
32
33
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 31

def name
  @name
end

#param_valuesObject

Returns the value of attribute param_values.



21
22
23
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 21

def param_values
  @param_values
end

#parametersObject

Returns the value of attribute parameters.



21
22
23
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 21

def parameters
  @parameters
end

#parentObject (readonly)

Returns the value of attribute parent.



31
32
33
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 31

def parent
  @parent
end

#templateObject (readonly)

Returns the value of attribute template.



31
32
33
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 31

def template
  @template
end

#template_versionObject (readonly)

Returns the value of attribute template_version.



31
32
33
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 31

def template_version
  @template_version
end

Instance Method Details

#build_distribution_urlObject



128
129
130
131
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 128

def build_distribution_url
  @distribution_location = @parent.distribute_url unless @parent.distribute_url.nil?
  @distribution_url = "#{@distribution_location}/#{@name}.compiled.#{@distribution_format}"
end

#config(key = '', value = '') ⇒ Object



175
176
177
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 175

def config(key = '', value = '')
  @component_loaded.config[key] = value
end

#ConfigParameter(config_key:, parameter:, defaultValue: '', type: 'String') ⇒ Object



179
180
181
182
183
184
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 179

def ConfigParameter(config_key:, parameter:, defaultValue: '', type: 'String')
  Parameters do
    ComponentParam parameter, defaultValue, type: type
  end
  config config_key, Ref(parameter)
end

#distribute_bucket=(value) ⇒ Object



115
116
117
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 115

def distribute_bucket=(value)
  @component_loaded.distribution_bucket = value
end

#distribute_prefix=(value) ⇒ Object



119
120
121
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 119

def distribute_prefix=(value)
  @component_loaded.distribution_prefix = value
end

#load(component_config_override = {}) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 133

def load(component_config_override = {})
  # Highest priority is DSL defined configuration
  component_config_override.extend @config

  @component_config_override = component_config_override

  @component_loaded.load @component_config_override
end

#parameter(name:, value: '', defaultValue: nil, type: nil, noEcho: nil, allowedValues: nil, allowedPattern: nil, maxLength: nil, maxValue: nil, minLength: nil, minValue: nil) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 142

def parameter(name:, value: '', defaultValue: nil, type: nil, noEcho: nil, allowedValues: nil, allowedPattern: nil, 
              maxLength: nil, maxValue: nil, minLength: nil, minValue: nil)
  existing_params = @component_loaded.highlander_dsl.parameters.param_list
  parameter = existing_params.find { |p| p.name == name}

  if !parameter      
    param_ovr = {}
    param_ovr[:type] = type.nil? ? 'String' : type 
    param_ovr[:noEcho] = noEcho unless noEcho.nil?
    param_ovr[:allowedValues] = allowedValues unless allowedValues.nil?
    param_ovr[:allowedPattern] = allowedPattern unless allowedPattern.nil?
    param_ovr[:maxLength] = maxLength unless maxLength.nil?
    param_ovr[:maxValue] = maxValue unless maxValue.nil?
    param_ovr[:minLength] = minLength unless minLength.nil?
    param_ovr[:minValue] = minValue unless minValue.nil?
    @component_loaded.highlander_dsl.Parameters do
      ComponentParam name, value, **param_ovr
    end
  else
    parameter.default_value = defaultValue unless defaultValue.nil?
    parameter.type = type unless type.nil?
    parameter.no_echo = noEcho unless noEcho.nil?
    parameter.allowed_values = allowedValues unless allowedValues.nil?
    parameter.allowed_pattern = allowedPattern unless allowedPattern.nil?
    parameter.max_length = maxLength unless maxLength.nil?
    parameter.max_value = maxValue unless maxValue.nil?
    parameter.min_length = minLength unless minLength.nil?
    parameter.min_value = minValue unless minValue.nil?
  end
  
  @param_values[name] = value
end

#resolve_parameter_values(available_outputs) ⇒ Object

Parameters should be lazy loaded, that is late-binding should happen once all parameters and mappings are known



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 197

def resolve_parameter_values(available_outputs)
  component_dsl = @component_loaded.highlander_dsl
  component_dsl.parameters.param_list.each do |component_param|
    param = Cfhighlander::Dsl::SubcomponentParameter.new
    param.name = component_param.name
    param.cfndsl_value = SubcomponentParamValueResolver.resolveValue(
        @parent,
        self,
        component_param,
        available_outputs)
    @parameters << param
  end
end

#version=(value) ⇒ Object



111
112
113
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 111

def version=(value)
  @component_loaded.version = value
end