cloudfront 0.0.1 → 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/Gemfile +11 -8
  2. data/LICENSE.txt +1 -1
  3. data/README.rdoc +64 -7
  4. data/Rakefile +11 -14
  5. data/cloudfront.gemspec +85 -27
  6. data/lib/cloudfront.rb +39 -0
  7. data/lib/cloudfront/connection.rb +25 -0
  8. data/lib/cloudfront/distribution/distribution.rb +136 -0
  9. data/lib/cloudfront/distribution/download_distribution.rb +54 -0
  10. data/lib/cloudfront/distribution/streaming_distribution.rb +53 -0
  11. data/lib/cloudfront/errors/cloudfront_error.rb +13 -0
  12. data/lib/cloudfront/errors/cname_already_exists_error.rb +7 -0
  13. data/lib/cloudfront/errors/distribution_already_exists_error.rb +7 -0
  14. data/lib/cloudfront/errors/illegal_update_error.rb +7 -0
  15. data/lib/cloudfront/errors/invalid_origin_access_identity_error.rb +7 -0
  16. data/lib/cloudfront/errors/invalid_origin_error.rb +7 -0
  17. data/lib/cloudfront/errors/invalid_required_protocol_error.rb +7 -0
  18. data/lib/cloudfront/errors/missing_body_error.rb +7 -0
  19. data/lib/cloudfront/errors/precondition_failed_error.rb +7 -0
  20. data/lib/cloudfront/errors/too_many_distribution_cnames_error.rb +7 -0
  21. data/lib/cloudfront/errors/too_many_distributions_error.rb +7 -0
  22. data/lib/cloudfront/errors/too_many_trusted_signers_error.rb +7 -0
  23. data/lib/cloudfront/errors/trusted_signer_does_not_exist_error.rb +7 -0
  24. data/lib/cloudfront/exceptions/delete_enabled_distribution_exception.rb +7 -0
  25. data/lib/cloudfront/exceptions/distribution_already_disabled_exception.rb +7 -0
  26. data/lib/cloudfront/exceptions/distribution_already_enabled_exception.rb +7 -0
  27. data/lib/cloudfront/exceptions/distribution_configuration_exception.rb +7 -0
  28. data/lib/cloudfront/exceptions/missing_etag_exception.rb +7 -0
  29. data/lib/cloudfront/helpers/aliases.rb +71 -0
  30. data/lib/cloudfront/helpers/cache_behavior.rb +166 -0
  31. data/lib/cloudfront/helpers/cache_behaviors.rb +108 -0
  32. data/lib/cloudfront/helpers/download_distribution.rb +157 -0
  33. data/lib/cloudfront/helpers/invalidation.rb +83 -0
  34. data/lib/cloudfront/helpers/logging.rb +71 -0
  35. data/lib/cloudfront/helpers/origin.rb +101 -0
  36. data/lib/cloudfront/helpers/origin_access_identity.rb +58 -0
  37. data/lib/cloudfront/helpers/origins.rb +94 -0
  38. data/lib/cloudfront/helpers/s3_origin.rb +51 -0
  39. data/lib/cloudfront/helpers/streaming_distribution.rb +150 -0
  40. data/lib/cloudfront/helpers/trusted_signers.rb +73 -0
  41. data/lib/cloudfront/invalidation/invalidations.rb +57 -0
  42. data/lib/cloudfront/origin_access_identity/origin_access_identity.rb +60 -0
  43. data/lib/cloudfront/utils/api.rb +18 -0
  44. data/lib/cloudfront/utils/array.rb +5 -0
  45. data/lib/cloudfront/utils/configuration_checker.rb +19 -0
  46. data/lib/cloudfront/utils/string.rb +11 -0
  47. data/lib/cloudfront/utils/util.rb +20 -0
  48. data/lib/cloudfront/utils/xml_serializer.rb +18 -0
  49. data/lib/faraday/request/cloudfront_signer.rb +35 -0
  50. data/lib/faraday/request/xml_content_type.rb +18 -0
  51. metadata +198 -31
  52. data/.document +0 -5
  53. data/Gemfile.lock +0 -27
  54. data/test/helper.rb +0 -19
  55. data/test/test_cloudfront.rb +0 -7
