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/condition.rb,
lib/convection/model/template/parameter.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_collection.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_ec2_route.rb,
lib/convection/model/template/resource/aws_iam_group.rb,
lib/convection/model/template/resource/aws_s3_bucket.rb,
lib/convection/model/template/resource/aws_sns_topic.rb,
lib/convection/model/template/resource/aws_sqs_queue.rb,
lib/convection/model/template/resource/aws_ec2_subnet.rb,
lib/convection/model/template/resource/aws_ec2_volume.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_lambda_alias.rb,
lib/convection/model/template/resource/aws_logs_loggroup.rb,
lib/convection/model/template/resource/aws_ec2_spot_fleet.rb,
lib/convection/model/template/resource/aws_ecr_repository.rb,
lib/convection/model/template/resource/aws_elbv2_listener.rb,
lib/convection/model/template/resource/aws_iam_access_key.rb,
lib/convection/model/template/resource/aws_lambda_version.rb,
lib/convection/model/template/resource/aws_ec2_nat_gateway.rb,
lib/convection/model/template/resource/aws_ec2_network_acl.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_efs_file_system.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_dhcp_options.rb,
lib/convection/model/template/resource/aws_ec2_vpc_endpoint.rb,
lib/convection/model/template/resource/aws_efs_mount_target.rb,
lib/convection/model/template/resource/aws_s3_bucket_policy.rb,
lib/convection/model/template/resource/aws_sns_topic_policy.rb,
lib/convection/model/template/resource/aws_sqs_queue_policy.rb,
lib/convection/model/template/resource/aws_api_gateway_model.rb,
lib/convection/model/template/resource/aws_api_gateway_stage.rb,
lib/convection/model/template/resource/aws_cloud_watch_alarm.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_api_gateway_method.rb,
lib/convection/model/template/resource/aws_ec2_security_group.rb,
lib/convection/model/template/resource/aws_ec2_vpn_connection.rb,
lib/convection/model/template/resource/aws_elbv2_target_group.rb,
lib/convection/model/template/resource/aws_iam_managed_policy.rb,
lib/convection/model/template/resource/aws_api_gateway_account.rb,
lib/convection/model/template/resource/aws_api_gateway_api_key.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/aws_elbv2_listener_rule.rb,
lib/convection/model/template/resource/aws_elbv2_load_balancer.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_attribute/update_policy.rb,
lib/convection/model/template/resource/aws_api_gateway_resource.rb,
lib/convection/model/template/resource/aws_api_gateway_rest_api.rb,
lib/convection/model/template/resource/aws_ec2_customer_gateway.rb,
lib/convection/model/template/resource/aws_ec2_internet_gateway.rb,
lib/convection/model/template/resource/aws_iam_instance_profile.rb,
lib/convection/model/template/resource/aws_route53_health_check.rb,
lib/convection/model/template/resource/aws_ec2_network_acl_entry.rb,
lib/convection/model/template/resource/aws_rds_db_security_group.rb,
lib/convection/model/template/resource/aws_api_gateway_authorizer.rb,
lib/convection/model/template/resource/aws_api_gateway_deployment.rb,
lib/convection/model/template/resource/aws_api_gateway_usage_plan.rb,
lib/convection/model/template/resource/aws_rds_db_parameter_group.rb,
lib/convection/model/template/resource/aws_rds_event_subscription.rb,
lib/convection/model/template/resource/aws_cloudfront_distribution.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_logs_subscription_filter.rb,
lib/convection/model/template/resource_property/aws_ec2_mount_point.rb,
lib/convection/model/template/resource/aws_ec2_security_group_ingres.rb,
lib/convection/model/template/resource/aws_api_gateway_usage_plan_key.rb,
lib/convection/model/template/resource/aws_ec2_vpc_gateway_attachment.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_origin.rb,
lib/convection/model/template/resource_property/aws_lambda_vpc_config.rb,
lib/convection/model/template/resource/aws_auto_scaling_scaling_policy.rb,
lib/convection/model/template/resource/aws_directory_service_simple_ad.rb,
lib/convection/model/template/resource/aws_elasticache_parameter_group.rb,
lib/convection/model/template/resource_property/aws_cloudfront_logging.rb,
lib/convection/model/template/resource_property/aws_events_rule_target.rb,
lib/convection/model/template/resource_property/aws_lambda_environment.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_route53_geolocation.rb,
lib/convection/model/template/resource/aws_api_gateway_base_path_mapping.rb,
lib/convection/model/template/resource/aws_auto_scaling_scheduled_action.rb,
lib/convection/model/template/resource/aws_elasticache_replication_group.rb,
lib/convection/model/template/resource_property/aws_lambda_function_code.rb,
lib/convection/model/template/resource_property/aws_route53_alias_target.rb,
lib/convection/model/template/resource/aws_api_gateway_client_certificate.rb,
lib/convection/model/template/resource_property/aws_ec2_network_interface.rb,
lib/convection/model/template/resource_property/aws_s3_cors_configuration.rb,
lib/convection/model/template/resource/aws_auto_scaling_auto_scaling_group.rb,
lib/convection/model/template/resource/aws_certificate_manager_certificate.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_auto_scaling_launch_configuration.rb,
lib/convection/model/template/resource/aws_ec2_vpn_gateway_route_propagation.rb,
lib/convection/model/template/resource_property/aws_cloudfront_cachebehavior.rb,
lib/convection/model/template/resource_property/aws_ec2_block_device_mapping.rb,
lib/convection/model/template/resource_property/aws_ec2_iam_instance_profile.rb,
lib/convection/model/template/resource_property/aws_s3_website_configuration.rb,
lib/convection/model/template/resource/aws_ec2_subnet_network_acl_association.rb,
lib/convection/model/template/resource/aws_ec2_subnet_route_table_association.rb,
lib/convection/model/template/resource/aws_elasticache_security_group_ingress.rb,
lib/convection/model/template/resource_property/aws_cloudfront_georestriction.rb,
lib/convection/model/template/resource/aws_elasticbeanstalk_applicationversion.rb,
lib/convection/model/template/resource_property/aws_cloudfront_forwardedvalues.rb,
lib/convection/model/template/resource_property/aws_elbv2_listener_rule_action.rb,
lib/convection/model/template/resource_property/aws_elbv2_target_group_matcher.rb,
lib/convection/model/template/resource_property/aws_s3_cors_configuration_rule.rb,
lib/convection/model/template/resource_property/aws_elbv2_listener_certificates.rb,
lib/convection/model/template/resource_property/aws_cloudfront_viewercertificate.rb,
lib/convection/model/template/resource_property/aws_ec2_block_store_block_device.rb,
lib/convection/model/template/resource_property/aws_elbv2_target_group_attribute.rb,
lib/convection/model/template/resource_property/aws_s3_replication_configuration.rb,
lib/convection/model/template/resource/aws_elasticbeanstalk_configurationtemplate.rb,
lib/convection/model/template/resource_property/aws_api_gateway_api_key_stage_key.rb,
lib/convection/model/template/resource_property/aws_elbv2_listener_default_action.rb,
lib/convection/model/template/resource_property/aws_elbv2_listener_rule_condition.rb,
lib/convection/model/template/resource_property/aws_elbv2_load_balancer_attribute.rb,
lib/convection/model/template/resource_property/aws_api_gateway_method_integration.rb,
lib/convection/model/template/resource_property/aws_cloudfront_customerrorresponse.rb,
lib/convection/model/template/resource_property/aws_cloudfront_distribution_config.rb,
lib/convection/model/template/resource_property/aws_cloudfront_defaultcachebehavior.rb,
lib/convection/model/template/resource_property/aws_api_gateway_rest_api_s3_location.rb,
lib/convection/model/template/resource_property/aws_api_gateway_usage_plan_api_stage.rb,
lib/convection/model/template/resource_property/aws_s3_replication_configuration_rule.rb,
lib/convection/model/template/resource_property/aws_api_gateway_method_method_response.rb,
lib/convection/model/template/resource_property/aws_ec2_spot_fleet_request_config_data.rb,
lib/convection/model/template/resource_property/aws_api_gateway_usage_plan_quota_settings.rb,
lib/convection/model/template/resource_property/aws_elbv2_target_group_target_description.rb,
lib/convection/model/template/resource_property/aws_s3_website_configuration_routing_rule.rb,
lib/convection/model/template/resource_property/aws_api_gateway_deployment_stage_description.rb,
lib/convection/model/template/resource_property/aws_api_gateway_usage_plan_throttle_settings.rb,
lib/convection/model/template/resource_property/aws_api_gateway_deployment_stage_method_setting.rb,
lib/convection/model/template/resource_property/aws_s3_replication_configuration_rule_destination.rb,
lib/convection/model/template/resource_property/aws_s3_website_configuration_redirect_all_requests_to.rb,
lib/convection/model/template/resource_property/aws_api_gateway_method_integration_integration_response.rb,
lib/convection/model/template/resource_property/aws_s3_website_configuration_routing_rule_redirect_rule.rb,
lib/convection/model/template/resource_property/aws_elasticache_replication_group_node_group_configuration.rb,
lib/convection/model/template/resource_property/aws_api_gateway_deployment_stage_description_method_setting.rb,
lib/convection/model/template/resource_property/aws_certificate_manager_certificate_domain_validation_option.rb,
lib/convection/model/template/resource_property/aws_ec2_spot_fleet_request_config_data_launch_specifications.rb,
lib/convection/model/template/resource_property/aws_s3_website_configuration_routing_rule_routing_rule_condition.rb,
lib/convection/model/template/resource_property/aws_ec2_spot_fleet_request_config_data_launch_specifications_security_groups.rb,
lib/convection/model/template/resource_property/aws_ec2_spot_fleet_request_config_data_launch_specifications_network_interface.rb,
lib/convection/model/template/resource_property/aws_ec2_spot_fleet_request_config_data_launch_specifications_block_device_mapping.rb,
lib/convection/model/template/resource_property/aws_ec2_spot_fleet_request_config_data_launch_specifications_block_device_mapping_ebs.rb

