class DrawCloud::EC2Instance

Attributes

ami[RW]
ami=[RW]
availability_zone[RW]
disable_api_termination[RW]
eip_name[RW]
image_id[RW]
instance_class[RW]
instance_class=[RW]
instance_monitoring[RW]
instance_monitoring=[RW]
instance_type[RW]
key_name[RW]
metadata[RW]
monitoring[RW]
name[RW]
placement_group_name[RW]
private_ip_address[RW]
source_dest_check[RW]
subnet[RW]
subnet=[RW]
subnet_id[RW]
tags[RW]
template[RW]
user_data[RW]

Public Class Methods

new(name, options={}, &block) click to toggle source
Calls superclass method DrawCloud::Base::new
# 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

Public Instance Methods

default_tags() click to toggle source
# File lib/draw_cloud/ec2_instance.rb, line 104
def default_tags
  {"Name" => resource_style(name)}
end
ec2_instance() click to toggle source
# File lib/draw_cloud/ec2_instance.rb, line 52
def ec2_instance
  self
end
elastic_ip=(eip) click to toggle source
# 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_association() click to toggle source
# File lib/draw_cloud/ec2_instance.rb, line 66
def elastic_ip_association
  DrawCloud::ElasticIp::ElasticIpAssociation.new(eip_name, self, vpc)
end
fetchmergeprop(name) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
Calls superclass method DrawCloud::Base#load_into_config
# 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_name() click to toggle source
# File lib/draw_cloud/ec2_instance.rb, line 76
def resource_name
  resource_style(name) + "EC2"
end
to_h() click to toggle source
# 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