[aws&rackspace|cdn] more cleanup and start of aws cdn

This commit is contained in:
geemus 2010-11-01 12:31:39 -07:00
parent 868ace8347
commit a6f4e14894
7 changed files with 211 additions and 5 deletions

View File

@ -7,6 +7,7 @@ module Fog
extend Fog::Provider
service_path 'fog/aws'
service 'cdn'
service 'compute'
service 'ec2'
service 'elb'

View File

@ -4,6 +4,8 @@ class AWS < Fog::Bin
def [](service)
@@connections ||= Hash.new do |hash, key|
hash[key] = case key
when :cdn
Fog::AWS::CDN.new
when :compute
Fog::AWS::Compute.new
when :ec2
@ -34,7 +36,7 @@ class AWS < Fog::Bin
end
def services
[:compute, :elb, :iam, :sdb, :storage]
[:cdn, :compute, :elb, :iam, :sdb, :storage]
end
end

94
lib/fog/aws/cdn.rb Normal file
View File

@ -0,0 +1,94 @@
module Fog
module AWS
class CDN < Fog::Service
requires :aws_access_key_id, :aws_secret_access_key
model_path 'fog/aws/models/cdn'
request_path 'fog/aws/requests/cdn'
request 'get_distribution_list'
class Mock
def self.data
@data ||= Hash.new do |hash, region|
hash[region] = Hash.new do |region_hash, key|
region_hash[key] = {
:buckets => {}
}
end
end
end
def self.reset_data(keys=data.keys)
for key in [*keys]
data.delete(key)
end
end
def initialize(options={})
require 'mime/types'
@aws_access_key_id = options[:aws_access_key_id]
@data = self.class.data[options[:region]][@aws_access_key_id]
end
def signature(params)
"foo"
end
end
class Real
# Initialize connection to Cloudfront
#
# ==== Notes
# options parameter must include values for :aws_access_key_id and
# :aws_secret_access_key in order to create a connection
#
# ==== Examples
# cdn = Fog::AWS::CDN.new(
# :aws_access_key_id => your_aws_access_key_id,
# :aws_secret_access_key => your_aws_secret_access_key
# )
#
# ==== Parameters
# * options<~Hash> - config arguments for connection. Defaults to {}.
#
# ==== Returns
# * cdn object with connection to aws.
def initialize(options={})
@aws_access_key_id = options[:aws_access_key_id]
@aws_secret_access_key = options[:aws_secret_access_key]
@hmac = Fog::HMAC.new('sha1', @aws_secret_access_key)
@host = options[:host] || 'cloudfront.amazonaws.com'
@path = options[:path] || '/'
@port = options[:port] || 443
@scheme = options[:scheme] || 'https'
@version = options[:version] || '2010-08-01'
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}#{@path}", options[:persistent] || true)
end
def reload
@connection.reset
end
private
def request(params, &block)
params[:headers] ||= {}
params[:headers]['Date'] = Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S +0000")
params[:headers]['Authorization'] = "AWS #{@aws_access_key_id}:#{signature(params)}"
params[:path] = "/#{@version}/#{params[:path]}"
@connection.request(params, &block)
end
def signature(params)
string_to_sign = params[:headers]['Date']
signed_string = @hmac.sign(string_to_sign)
signature = Base64.encode64(signed_string).chomp!
end
end
end
end
end

View File

@ -0,0 +1,48 @@
module Fog
module Parsers
module AWS
module CDN
class GetDistributionList < Fog::Parsers::Base
def reset
@distribution_summary = { 'CNAME' => [], 'TrustedSigners' => [] }
@response = { 'DistributionSummary' => [] }
end
def end_element(name)
case name
when 'DistributionSummary'
@response['DistributionSummary'] << @distribution_summary
@distribution_summary = { 'CNAME' => [], 'TrustedSigners' => [] }
when 'Comment', 'DomainName', 'ID', 'Origin', 'Status'
@distribution_summary[name] = @value
when 'CNAME'
@distribution_summary[name] << @value
when 'Enabled'
if @value == 'true'
@distribution_summary[name] = true
else
@distribution_summary[name] = false
end
when 'LastModifiedTime'
@distribution_summary[name] = Time.parse(@value)
when 'IsTruncated'
if @value == 'true'
@response[name] = true
else
@response[name] = false
end
when 'Marker', 'NextMarker'
@response[name] = @value
when 'MaxItems'
@response[name] = @value.to_i
end
end
end
end
end
end
end

View File

@ -0,0 +1,59 @@
module Fog
module AWS
class CDN
class Real
require 'fog/aws/parsers/cdn/get_distribution_list'
# List information about distributions in CloudFront
#
# ==== Parameters
# * options<~Hash> - config arguments for list. Defaults to {}.
# * 'Marker'<~String> - limits object keys to only those that appear
# lexicographically after its value.
# * 'MaxItems'<~Integer> - limits number of object keys returned
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'IsTruncated'<~Boolean> - Whether or not the listing is truncated
# * 'Marker'<~String> - Marker specified for query
# * 'MaxItems'<~Integer> - Maximum number of keys specified for query
# * 'NextMarker'<~String> - Marker to specify for next page (id of last result of current page)
# * 'DistributionSummary'<~Array>:
# * 'Id'<~String>: Id of distribution
# * 'LastModifiedTime'<~String>: Timestamp of last modification of distribution
# * 'Status'<~String>: Status of distribution
# * 'DomainName'<~String>: Domain name of distribution
# * 'Origin'<~String>: s3 origin bucket
# * 'CNAME'<~Array>: array of associated cnames
# * 'Comment'<~String>: comment associated with distribution
# * 'Enabled'<~Boolean>: whether or not distribution is enabled
# * 'TrustedSigners'<~Array>: trusted signers
#
# ==== See Also
# http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/ListDistributions.html
def get_distribution_list(options = {})
request({
:expects => 200,
:idempotent => true,
:method => 'GET',
:parser => Fog::Parsers::AWS::CDN::GetDistributionList.new,
:path => "/distribution",
:query => options
})
end
end
class Mock # :nodoc:all
def get_distribution_list(options = {})
Fog::Mock.not_implemented
end
end
end
end
end

View File

@ -4,6 +4,8 @@ class Rackspace < Fog::Bin
def [](service)
@@connections ||= Hash.new do |hash, key|
hash[key] = case key
when :cdn
Fog::Rackspace::CDN.new
when :compute
Fog::Rackspace::Compute.new
when :files
@ -26,7 +28,7 @@ class Rackspace < Fog::Bin
end
def services
[:compute, :storage]
[:cdn, :compute, :storage]
end
end

View File

@ -1,16 +1,17 @@
module Fog
module Rackspace
class Storage < Fog::Service
class CDN < Fog::Service
requires :rackspace_api_key, :rackspace_username
model_path 'fog/rackspace/models/cdn'
request_path 'fog/rackspace/requests/cdn'
request :get_cdn_containers
request :head_cdn_container
request :put_cdn_container
class Mock
include Utils
def self.data
@data ||= Hash.new do |hash, key|
@ -32,7 +33,6 @@ module Fog
end
class Real
include Utils
def initialize(options={})
require 'json'