data/Gemfile CHANGED
@@ -1,15 +1,18 @@
1
1
  source "http://rubygems.org"
2
- # Add dependencies required to use your gem here.
3
- # Example:
4
- # gem "activesupport", ">= 2.3.5"
2
+
3
+ gem "faraday"
4
+ gem "faraday_middleware"
5
+ gem "multi_xml"
6
+ gem "nokogiri"
5
7
 
6
8
  # Add dependencies to develop your gem here.
7
- # Include everything needed to run rake, tests, features, etc.
8
9
  group :development do
9
10
  gem "minitest", ">= 0"
10
- gem "yard", "~> 0.7"
11
- gem "rdoc", "~> 3.12"
11
+ gem "yard", "~> 0.6.0"
12
12
  gem "bundler", "~> 1.0.0"
13
- gem "jeweler", "~> 1.8.3"
14
- gem "rcov", ">= 0"
13
+ gem "jeweler", "~> 1.6.4"
14
+ gem 'simplecov', :require => false
15
+ gem "vcr", ">= 0"
16
+ gem "webmock", ">= 0"
17
+ gem "timecop", ">= 0"
15
18
  end
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012 Belkacem REBBOUH
1
+ Copyright (c) 2011 Belkacem REBBOUH
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -1,19 +1,76 @@
1
1
  = cloudfront
2
2
 
3
- Description goes here.
3
+ A simple wrapper to cloudfront API.
4
+
5
+ == Usage
6
+
7
+ cloudfront = Cloudfront.new('aws_access_key_id', 'aws_secret_access_key')
8
+
9
+ Create a distribution
10
+ download_distribution = DownloadDistribution.new do
11
+ self.cnames.concat ["test.com", "test2.com"]
12
+ self.default_root_object = "/index.php"
13
+ self.origins.push Origin.new {
14
+ self.id = "test origin"
15
+ self.domain_name = "example.com"
16
+ self.http_port = 90
17
+ self.https_port = 90
18
+ }
19
+ self.origins.push Origin.new {
20
+ self.id = "test origin 2"
21
+ self.domain_name = "example2.com"
22
+ self.origin_access_identity = "origin-access-identity/cloudfront/ID"
23
+ }
24
+ self.default_cache_behavior = CacheBehavior.new {
25
+ self.is_default = true
26
+ self.target_origin_id = "test origin"
27
+ self.query_string_forward = true
28
+ }
29
+ self.cache_behaviors.push CacheBehavior.new {
30
+ self.path_pattern = "*.jpg"
31
+ self.target_origin_id = "test origin"
32
+ self.query_string_forward = true
33
+ self.cookies_forward_policy = "whitelist"
34
+ self.cookies_to_forward = "cookies"
35
+ self.trusted_signers = TrustedSigners.new {
36
+ self.enabled = true
37
+ self.trusted_signers.push "signer"
38
+ }
39
+ }
40
+
41
+ self.logging = Logging.new {
42
+ self.enabled = true
43
+ self.bucket = "belkacem.rebbouh.com.s3.amazonaws.com"
44
+ self.prefix = "distribution_test"
45
+ }
46
+ self.price_class = "PriceClass_All"
47
+ end
48
+
49
+ Then call
50
+ cloudfront.download_distribution_create(download_distribution)
51
+
52
+ See tests and documentation for more actions.
53
+
54
+ == Installation
55
+
56
+ gem install cloudfront
57
+
58
+ Then run the following command in the gem folder:
59
+
60
+ rake install
4
61
 
5
62
  == Contributing to cloudfront
