From afde136b89c3b13613667064ddee85a8a93bfbd4 Mon Sep 17 00:00:00 2001 From: Christopher Oliver Date: Mon, 25 Jul 2011 15:42:12 -0400 Subject: [PATCH] [aws|cdn] Added commands for streaming distribution lists. --- lib/fog/cdn/aws.rb | 6 ++ .../cdn/parsers/aws/get_invalidation_list.rb | 43 ++++++++++ .../aws/get_streaming_distribution_list.rb | 59 +++++++++++++ .../cdn/parsers/aws/streaming_distribution.rb | 59 +++++++++++++ .../aws/delete_streaming_distribution.rb | 28 +++++++ .../cdn/requests/aws/get_invalidation_list.rb | 42 ++++++++++ .../aws/get_streaming_distribution.rb | 50 +++++++++++ .../aws/get_streaming_distribution_list.rb | 59 +++++++++++++ .../aws/post_streaming_distribution.rb | 78 +++++++++++++++++ .../aws/put_streaming_distribution_config.rb | 83 +++++++++++++++++++ 10 files changed, 507 insertions(+) create mode 100644 lib/fog/cdn/parsers/aws/get_invalidation_list.rb create mode 100644 lib/fog/cdn/parsers/aws/get_streaming_distribution_list.rb create mode 100644 lib/fog/cdn/parsers/aws/streaming_distribution.rb create mode 100644 lib/fog/cdn/requests/aws/delete_streaming_distribution.rb create mode 100644 lib/fog/cdn/requests/aws/get_invalidation_list.rb create mode 100644 lib/fog/cdn/requests/aws/get_streaming_distribution.rb create mode 100644 lib/fog/cdn/requests/aws/get_streaming_distribution_list.rb create mode 100644 lib/fog/cdn/requests/aws/post_streaming_distribution.rb create mode 100644 lib/fog/cdn/requests/aws/put_streaming_distribution_config.rb diff --git a/lib/fog/cdn/aws.rb b/lib/fog/cdn/aws.rb index 340c04f4d..e9d9ed3e1 100644 --- a/lib/fog/cdn/aws.rb +++ b/lib/fog/cdn/aws.rb @@ -9,11 +9,17 @@ module Fog request_path 'fog/cdn/requests/aws' request 'delete_distribution' + request 'delete_streaming_distribution' request 'get_distribution' request 'get_distribution_list' + request 'get_invalidation_list' + request 'get_streaming_distribution' + request 'get_streaming_distribution_list' request 'post_distribution' + request 'post_streaming_distribution' request 'post_invalidation' request 'put_distribution_config' + request 'put_streaming_distribution_config' class Mock diff --git a/lib/fog/cdn/parsers/aws/get_invalidation_list.rb b/lib/fog/cdn/parsers/aws/get_invalidation_list.rb new file mode 100644 index 000000000..0dba8c26c --- /dev/null +++ b/lib/fog/cdn/parsers/aws/get_invalidation_list.rb @@ -0,0 +1,43 @@ +module Fog + module Parsers + module CDN + module AWS + + class GetInvalidationList < Fog::Parsers::Base + + def reset + @invalidation_summary = { } + @response = { 'InvalidationSummary' => [] } + end + + def start_element(name, attrs = []) + super + end + + def end_element(name) + puts name + case name + when 'InvalidationSummary' + @response['InvalidationSummary'] << @invalidation_summary + @invalidation_summary = {} + when 'Id', 'Status' + @invalidation_summary[name] = @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 diff --git a/lib/fog/cdn/parsers/aws/get_streaming_distribution_list.rb b/lib/fog/cdn/parsers/aws/get_streaming_distribution_list.rb new file mode 100644 index 000000000..5a3537d61 --- /dev/null +++ b/lib/fog/cdn/parsers/aws/get_streaming_distribution_list.rb @@ -0,0 +1,59 @@ +module Fog + module Parsers + module CDN + module AWS + + class GetStreamingDistributionList < Fog::Parsers::Base + + def reset + @distribution_summary = { 'CNAME' => [], 'TrustedSigners' => [] } + @response = { 'StreamingDistributionSummary' => [] } + end + + def start_element(name, attrs = []) + super + case name + when 'S3Origin' + @origin = name + @distribution_summary[@origin] = {} + end + end + + def end_element(name) + case name + when 'StreamingDistributionSummary' + @response['StreamingDistributionSummary'] << @distribution_summary + @distribution_summary = { 'CNAME' => [], 'TrustedSigners' => [] } + when 'Comment', 'DomainName', 'Id', 'Status' + @distribution_summary[name] = @value + when 'CNAME' + @distribution_summary[name] << @value + when 'DNSName', 'OriginAccessIdentity' + @distribution_summary[@origin][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 diff --git a/lib/fog/cdn/parsers/aws/streaming_distribution.rb b/lib/fog/cdn/parsers/aws/streaming_distribution.rb new file mode 100644 index 000000000..fb6cd5ba9 --- /dev/null +++ b/lib/fog/cdn/parsers/aws/streaming_distribution.rb @@ -0,0 +1,59 @@ +module Fog + module Parsers + module CDN + module AWS + + class StreamingDistribution < Fog::Parsers::Base + + def reset + @response = { 'StreamingDistributionConfig' => { 'CNAME' => [], 'Logging' => {}, 'TrustedSigners' => [] } } + end + + def start_element(name, attrs = []) + super + case name + when 'CustomOrigin', 'S3Origin' + @origin = name + @response['StreamingDistributionConfig'][@origin] = {} + end + end + + def end_element(name) + case name + when 'AwsAccountNumber' + @response['StreamingDistributionConfig']['TrustedSigners'] << @value + when 'Bucket', 'Prefix' + @response['StreamingDistributionConfig']['Logging'][name] = @value + when 'CNAME' + @response['StreamingDistributionConfig']['CNAME'] << @value + when 'DNSName', 'OriginAccessIdentity', 'OriginProtocolPolicy' + @response['StreamingDistributionConfig'][@origin][name] = @value + when 'DomainName', 'Id', 'Status' + @response[name] = @value + when 'CallerReference', 'Comment', 'DefaultRootObject', 'Origin', 'OriginAccessIdentity' + @response['StreamingDistributionConfig'][name] = @value + when 'Enabled' + if @value == 'true' + @response['StreamingDistributionConfig'][name] = true + else + @response['StreamingDistributionConfig'][name] = false + end + when 'HTTPPort', 'HTTPSPort' + @response['StreamingDistributionConfig'][@origin][name] = @value.to_i + when 'InProgressInvalidationBatches' + @response[name] = @value.to_i + when 'LastModifiedTime' + @response[name] = Time.parse(@value) + when 'Protocol' + @response['StreamingDistributionConfig']['RequireProtocols'] = @value + when 'Self' + @response['StreamingDistributionConfig']['TrustedSigners'] << 'Self' + end + end + + end + + end + end + end +end diff --git a/lib/fog/cdn/requests/aws/delete_streaming_distribution.rb b/lib/fog/cdn/requests/aws/delete_streaming_distribution.rb new file mode 100644 index 000000000..246da13ff --- /dev/null +++ b/lib/fog/cdn/requests/aws/delete_streaming_distribution.rb @@ -0,0 +1,28 @@ +module Fog + module CDN + class AWS + class Real + + # Delete a streaming distribution from CloudFront + # + # ==== Parameters + # * distribution_id<~String> - Id of distribution to delete + # * etag<~String> - etag of that distribution from earlier get or put + # + # ==== See Also + # http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/DeleteStreamingDistribution.html + + def delete_streaming_distribution(distribution_id, etag) + request({ + :expects => 204, + :headers => { 'If-Match' => etag }, + :idempotent => true, + :method => 'DELETE', + :path => "/streaming-distribution/#{distribution_id}" + }) + end + + end + end + end +end diff --git a/lib/fog/cdn/requests/aws/get_invalidation_list.rb b/lib/fog/cdn/requests/aws/get_invalidation_list.rb new file mode 100644 index 000000000..a906ad1e3 --- /dev/null +++ b/lib/fog/cdn/requests/aws/get_invalidation_list.rb @@ -0,0 +1,42 @@ +module Fog + module CDN + class AWS + class Real + + require 'fog/cdn/parsers/aws/get_invalidation_list' + + # ==== 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) + # * 'InvalidationSummary'<~Array>: + # * 'Id'<~String>: + # * 'Status'<~String>: + # + # ==== See Also + # http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/ListInvalidation.html + + def get_invalidation_list(distribution_id, options = {}) + request({ + :expects => 200, + :idempotent => true, + :method => 'GET', + :parser => Fog::Parsers::CDN::AWS::GetInvalidationList.new, + :path => "/distribution/#{distribution_id}/invalidation", + :query => options + }) + end + + end + end + end +end diff --git a/lib/fog/cdn/requests/aws/get_streaming_distribution.rb b/lib/fog/cdn/requests/aws/get_streaming_distribution.rb new file mode 100644 index 000000000..bff94035e --- /dev/null +++ b/lib/fog/cdn/requests/aws/get_streaming_distribution.rb @@ -0,0 +1,50 @@ +module Fog + module CDN + class AWS + class Real + + require 'fog/cdn/parsers/aws/streaming_distribution' + + # Get information about a streaming distribution from CloudFront + # + # ==== Parameters + # * distribution_id<~String> - id of distribution + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * 'S3Origin'<~Hash>: + # * 'DNSName'<~String> - origin to associate with distribution, ie 'mybucket.s3.amazonaws.com' + # * 'OriginAccessIdentity'<~String> - Optional: Used when serving private content + # * 'Id'<~String> - Id of distribution + # * 'LastModifiedTime'<~String> - Timestamp of last modification of distribution + # * 'Status'<~String> - Status of distribution + # * 'StreamingDistributionConfig'<~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 + # * 'InProgressInvalidationBatches'<~Integer> - number of invalidation batches in progress + # * '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/GetStreamingDistribution.html + + def get_streaming_distribution(distribution_id) + request({ + :expects => 200, + :idempotent => true, + :method => 'GET', + :parser => Fog::Parsers::CDN::AWS::StreamingDistribution.new, + :path => "/streaming-distribution/#{distribution_id}" + }) + end + + end + end + end +end diff --git a/lib/fog/cdn/requests/aws/get_streaming_distribution_list.rb b/lib/fog/cdn/requests/aws/get_streaming_distribution_list.rb new file mode 100644 index 000000000..f6083a176 --- /dev/null +++ b/lib/fog/cdn/requests/aws/get_streaming_distribution_list.rb @@ -0,0 +1,59 @@ +module Fog + module CDN + class AWS + class Real + + require 'fog/cdn/parsers/aws/get_streaming_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) + # * 'StreamingDistributionSummary'<~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 + # * 'Enabled'<~Boolean> - whether or not distribution is enabled + # * 'Id'<~String> - Id of distribution + # * 'LastModifiedTime'<~String> - Timestamp of last modification of distribution + # * 'Origin'<~String> - s3 origin bucket + # * 'Status'<~String> - Status of distribution + # * 'TrustedSigners'<~Array> - trusted signers + # + # ==== See Also + # http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/ListStreamingDistributions.html + + def get_streaming_distribution_list(options = {}) + request({ + :expects => 200, + :idempotent => true, + :method => 'GET', + :parser => Fog::Parsers::CDN::AWS::GetStreamingDistributionList.new, + :path => "/streaming-distribution", + :query => options + }) + end + + end + end + end +end diff --git a/lib/fog/cdn/requests/aws/post_streaming_distribution.rb b/lib/fog/cdn/requests/aws/post_streaming_distribution.rb new file mode 100644 index 000000000..8ac09e6fd --- /dev/null +++ b/lib/fog/cdn/requests/aws/post_streaming_distribution.rb @@ -0,0 +1,78 @@ +module Fog + module CDN + class AWS + class Real + + require 'fog/cdn/parsers/aws/streaming_distribution' + + # create a new streaming distribution in CloudFront + # + # ==== Parameters + # * options<~Hash> - config for distribution. Defaults to {}. + # REQUIRED: + # * 'S3Origin'<~Hash>: + # * 'DNSName'<~String> - origin to associate with distribution, ie 'mybucket.s3.amazonaws.com' + # 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 + # * '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/' + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * 'Id'<~String> - Id of distribution + # * 'Status'<~String> - Status of distribution + # * 'LastModifiedTime'<~String> - Timestamp of last modification of distribution + # * 'DomainName'<~String>: Domain name of distribution + # * 'StreamingDistributionConfig'<~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 + # + # ==== See Also + # http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/CreateStreamingDistribution.html + + def post_streaming_distribution(options = {}) + options['CallerReference'] = Time.now.to_i.to_s + 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 => 201, + :headers => { 'Content-Type' => 'text/xml' }, + :idempotent => true, + :method => 'POST', + :parser => Fog::Parsers::CDN::AWS::StreamingDistribution.new, + :path => "/streaming-distribution" + }) + end + + end + end + end +end diff --git a/lib/fog/cdn/requests/aws/put_streaming_distribution_config.rb b/lib/fog/cdn/requests/aws/put_streaming_distribution_config.rb new file mode 100644 index 000000000..88deacc3f --- /dev/null +++ b/lib/fog/cdn/requests/aws/put_streaming_distribution_config.rb @@ -0,0 +1,83 @@ +module Fog + module CDN + class AWS + class Real + + require 'fog/cdn/parsers/aws/streaming_distribution' + + # update a streaming 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' + # 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 + # * '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/' + # + # ==== 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 + # * 'StreamingDistributionConfig'<~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/PutStreamingDistribution.html + + def put_streaming_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::CDN::AWS::StreamingDistribution.new, + :path => "/streaming-distribution/#{distribution_id}/config" + }) + end + + end + end + end +end