Overview

Template container class

Defined Under Namespace

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

Constant Summary collapse

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, attach_resource_collection, resource_collection_dsl_methods, resource_dsl_methods

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_import_value, #fn_not, #fn_or, #fn_ref, #fn_sub, #get_att, #get_azs, #join, #select

Constructor Details

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

Returns a new instance of Template.



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

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
  @resource_collections = Collection.new
  @outputs = Collection.new
  @metadata = Collection.new
end

Instance Attribute Details

#attribute_mappingsObject (readonly)

Returns the value of attribute attribute_mappings.



210
211
212
# File 'lib/convection/model/template.rb', line 210

def attribute_mappings
  @attribute_mappings
end

#conditionsObject (readonly)

Returns the value of attribute conditions.



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

def conditions
  @conditions
end

#mappingsObject (readonly)

Returns the value of attribute mappings.



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

def mappings
  @mappings
end

#outputsObject (readonly)

Returns the value of attribute outputs.



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

def outputs
  @outputs
end

#parametersObject (readonly)

Returns the value of attribute parameters.



212
213
214
# File 'lib/convection/model/template.rb', line 212

def parameters
  @parameters
end

#resource_collectionsObject (readonly)

Returns the value of attribute resource_collections.



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

def resource_collections
  @resource_collections