6
63
 
7
- * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
- * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
- * Fork the project.
10
- * Start a feature/bugfix branch.
11
- * Commit and push until you are happy with your contribution.
64
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
65
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
66
+ * Fork the project
67
+ * Start a feature/bugfix branch
68
+ * Commit and push until you are happy with your contribution
12
69
  * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
70
  * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
71
 
15
72
  == Copyright
16
73
 
17
- Copyright (c) 2012 Belkacem REBBOUH. See LICENSE.txt for
74
+ Copyright (c) 2011 Belkacem REBBOUH. See LICENSE.txt for
18
75
  further details.
19
76
 
data/Rakefile CHANGED
@@ -15,13 +15,18 @@ require 'jeweler'
15
15
  Jeweler::Tasks.new do |gem|
16
16
  # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
17
  gem.name = "cloudfront"
18
- gem.version = "0.0.1"
19
- gem.homepage = "http://github.com/Belkacem/cloudfront"
18
+ gem.version = '1.0'
19
+ gem.homepage = "https://github.com/Belkacem/cloudfront"
20
20
  gem.license = "MIT"
21
- gem.summary = %Q{one-line summary of your gem}
22
- gem.description = %Q{longer description of your gem}
21
+ gem.summary = %Q{A tested Cloudfront™ gem}
22
+ gem.description = %Q{A tested Cloudfront™ client, that handles all api actions on distributions and invalidations}
23
23
  gem.email = "belkacem@rebbouh.com"
24
24
  gem.authors = ["Belkacem REBBOUH"]
25
+ gem.files = Dir['lib/**/*.rb'] + %w(Gemfile
26
+ LICENSE.txt
27
+ README.rdoc
28
+ Rakefile
29
+ cloudfront.gemspec)
25
30
  # dependencies defined in Gemfile
26
31
  end
27
32
  Jeweler::RubygemsDotOrgTasks.new
@@ -29,16 +34,8 @@ Jeweler::RubygemsDotOrgTasks.new
29
34
  require 'rake/testtask'
30
35
  Rake::TestTask.new(:test) do |test|
31
36
  test.libs << 'lib' << 'test'
32
- test.pattern = 'test/**/test_*.rb'
33
- test.verbose = true
34
- end
35
-
36
- require 'rcov/rcovtask'
37
- Rcov::RcovTask.new do |test|
38
- test.libs << 'test'
39
- test.pattern = 'test/**/test_*.rb'
40
- test.verbose = true
41
- test.rcov_opts << '--exclude "gems/*"'
37
+ test.pattern = 'test/**/*_test.rb'
38
+ test.verbose = false
42
39
  end
43
40
 
44
41
  task :default => :test
@@ -4,61 +4,119 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{cloudfront}
8
- s.version = "0.0.1"
7
+ s.name = "cloudfront"
8
+ s.version = "1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = [%q{Belkacem REBBOUH}]
12
- s.date = %q{2012-05-27}
13
- s.description = %q{longer description of your gem}
14
- s.email = %q{belkacem@rebbouh.com}
11
+ s.authors = ["Belkacem REBBOUH"]
12
+ s.date = "2013-05-09"
13
+ s.description = "A tested Cloudfront\u{2122} client, that handles all api actions on distributions and invalidations"
14
+ s.email = "belkacem@rebbouh.com"
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE.txt",
17
17
  "README.rdoc"
18
18
  ]
