Skip to content

Instantly share code, notes, and snippets.

@pas256
Created January 23, 2013 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pas256/4615819 to your computer and use it in GitHub Desktop.
Save pas256/4615819 to your computer and use it in GitHub Desktop.
Module to help make dealing with AWS Ruby SDK endpoints and clients simpler
module AwsHelpers::Api
# Gets the AWS account this is running in
def self.get_aws_account
AWS_ACCOUNT # Configured in initializer
end
# Gets the credentials for the environment this is running in, or the
# specified account
def self.get_aws_credentials(account = get_aws_account)
AWS_CREDENTIALS[account]
end
# Generic method to piece together an API endpoint
# This is a code representation of:
# http://docs.aws.amazon.com/general/latest/gr/rande.html
def self.get_endpoint(service, region = nil)
format_generic = '%s.amazonaws.com'
format_region = '%s.%s.amazonaws.com'
case
when service == AWS::AutoScaling
name = 'autoscaling'
when service == AWS::CloudFormation
name = 'cloudformation'
when service == AWS::CloudFront
name = 'cloudfront'
region = nil
when service == AWS::CloudSearch
name = 'cloudsearch'
region = 'us-east-1'
when service == AWS::CloudWatch
name = 'monitoring'
when service == AWS::DynamoDB
name = 'dynamodb'
when service == AWS::EC2
name = 'ec2'
when service == AWS::ELB
name = 'elasticloadbalancing'
when service == AWS::EMR
name = 'elasticmapreduce'
when service == AWS::ElastiCache
name = 'elasticache'
when service == AWS::ElasticBeanstalk
name = 'elasticbeanstalk'
when service == AWS::Glacier
name = 'glacier'
when service == AWS::IAM
name = 'iam'
region = nil
when service == AWS::ImportExport
name = 'importexport'
region = nil
when service == AWS::RDS
name = 'rds'
when service == AWS::Route53
name = 'route53'
region = nil
when service == AWS::S3
name = 's3'
if region == 'us-east-1'
region = nil
else
format_region = '%s-%s.amazonaws.com'
end
when service == AWS::SNS
name = 'sns'
when service == AWS::SQS
name = 'sqs'
when service == AWS::StorageGateway
name = 'storagegateway'
when service == AWS::STS
name = 'sts'
region = nil
when service == AWS::SimpleDB
name = 'sdb'
region = nil if region == 'us-east-1'
when service == AWS::SimpleEmailService
name = 'email'
region = 'us-east-1'
when service == AWS::SimpleWorkflow
name = 'swf'
region = 'us-east-1'
else
name = service
end
# Put it all together
if region
format_region % [name, region]
else
format_generic % name
end
end
# Gets the config symbol to set the endpoint to
def self.get_endpoint_symbol(service)
case
when service == AWS::AutoScaling
:auto_scaling_endpoint
when service == AWS::CloudFormation
:cloud_formation_endpoint
when service == AWS::CloudFront
:cloud_front_endpoint
when service == AWS::CloudSearch
:cloud_search
when service == AWS::CloudWatch
:cloud_watch_endpoint
when service == AWS::DynamoDB
:dynamo_db_endpoint
when service == AWS::EC2
:ec2_endpoint
when service == AWS::ElastiCache
:elasticache_endpoint
when service == AWS::ElasticBeanstalk
:elastic_beanstalk_endpoint
when service == AWS::ELB
:elb_endpoint
when service == AWS::Glacier
:glacier_endpoint
when service == AWS::IAM
:iam_endpoint
when service == AWS::ImportExport
:import_export_endpoint
when service == AWS::RDS
:rds_endpoint
when service == AWS::Route53
:route_53_endpoint
when service == AWS::S3
:s3_endpoint
when service == AWS::SimpleDB
:simple_db_endpoint
when service == AWS::SimpleEmailService
:simple_email_service_endpoint
when service == AWS::SimpleWorkflow
:simple_workflow_service # This is not a typo, it does not end with endpoint
when service == AWS::SNS
:sns_endpoint
when service == AWS::SQS
:sqs_endpoint
when service == AWS::StorageGateway
:storage_gateway_endpoint
when service == AWS::STS
:sts_endpoint
end
end
# Gets a pre-configured client for a given region and account
def self.get_client(service, region = nil, account = get_aws_account)
endpoint = get_endpoint(service, region)
sym = get_endpoint_symbol(service)
credentials = get_aws_credentials(account)
service::Client.new(
:access_key_id => credentials['key'],
:secret_access_key => credentials['secret'],
sym => endpoint)
end
end
require 'spec_helper'
describe AwsHelpers::Api do
describe "#get_aws_account" do
it "gets the account this code is executing in" do
account = AwsHelpers::Api::get_aws_account
account.should eq(AWS_ACCOUNT)
end
end
describe "#get_aws_credentials" do
it "gets the credentials for the account this code is executing in" do
cred = AwsHelpers::Api::get_aws_credentials
cred.should eq(AWS_CREDENTIALS[AWS_ACCOUNT])
end
it "gets the credentials for the rnd account" do
cred = AwsHelpers::Api::get_aws_credentials('rnd')
cred.should eq(AWS_CREDENTIALS['rnd'])
end
it "gets the credentials for the qa account" do
cred = AwsHelpers::Api::get_aws_credentials('qa')
cred.should eq(AWS_CREDENTIALS['qa'])
end
end
describe "#get_endpoint" do
it "gets the AutoScaling API endpoint" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::AutoScaling, 'us-east-1')
endpoint.should eq('autoscaling.us-east-1.amazonaws.com')
end
it "gets the CloudFormation API endpoint" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::CloudFormation, 'us-east-1')
endpoint.should eq('cloudformation.us-east-1.amazonaws.com')
end
it "gets the CloudFront API endpoint" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::CloudFront)
endpoint.should eq('cloudfront.amazonaws.com')
end
it "gets the CloudSearch API endpoint" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::CloudSearch)
endpoint.should eq('cloudsearch.us-east-1.amazonaws.com')
end
it "gets the CloudWatch API endpoint" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::CloudWatch, 'eu-west-1')
endpoint.should eq('monitoring.eu-west-1.amazonaws.com')
end
it "gets the DynamoDB API endpoint" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::DynamoDB, 'ap-southeast-2')
endpoint.should eq('dynamodb.ap-southeast-2.amazonaws.com')
end
it "gets the EC2 API endpoint" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::EC2, 'us-east-1')
endpoint.should eq('ec2.us-east-1.amazonaws.com')
end
it "gets the ELB API endpoint" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::ELB, 'us-west-1')
endpoint.should eq('elasticloadbalancing.us-west-1.amazonaws.com')
end
it "gets the EMR API endpoint" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::EMR, 'us-west-2')
endpoint.should eq('elasticmapreduce.us-west-2.amazonaws.com')
end
it "gets the ElastiCache API endpoint" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::ElastiCache, 'us-west-2')
endpoint.should eq('elasticache.us-west-2.amazonaws.com')
end
it "gets the ElasticBeanstalk API endpoint" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::ElasticBeanstalk, 'us-east-1')
endpoint.should eq('elasticbeanstalk.us-east-1.amazonaws.com')
end
it "gets the Glacier API endpoint" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::Glacier, 'us-east-1')
endpoint.should eq('glacier.us-east-1.amazonaws.com')
end
it "gets the IAM API endpoint" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::IAM)
endpoint.should eq('iam.amazonaws.com')
end
it "gets the Import/Export API endpoint" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::ImportExport)
endpoint.should eq('importexport.amazonaws.com')
end
it "gets the RDS API endpoint" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::RDS, 'sa-east-1')
endpoint.should eq('rds.sa-east-1.amazonaws.com')
end
it "gets the Route53 API endpoint" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::Route53)
endpoint.should eq('route53.amazonaws.com')
end
it "gets the S3 API endpoint for us-east-1" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::S3, 'us-east-1')
endpoint.should eq('s3.amazonaws.com')
end
it "gets the S3 API endpoint for us-west-1" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::S3, 'us-west-1')
endpoint.should eq('s3-us-west-1.amazonaws.com')
end
it "gets the SES API endpoint" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::SimpleEmailService, 'us-west-1')
endpoint.should eq('email.us-east-1.amazonaws.com')
end
it "gets the SNS API endpoint" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::SNS, 'sa-east-1')
endpoint.should eq('sns.sa-east-1.amazonaws.com')
end
it "gets the SQS API endpoint" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::SQS, 'us-east-1')
endpoint.should eq('sqs.us-east-1.amazonaws.com')
end
it "gets the Storage Gateway API endpoint" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::StorageGateway, 'us-west-1')
endpoint.should eq('storagegateway.us-west-1.amazonaws.com')
end
it "gets the STS API endpoint" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::STS)
endpoint.should eq('sts.amazonaws.com')
end
it "gets the SimpleDB API endpoint for us-east-1" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::SimpleDB, 'us-east-1')
endpoint.should eq('sdb.amazonaws.com')
end
it "gets the SimpleDB API endpoint for us-west-1" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::SimpleDB, 'us-west-1')
endpoint.should eq('sdb.us-west-1.amazonaws.com')
end
it "gets the SWF API endpoint for" do
endpoint = AwsHelpers::Api::get_endpoint(AWS::SimpleWorkflow)
endpoint.should eq('swf.us-east-1.amazonaws.com')
end
end
describe "#get_endpoint_symbol" do
it "gets the config endpoint symbols" do
AwsHelpers::Api::get_endpoint_symbol(AWS::AutoScaling).should eq(:auto_scaling_endpoint)
AwsHelpers::Api::get_endpoint_symbol(AWS::CloudFormation).should eq(:cloud_formation_endpoint)
AwsHelpers::Api::get_endpoint_symbol(AWS::CloudFront).should eq(:cloud_front_endpoint)
AwsHelpers::Api::get_endpoint_symbol(AWS::CloudSearch).should eq(:cloud_search)
AwsHelpers::Api::get_endpoint_symbol(AWS::CloudWatch).should eq(:cloud_watch_endpoint)
AwsHelpers::Api::get_endpoint_symbol(AWS::DynamoDB).should eq(:dynamo_db_endpoint)
AwsHelpers::Api::get_endpoint_symbol(AWS::EC2).should eq(:ec2_endpoint)
AwsHelpers::Api::get_endpoint_symbol(AWS::ElastiCache).should eq(:elasticache_endpoint)
AwsHelpers::Api::get_endpoint_symbol(AWS::ElasticBeanstalk).should eq(:elastic_beanstalk_endpoint)
AwsHelpers::Api::get_endpoint_symbol(AWS::ELB).should eq(:elb_endpoint)
AwsHelpers::Api::get_endpoint_symbol(AWS::Glacier).should eq(:glacier_endpoint)
AwsHelpers::Api::get_endpoint_symbol(AWS::IAM).should eq(:iam_endpoint)
AwsHelpers::Api::get_endpoint_symbol(AWS::ImportExport).should eq(:import_export_endpoint)
AwsHelpers::Api::get_endpoint_symbol(AWS::RDS).should eq(:rds_endpoint)
AwsHelpers::Api::get_endpoint_symbol(AWS::Route53).should eq(:route_53_endpoint)
AwsHelpers::Api::get_endpoint_symbol(AWS::S3).should eq(:s3_endpoint)
AwsHelpers::Api::get_endpoint_symbol(AWS::SimpleDB).should eq(:simple_db_endpoint)
AwsHelpers::Api::get_endpoint_symbol(AWS::SimpleEmailService).should eq(:simple_email_service_endpoint)
AwsHelpers::Api::get_endpoint_symbol(AWS::SimpleWorkflow).should eq(:simple_workflow_service) # This is not a typo
AwsHelpers::Api::get_endpoint_symbol(AWS::SNS).should eq(:sns_endpoint)
AwsHelpers::Api::get_endpoint_symbol(AWS::SQS).should eq(:sqs_endpoint)
AwsHelpers::Api::get_endpoint_symbol(AWS::StorageGateway).should eq(:storage_gateway_endpoint)
AwsHelpers::Api::get_endpoint_symbol(AWS::STS).should eq(:sts_endpoint)
end
end
describe "#get_client" do
it "gets a CloudFormation client" do
client = AwsHelpers::Api::get_client(AWS::CloudFormation, 'us-east-1')
client.should be_instance_of AWS::CloudFormation::Client
client.config.cloud_formation_endpoint.should eq('cloudformation.us-east-1.amazonaws.com')
end
it "gets a CloudFront client" do
client = AwsHelpers::Api::get_client(AWS::CloudFront)
client.should be_instance_of AWS::CloudFront::Client
client.config.cloud_front_endpoint.should eq('cloudfront.amazonaws.com')
end
it "gets an EC2 client" do
client = AwsHelpers::Api::get_client(AWS::EC2, 'us-west-1')
client.should be_instance_of AWS::EC2::Client
client.config.ec2_endpoint.should eq('ec2.us-west-1.amazonaws.com')
end
# TODO Test the rest of these, but I was getting tried and wanted to do real work
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment