Class: DrawCloud::EC2Instance

Inherits:
Base
  • Object
show all
Defined in:
lib/draw_cloud/ec2_instance.rb

Direct Known Subclasses

EC2InstanceTemplate

Instance Attribute Summary collapse

Attributes inherited from Base

#as_groups, #as_launch_configurations, #deletion_policy, #depends_on, #ec2_instances, #elastic_ips, #gateways, #iam_access_keys, #iam_policies, #iam_users, #mappings, #network_acls, #network_interfaces, #outputs, #parameters, #parent, #rdses, #resources, #route_tables, #security_groups, #sns_topics, #subnets, #vpcs, #wait_handles

Instance Method Summary collapse

Methods inherited from Base

#[], #accessor, #add_standard_properties, #create_as_group, #create_as_launch_configuration, #create_ec2_instance, #create_ec2_instance_template, #create_elastic_ip, #create_iam_access_key, #create_iam_policy, #create_iam_user, #create_mapping, #create_network_acl, #create_network_interface, #create_output, #create_parameter, #create_rds, #create_route_table, #create_security_group, #create_service, #create_sns_topic, #create_subnet, #create_vpc, #create_wait_handle, #ref

Methods included from Locations

#arn_s3

Methods included from Utilities

#desplice, #fnbase64, #fngetatt, #fnjoin, #hash_to_tag_array, #region, #resource_style, #splice, #stack_name

Constructor Details

#initialize(name, options = {}, &block) ⇒ EC2Instance

Returns a new instance of EC2Instance.



45
46
47
48
49
50
# File 'lib/draw_cloud/ec2_instance.rb', line 45

def initialize(name, options={}, &block)
  @name = name
  @tags = {}
  @template = options.fetch(:template, nil)
  super(options, &block)
end

Instance Attribute Details

#availability_zoneObject

Returns the value of attribute availability_zone.



21
22
23
# File 'lib/draw_cloud/ec2_instance.rb', line 21

def availability_zone
  @availability_zone
end

#disable_api_terminationObject

Returns the value of attribute disable_api_termination.



21
22
23
# File 'lib/draw_cloud/ec2_instance.rb', line 21

def disable_api_termination
  @disable_api_termination
end

#eip_nameObject

Returns the value of attribute eip_name.



21
22
23
# File 'lib/draw_cloud/ec2_instance.rb', line 21

def eip_name
  @eip_name
end

#image_idObject Also known as: ami

Returns the value of attribute image_id.



21
22
23
# File 'lib/draw_cloud/ec2_instance.rb', line 21

def image_id
  @image_id
end

#instance_typeObject Also known as: instance_class

Returns the value of attribute instance_type.



21
22
23
# File 'lib/draw_cloud/ec2_instance.rb', line 21

def instance_type
  @instance_type
end

#key_nameObject

Returns the value of attribute key_name.



21
22
23
# File 'lib/draw_cloud/ec2_instance.rb', line 21

def key_name
  @key_name
end

#metadataObject

Returns the value of attribute metadata.



21
22
23
# File 'lib/draw_cloud/ec2_instance.rb', line 21

def 
  
end

#monitoringObject Also known as: instance_monitoring

Returns the value of attribute monitoring.



21
22
23
# File 'lib/draw_cloud/ec2_instance.rb', line 21

def monitoring
  @monitoring
end

#nameObject

Returns the value of attribute name.



21
22
23
# File 'lib/draw_cloud/ec2_instance.rb', line 21

def name
  @name
end

#placement_group_nameObject

Returns the value of attribute placement_group_name.



21
22
23
# File 'lib/draw_cloud/ec2_instance.rb', line 21

def placement_group_name
  @placement_group_name
end

#private_ip_addressObject

Returns the value of attribute private_ip_address.



21
22
23
# File 'lib/draw_cloud/ec2_instance.rb', line 21

def private_ip_address
  @private_ip_address
end

#source_dest_checkObject

Returns the value of attribute source_dest_check.



21
22
23
# File 'lib/draw_cloud/ec2_instance.rb', line 21

def source_dest_check
  @source_dest_check
end

#subnet_idObject Also known as: subnet

Returns the value of attribute subnet_id.



21
22
23
# File 'lib/draw_cloud/ec2_instance.rb', line 21

def subnet_id
  @subnet_id
end

#tagsObject

Returns the value of attribute tags.



21
22
23
# File 'lib/draw_cloud/ec2_instance.rb', line 21

