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

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

  @parent = parent
  @config = config
  @export_config = export_config
  @component_sources = component_sources
  @conditional = conditional

  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



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

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_name ⇒ Object (readonly)

Returns the value of attribute cfn_name.



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

def cfn_name
  @cfn_name
end

#component_config_override ⇒ Object

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_loaded ⇒ Object

Returns the value of attribute component_loaded.



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

def component_loaded
  @component_loaded
end

#conditional ⇒ Object (readonly)

Returns the value of attribute conditional.



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

def conditional
  @conditional
end

#distribution_format ⇒ Object

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_location ⇒ Object

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_url ⇒ Object

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_config ⇒ Object

Returns the value of attribute export_config.



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

def export_config
  @export_config
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#param_values ⇒ Object

Returns the value of attribute param_values.



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

def param_values
  @param_values
end

#parameters ⇒ Object

Returns the value of attribute parameters.



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

def parameters
  @parameters
end

#parent ⇒ Object (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

#template ⇒ Object (readonly)

Returns the value of attribute template.



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

def template
  @template
end

#template_version ⇒ Object (readonly)

Returns the value of attribute template_version.



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

def template_version
  @template_version
end

Instance Method Details

#build_distribution_url ⇒ Object



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

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



134
135
136
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 134

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

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



138
139
140
141
142
143
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 138

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



103
104
105
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 103

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

#distribute_prefix=(value) ⇒ Object



107
108
109
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 107

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

#load(component_config_override = {}) ⇒ Object



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

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



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

def parameter(name:, value:)
  @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



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 156

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



99
100
101
# File 'lib/cfhighlander.dsl.subcomponent.rb', line 99

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