Class: Convection::Model::Template

Inherits:
Object
  • Object
show all
Includes:
DSL::IntrinsicFunctions, DSL::Template
Defined in:
lib/convection/model/template.rb,
lib/convection/model/template/output.rb,
lib/convection/model/template/mapping.rb,
lib/convection/model/template/metadata.rb,
lib/convection/model/template/resource.rb,
lib/convection/model/template/parameter.rb,
lib/convection/model/template/condition.rb,
lib/convection/model/template/resource/aws_elb.rb,
lib/convection/model/template/resource_property.rb,
lib/convection/model/template/resource_attribute.rb,
lib/convection/model/template/resource/aws_ec2_eip.rb,
lib/convection/model/template/resource/aws_ec2_vpc.rb,
lib/convection/model/template/resource/aws_kms_key.rb,
lib/convection/model/template/resource/aws_iam_role.rb,
lib/convection/model/template/resource/aws_iam_user.rb,
lib/convection/model/template/resource/aws_s3_bucket.rb,
lib/convection/model/template/resource/aws_iam_group.rb,
lib/convection/model/template/resource/aws_sns_topic.rb,
lib/convection/model/template/resource/aws_ec2_route.rb,
lib/convection/model/template/resource/aws_sqs_queue.rb,
lib/convection/model/template/resource/aws_ec2_volume.rb,
lib/convection/model/template/resource/aws_ec2_subnet.rb,
lib/convection/model/template/resource/aws_iam_policy.rb,
lib/convection/model/template/resource/aws_events_rule.rb,
lib/convection/model/template/resource/aws_ec2_instance.rb,
lib/convection/model/template/resource/aws_logs_loggroup.rb,
lib/convection/model/template/resource/aws_iam_access_key.rb,
lib/convection/model/template/resource/aws_ec2_network_acl.rb,
lib/convection/model/template/resource/aws_lambda_function.rb,
lib/convection/model/template/resource/aws_rds_db_instance.rb,
lib/convection/model/template/resource/aws_ec2_nat_gateway.rb,
lib/convection/model/template/resource/aws_ec2_route_table.rb,
lib/convection/model/template/resource/aws_ec2_vpn_gateway.rb,
lib/convection/model/template/resource/aws_sns_topic_policy.rb,
lib/convection/model/template/resource/aws_ec2_vpc_endpoint.rb,
lib/convection/model/template/resource/aws_ec2_dhcp_options.rb,
lib/convection/model/template/resource/aws_sqs_queue_policy.rb,
lib/convection/model/template/resource/aws_s3_bucket_policy.rb,
lib/convection/model/template/resource/aws_lambda_permission.rb,
lib/convection/model/template/resource/aws_route53_recordset.rb,
lib/convection/model/template/resource/aws_cloud_watch_alarm.rb,
lib/convection/model/template/resource/aws_iam_managed_policy.rb,
lib/convection/model/template/resource/aws_ec2_vpn_connection.rb,
lib/convection/model/template/resource/aws_ec2_security_group.rb,
lib/convection/model/template/resource/aws_rds_db_subnet_group.rb,
lib/convection/model/template/resource/aws_route53_hosted_zone.rb,
lib/convection/model/template/resource/aws_ec2_eip_association.rb,
lib/convection/model/template/resource/aws_elasticache_cluster.rb,
lib/convection/model/template/resource_attribute/update_policy.rb,
lib/convection/model/template/resource/aws_iam_instance_profile.rb,
lib/convection/model/template/resource/aws_ec2_customer_gateway.rb,
lib/convection/model/template/resource/aws_route53_health_check.rb,
lib/convection/model/template/resource/aws_ec2_internet_gateway.rb,
lib/convection/model/template/resource/aws_rds_db_security_group.rb,
lib/convection/model/template/resource/aws_ec2_network_acl_entry.rb,
lib/convection/model/template/resource/aws_rds_db_parameter_group.rb,
lib/convection/model/template/resource/aws_cloudfront_distribution.rb,
lib/convection/model/template/resource_property/aws_ec2_mount_point.rb,
lib/convection/model/template/resource/aws_ec2_vpn_connection_route.rb,
lib/convection/model/template/resource/aws_elasticache_subnet_group.rb,
lib/convection/model/template/resource/aws_ec2_security_group_ingres.rb,
lib/convection/model/template/resource/aws_ec2_vpc_gateway_attachment.rb,
lib/convection/model/template/resource_property/aws_lambda_vpc_config.rb,
lib/convection/model/template/resource_property/aws_cloudfront_origin.rb,
lib/convection/model/template/resource/aws_ec2_vpc_peering_connection.rb,
lib/convection/model/template/resource/aws_elasticache_security_group.rb,
lib/convection/model/template/resource_property/aws_cloudfront_logging.rb,
lib/convection/model/template/resource/aws_directory_service_simple_ad.rb,
lib/convection/model/template/resource_property/aws_events_rule_target.rb,
lib/convection/model/template/resource/aws_auto_scaling_scaling_policy.rb,
lib/convection/model/template/resource/aws_elasticache_parameter_group.rb,
lib/convection/model/template/resource/aws_elasticbeanstalk_application.rb,
lib/convection/model/template/resource/aws_elasticbeanstalk_environment.rb,
lib/convection/model/template/resource_property/aws_cloudfront_s3origin.rb,
lib/convection/model/template/resource_property/aws_lambda_function_code.rb,
lib/convection/model/template/resource/aws_elasticache_replication_group.rb,
lib/convection/model/template/resource_property/aws_s3_cors_configuration.rb,
lib/convection/model/template/resource_property/aws_ec2_network_interface.rb,
lib/convection/model/template/resource/aws_auto_scaling_auto_scaling_group.rb,
lib/convection/model/template/resource_property/aws_cloudfront_customorigin.rb,
lib/convection/model/template/resource_property/aws_cloudfront_restrictions.rb,
lib/convection/model/template/resource/aws_ec2_vpn_gateway_route_propagation.rb,
lib/convection/model/template/resource/aws_auto_scaling_launch_configuration.rb,
lib/convection/model/template/resource_property/aws_ec2_block_device_mapping.rb,
lib/convection/model/template/resource_property/aws_cloudfront_cachebehavior.rb,
lib/convection/model/template/resource_property/aws_cloudfront_georestriction.rb,
lib/convection/model/template/resource/aws_ec2_subnet_network_acl_association.rb,
lib/convection/model/template/resource/aws_elasticache_security_group_ingress.rb,
lib/convection/model/template/resource/aws_ec2_subnet_route_table_association.rb,
lib/convection/model/template/resource_property/aws_cloudfront_forwardedvalues.rb,
lib/convection/model/template/resource/aws_elasticbeanstalk_applicationversion.rb,
lib/convection/model/template/resource_property/aws_s3_cors_configuration_rule.rb,
lib/convection/model/template/resource_property/aws_ec2_block_store_block_device.rb,
lib/convection/model/template/resource_property/aws_cloudfront_viewercertificate.rb,
lib/convection/model/template/resource/aws_elasticbeanstalk_configurationtemplate.rb,
lib/convection/model/template/resource_property/aws_cloudfront_distribution_config.rb,
lib/convection/model/template/resource_property/aws_cloudfront_customerrorresponse.rb,
lib/convection/model/template/resource_property/aws_cloudfront_defaultcachebehavior.rb

