class Cfhighlander::Dsl::Subcomponent

Attributes

cfn_name[R]
component_config_override[RW]
component_loaded[RW]
condition[R]
conditional[R]
dependson[R]
distribution_format[RW]
distribution_location[RW]
distribution_url[RW]
export_config[RW]
inlined[R]
name[R]
param_values[RW]
parameters[RW]
parent[R]
template[R]
template_version[R]

Public Class Methods

new(parent, name, template, param_values, component_sources = [], config = {}, export_config = {}, conditional = false, condition = nil, enabled = true, dependson = [], inline = false, distribution_format = 'yaml') click to toggle source
# 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

Public Instance Methods

ConfigParameter(config_key:, parameter:, defaultValue: '', type: 'String') click to toggle source
# 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
build_distribution_url() click to toggle source
# 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 = '') click to toggle source
# File lib/cfhighlander.dsl.subcomponent.rb, line 175
def config(key = '', value = '')
  @component_loaded.config[key] = value
end
distribute_bucket=(value) click to toggle source
# File lib/cfhighlander.dsl.subcomponent.rb, line 115
def distribute_bucket=(value)
  @component_loaded.distribution_bucket = value
end
distribute_prefix=(value) click to toggle source
# File lib/cfhighlander.dsl.subcomponent.rb, line 119
def distribute_prefix=(value)
  @component_loaded.distribution_prefix = value
end
distribution_format=(value) click to toggle source
# File lib/cfhighlander.dsl.subcomponent.rb, line 123
def distribution_format=(value)
  @distribution_format = value
  build_distribution_url
end
load(component_config_override = {}) click to toggle source
# 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
method_missing(method, *args, &block) click to toggle source

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

# 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
parameter(name:, value: '', defaultValue: nil, type: nil, noEcho: nil, allowedValues: nil, allowedPattern: nil, maxLength: nil, maxValue: nil, minLength: nil, minValue: nil) click to toggle source
# 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) click to toggle source

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

# 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) click to toggle source
# File lib/cfhighlander.dsl.subcomponent.rb, line 111
def version=(value)
  @component_loaded.version = value
end