Skip to content
This repository was archived by the owner on Jan 20, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
=== 0.9.18 2012-01-26
* Changed top-level namespace from AWS to AWSAPI, as it was conflicting with the official Amazon aws-sdk gem, which also defines AWS::EC2 but as a class rather than a module.

=== 0.9.17 2010-11-21
* Converted from Jeweler to Bundler, 'bundle install' to install dependencies
Expand Down
24 changes: 12 additions & 12 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ To install from git for adding features or fixing bugs, you'll need to clone and

== Using amazon-ec2

The library exposes one main interface class AWS::EC2::Base. It is through an instance of this class that you will perform all the operations for using the EC2 service including query string header signing.
The library exposes one main interface class AWSAPI::EC2::Base. It is through an instance of this class that you will perform all the operations for using the EC2 service including query string header signing.

The public methods on AWS::EC2::Base closely mirror the EC2 Query API, and as such the Query API Reference in the EC2 Developer Guide ( http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=84 ) will prove helpful.
The public methods on AWSAPI::EC2::Base closely mirror the EC2 Query API, and as such the Query API Reference in the EC2 Developer Guide ( http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=84 ) will prove helpful.


=== Setting up
Expand Down Expand Up @@ -82,11 +82,11 @@ You can verify that this setup is complete by running the 'set' in a command win

The library exposes one main interface module

AWS::EC2::Base
AWSAPI::EC2::Base

This method requires arguments which include your AWS credentials and it will return an object that you can use to make method calls directly against EC2. All the operations for using the EC2 service, including query string header signing, are handled automatically for you. The connection string will look something like this:

@ec2 = AWS::EC2::Base.new(:access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY)
@ec2 = AWSAPI::EC2::Base.new(:access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY)

I've tried to keep the public methods on 'amazon-ec2' as close as possible to the AWS EC2 Query API. This similarity allows you to reference the Query API Reference in the EC2 Developer Guide and be able to get started right away. In most cases the methods names only differ in how they are presented. e.g. 'DescribeImages' becomes '#describe_images()' in Ruby. Feel free to browse the full RDoc documentation for all classes and methods of 'amazon-ec2' if you want more details.

Expand Down Expand Up @@ -131,28 +131,28 @@ If you're not in front of a terminal shell now (perhaps you're browsing this sit
returns : a string representation of ALL images
>> @ec2.describe_images.to_s

returns : an Array of AWS::Response objects, each an EC2 image and its data
returns : an Array of AWSAPI::Response objects, each an EC2 image and its data
>> @ec2.describe_images.imagesSet.item
>> @ec2.describe_images.imagesSet.item[0] (a hash representing a single item in that array)
>> @ec2.describe_images.imagesSet.item[0].to_s (a String representation of that item)

>> @ec2.describe_images.imagesSet.item[0].to_s
=> "#<AWS::Response:0x100A465B4 imageId=\"ami-018e6b68\" imageLocation=\"rbuilder-online/phonehome-1.5.6-x86_10132.img.manifest.xml\" imageOwnerId=\"099034111737\" imageState=\"available\" isPublic=\"true\" parent=#<AWS::Response:0x100A469A6 ...>>"
=> "#<AWSAPI::Response:0x100A465B4 imageId=\"ami-018e6b68\" imageLocation=\"rbuilder-online/phonehome-1.5.6-x86_10132.img.manifest.xml\" imageOwnerId=\"099034111737\" imageState=\"available\" isPublic=\"true\" parent=#<AWSAPI::Response:0x100A469A6 ...>>"


=== Ruby script usage example:

Try out the following bit of code. This should walk through each image returned by a call to #describe_images and print out its key data. Note in the example below that you cannot walk through the results of the #describe_images call with the '.each' iterator (You'll get errors if you try). You need to instead walk through the Array of items which are in the 'imagesSet' embedded in the response. This reflects exactly the XML hierarchy of data returned from EC2 which we parse to Ruby OpenStruct objects (AWS::Response).
Try out the following bit of code. This should walk through each image returned by a call to #describe_images and print out its key data. Note in the example below that you cannot walk through the results of the #describe_images call with the '.each' iterator (You'll get errors if you try). You need to instead walk through the Array of items which are in the 'imagesSet' embedded in the response. This reflects exactly the XML hierarchy of data returned from EC2 which we parse to Ruby OpenStruct objects (AWSAPI::Response).