Overview

Template container class

Defined Under Namespace

Classes: Condition, Mapping, Metadata, Output, Parameter, Resource, ResourceAttribute, ResourceProperty

Constant Summary

DEFAULT_VERSION =
'2010-09-09'.freeze

Constants included from DSL::Template

DSL::Template::CF_MAX_BYTESIZE, DSL::Template::CF_MAX_DESCRIPTION_BYTESIZE, DSL::Template::CF_MAX_MAPPINGS, DSL::Template::CF_MAX_MAPPING_ATTRIBUTES, DSL::Template::CF_MAX_MAPPING_ATTRIBUTE_NAME, DSL::Template::CF_MAX_MAPPING_NAME, DSL::Template::CF_MAX_OUTPUTS, DSL::Template::CF_MAX_OUTPUT_NAME_CHARACTERS, DSL::Template::CF_MAX_PARAMETERS, DSL::Template::CF_MAX_PARAMETER_NAME_CHARACTERS, DSL::Template::CF_MAX_PARAMETER_VALUE_BYTESIZE, DSL::Template::CF_MAX_RESOURCES, DSL::Template::CF_MAX_RESOURCE_NAME

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DSL::Template

#condition, #logs_log_group, #mapping, #metadata, #output, #parameter, #resource

Methods included from DSL::Template::Resource

