File: post_invalidation.rb

package info (click to toggle)
ruby-fog-aws 3.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,816 kB
  • sloc: ruby: 68,587; makefile: 6
file content (75 lines) | stat: -rw-r--r-- 2,909 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
module Fog
  module AWS
    class CDN
      class Real
        require 'fog/aws/parsers/cdn/post_invalidation'

        # List information about distributions in CloudFront.
        #
        # @param distribution_id [String] Id of distribution for invalidations.
        # @param paths [Array] Array of string paths to objects to invalidate.
        # @param caller_reference [String] Used to prevent replay, defaults to Time.now.to_i.to_s.
        #
        # @return [Excon::Response]
        #   * body [Hash]:
        #     * Id [String] - Id of invalidation.
        #     * Status [String] - Status of invalidation.
        #     * CreateTime [Integer] - Time of invalidation creation.
        #     * InvalidationBatch [Array]:
        #       * Path [Array] - Array of strings of objects to invalidate.
        #       * CallerReference [String] - Used to prevent replay, defaults to Time.now.to_i.to_s.
        #
        # @see http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/CreateInvalidation.html

        def post_invalidation(distribution_id, paths, caller_reference = Time.now.to_i.to_s)
          body = '<?xml version="1.0" encoding="UTF-8"?>'
          body << "<InvalidationBatch>"
          for path in [*paths]
            body << "<Path>" << path << "</Path>"
          end
          body << "<CallerReference>" << caller_reference << "</CallerReference>"
          body << "</InvalidationBatch>"
          request({
            :body       => body,
            :expects    => 201,
            :headers    => {'Content-Type' => 'text/xml'},
            :idempotent => true,
            :method     => 'POST',
            :parser     => Fog::Parsers::AWS::CDN::PostInvalidation.new,
            :path       => "/distribution/#{distribution_id}/invalidation"
          })
        end
      end

      class Mock
        def post_invalidation(distribution_id, paths, caller_reference = Time.now.to_i.to_s)
          distribution = self.data[:distributions][distribution_id]
          if distribution
            invalidation_id = Fog::AWS::CDN::Mock.distribution_id
            invalidation = {
              'Id' => invalidation_id,
              'Status' => 'InProgress',
              'CreateTime' => Time.now.utc.iso8601,
              'InvalidationBatch' => {
                'CallerReference' => caller_reference,
                'Path' => paths
              }
            }

            distribution['InProgressInvalidationBatches'] += 1

            self.data[:invalidations][distribution_id] ||= {}
            self.data[:invalidations][distribution_id][invalidation_id] = invalidation

            response = Excon::Response.new
            response.status = 201
            response.body = invalidation
            response
          else
            Fog::AWS::CDN::Mock.error(:no_such_distribution)
          end
        end
      end
    end
  end
end