#!/usr/bin/env ruby

require 'rubygems'
require 'AWS'
require 'AWSAPI'

ACCESS_KEY_ID = '--YOUR AWS ACCESS KEY ID--'
SECRET_ACCESS_KEY = '--YOUR AWS SECRET ACCESS KEY--'

ec2 = AWS::EC2::Base.new(:access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY)
ec2 = AWSAPI::EC2::Base.new(:access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY)

puts "----- listing images owned by 'amazon' -----"
ec2.describe_images(:owner_id => "amazon").imagesSet.item.each do |image|
Expand Down Expand Up @@ -184,7 +184,7 @@ Try out the following bit of code. This should walk through each image returned

[some controller code ...]

ec2 = AWS::EC2::Base.new(:access_key_id => "YOUR_AWS_ACCESS_KEY_ID", :secret_access_key => "YOUR_AWS_SECRET_ACCESS_KEY")
ec2 = AWSAPI::EC2::Base.new(:access_key_id => "YOUR_AWS_ACCESS_KEY_ID", :secret_access_key => "YOUR_AWS_SECRET_ACCESS_KEY")

# get ALL public images
@ec2_images = ec2.describe_images().imagesSet.item
Expand Down Expand Up @@ -237,7 +237,7 @@ Try out the following bit of code. This should walk through each image returned
<% end %>


=== Important notes regarding the structure of AWS::Response Objects
=== Important notes regarding the structure of AWSAPI::Response Objects

One of the key benefits of this new version of the library is that all responses from EC2 are bundled up in a real data structure and no longer require parsing of text. The hash returned is populated directly from the XML given to us by EC2 in response to any command we issue. This means that future changes to the API and what is returned by EC2 will be handled transparently by the gem. This is a huge benefit. What this means though, is that you may have to do a little homework on what actually gets returned by EC2 as XML. For example, when you make a #describe_images call in 'awshell' what AWS returns behind the scenes looks like:

Expand Down Expand Up @@ -296,7 +296,7 @@ So, for example, if you wanted to get the image ID of the third image listed in
>> puts @ec2.describe_images(:owner_id => 'amazon').imagesSet.item[2].imageId
ami-23b6534a

EC2 will typically return sets of things (imagesSet, reservationSet, etc.) which we map to ruby Arrays (.imagesSet.item in the example above). If you want to iterate over a response set you will need to iterate over this array. The Arrays will typically contain additional AWS::Response objects that represent each individual item. You'll find that you can use the 'awshell' to help you understand the structure more completely if you try issuing commands there as a way to practice seeing what will be returned and making sure you get exactly what you want.
EC2 will typically return sets of things (imagesSet, reservationSet, etc.) which we map to ruby Arrays (.imagesSet.item in the example above). If you want to iterate over a response set you will need to iterate over this array. The Arrays will typically contain additional AWSAPI::Response objects that represent each individual item. You'll find that you can use the 'awshell' to help you understand the structure more completely if you try issuing commands there as a way to practice seeing what will be returned and making sure you get exactly what you want.