attach_resource

Methods included from DSL::Helpers

#camel_case, included, method_name, #screaming_snake_case, #snake_case

Methods included from DSL::IntrinsicFunctions

#base64, #find_in_map, #fn_and, #fn_equals, #fn_if, #fn_not, #fn_or, #fn_ref, #get_att, #get_azs, #join, #select

Constructor Details

#initialize(stack = nil, &block) ⇒ Template

Returns a new instance of Template



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/convection/model/template.rb', line 197

def initialize(stack = nil, &block)
  @definition = block

  @stack = stack
  @attribute_mappings = {}

  @version = DEFAULT_VERSION
  @description = ''

  @parameters = Collection.new
  @mappings = Collection.new
  @conditions = Collection.new
  @resources = Collection.new
  @outputs = Collection.new
  @metadata = Collection.new
end

Instance Attribute Details

#attribute_mappingsObject (readonly)

Returns the value of attribute attribute_mappings



185
186
187
# File 'lib/convection/model/template.rb', line 185

def attribute_mappings
  @attribute_mappings
end

#conditionsObject (readonly)

Returns the value of attribute conditions



189
190
191
# File 'lib/convection/model/template.rb', line 189

def conditions
  @conditions
end

#mappingsObject (readonly)

Returns the value of attribute mappings



188
189
190
# File 'lib/convection/model/template.rb', line 188

def mappings
  @mappings
end

#outputsObject (readonly)

Returns the value of attribute outputs



191
192
193
# File 'lib/convection/model/template.rb', line 191

def outputs
  @outputs
end

#parametersObject (readonly)

Returns the value of attribute parameters



187
188
189
# File 'lib/convection/model/template.rb', line 187

def parameters
  @parameters
end

#resourcesObject (readonly)

Returns the value of attribute resources



190
191
192
# File 'lib/convection/model/template.rb', line 190

def resources
  @resources
end

#stackObject (readonly)

Returns the value of attribute stack



184
185
186
# File 'lib/convection/model/template.rb', line 184

def stack
  @stack
end

Instance Method Details

#clone(stack_) ⇒ Object



214
215
216
# File 'lib/convection/model/template.rb', line 214

def clone(stack_)
  Template.new(stack_, &@definition)
end

#diff(other, stack_ = nil) ⇒ Object



240
241
242
# File 'lib/convection/model/template.rb', line 240

def diff(other, stack_ = nil)
  render(stack_).diff(other).map { |diff| Diff.new(diff[0], *diff[1]) }
end

#executeObject



218
219
220
# File 'lib/convection/model/template.rb', line 218

def execute
  instance_exec(&@definition)
end

#render(stack_ = nil) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/convection/model/template.rb', line 222

def render(stack_ = nil)
  ## Instantiate a new template with the definition block and an other stack
  return clone(stack_).render unless stack_.nil?

  execute ## Process the template document

  {
    'AWSTemplateFormatVersion' => version,
    'Description' => description,
    'Parameters' => parameters.map(&:render),
    'Mappings' => mappings.map(&:render),
    'Conditions' => conditions.map(&:render),
    'Resources' => resources.map(&:render),
    'Outputs' => outputs.map(&:render),
    'Metadata' => .map(&:render)
  }
end

#templateObject



193
194
195
# File 'lib/convection/model/template.rb', line 193

def template
  self
end

#to_json(stack_ = nil, pretty = false) ⇒ Object



244
245
246
247
248
249
# File 'lib/convection/model/template.rb', line 244

def to_json(stack_ = nil, pretty = false)
  rendered_stack = render(stack_)
  validate(rendered_stack)
  return JSON.generate(rendered_stack) unless pretty
  JSON.pretty_generate(rendered_stack)