end

#resourcesObject (readonly)

Returns the value of attribute resources.



216
217
218
# File 'lib/convection/model/template.rb', line 216

def resources
  @resources
end

#stackObject (readonly)

Returns the value of attribute stack.



209
210
211
# File 'lib/convection/model/template.rb', line 209

def stack
  @stack
end

Instance Method Details

#all_resourcesObject



272
273
274
275
276
# File 'lib/convection/model/template.rb', line 272

def all_resources
  resource_collections.reduce(resources) do |result, (_name, resource_collection)|
    result.merge(resource_collection.resources)
  end
end

#clone(stack_) ⇒ Object



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

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

#diff(other, stack_ = nil) ⇒ Object



278
279
280
# File 'lib/convection/model/template.rb', line 278

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

#executeObject



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

def execute
  instance_exec(&@definition)

  resource_collections.each do |_, group|
    group.run_definition
    group.execute
  end
end

#render(stack_ = nil) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/convection/model/template.rb', line 254

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' => all_resources.map(&:render),
    'Outputs' => outputs.map(&:render),
    'Metadata' => .map(&:render)
  }
end

#templateObject



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

def template
  self
end

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



282
283
284
285
286
287
# File 'lib/convection/model/template.rb', line 282

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



289
290
291
292
293
# File 'lib/convection/model/template.rb', line 289

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



378
379
380
381
382
383
# File 'lib/convection/model/template.rb', line 378

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



295
296
297
# File 'lib/convection/model/template.rb', line 295

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

#validate_description(rendered_stack) ⇒ Object



372
373
374
375
376
# File 'lib/convection/model/template.rb', line 372

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

#validate_mappings(rendered_stack) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/convection/model/template.rb', line 310

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



360
361
362
363
364
365
366
367
368
369
370
# File 'lib/convection/model/template.rb', line 360

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



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/convection/model/template.rb', line 343

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



299
300
301
302
303
304
305
306
307
308
# File 'lib/convection/model/template.rb', line 299

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