=== Handling Exceptions
If for some reason an error occurs when executing a method (e.g. its arguments were
Expand Down
4 changes: 2 additions & 2 deletions amazon-ec2.gemspec
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "AWS/version"
require "AWSAPI/version"

Gem::Specification.new do |s|
s.name = "amazon-ec2"
s.version = AWS::VERSION
s.version = AWSAPI::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Glenn Rempe"]
s.email = ["glenn@rempe.us"]
Expand Down
4 changes: 2 additions & 2 deletions bin/awshell
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require 'irb'
# CREDITS : Credit for this bit of shameful ripoff coolness
# goes to Marcel Molina and his AWS::S3 gem. Thanks!

require File.dirname(__FILE__) + '/../lib/AWS'
require File.dirname(__FILE__) + '/../lib/AWSAPI'

setup = File.dirname(__FILE__) + '/setup'

Expand Down Expand Up @@ -69,7 +69,7 @@ def welcome!
returns : Pretty Print a Hash describing your EC2 images
>> @ec2.describe_images(:owner_id => ['self'])

returns : an Array of AWS::Response objects, each an EC2 image and its data
returns : an Array of AWSAPI::Response objects, each an EC2 image and its data
>> @ec2.describe_images(:owner_id => ['self']).imagesSet.item
>> @ec2.describe_images(:owner_id => ['self']).imagesSet.item[0]
MESSAGE
Expand Down
12 changes: 6 additions & 6 deletions bin/ec2-gem-example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
# test different servers by running something like:
# export EC2_URL='https://ec2.amazonaws.com';./bin/ec2-gem-example.rb
if ENV['EC2_URL']
ec2 = AWS::EC2::Base.new( :access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY, :server => URI.parse(ENV['EC2_URL']).host )
ec2 = AWSAPI::EC2::Base.new( :access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY, :server => URI.parse(ENV['EC2_URL']).host )
else
# default server is US ec2.amazonaws.com
ec2 = AWS::EC2::Base.new( :access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY )
ec2 = AWSAPI::EC2::Base.new( :access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY )
end

puts "----- ec2.methods.sort -----"
Expand Down Expand Up @@ -68,9 +68,9 @@
# ELB examples
# Autoscaling examples
if ENV['ELB_URL']
elb = AWS::ELB::Base.new( :access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY, :server => URI.parse(ENV['ELB_URL']).host )
elb = AWSAPI::ELB::Base.new( :access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY, :server => URI.parse(ENV['ELB_URL']).host )
else
elb = AWS::ELB::Base.new( :access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY)
elb = AWSAPI::ELB::Base.new( :access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY)
end

puts "----- creating an elastic load balancer -----"
Expand All @@ -88,9 +88,9 @@

# Autoscaling examples
if ENV['AS_URL']
as = AWS::Autoscaling::Base.new( :access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY, :server => URI.parse(ENV['AS_URL']).host )
as = AWSAPI::Autoscaling::Base.new( :access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY, :server => URI.parse(ENV['AS_URL']).host )
else
as = AWS::Autoscaling::Base.new( :access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY)
as = AWSAPI::Autoscaling::Base.new( :access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY)
end

puts "---- creating a launch configuration group -----"
Expand Down
2 changes: 1 addition & 1 deletion bin/ec2-gem-profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
ACCESS_KEY_ID = ENV['AWS_ACCESS_KEY_ID'] || ENV['AMAZON_ACCESS_KEY_ID']
SECRET_ACCESS_KEY = ENV['AWS_SECRET_ACCESS_KEY'] || ENV['AMAZON_SECRET_ACCESS_KEY']

ec2 = AWS::EC2::Base.new( :access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY )
ec2 = AWSAPI::EC2::Base.new( :access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY )

@images = ec2.describe_images
26 changes: 13 additions & 13 deletions bin/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@



if(AWS::ACCESS_KEY_ID and AWS::SECRET_ACCESS_KEY)
if(AWSAPI::ACCESS_KEY_ID and AWSAPI::SECRET_ACCESS_KEY)

opts = {
:access_key_id => AWS::ACCESS_KEY_ID,
:secret_access_key => AWS::SECRET_ACCESS_KEY
:access_key_id => AWSAPI::ACCESS_KEY_ID,
:secret_access_key => AWSAPI::SECRET_ACCESS_KEY
}

if ENV['EC2_URL']
Expand All @@ -30,37 +30,37 @@
opts[:use_ssl] = false
end
end
@ec2 = AWS::EC2::Base.new(opts)
@ec2 = AWSAPI::EC2::Base.new(opts)
else
@ec2 = AWS::EC2::Base.new(opts)
@ec2 = AWSAPI::EC2::Base.new(opts)
end

if ENV['ELB_URL']
opts[:server] = URI.parse(ENV['ELB_URL']).host
@elb = AWS::ELB::Base.new(opts)
@elb = AWSAPI::ELB::Base.new(opts)
else
@elb = AWS::ELB::Base.new(opts)
@elb = AWSAPI::ELB::Base.new(opts)
end

if ENV['AS_URL']
opts[:server] = URI.parse(ENV['AS_URL']).host
@as = AWS::Autoscaling::Base.new(opts)
@as = AWSAPI::Autoscaling::Base.new(opts)
else
@as = AWS::Autoscaling::Base.new(opts)
@as = AWSAPI::Autoscaling::Base.new(opts)
end

if ENV['RDS_URL']
opts[:server] = URI.parse(ENV['RDS_URL']).host
@rds = AWS::RDS::Base.new(opts)
@rds = AWSAPI::RDS::Base.new(opts)
else
@rds = AWS::RDS::Base.new(opts)
@rds = AWSAPI::RDS::Base.new(opts)
end

if ENV['AWS_CLOUDWATCH_URL']
opts[:server] = URI.parse(ENV['AWS_CLOUDWATCH_URL']).host
@cw = AWS::Cloudwatch::Base.new(opts)
@cw = AWSAPI::Cloudwatch::Base.new(opts)
else
@cw = AWS::Cloudwatch::Base.new(opts)
@cw = AWSAPI::Cloudwatch::Base.new(opts)
end

puts ""
Expand Down
3 changes: 0 additions & 3 deletions lib/AWS/version.rb

This file was deleted.

24 changes: 12 additions & 12 deletions lib/AWS.rb → lib/AWSAPI.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def does_not_have?(key)
end


module AWS
module AWSAPI
ACCESS_KEY_ID = ENV['AWS_ACCESS_KEY_ID'] || ENV['AMAZON_ACCESS_KEY_ID'] || ""
SECRET_ACCESS_KEY = ENV['AWS_SECRET_ACCESS_KEY'] || ENV['AMAZON_SECRET_ACCESS_KEY'] || ""

Expand All @@ -62,7 +62,7 @@ module AWS
# @param [String] method the HTTP method that will be used to submit the params.
# @param [String] base the URI path that this information will be submitted to.
# @return [String] the canonical request description string.
def AWS.canonical_string(params, host, method="POST", base="/")
def AWSAPI.canonical_string(params, host, method="POST", base="/")
# Sort, and encode parameters into a canonical string.
sorted_params = params.sort {|x,y| x[0] <=> y[0]}
encoded_params = sorted_params.collect do |p|
Expand Down Expand Up @@ -94,7 +94,7 @@ def AWS.canonical_string(params, host, method="POST", base="/")
# @param [String] str the string to be hashed and encoded.
# @param [Boolean] urlencode whether or not to url encode the result., true or false
# @return [String] the signed and encoded string.
def AWS.encode(secret_access_key, str, urlencode=true)
def AWSAPI.encode(secret_access_key, str, urlencode=true)
digest = OpenSSL::Digest::Digest.new('sha256')
b64_hmac =
Base64.encode64(
Expand All @@ -111,7 +111,7 @@ def AWS.encode(secret_access_key, str, urlencode=true)
# including the handling of header signing and other security concerns.
# This class uses the Net::HTTP library to interface with the AWS Query API
# interface. You should not instantiate this directly, instead
# you should setup an instance of 'AWS::EC2::Base' or 'AWS::ELB::Base'.
# you should setup an instance of 'AWSAPI::EC2::Base' or 'AWSAPI::ELB::Base'.
class Base
attr_reader :use_ssl, :server, :proxy_server, :port

Expand Down Expand Up @@ -307,12 +307,12 @@ def make_request(action, params, data='')

# Set the Authorization header using AWS signed header authentication
def get_aws_auth_param(params, secret_access_key, server)
canonical_string = AWS.canonical_string(params, server,"POST", @path)
encoded_canonical = AWS.encode(secret_access_key, canonical_string)
canonical_string = AWSAPI.canonical_string(params, server,"POST", @path)
encoded_canonical = AWSAPI.encode(secret_access_key, canonical_string)
end

# allow us to have a one line call in each method which will do all of the work
# in making the actual request to AWS.
# in making the actual request to AWSAPI.
def response_generator( options = {} )

options = {
Expand All @@ -339,7 +339,7 @@ def aws_error?(response)
# exceptions.rb
return false if response.is_a?(Net::HTTPSuccess)

raise AWS::Error, "Unexpected server error. response.body is: #{response.body}" if response.is_a?(Net::HTTPServerError)
raise AWSAPI::Error, "Unexpected server error. response.body is: #{response.body}" if response.is_a?(Net::HTTPServerError)

# parse the XML document so we can walk through it
doc = REXML::Document.new(response.body)
Expand All @@ -360,16 +360,16 @@ def aws_error?(response)

# Raise one of our specific error classes if it exists.
# otherwise, throw a generic EC2 Error with a few details.
if AWS.const_defined?(error_code)
raise AWS.const_get(error_code), error_message
if AWSAPI.const_defined?(error_code)
raise AWSAPI.const_get(error_code), error_message
else
raise AWS::Error, error_message
raise AWSAPI::Error, error_message
end

end

end
end

Dir[File.join(File.dirname(__FILE__), 'AWS/**/*.rb')].sort.each { |lib| require lib }
Dir[File.join(File.dirname(__FILE__), 'AWSAPI/**/*.rb')].sort.each { |lib| require lib }

10 changes: 5 additions & 5 deletions lib/AWS/Autoscaling.rb → lib/AWSAPI/Autoscaling.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module AWS
module AWSAPI
module Autoscaling
# Which host FQDN will we connect to for all API calls to AWS?
# If AS_URL is defined in the users ENV we can override the default with that.
Expand All @@ -15,7 +15,7 @@ module Autoscaling

API_VERSION = '2009-05-15'

class Base < AWS::Base
class Base < AWSAPI::Base
def api_version
API_VERSION
end
Expand Down Expand Up @@ -54,10 +54,10 @@ def aws_error?(response)

# Raise one of our specific error classes if it exists.
# otherwise, throw a generic EC2 Error with a few details.
if AWS.const_defined?(error_code)
raise AWS.const_get(error_code), error_message
if AWSAPI.const_defined?(error_code)
raise AWSAPI.const_get(error_code), error_message
else
raise AWS::Error, error_message
raise AWSAPI::Error, error_message
end

end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module AWS
module AWSAPI
module Autoscaling
class Base < AWS::Base
class Base < AWSAPI::Base

# Create a launch configuration
# Creates a new Launch Configuration. Please note that the launch configuration name used must be unique, within the scope of your AWS account, and the maximum limit of launch configurations must not yet have been met, or else the call will fail.
Expand Down
4 changes: 2 additions & 2 deletions lib/AWS/Cloudwatch.rb → lib/AWSAPI/Cloudwatch.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module AWS
module AWSAPI
module Cloudwatch

# Which host FQDN will we connect to for all API calls to AWS?
Expand All @@ -16,7 +16,7 @@ module Cloudwatch

API_VERSION = '2009-05-15'

class Base < AWS::Base
class Base < AWSAPI::Base
def api_version
API_VERSION
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module AWS
module AWSAPI
module Cloudwatch
class Base < AWS::Base
class Base < AWSAPI::Base

# This method call lists available Cloudwatch metrics attached to your EC2
# account. To get further information from the metrics, you'll then need to
Expand Down
4 changes: 2 additions & 2 deletions lib/AWS/EC2.rb → lib/AWSAPI/EC2.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module AWS
module AWSAPI
module EC2

# Which host FQDN will we connect to for all API calls to AWS?
Expand All @@ -16,7 +16,7 @@ module EC2

API_VERSION = '2010-08-31'

class Base < AWS::Base
class Base < AWSAPI::Base
def api_version
API_VERSION
end
Expand Down
Loading