end

#validate(rendered_stack = nil) ⇒ Object



251
252
253
254
255
# File 'lib/convection/model/template.rb', line 251

def validate(rendered_stack = nil)
  %w(resources mappings parameters outputs description bytesize).map do |method|
    send("validate_#{method}", rendered_stack)
  end
end

#validate_bytesize(rendered_stack) ⇒ Object



340
341
342
343
344
345
# File 'lib/convection/model/template.rb', line 340

def validate_bytesize(rendered_stack)
  json = JSON.generate(rendered_stack)
  validate_compare(json.bytesize,
                   CF_MAX_BYTESIZE,
                   ExcessiveTemplateSizeError)
end

#validate_compare(value, cf_max, error) ⇒ Object



257
258
259
# File 'lib/convection/model/template.rb', line 257

def validate_compare(value, cf_max, error)
  limit_exceeded_error(value, cf_max, error) if value > cf_max
end

#validate_description(rendered_stack) ⇒ Object



334
335
336
337
338
# File 'lib/convection/model/template.rb', line 334

def validate_description(rendered_stack)
  validate_compare(rendered_stack['Description'].bytesize,
                   CF_MAX_DESCRIPTION_BYTESIZE,
                   ExcessiveDescriptionError)
end

#validate_mappings(rendered_stack) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/convection/model/template.rb', line 272

def validate_mappings(rendered_stack)
  mappings = rendered_stack ['Mappings']
  validate_compare(mappings.count,
                   CF_MAX_MAPPINGS,
                   ExcessiveMappingsError)
  mappings.each do |_, value|
    validate_compare(
      value.count,
      CF_MAX_MAPPING_ATTRIBUTES,
      ExcessiveMappingAttributesError
    )
  end

  mappings.keys.each do |key|
    validate_compare(key.length,
                     CF_MAX_MAPPING_NAME,
                     ExcessiveMappingNameError)
  end

  ## XXX What are we trying to do here @aburke
  mapping_attributes = mappings.values.flat_map do |inner_hash|
    inner_hash.keys.select do |key|
      value = inner_hash[key]
    end
  end

  mapping_attributes.each do |attribute|
    validate_compare(attribute.length,
                     CF_MAX_MAPPING_ATTRIBUTE_NAME,
                     ExcessiveMappingAttributeNameError)
  end
end

#validate_outputs(rendered_stack) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
# File 'lib/convection/model/template.rb', line 322

def validate_outputs(rendered_stack)
  outputs = rendered_stack['Outputs']
  validate_compare(outputs.count,
                   CF_MAX_OUTPUTS,
                   ExcessiveOutputsError)
  largest_output_name = outputs.keys.max
  largest_output_name ||= ''
  validate_compare(largest_output_name.length,
                   CF_MAX_OUTPUT_NAME_CHARACTERS,
                   ExcessiveOutputNameError)
end

#validate_parameters(rendered_stack) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/convection/model/template.rb', line 305

def validate_parameters(rendered_stack)
  parameters = rendered_stack['Parameters']
  validate_compare(parameters.count,
                   CF_MAX_PARAMETERS,
                   ExcessiveParametersError)
  largest_parameter_name = parameters.keys.max
  largest_parameter_name ||= ''
  validate_compare(largest_parameter_name.length,
                   CF_MAX_PARAMETER_NAME_CHARACTERS,
                   ExcessiveParameterNameError)
  parameters.values.each do |value|
    validate_compare(JSON.generate(value).bytesize,
                     CF_MAX_PARAMETER_VALUE_BYTESIZE,
                     ExcessiveParameterBytesizeError)
  end
end

#validate_resources(rendered_stack) ⇒ Object



261
262
263
264
265
266
267
268
269
270
# File 'lib/convection/model/template.rb', line 261

def validate_resources(rendered_stack)
  validate_compare(rendered_stack['Resources'].count,
                   CF_MAX_RESOURCES,
                   ExcessiveResourcesError)

  largest_resource_name = resources.keys.max || ''
  validate_compare(largest_resource_name.length,
                   CF_MAX_RESOURCE_NAME,
                   ExcessiveResourceNameError)
end