class Ec2::Profile

Attributes

api[R]
data[R]

Public Class Methods

new(api: nil, data: nil) click to toggle source
# File lib/ec2/profile.rb, line 14
def initialize(api: nil, data: nil)
  @data = deep_copy(data) || {}
  @api = api
  init_network
end

Public Instance Methods

create(transform: true, &block) click to toggle source
# File lib/ec2/profile.rb, line 20
def create(transform: true, &block)
  instance_eval &block
  transform_name_to_id if transform
end
extends(value) click to toggle source
# File lib/ec2/profile.rb, line 36
def extends(value)
  @data.store "extends", value
end
init_network() click to toggle source
# File lib/ec2/profile.rb, line 25
def init_network
  return if @data['network_interfaces'].is_a? Array
  @data['network_interfaces'] = [
    {
      "DeviceIndex" => 0,
      "AssociatePublicIpAddress" => true,
      "SecurityGroupId" => []
     }
  ]
end
security_group(name, interface: 0) click to toggle source
# File lib/ec2/profile.rb, line 44
def security_group(name, interface: 0)
  @data["network_interfaces"][interface]["SecurityGroupId"] << name
end
security_groups(*names, interface: 0) click to toggle source
# File lib/ec2/profile.rb, line 52
def security_groups(*names, interface: 0)
  names.each { |name| security_group name, interface: interface }
end
size(value) click to toggle source
# File lib/ec2/profile.rb, line 40
def size(value)
  @data.store "size", value
end
subnet(name, interface: 0) click to toggle source
# File lib/ec2/profile.rb, line 48
def subnet(name, interface: 0)
  @data["network_interfaces"][interface]["SubnetId"] = name
end
volume(device:, type: "gp2", size:) click to toggle source
# File lib/ec2/profile.rb, line 56
def volume(device:, type: "gp2", size:)
  @data["volumes"] ||= []
  volumes = @data["volumes"]
  volume = {
    "device" => device,
    "type" => type,
    "size" => size
  }
  existing_volume = volumes.find { |v| v["device"] == device }
  if existing_volume
    existing_volume.merge! volume
  else
    volumes << volume
  end
end

Private Instance Methods

deep_copy(hash) click to toggle source
# File lib/ec2/profile.rb, line 74
def deep_copy(hash)
  return nil if not hash.is_a? Hash
  Marshal.load(Marshal.dump hash)
end
transform_name_to_id() click to toggle source
# File lib/ec2/profile.rb, line 80
def transform_name_to_id
  @data["network_interfaces"].each do |n|
    n["SubnetId"] = api.subnet(n["SubnetId"])
    security_groups = n["SecurityGroupId"]

    security_groups.each_with_index do |name, i|
      logger.debug "resolving security_group #{name}"
      security_groups[i] = api.security_group(name)
    end
  end
end