Class | RightAws::AcfInterface |
In: |
lib/acf/right_acf_interface.rb
|
Parent: | RightAwsBase |
The AcfInterface class provides a complete interface to Amazon‘s CloudFront service.
For explanations of the semantics of each call, please refer to Amazon‘s documentation at developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=211
Example:
acf = RightAws::AcfInterface.new('1E3GDYEOGFJPIT7XXXXXX','hgTHt68JY07JKUY08ftHYtERkjgtfERn57XXXXXX') list = acf.list_distributions #=> [{:status => "Deployed", :domain_name => "d74zzrxmpmygb.6hops.net", :aws_id => "E4U91HCJHGXVC", :origin => "my-bucket.s3.amazonaws.com", :cnames => ["x1.my-awesome-site.net", "x1.my-awesome-site.net"] :comment => "My comments", :last_modified_time => Wed Sep 10 17:00:04 UTC 2008 }, ..., {...} ] distibution = list.first info = acf.get_distribution(distibution[:aws_id]) #=> {:enabled => true, :caller_reference => "200809102100536497863003", :e_tag => "E39OHHU1ON65SI", :status => "Deployed", :domain_name => "d3dxv71tbbt6cd.6hops.net", :cnames => ["web1.my-awesome-site.net", "web2.my-awesome-site.net"] :aws_id => "E2REJM3VUN5RSI", :comment => "Woo-Hoo!", :origin => "my-bucket.s3.amazonaws.com", :last_modified_time => Wed Sep 10 17:00:54 UTC 2008 } config = acf.get_distribution_config(distibution[:aws_id]) #=> {:enabled => true, :caller_reference => "200809102100536497863003", :e_tag => "E39OHHU1ON65SI", :cnames => ["web1.my-awesome-site.net", "web2.my-awesome-site.net"] :comment => "Woo-Hoo!", :origin => "my-bucket.s3.amazonaws.com"} config[:comment] = 'Olah-lah!' config[:enabled] = false config[:cnames] << "web3.my-awesome-site.net" acf.set_distribution_config(distibution[:aws_id], config) #=> true
API_VERSION | = | "2008-06-30" |
DEFAULT_HOST | = | 'cloudfront.amazonaws.com' |
DEFAULT_PORT | = | 443 |
DEFAULT_PROTOCOL | = | 'https' |
DEFAULT_PATH | = | '/' |
# File lib/acf/right_acf_interface.rb, line 89 89: def self.bench_service 90: @@bench.service 91: end
Create a new handle to a CloudFront account. All handles share the same per process or per thread HTTP connection to CloudFront. Each handle is for a specific account. The params have the following options:
acf = RightAws::AcfInterface.new('1E3GDYEOGFJPIT7XXXXXX','hgTHt68JY07JKUY08ftHYtERkjgtfERn57XXXXXX', {:multi_thread => true, :logger => Logger.new('/tmp/x.log')}) #=> #<RightAws::AcfInterface::0xb7b3c30c>
# File lib/acf/right_acf_interface.rb, line 106 106: def initialize(aws_access_key_id=nil, aws_secret_access_key=nil, params={}) 107: init({ :name => 'ACF', 108: :default_host => ENV['ACF_URL'] ? URI.parse(ENV['ACF_URL']).host : DEFAULT_HOST, 109: :default_port => ENV['ACF_URL'] ? URI.parse(ENV['ACF_URL']).port : DEFAULT_PORT, 110: :default_service => ENV['ACF_URL'] ? URI.parse(ENV['ACF_URL']).path : DEFAULT_PATH, 111: :default_protocol => ENV['ACF_URL'] ? URI.parse(ENV['ACF_URL']).scheme : DEFAULT_PROTOCOL }, 112: aws_access_key_id || ENV['AWS_ACCESS_KEY_ID'], 113: aws_secret_access_key || ENV['AWS_SECRET_ACCESS_KEY'], 114: params) 115: end
Create a new distribution. Returns the just created distribution or RightAws::AwsError exception.
acf.create_distribution('bucket-for-k-dzreyev.s3.amazonaws.com', 'Woo-Hoo!', true, ['web1.my-awesome-site.net'] ) #=> {:comment => "Woo-Hoo!", :enabled => true, :location => "https://cloudfront.amazonaws.com/2008-06-30/distribution/E2REJM3VUN5RSI", :status => "InProgress", :aws_id => "E2REJM3VUN5RSI", :domain_name => "d3dxv71tbbt6cd.6hops.net", :origin => "my-bucket.s3.amazonaws.com", :cnames => ["web1.my-awesome-site.net"] :last_modified_time => Wed Sep 10 17:00:54 UTC 2008, :caller_reference => "200809102100536497863003"}
# File lib/acf/right_acf_interface.rb, line 213 213: def create_distribution(origin, comment='', enabled=true, cnames=[], caller_reference=nil) 214: # join CNAMES 215: cnames_str = '' 216: unless cnames.blank? 217: cnames.to_a.each { |cname| cnames_str += "\n <CNAME>#{cname}</CNAME>" } 218: end 219: # reference 220: caller_reference ||= generate_call_reference 221: body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<DistributionConfig xmlns=\#{xmlns}>\n<Origin>\#{origin}</Origin>\n<CallerReference>\#{caller_reference}</CallerReference>\n\#{cnames_str.lstrip}\n<Comment>\#{AcfInterface::escape(comment.to_s)}</Comment>\n<Enabled>\#{enabled}</Enabled>\n</DistributionConfig>\n" 222: request_hash = generate_request('POST', 'distribution', body.strip) 223: merge_headers(request_info(request_hash, AcfDistributionParser.new)) 224: end
Delete a distribution. The enabled distribution cannot be deleted. Returns true on success or RightAws::AwsError exception.
acf.delete_distribution('E2REJM3VUN5RSI', 'E39OHHU1ON65SI') #=> true
# File lib/acf/right_acf_interface.rb, line 315 315: def delete_distribution(aws_id, e_tag) 316: request_hash = generate_request('DELETE', "distribution/#{aws_id}", nil, 317: 'If-Match' => e_tag) 318: request_info(request_hash, RightHttp2xxParser.new) 319: end
Get a distribution‘s information. Returns a distribution‘s information or RightAws::AwsError exception.
acf.get_distribution('E2REJM3VUN5RSI') #=> {:enabled => true, :caller_reference => "200809102100536497863003", :e_tag => "E39OHHU1ON65SI", :status => "Deployed", :domain_name => "d3dxv71tbbt6cd.6hops.net", :cnames => ["web1.my-awesome-site.net", "web2.my-awesome-site.net"] :aws_id => "E2REJM3VUN5RSI", :comment => "Woo-Hoo!", :origin => "my-bucket.s3.amazonaws.com", :last_modified_time => Wed Sep 10 17:00:54 UTC 2008 }
# File lib/acf/right_acf_interface.rb, line 251 251: def get_distribution(aws_id) 252: request_hash = generate_request('GET', "distribution/#{aws_id}") 253: merge_headers(request_info(request_hash, AcfDistributionParser.new)) 254: end
Get a distribution‘s configuration. Returns a distribution‘s configuration or RightAws::AwsError exception.
acf.get_distribution_config('E2REJM3VUN5RSI') #=> {:enabled => true, :caller_reference => "200809102100536497863003", :e_tag => "E39OHHU1ON65SI", :cnames => ["web1.my-awesome-site.net", "web2.my-awesome-site.net"] :comment => "Woo-Hoo!", :origin => "my-bucket.s3.amazonaws.com"}
# File lib/acf/right_acf_interface.rb, line 267 267: def get_distribution_config(aws_id) 268: request_hash = generate_request('GET', "distribution/#{aws_id}/config") 269: merge_headers(request_info(request_hash, AcfDistributionConfigParser.new)) 270: end
List distributions. Returns an array of distributions or RightAws::AwsError exception.
acf.list_distributions #=> [{:status => "Deployed", :domain_name => "d74zzrxmpmygb.6hops.net", :aws_id => "E4U91HCJHGXVC", :cnames => ["web1.my-awesome-site.net", "web2.my-awesome-site.net"] :origin => "my-bucket.s3.amazonaws.com", :comment => "My comments", :last_modified_time => Wed Sep 10 17:00:04 UTC 2008 }, ..., {...} ]
# File lib/acf/right_acf_interface.rb, line 193 193: def list_distributions 194: request_hash = generate_request('GET', 'distribution') 195: request_cache_or_info :list_distributions, request_hash, AcfDistributionListParser, @@bench 196: end
Set a distribution‘s configuration (the :origin and the :caller_reference cannot be changed). Returns true on success or RightAws::AwsError exception.
config = acf.get_distribution_config('E2REJM3VUN5RSI') #=> {:enabled => true, :caller_reference => "200809102100536497863003", :e_tag => "E39OHHU1ON65SI", :cnames => ["web1.my-awesome-site.net", "web2.my-awesome-site.net"] :comment => "Woo-Hoo!", :origin => "my-bucket.s3.amazonaws.com"} config[:comment] = 'Olah-lah!' config[:enabled] = false acf.set_distribution_config('E2REJM3VUN5RSI', config) #=> true
# File lib/acf/right_acf_interface.rb, line 287 287: def set_distribution_config(aws_id, config) 288: # join CNAMES 289: cnames_str = '' 290: unless config[:cnames].blank? 291: config[:cnames].to_a.each { |cname| cnames_str += "\n <CNAME>#{cname}</CNAME>" } 292: end 293: # format request's XML body 294: body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<DistributionConfig xmlns=\#{xmlns}>\n<Origin>\#{config[:origin]}</Origin>\n<CallerReference>\#{config[:caller_reference]}</CallerReference>\n\#{cnames_str.lstrip}\n<Comment>\#{AcfInterface::escape(config[:comment].to_s)}</Comment>\n<Enabled>\#{config[:enabled]}</Enabled>\n</DistributionConfig>\n" 295: request_hash = generate_request('PUT', "distribution/#{aws_id}/config", body.strip, 296: 'If-Match' => config[:e_tag]) 297: request_info(request_hash, RightHttp2xxParser.new) 298: end