def tags
  @tags
end

#templateObject

Returns the value of attribute template.



21
22
23
# File 'lib/draw_cloud/ec2_instance.rb', line 21

def template
  @template
end

#user_dataObject

Returns the value of attribute user_data.



21
22
23
# File 'lib/draw_cloud/ec2_instance.rb', line 21

def user_data
  @user_data
end

Instance Method Details

#default_tagsObject



104
105
106
# File 'lib/draw_cloud/ec2_instance.rb', line 104

def default_tags
  {"Name" => resource_style(name)}
end

#ec2_instanceObject



52
53
54
# File 'lib/draw_cloud/ec2_instance.rb', line 52

def ec2_instance
  self
end

#elastic_ip=(eip) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/draw_cloud/ec2_instance.rb', line 56

def elastic_ip=(eip)
  case eip
  when DrawCloud::ElasticIp
    eip.instance_id = self
    self.eip_name = nil
  else
    self.eip_name = eip
  end
end

#elastic_ip_associationObject



66
67
68
# File 'lib/draw_cloud/ec2_instance.rb', line 66

def elastic_ip_association
  DrawCloud::ElasticIp::ElasticIpAssociation.new(eip_name, self, vpc)
end

#fetchmergeprop(name) ⇒ Object



80
81
82
83
84
85
# File 'lib/draw_cloud/ec2_instance.rb', line 80

def fetchmergeprop(name)
  s = {}
  s.deep_merge!(template.fetchmergeprop(name)) if template
  s.deep_merge!(self.send(name))
  s
end

#fetchprop(name) ⇒ Object



87
88
89
90
91
92
# File 'lib/draw_cloud/ec2_instance.rb', line 87

def fetchprop(name)
  s = self.send(name)
  return s unless s.nil?
  return template.fetchprop(name) unless template.nil?
  nil
end

#fetchunionprop(name) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/draw_cloud/ec2_instance.rb', line 94

def fetchunionprop(name)
  if template
    p = template.fetchunionprop(name).clone
    p.concat self.send(name)
    p.uniq
  else
    self.send(name)
  end
end

#load_into_config(config) ⇒ Object



70
71
72
73
74
# File 'lib/draw_cloud/ec2_instance.rb', line 70

def load_into_config(config)
  config.cf_add_resource resource_name, self
  config.cf_add_resource(elastic_ip_association.resource_name, elastic_ip_association) if eip_name
  super(config)
end

#resource_nameObject



76
77
78
# File 'lib/draw_cloud/ec2_instance.rb', line 76

def resource_name
  resource_style(name) + "EC2"
end

#to_hObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/draw_cloud/ec2_instance.rb', line 108

def to_h
  h = {
    "Type" => "AWS::EC2::Instance",
    "Properties" => {
      "ImageId" => DrawCloud.ref(fetchprop :image_id),
      "InstanceType" => DrawCloud.ref(fetchprop :instance_type),
    }
  }
  p = h["Properties"]
  %w(availability_zone disable_api_termination key_name
     monitoring placement_group_name private_ip_address
     source_dest_check subnet_id user_data).each do |prop_str|
    prop = prop_str.intern
    p[resource_style(prop)] = DrawCloud.ref(fetchprop(prop)) unless fetchprop(prop).nil?
  end
  p["Tags"] = hash_to_tag_array(default_tags.merge(fetchmergeprop(:tags)))
  h["DependsOn"] = DrawCloud.resource_name(fetchprop(:depends_on)) unless fetchprop(:depends_on).nil?
  h["Metadata"] = DrawCloud.ref(fetchmergeprop(:metadata)) unless fetchmergeprop(:metadata).empty?

  enis = fetchunionprop(:network_interfaces)
  p["NetworkInterfaces"] = enis.enum_for(:each_with_index).collect do |e, i|
    { "NetworkInterfaceId" => DrawCloud.ref(e),
      "DeviceIndex" => (i+1).to_s }
  end unless enis.empty?

  security_groups = fetchunionprop(:security_groups)
  vpc_security_groups = security_groups.find_all(&:vpc)
  regular_security_groups = security_groups.reject(&:vpc)
  p["SecurityGroups"] = regular_security_groups.collect {|s| DrawCloud.ref(s) } unless regular_security_groups.empty?
  p["SecurityGroupIds"] = vpc_security_groups.collect {|s| DrawCloud.ref(s) } unless vpc_security_groups.empty?
  h
end