From b498079f80dc2fad7ce39fff1c10e32f5226d36e Mon Sep 17 00:00:00 2001 From: geemus Date: Tue, 9 Nov 2010 15:49:38 -0800 Subject: [PATCH] [aws|cdn] update to support custom origins --- lib/fog/aws/cdn.rb | 3 +- lib/fog/aws/parsers/cdn/distribution.rb | 17 ++- .../aws/parsers/cdn/get_distribution_list.rb | 13 +++ lib/fog/aws/requests/cdn/get_distribution.rb | 12 ++- .../aws/requests/cdn/get_distribution_list.rb | 10 +- lib/fog/aws/requests/cdn/post_distribution.rb | 21 +++- .../requests/cdn/put_distribution_config.rb | 102 ++++++++++++++++++ 7 files changed, 169 insertions(+), 9 deletions(-) create mode 100644 lib/fog/aws/requests/cdn/put_distribution_config.rb diff --git a/lib/fog/aws/cdn.rb b/lib/fog/aws/cdn.rb index ae7d0450b..a9898b200 100644 --- a/lib/fog/aws/cdn.rb +++ b/lib/fog/aws/cdn.rb @@ -12,6 +12,7 @@ module Fog request 'get_distribution_list' request 'post_distribution' request 'post_invalidation' + request 'put_distribution_config' class Mock @@ -69,7 +70,7 @@ module Fog @path = options[:path] || '/' @port = options[:port] || 443 @scheme = options[:scheme] || 'https' - @version = options[:version] || '2010-08-01' + @version = options[:version] || '2010-11-01' @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}#{@path}", options[:persistent] || true) end diff --git a/lib/fog/aws/parsers/cdn/distribution.rb b/lib/fog/aws/parsers/cdn/distribution.rb index ea150d86c..0e8b74ea8 100644 --- a/lib/fog/aws/parsers/cdn/distribution.rb +++ b/lib/fog/aws/parsers/cdn/distribution.rb @@ -9,6 +9,15 @@ module Fog @response = { 'DistributionConfig' => { 'CNAME' => [], 'Logging' => {}, 'TrustedSigners' => [] } } end + def start_element(name, attrs = []) + super + case name + when 'CustomOrigin', 'S3Origin' + @origin = name + @response['DistributionConfig'][@origin] = {} + end + end + def end_element(name) case name when 'AwsAccountNumber' @@ -17,16 +26,22 @@ module Fog @response['DistributionConfig']['Logging'][name] = @value when 'CNAME' @response['DistributionConfig']['CNAME'] << @value + when 'DNSName', 'OriginAccessIdentity', 'OriginProtocolPolicy' + @response['DistributionConfig'][@origin][name] = @value when 'DomainName', 'Id', 'Status' @response[name] = @value when 'CallerReference', 'Comment', 'DefaultRootObject', 'Origin', 'OriginAccessIdentity' - @response['DistributionConfig'][name] << @value + @response['DistributionConfig'][name] = @value when 'Enabled' if @value == 'true' @response['DistributionConfig'][name] = true else @response['DistributionConfig'][name] = false end + when 'HTTPPort', 'HTTPSPort' + @response['DistributionConfig'][@origin][name] = @value.to_i + when 'InProgressInvalidationBatches' + @response[name] = @value.to_i when 'LastModifiedTime' @response[name] = Time.parse(@value) when 'Protocol' diff --git a/lib/fog/aws/parsers/cdn/get_distribution_list.rb b/lib/fog/aws/parsers/cdn/get_distribution_list.rb index aa0e1b90b..3e1a97aab 100644 --- a/lib/fog/aws/parsers/cdn/get_distribution_list.rb +++ b/lib/fog/aws/parsers/cdn/get_distribution_list.rb @@ -10,6 +10,15 @@ module Fog @response = { 'DistributionSummary' => [] } end + def start_element(name, attrs = []) + super + case name + when 'CustomOrigin', 'S3Origin' + @origin = name + @distribution_summary[@origin] = {} + end + end + def end_element(name) case name when 'DistributionSummary' @@ -19,12 +28,16 @@ module Fog @distribution_summary[name] = @value when 'CNAME' @distribution_summary[name] << @value + when 'DNSName', 'OriginAccessIdentity', 'OriginProtocolPolicy' + @distribution_summary[@origin][name] = @value when 'Enabled' if @value == 'true' @distribution_summary[name] = true else @distribution_summary[name] = false end + when 'HTTPPort', 'HTTPSPort' + @distribution_summary[@origin][name] = @value.to_i when 'LastModifiedTime' @distribution_summary[name] = Time.parse(@value) when 'IsTruncated' diff --git a/lib/fog/aws/requests/cdn/get_distribution.rb b/lib/fog/aws/requests/cdn/get_distribution.rb index a4025b479..db5b64db1 100644 --- a/lib/fog/aws/requests/cdn/get_distribution.rb +++ b/lib/fog/aws/requests/cdn/get_distribution.rb @@ -13,7 +13,16 @@ module Fog # ==== Returns # * response<~Excon::Response>: # * body<~Hash>: - # * 'DomainName'<~String>: Domain name of distribution + # * 'S3Origin'<~Hash>: + # * 'DNSName'<~String> - origin to associate with distribution, ie 'mybucket.s3.amazonaws.com' + # * 'OriginAccessIdentity'<~String> - Optional: Used when serving private content + # or + # * 'CustomOrigin'<~Hash>: + # * 'DNSName'<~String> - origin to associate with distribution, ie 'www.example.com' + # * 'HTTPPort'<~Integer> - HTTP port of origin, in [80, 443] or (1024...65535) + # * 'HTTPSPort'<~Integer> - HTTPS port of origin, in [80, 443] or (1024...65535) + # * 'OriginProtocolPolicy'<~String> - Policy on using http vs https, in ['http-only', 'match-viewer'] + # # * 'Id'<~String> - Id of distribution # * 'LastModifiedTime'<~String> - Timestamp of last modification of distribution # * 'Status'<~String> - Status of distribution @@ -22,6 +31,7 @@ module Fog # * 'CNAME'<~Array> - array of associated cnames # * 'Comment'<~String> - comment associated with distribution # * 'Enabled'<~Boolean> - whether or not distribution is enabled + # * 'InProgressInvalidationBatches'<~Integer> - number of invalidation batches in progress # * 'Logging'<~Hash>: # * 'Bucket'<~String> - bucket logs are stored in # * 'Prefix'<~String> - prefix logs are stored with diff --git a/lib/fog/aws/requests/cdn/get_distribution_list.rb b/lib/fog/aws/requests/cdn/get_distribution_list.rb index a3ab3c42c..1d5b641dc 100644 --- a/lib/fog/aws/requests/cdn/get_distribution_list.rb +++ b/lib/fog/aws/requests/cdn/get_distribution_list.rb @@ -21,9 +21,17 @@ module Fog # * '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>: + # * 'S3Origin'<~Hash>: + # * 'DNSName'<~String> - origin to associate with distribution, ie 'mybucket.s3.amazonaws.com' + # * 'OriginAccessIdentity'<~String> - Optional: Used when serving private content + # or + # * 'CustomOrigin'<~Hash>: + # * 'DNSName'<~String> - origin to associate with distribution, ie 'www.example.com' + # * 'HTTPPort'<~Integer> - HTTP port of origin, in [80, 443] or (1024...65535) + # * 'HTTPSPort'<~Integer> - HTTPS port of origin, in [80, 443] or (1024...65535) + # * 'OriginProtocolPolicy'<~String> - Policy on using http vs https, in ['http-only', 'match-viewer'] # * 'Comment'<~String> - comment associated with distribution # * 'CNAME'<~Array> - array of associated cnames - # * 'DomainName'<~String> - Domain name of distribution # * 'Enabled'<~Boolean> - whether or not distribution is enabled # * 'Id'<~String> - Id of distribution # * 'LastModifiedTime'<~String> - Timestamp of last modification of distribution diff --git a/lib/fog/aws/requests/cdn/post_distribution.rb b/lib/fog/aws/requests/cdn/post_distribution.rb index 13c7a0a7a..42923a8b8 100644 --- a/lib/fog/aws/requests/cdn/post_distribution.rb +++ b/lib/fog/aws/requests/cdn/post_distribution.rb @@ -8,8 +8,18 @@ module Fog # create a new distribution in CloudFront # # ==== Parameters - # * origin<~String> - s3 bucket, ie 'mybucket.s3.amazonaws.com' - # * config<~Hash> - config for distribution. Defaults to {}. + # * options<~Hash> - config for distribution. Defaults to {}. + # REQUIRED: + # * 'S3Origin'<~Hash>: + # * 'DNSName'<~String> - origin to associate with distribution, ie 'mybucket.s3.amazonaws.com' + # * 'OriginAccessIdentity'<~String> - Optional: Used when serving private content + # or + # * 'CustomOrigin'<~Hash>: + # * 'DNSName'<~String> - origin to associate with distribution, ie 'www.example.com' + # * 'HTTPPort'<~Integer> - HTTP port of origin, in [80, 443] or (1024...65535) + # * 'HTTPSPort'<~Integer> - HTTPS port of origin, in [80, 443] or (1024...65535) + # * 'OriginProtocolPolicy'<~String> - Policy on using http vs https, in ['http-only', 'match-viewer'] + # OPTIONAL: # * 'CallerReference'<~String> - Used to prevent replay, defaults to Time.now.to_i.to_s # * 'Comment'<~String> - Optional comment about distribution # * 'CNAME'<~Array> - Optional array of strings to set as CNAMEs @@ -43,7 +53,8 @@ module Fog # ==== See Also # http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/CreateDistribution.html - def post_distribution(origin, options = {}) + def post_distribution(options = {}) + options['CallerReference'] = Time.now.to_i.to_s data = '' data << "" for key, value in options @@ -78,11 +89,11 @@ module Fog class Mock # :nodoc:all - def post_distribution(origin, options = {}) + def post_distribution(options = {}) Fog::Mock.not_implemented end end end end -end +end \ No newline at end of file diff --git a/lib/fog/aws/requests/cdn/put_distribution_config.rb b/lib/fog/aws/requests/cdn/put_distribution_config.rb new file mode 100644 index 000000000..fa935a839 --- /dev/null +++ b/lib/fog/aws/requests/cdn/put_distribution_config.rb @@ -0,0 +1,102 @@ +module Fog + module AWS + class CDN + class Real + + require 'fog/aws/parsers/cdn/distribution' + + # update a distribution in CloudFront + # + # ==== Parameters + # * distribution_id<~String> - Id of distribution to update config for + # * options<~Hash> - config for distribution. Defaults to {}. + # REQUIRED: + # * 'S3Origin'<~Hash>: + # * 'DNSName'<~String> - origin to associate with distribution, ie 'mybucket.s3.amazonaws.com' + # * 'OriginAccessIdentity'<~String> - Optional: Used when serving private content + # or + # * 'CustomOrigin'<~Hash>: + # * 'DNSName'<~String> - origin to associate with distribution, ie 'www.example.com' + # * 'HTTPPort'<~Integer> - HTTP port of origin, in [80, 443] or (1024...65535) + # * 'HTTPSPort'<~Integer> - HTTPS port of origin, in [80, 443] or (1024...65535) + # * 'OriginProtocolPolicy'<~String> - Policy on using http vs https, in ['http-only', 'match-viewer'] + # OPTIONAL: + # * 'CallerReference'<~String> - Used to prevent replay, defaults to Time.now.to_i.to_s + # * 'Comment'<~String> - Optional comment about distribution + # * 'CNAME'<~Array> - Optional array of strings to set as CNAMEs + # * 'DefaultRootObject'<~String> - Optional default object to return for '/' + # * 'Enabled'<~Boolean> - Whether or not distribution should accept requests, defaults to true + # * 'Logging'<~Hash>: Optional logging config + # * 'Bucket'<~String> - Bucket to store logs in, ie 'mylogs.s3.amazonaws.com' + # * 'Prefix'<~String> - Optional prefix for log filenames, ie 'myprefix/' + # * 'OriginAccessIdentity'<~String> - Used for serving private content, in format 'origin-access-identity/cloudfront/ID' + # * 'RequiredProtocols'<~String> - Optional, set to 'https' to force https connections + # * 'TrustedSigners'<~Array> - Optional grant of rights to up to 5 aws accounts to generate signed URLs for private content, elements are either 'Self' for your own account or an AWS Account Number + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * 'DomainName'<~String>: Domain name of distribution + # * 'Id'<~String> - Id of distribution + # * 'LastModifiedTime'<~String> - Timestamp of last modification of distribution + # * 'Status'<~String> - Status of distribution + # * 'DistributionConfig'<~Array>: + # * 'CallerReference'<~String> - Used to prevent replay, defaults to Time.now.to_i.to_s + # * 'CNAME'<~Array> - array of associated cnames + # * 'Comment'<~String> - comment associated with distribution + # * 'Enabled'<~Boolean> - whether or not distribution is enabled + # * 'Logging'<~Hash>: + # * 'Bucket'<~String> - bucket logs are stored in + # * 'Prefix'<~String> - prefix logs are stored with + # * 'Origin'<~String> - s3 origin bucket + # * 'TrustedSigners'<~Array> - trusted signers + # + # ==== See Also + # http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/CreateDistribution.html + + def put_distribution_config(distribution_id, etag, options = {}) + data = '' + data << "" + for key, value in options + case value + when Array + for item in value + data << "<#{key}>#{item}" + end + when Hash + data << "<#{key}>" + for inner_key, inner_value in value + data << "<#{inner_key}>#{inner_value}" + end + data << "" + else + data << "<#{key}>#{value}" + end + end + data << "" + request({ + :body => data, + :expects => 200, + :headers => { + 'Content-Type' => 'text/xml', + 'If-Match' => etag + }, + :idempotent => true, + :method => 'PUT', + :parser => Fog::Parsers::AWS::CDN::Distribution.new, + :path => "/distribution/#{distribution_id}/config" + }) + end + + end + + class Mock # :nodoc:all + + def put_distribution_config(distribution_id, etag, options = {}) + Fog::Mock.not_implemented + end + + end + end + end +end \ No newline at end of file