19
19
  s.files = [
20
- ".document",
21
20
  "Gemfile",
22
- "Gemfile.lock",
23
21
  "LICENSE.txt",
24
22
  "README.rdoc",
25
23
  "Rakefile",
26
24
  "cloudfront.gemspec",
27
25
  "lib/cloudfront.rb",
28
- "test/helper.rb",
29
- "test/test_cloudfront.rb"
26
+ "lib/cloudfront/connection.rb",
27
+ "lib/cloudfront/distribution/distribution.rb",
28
+ "lib/cloudfront/distribution/download_distribution.rb",
29
+ "lib/cloudfront/distribution/streaming_distribution.rb",
30
+ "lib/cloudfront/errors/cloudfront_error.rb",
31
+ "lib/cloudfront/errors/cname_already_exists_error.rb",
32
+ "lib/cloudfront/errors/distribution_already_exists_error.rb",
33
+ "lib/cloudfront/errors/illegal_update_error.rb",
34
+ "lib/cloudfront/errors/invalid_origin_access_identity_error.rb",
35
+ "lib/cloudfront/errors/invalid_origin_error.rb",
36
+ "lib/cloudfront/errors/invalid_required_protocol_error.rb",
37
+ "lib/cloudfront/errors/missing_body_error.rb",
38
+ "lib/cloudfront/errors/precondition_failed_error.rb",
39
+ "lib/cloudfront/errors/too_many_distribution_cnames_error.rb",
40
+ "lib/cloudfront/errors/too_many_distributions_error.rb",
41
+ "lib/cloudfront/errors/too_many_trusted_signers_error.rb",
42
+ "lib/cloudfront/errors/trusted_signer_does_not_exist_error.rb",
43
+ "lib/cloudfront/exceptions/delete_enabled_distribution_exception.rb",
44
+ "lib/cloudfront/exceptions/distribution_already_disabled_exception.rb",
45
+ "lib/cloudfront/exceptions/distribution_already_enabled_exception.rb",
46
+ "lib/cloudfront/exceptions/distribution_configuration_exception.rb",
47
+ "lib/cloudfront/exceptions/missing_etag_exception.rb",
48
+ "lib/cloudfront/helpers/aliases.rb",
49
+ "lib/cloudfront/helpers/cache_behavior.rb",
50
+ "lib/cloudfront/helpers/cache_behaviors.rb",
51
+ "lib/cloudfront/helpers/download_distribution.rb",
52
+ "lib/cloudfront/helpers/invalidation.rb",
53
+ "lib/cloudfront/helpers/logging.rb",
54
+ "lib/cloudfront/helpers/origin.rb",
55
+ "lib/cloudfront/helpers/origin_access_identity.rb",
56
+ "lib/cloudfront/helpers/origins.rb",
57
+ "lib/cloudfront/helpers/s3_origin.rb",
58
+ "lib/cloudfront/helpers/streaming_distribution.rb",
59
+ "lib/cloudfront/helpers/trusted_signers.rb",
60
+ "lib/cloudfront/invalidation/invalidations.rb",
61
+ "lib/cloudfront/origin_access_identity/origin_access_identity.rb",
62
+ "lib/cloudfront/utils/api.rb",
63
+ "lib/cloudfront/utils/array.rb",
64
+ "lib/cloudfront/utils/configuration_checker.rb",
65
+ "lib/cloudfront/utils/string.rb",
66
+ "lib/cloudfront/utils/util.rb",
67
+ "lib/cloudfront/utils/xml_serializer.rb",
68
+ "lib/faraday/request/cloudfront_signer.rb",
69
+ "lib/faraday/request/xml_content_type.rb"
30
70
  ]
