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, enabled = true, inline = false, distribution_format = 'yaml') ⇒ Subcomponent

Returns a new instance of Subcomponent.



39
40
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
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 39

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

  @parent = parent
  @config = config
  @export_config = export_config
  @component_sources = component_sources
  @conditional = conditional
  @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)
  @component_loaded = factory.loadComponentFromTemplate(
      @template,
      @template_version,
      @name
  )
  @component_loaded.config.extend @config

  @parameters = []

  # add condition to parent if conditonal component
  if @conditional
    condition_param_name = "Enable#{@cfn_name}"
    @parent.Condition(condition_param_name, CfnDsl::Fn.new('Equals', [
        CfnDsl::RefDefinition.new(condition_param_name),
        'true'
    ]).to_json)
    @parent.Parameters do
      ComponentParam condition_param_name, enabled.to_s, allowedValues: %w(true false)
    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



155
156
157
158
159
160
161
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 155

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

#conditionalObject (readonly)

Returns the value of attribute conditional.



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

def conditional
  @conditional
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



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

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



143
144
145
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 143

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

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



147
148
149
150
151
152
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 147

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



105
106
107
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 105

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

#distribute_prefix=(value) ⇒ Object



109
110
111
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 109

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

#load(component_config_override = {}) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 123

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:) ⇒ Object



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

def parameter(name:, value:)
  existing_params = @component_loaded.highlander_dsl.parameters.param_list
  param_exists = existing_params.find { |p| p.name == name}
  if (not param_exists)
    @component_loaded.highlander_dsl.Parameters do
      ComponentParam name, '', type: 'String'
    end
  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



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 165

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



101
102
103
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 101

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