class MistAws::Ec2

Attributes

credentials[R]
ec2[R]
ec2_client[R]
iam[R]
logger[R]
profile_name[R]

These are read-only accessor and are initializeds by initialize method

region[R]

Public Class Methods

new(opts={}) click to toggle source
# File lib/mist_aws/ec2.rb, line 16
def initialize(opts={})
  # Ruby 1.9 backwards compatability
  opts = {profile_name: nil, region: nil, logger: ::Logger.new(STDERR)}.merge(opts)
  opts.each do |key, value|
    instance_variable_set "@#{key}", value
  end

  @iam = Iam.new(opts)
  @ec2_client = Aws::EC2::Client.new(credentials: @iam.credentials, region: @iam.region)
  @ec2 = Aws::EC2::Resource.new(client: @ec2_client)

end

Public Instance Methods

authorize_security_group_ingress(opts={}) click to toggle source

Note the keys for opts must be strings not symbols

# File lib/mist_aws/ec2.rb, line 60
def authorize_security_group_ingress(opts={})
  begin
    ec2_client.authorize_security_group_ingress(opts)
  rescue Aws::EC2::Errors::InvalidPermissionDuplicate
  end
end
create_security_group(group_name, vpc_id, description=group_name) click to toggle source
# File lib/mist_aws/ec2.rb, line 50
def create_security_group(group_name, vpc_id, description=group_name)
  begin
    ec2_client.create_security_group(group_name: group_name, vpc_id: vpc_id, description: description)
  rescue Aws::EC2::Errors::InvalidGroupDuplicate
  end
  # Seem to need to fetch it to get a valid security group struct
  get_security_group(group_name, vpc_id)
end
create_vpc(cdr_block) click to toggle source
# File lib/mist_aws/ec2.rb, line 67
def create_vpc(cdr_block)
  result = ec2_client.describe_vpcs(filters: [{ name: "cidr", values: [cdr_block]}])
  if result.vpcs == []
    ec2_client.create_vpc(cidr_block: cdr_block).vpc
  else
    result.vpcs.first
  end
end
delete_security_group(group_name, vpc_id) click to toggle source
# File lib/mist_aws/ec2.rb, line 45
def delete_security_group(group_name, vpc_id)
  group_id = get_security_group_id(group_name, vpc_id)
  ec2_client.delete_security_group(group_id: group_id) if group_id
end
delete_vpc(vpc_id) click to toggle source
# File lib/mist_aws/ec2.rb, line 76
def delete_vpc(vpc_id)
  begin
    ec2_client.delete_vpc(vpc_id: vpc_id)
  rescue Aws::EC2::Errors::InvalidVpcIDNotFound
    nil
  end
end
get_security_group(group_name, vpc_id) click to toggle source
# File lib/mist_aws/ec2.rb, line 29
def get_security_group(group_name, vpc_id)
  ec2_client.describe_security_groups(filters: [{ name: "vpc-id", values: [vpc_id]}]).security_groups.detect { |g| g.group_name == group_name }
end
get_security_group_id(group_name, vpc_id) click to toggle source
# File lib/mist_aws/ec2.rb, line 33
def get_security_group_id(group_name, vpc_id)
  if (group = get_security_group(group_name, vpc_id))
    group.group_id
  else
    nil
  end
end
security_group_exists?(group_name, vpc_id) click to toggle source
# File lib/mist_aws/ec2.rb, line 41
def security_group_exists?(group_name, vpc_id)
  get_security_group(group_name, vpc_id)
end