31
- s.homepage = %q{http://github.com/Belkacem/cloudfront}
32
- s.licenses = [%q{MIT}]
33
- s.require_paths = [%q{lib}]
34
- s.rubygems_version = %q{1.8.8}
35
- s.summary = %q{one-line summary of your gem}
71
+ s.homepage = "https://github.com/Belkacem/cloudfront"
72
+ s.licenses = ["MIT"]
73
+ s.require_paths = ["lib"]
74
+ s.rubygems_version = "1.8.24"
75
+ s.summary = "A tested Cloudfront\u{2122} gem"
36
76
 
37
77
  if s.respond_to? :specification_version then
38
78
  s.specification_version = 3
39
79
 
40
80
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
81
+ s.add_runtime_dependency(%q<faraday>, [">= 0"])
82
+ s.add_runtime_dependency(%q<faraday_middleware>, [">= 0"])
83
+ s.add_runtime_dependency(%q<multi_xml>, [">= 0"])
84
+ s.add_runtime_dependency(%q<nokogiri>, [">= 0"])
41
85
  s.add_development_dependency(%q<minitest>, [">= 0"])
42
- s.add_development_dependency(%q<yard>, ["~> 0.7"])
43
- s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
86
+ s.add_development_dependency(%q<yard>, ["~> 0.6.0"])
44
87
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
45
- s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
46
- s.add_development_dependency(%q<rcov>, [">= 0"])
88
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
89
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
90
+ s.add_development_dependency(%q<vcr>, [">= 0"])
91
+ s.add_development_dependency(%q<webmock>, [">= 0"])
92
+ s.add_development_dependency(%q<timecop>, [">= 0"])
47
93
  else
94
+ s.add_dependency(%q<faraday>, [">= 0"])
95
+ s.add_dependency(%q<faraday_middleware>, [">= 0"])
96
+ s.add_dependency(%q<multi_xml>, [">= 0"])
97
+ s.add_dependency(%q<nokogiri>, [">= 0"])
48
98
  s.add_dependency(%q<minitest>, [">= 0"])
49
- s.add_dependency(%q<yard>, ["~> 0.7"])
50
- s.add_dependency(%q<rdoc>, ["~> 3.12"])
99
+ s.add_dependency(%q<yard>, ["~> 0.6.0"])
51
100
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
52
- s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
53
- s.add_dependency(%q<rcov>, [">= 0"])
101
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
102
+ s.add_dependency(%q<simplecov>, [">= 0"])
103
+ s.add_dependency(%q<vcr>, [">= 0"])
104
+ s.add_dependency(%q<webmock>, [">= 0"])
105
+ s.add_dependency(%q<timecop>, [">= 0"])
54
106
  end
55
107
  else
108
+ s.add_dependency(%q<faraday>, [">= 0"])
109
+ s.add_dependency(%q<faraday_middleware>, [">= 0"])
110
+ s.add_dependency(%q<multi_xml>, [">= 0"])
111
+ s.add_dependency(%q<nokogiri>, [">= 0"])
56
112
  s.add_dependency(%q<minitest>, [">= 0"])
57
- s.add_dependency(%q<yard>, ["~> 0.7"])
58
- s.add_dependency(%q<rdoc>, ["~> 3.12"])
113
+ s.add_dependency(%q<yard>, ["~> 0.6.0"])
59
114
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
60
- s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
61
- s.add_dependency(%q<rcov>, [">= 0"])
115
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
116
+ s.add_dependency(%q<simplecov>, [">= 0"])
117
+ s.add_dependency(%q<vcr>, [">= 0"])
118
+ s.add_dependency(%q<webmock>, [">= 0"])
119
+ s.add_dependency(%q<timecop>, [">= 0"])
62
120
  end
63
121
  end
64
122
 
@@ -0,0 +1,39 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'cloudfront/connection'
4
+
5
+ require 'cloudfront/utils/util'
6
+ require 'cloudfront/utils/api'
7
+ require 'cloudfront/utils/configuration_checker'
8
+ require 'cloudfront/utils/xml_serializer'
9
+
10
+ require 'cloudfront/distribution/download_distribution'
11
+ require 'cloudfront/distribution/streaming_distribution'
12
+
13
+ require 'cloudfront/origin_access_identity/origin_access_identity'
14
+ require 'cloudfront/invalidation/invalidations'
15
+
16
+ # Errors and exceptions
17
+ require 'cloudfront/exceptions/distribution_configuration_exception'
18
+
19
+ class Cloudfront
20
+ include Cloudfront::Connection
21
+ include Cloudfront::Distribution::DownloadDistribution
22
+ include Cloudfront::Distribution::StreamingDistribution
23
+ include Cloudfront::Invalidation::Invalidations
24
+ include Cloudfront::OriginAccessIdentity
25
+
26
+ # @attr_reader [Faraday::Connection] connection accession to the connection established with cloudfront.
27
+ attr_accessor :connection
28
+ def initialize(key_id, key_secret)
29
+ @connection = build_connection(key_id, key_secret)
30
+ end
31
+
32
+ def credentials_test
33
+ distribution_list.status == 200
34
+ end
35
+ end
36
+
37
+ # For easier handling of helpers
38
+ include Cloudfront::Helpers
39
+
@@ -0,0 +1,25 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'faraday'
4
+ require 'faraday/response/parse_xml'
5
+ require 'faraday/request/cloudfront_signer'
6
+ require 'faraday/request/xml_content_type.rb'
7
+
8
+ class Cloudfront
9
+ module Connection
10
+ HOST = 'https://cloudfront.amazonaws.com'
11
+
12
+ def build_connection(key_id, key_secret)
13
+ Faraday::Connection.new(HOST) do |builder|
14
+ builder.use Faraday::Request::CloudfrontSigner, key_id, key_secret
15
+ builder.use Faraday::Request::XmlContentType
16
+ builder.use Faraday::Response::ParseXml
17
+
18
+ # Used for debug purpose only.
19
+ # builder.use Faraday::Response::Logger
20
+
21
+ builder.use Faraday::Adapter::NetHttp
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,136 @@
1
+ # encoding: UTF-8
2
+
3
+ class Cloudfront
4
+ module Distribution
5
+ module Distribution
6
+ private
7
+ # Creates a distribution.
8
+ # @param distribution [Cloudfront::Helpers::Distribution] a distribution configuration wrapper
9
+ # @param url [String] the resource url
10
+ # @return [Faraday::Response] the response from cloudfront
11
+ def distribution_create(url, distribution)
12
+ connection.post do |request|
13
+ request.url url
14
+ request.body = distribution.to_xml
15
+ end
16
+ end
17
+
18
+ # Lists all the distributions.
19
+ # @param url [String] the resource url
20
+ # @param max_items [int] the max items to be retrieved
21
+ # @param marker [String] The distribution id from which begins the list.
22
+ # @return [Faraday::Response] the response from cloudfront
23
+ def distribution_list(url, max_items = 0, marker = "")
24
+ connection.get do |request|
25
+ request.url url
26
+ request.params['Marker'] = marker unless marker.nil? || marker.blank?
27
+ request.params['MaxItems'] = max_items if max_items > 0
28
+ end
29
+ end
30
+
31
+ # Gets the distribution information.
32
+ # @param url [String] the resource url
33
+ # @param distribution_id [String] The id of the distribution to be returned.
34
+ # @return [Faraday::Response] the response from cloudfront
35
+ def distribution_get(url, distribution_id)
36
+ connection.get url + '/' + distribution_id
37
+ end
38
+
39
+ # Returns the distribution configuration.
40
+ # @param url [String] the resource url
41
+ # @param distribution_id [String] The id of the distribution to be returned.
42
+ # @return [Faraday::Response] the response from cloudfront
43
+ def distribution_get_config(url, distribution_id)
44
+ connection.get url + '/' + distribution_id + '/config'
45
+ end
46
+
47
+ # Puts the distribution configuration.
48
+ # To put a distribution config we must follow a certain process defined in
49
+ # http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/PutConfig.html
50
+ # and
51
+ # http://docs.amazonwebservices.com/AmazonCloudFront/latest/DeveloperGuide//HowToUpdateDistribution.html
52
+ # @param url [String] the resource url
53
+ # @param distribution_id [String] The id of the distribution.
54
+ # @param distribution [Cloudfront::Helpers::Distribution] a distribution configuration wrapper
55
+ # @param etag [String] The etag.
56
+ # @return [Faraday::Response] the response from cloudfront
57
+ def distribution_put_config(url, distribution_id, distribution, etag)
58
+ connection.put do |request|
59
+ request.url url + '/' + distribution_id + '/config'
60
+ request.headers['If-Match'] = etag
61
+ request.body = distribution.to_xml
62
+ end
63
+ end
64
+
65
+ # Enables a distribution.
66
+ # @param url [String] the resource url
67
+ # @param distribution_id [String] The id of the distribution to be enabled.
68
+ # @return [Faraday::Response] the response from cloudfront
69
+ def distribution_enable(url, distribution_id)
70
+ # 1. get the distribution configuration
71
+ response = distribution_get_config(url, distribution_id)
72
+ etag = response.headers['etag']
73
+ raise Cloudfront::Exceptions::MissingEtagException.new("The etag for the distribution #{distribution_id} is missing") if etag.nil? || etag.blank?
74
+
75
+ distribution = get_distribution_wrapper(url, response.body)
76
+ raise Cloudfront::Exceptions::DistributionAlreadyEnabledException.new("The distribution #{distribution_id} is already enabled!") if distribution.enabled
77
+
78
+ # 2. change the Enabled state
79
+ distribution.enabled = true
80
+
81
+ # 3. send a put configuration request with the returned etag
82
+ distribution_put_config(url, distribution_id, distribution, etag)
83
+ end
84
+
85
+ # Disables a distribution.
86
+ # @param url [String] the resource url
87
+ # @param distribution_id [String] The id of the distribution to be disabled.
88
+ # @return [Faraday::Response] the response from cloudfront
89
+ def distribution_disable(url, distribution_id)
90
+ # 1. get the distribution configuration
91
+ response = distribution_get_config(url, distribution_id)
92
+ etag = response.headers['etag']
93
+ raise Cloudfront::Exceptions::MissingEtagException.new("The etag for the distribution #{distribution_id} is missing") if etag.nil? || etag.blank?
94
+
95
+ distribution = get_distribution_wrapper(url, response.body)
96
+ raise Cloudfront::Exceptions::DistributionAlreadyDisabledException.new("The distribution #{distribution_id} is already disabled") unless distribution.enabled
97
+
98
+ # 2. change the enabled to false
99
+ distribution.enabled = false
100
+ # 2. send a put configuration request with the returned etag
101
+ distribution_put_config(url, distribution_id, distribution, etag)
102
+ end
103
+
104
+ # Deletes a distribution.
105
+ # @param url [String] the resource url
106
+ # @param distribution_id [String] The id of the distribution to be deleted.
107
+ # @return [Faraday::Response] the response from cloudfront
108
+ def distribution_delete(url, distribution_id)
109
+ # 1. get the distribution configuration
110
+ response = distribution_get_config(url, distribution_id)
111
+ etag = response.headers['etag']
112
+ raise Cloudfront::Exceptions::MissingEtagException.new("The etag for the distribution #{distribution_id} is missing") if etag.nil? || etag.blank?
113
+
114
+ distribution = get_distribution_wrapper(url, response.body)
115
+ raise Cloudfront::Exceptions::DeleteEnabledDistributionException.new("The distribution #{distribution_id} can't be deleted, disabled it first!") if distribution.enabled
116
+
117
+ # 2. send a delete request with the returned etag
118
+ connection.delete do |request|
119
+ request.url url + '/' + distribution_id
120
+ request.headers['If-Match'] = etag
121
+ end
122
+ end
123
+
124
+ def get_distribution_wrapper(url, hash)
125
+ if (url.include? "streaming")
126
+ get_streaming_distribution_wrapper(hash)
127
+ else
128
+ get_download_distribution_wrapper(hash)
129
+ end
130
+ end
131
+
132
+ end
133
+ end
134
+ end
135
+
136
+