Class: Convection::Model::Template::Resource

Inherits:
Object
  • Object
show all
Includes:
DSL::Helpers, DSL::Template::Resource, Mixin::Conditional
Defined in:
lib/convection/model/template/resource.rb,
lib/convection/model/template/resource/aws_elb.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/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/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/aws_ec2_vpc_peering_connection.rb,
lib/convection/model/template/resource/aws_elasticache_security_group.rb,
lib/convection/model/template/resource/aws_directory_service_simple_ad.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/aws_elasticache_replication_group.rb,
lib/convection/model/template/resource/aws_auto_scaling_auto_scaling_group.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/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/aws_elasticbeanstalk_applicationversion.rb,
lib/convection/model/template/resource/aws_elasticbeanstalk_configurationtemplate.rb

Overview

Resource

Defined Under Namespace

Classes: AutoScalingGroup, CloudFrontDistribution, CloudWatchAlarm, DirectoryServiceSimpleAD, EC2CustomerGateway, EC2DHCPOptions, EC2EIP, EC2Instance, EC2InternetGateway, EC2NatGateway, EC2NetworkACL, EC2NetworkACLEntry, EC2Route, EC2RouteTable, EC2SecurityGroup, EC2SecurityGroupIngres, EC2Subnet, EC2SubnetNetworkACLAssociation, EC2SubnetRouteTableAssociation, EC2VPC, EC2VPCEndpoint, EC2VPCGatewayAttachment, EC2VPCPeeringConnection, EC2VPNConnection, EC2VPNConnectionRoute, EC2VPNGateway, EC2VPNGatewayRoutePropagation, EC2Volume, EIPAssociation, ELB, ElastiCacheCluster, ElastiCacheParameterGroup, ElastiCacheReplicationGroup, ElastiCacheSecurityGroup, ElastiCacheSecurityGroupIngress, ElastiCacheSubnetGroup, ElasticBeanstalkApplication, ElasticBeanstalkApplicationVersion, ElasticBeanstalkConfigurationTemplate, ElasticBeanstalkEnvironment, EventsRule, HashProperty, HashPropertyInstance, IAMAccessKey, IAMGroup, IAMInstanceProfile, IAMManagedPolicy, IAMPolicy, IAMRole, IAMUser, KmsKey, Lambda, LambdaPermission, LaunchConfiguration, ListProperty, ListPropertyInstance, LogGroup, Property, PropertyInstance, RDSDBInstance, RDSDBParameterGroup, RDSDBSecurityGroup, RDSDBSubnetGroup, Route53HealthCheck, Route53HostedZone, Route53RecordSet, S3Bucket, S3BucketPolicy, SNSTopic, SNSTopicPolicy, SQSQueue, SQSQueuePolicy, ScalarProperty, ScalarPropertyInstance, ScalingPolicy

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::Conditional

#condition, #render_condition

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(name, parent) ⇒ Resource

Returns a new instance of Resource



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/convection/model/template/resource.rb', line 295

def initialize(name, parent)
  @name = name
  @parent = parent
  @template = parent.template
  @type = self.class.type
  @depends_on = []
  @deletion_policy = nil
  @exist = false

  @resource_attributes = []

  ## Instantiate properties
  @properties = Model::Collection.new
  resource = self
  resource.class.properties.each do |_, property|
    @properties[property.property_name] = property.instance(resource)
  end
end

Instance Attribute Details

#existObject (readonly) Also known as: exist?

Returns the value of attribute exist



292
293
294
# File 'lib/convection/model/template/resource.rb', line 292

def exist
  @exist
end

#nameObject (readonly)

Returns the value of attribute name



287
288
289
# File 'lib/convection/model/template/resource.rb', line 287

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent



288
289
290
# File 'lib/convection/model/template/resource.rb', line 288

def parent
  @parent
end

#propertiesObject (readonly)

Returns the value of attribute properties



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

def properties
  @properties
end

#resource_attributesObject (readonly)

Returns the value of attribute resource_attributes



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

def resource_attributes
  @resource_attributes
end

#templateObject (readonly)

Returns the value of attribute template



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

def template
  @template
end

Class Method Details

.attach_method(name, &block) ⇒ Object



40
41
42
# File 'lib/convection/model/template/resource.rb', line 40

def attach_method(name, &block)
  define_method(name, &block)
end

.propertiesObject



17
18
19
# File 'lib/convection/model/template/resource.rb', line 17

def properties
  @properties ||= {}
end

.property(accesor, property_name, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/convection/model/template/resource.rb', line 30

def property(accesor, property_name, options = {})
  ## Handle usage of old property interface
  options = {}.tap do |o|
    o[:type] = options
  end if options.is_a?(Symbol)

  properties[accesor] = Property.create(accesor, property_name, options)
  properties[accesor].attach(self)
end

.type(cf_type = nil, dsl_name = nil) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/convection/model/template/resource.rb', line 21

def type(cf_type = nil, dsl_name = nil)
  return @type if cf_type.nil?

  @type = cf_type
  @name = dsl_name || DSL::Helpers.method_name(cf_type)

  DSL::Template::Resource.attach_resource(@name, self)
end

Instance Method Details

#as_attribute(attr_name, attr_type = :string) ⇒ Object



349
350
351
352
353
354
# File 'lib/convection/model/template/resource.rb', line 349

def as_attribute(attr_name, attr_type = :string)
  @template.attribute_mappings[name] = {
    :name => attr_name,
    :type => attr_type
  }
end

#deletion_policy(deletion_policy) ⇒ Object

rubocop:disable Style/TrivialAccessors

We don't want to use an accessor (e.g. deletion_policy=) because


329
330
331
# File 'lib/convection/model/template/resource.rb', line 329

def deletion_policy(deletion_policy)
  @deletion_policy = deletion_policy
end

#depends_on(resource) ⇒ Object



322
323
324
# File 'lib/convection/model/template/resource.rb', line 322

def depends_on(resource)
  @depends_on << (resource.is_a?(Resource) ? resource.name : resource)
end

#property(key, *value) ⇒ Object



314
315
316
317
318
319
320
# File 'lib/convection/model/template/resource.rb', line 314

def property(key, *value)
  return properties[key].value if value.empty?

  ## Define a property instance on the fly
  properties[key] = ScalarPropertyInstance.new(self) unless properties.include?(key)
  properties[key].set(*value)
end

#referenceObject

rubocop:enable Style/TrivialAccessors



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

def reference
  {
    'Ref' => name
  }
end

#renderObject



356
357
358
359
360
361
362
363
364
365
366
# File 'lib/convection/model/template/resource.rb', line 356

def render
  {
    'Type' => type,
    'Properties' => properties.map(true, &:render)
  }.tap do |resource|
    resource_attributes.map { |a| a.render resource }
    resource['DependsOn'] = @depends_on unless @depends_on.empty?
    resource['DeletionPolicy'] = @deletion_policy unless @deletion_policy.nil?
    render_condition(resource)
  end
end

#typeObject

Resource Instance Methods



286
# File 'lib/convection/model/template/resource.rb', line 286

attribute :type

#with_output(output_name = name, value = reference, &block) ⇒ Object



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

def with_output(output_name = name, value = reference, &block)
  o = Model::Template::Output.new(output_name, @template)
  o.value = value
  o.description = "Resource #{ type }/#{ name }"

  o.instance_exec(&block) if block
  @template.outputs[output_name] = o
end