Class: Fog::CDN::AWS::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/aws/cdn.rb,
lib/fog/aws/requests/cdn/get_distribution.rb,
lib/fog/aws/requests/cdn/get_invalidation.rb,
lib/fog/aws/requests/cdn/post_distribution.rb,
lib/fog/aws/requests/cdn/post_invalidation.rb,
lib/fog/aws/requests/cdn/delete_distribution.rb,
lib/fog/aws/requests/cdn/get_distribution_list.rb,
lib/fog/aws/requests/cdn/get_invalidation_list.rb,
lib/fog/aws/requests/cdn/put_distribution_config.rb,
lib/fog/aws/requests/cdn/get_streaming_distribution.rb,
lib/fog/aws/requests/cdn/post_streaming_distribution.rb,
lib/fog/aws/requests/cdn/delete_streaming_distribution.rb,
lib/fog/aws/requests/cdn/get_streaming_distribution_list.rb,
lib/fog/aws/requests/cdn/put_streaming_distribution_config.rb

Constant Summary collapse

CDN_ERRORS =
{
  :access_denies => {:code => 'AccessDenied',:msg  => 'Access denied.',:status => 403},
  :inappropriate_xml => {:code => 'InappropriateXML',:msg  => 'The XML document you provided was well-formed and valid, but not appropriate for this operation.',:status => 400},
  :internal_error => {:code => 'InternalError',:msg  => 'We encountered an internal error. Please try again.',:status => 500},
  :invalid_action => {:code => 'InvalidAction',:msg  => 'The action specified is not valid.',:status => 400},
  :invalid_argument => {:code => 'InvalidArgument',:msg  => '%s', :status => 400},
  :not_implemented => {:code => 'NotImplemented', :msg  => 'Not implemented.',:status => 501},
  :no_such_distribution => { :code => 'NoSuchDistribution', :msg => 'The specified distribution does not exist', :status => 404 },
  :no_such_streaming_distribution => { :code => 'NoSuchStreamingDistribution', :msg => 'The specified streaming distribution does not exist', :status => 404 },
  :no_such_invalidation => { :code => 'NoSuchInvalidation', :msg => 'The specified invalidation does not exist', :status => 404 },
  :cname_exists => { :code => 'CNAMEAlreadyExists', :msg => 'One or more of the CNAMEs you provided are already associated with a different distribution', :status => 409 },
  :illegal_update => { :code => 'IllegalUpdate', :msg => 'Origin and CallerReference cannot be updated.', :status => 400 },
  :invalid_if_match_version => { :code => 'InvalidIfMatchVersion', :msg => 'The If-Match version is missing or not valid for the distribution.', :status => 400},
  :distribution_not_disabled => { :code => 'DistributionNotDisabled', :msg => 'The distribution you are trying to delete has not been disabled.', :status => 409 },

}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



47
48
49
50
# File 'lib/fog/aws/cdn.rb', line 47

def initialize(options={})
  @use_iam_profile = options[:use_iam_profile]
  setup_credentials(options)
end

Class Method Details

.dataObject



33
34
35
36
37
38
39
40
41
# File 'lib/fog/aws/cdn.rb', line 33

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] =  {
        :distributions => {},
        :streaming_distributions => {},
        :invalidations => {}
      }
  end
end

.distribution_idObject



68
69
70
# File 'lib/fog/aws/cdn.rb', line 68

def self.distribution_id
  random_id(14)
end

.domain_nameObject



76
77
78
# File 'lib/fog/aws/cdn.rb', line 76

def self.domain_name
  "#{random_id(12).downcase}.cloudfront.net"
end

.error(code, argument = '') ⇒ Object



101
102
103
104
105
# File 'lib/fog/aws/cdn.rb', line 101

def self.error(code, argument = '')
  if error = CDN_ERRORS[code]
    raise_error(error[:status], error[:code], error[:msg] % argument)
  end
end

.generic_idObject



72
73
74
# File 'lib/fog/aws/cdn.rb', line 72

def self.generic_id
  random_id(14)
end

.raise_error(status, code, message = '') ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/fog/aws/cdn.rb', line 107

def self.raise_error(status, code, message='')
  response = Excon::Response.new
  response.status = status
  response.body = <<EOF
<ErrorResponse xmlns="http://cloudfront.amazonaws.com/doc/2010-11-01/">
   <Error>
      <Type>Sender</Type>
      <Code>#{code}</Code>
      <Message>#{message}.</Message>
   </Error>
   <RequestId>#{Fog::AWS::Mock.request_id}</RequestId>
</ErrorResponse>
EOF

  raise(Excon::Errors.status_error({:expects => 201}, response))
end

.random_id(length) ⇒ Object



80
81
82
# File 'lib/fog/aws/cdn.rb', line 80

def self.random_id(length)
  Fog::Mock.random_selection("abcdefghijklmnopqrstuvwxyz0123456789", length).upcase
end

.resetObject



43
44
45
# File 'lib/fog/aws/cdn.rb', line 43

def self.reset
  @data = nil
end

Instance Method Details

#dataObject



52
53
54
# File 'lib/fog/aws/cdn.rb', line 52

def data
  self.class.data[@aws_access_key_id]
end

#delete_distribution(distribution_id, etag) ⇒ Object



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
# File 'lib/fog/aws/requests/cdn/delete_distribution.rb', line 24

def delete_distribution(distribution_id, etag)
  distribution = self.data[:distributions][distribution_id]

  if distribution
    if distribution['ETag'] != etag
      Fog::CDN::AWS::Mock.error(:invalid_if_match_version)
    end
    unless distribution['DistributionConfig']['CallerReference']
      Fog::CDN::AWS::Mock.error(:illegal_update)
    end
    if distribution['DistributionConfig']['Enabled']
      Fog::CDN::AWS::Mock.error(:distribution_not_disabled)
    end

    self.data[:distributions].delete(distribution_id)
    self.data[:invalidations].delete(distribution_id)

    response = Excon::Response.new
    response.status = 204
    response.body = "x-amz-request-id: #{Fog::AWS::Mock.request_id}"
    response
  else
    Fog::CDN::AWS::Mock.error(:no_such_distribution)
  end
end

#delete_streaming_distribution(distribution_id, etag) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fog/aws/requests/cdn/delete_streaming_distribution.rb', line 24

def delete_streaming_distribution(distribution_id, etag)
  distribution = self.data[:streaming_distributions][distribution_id]

  if distribution
    if distribution['ETag'] != etag
      Fog::CDN::AWS::Mock.error(:invalid_if_match_version)
    end
    unless distribution['StreamingDistributionConfig']['CallerReference']
      Fog::CDN::AWS::Mock.error(:illegal_update)
    end
    if distribution['StreamingDistributionConfig']['Enabled']
      Fog::CDN::AWS::Mock.error(:distribution_not_disabled)
    end

    self.data[:streaming_distributions].delete(distribution_id)

    response = Excon::Response.new
    response.status = 204
    response.body = "x-amz-request-id: #{Fog::AWS::Mock.request_id}"
    response
  else
    Fog::CDN::AWS::Mock.error(:no_such_streaming_distribution)
  end
end

#get_distribution(distribution_id) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fog/aws/requests/cdn/get_distribution.rb', line 52

def get_distribution(distribution_id)
  response = Excon::Response.new

  distribution = self.data[:distributions][distribution_id]
  unless distribution
    Fog::CDN::AWS::Mock.error(:no_such_distribution)
  end

  if distribution['Status'] == 'InProgress' && (Time.now - Time.parse(distribution['LastModifiedTime']) >= Fog::Mock.delay * 2)
    distribution['Status'] = 'Deployed'
  end

  etag = Fog::CDN::AWS::Mock.generic_id
  response.status = 200
  response.body = {
    'InProgressInvalidationBatches' => 0,
  }.merge(distribution.reject { |k,v| k == 'ETag' })

  response.headers['ETag'] = etag
  distribution['ETag'] = etag

  response
end

#get_distribution_list(options = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fog/aws/requests/cdn/get_distribution_list.rb', line 53

def get_distribution_list(options = {})
  response = Excon::Response.new
  response.status = 200

  distributions = self.data[:distributions].values

  response.body = {
    'Marker' => Fog::Mock.random_hex(16),
    'IsTruncated' => false,
    'MaxItems' => 100,
    'DistributionSummary' => distributions.map { |d| to_distribution_summary(d) }
  }

  response
end

#get_invalidation(distribution_id, invalidation_id) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fog/aws/requests/cdn/get_invalidation.rb', line 34

def get_invalidation(distribution_id, invalidation_id)
  distribution = self.data[:distributions][distribution_id]
  unless distribution
    Fog::CDN::AWS::Mock.error(:no_such_distribution)
  end

  invalidation = self.data[:invalidations][distribution_id][invalidation_id]
  unless invalidation
    Fog::CDN::AWS::Mock.error(:no_such_invalidation)
  end

  if invalidation['Status'] == 'InProgress' && (Time.now - Time.parse(invalidation['CreateTime']) >= Fog::Mock.delay * 2)
    invalidation['Status'] = 'Completed'
    distribution['InProgressInvalidationBatches'] -= 1
  end

  response = Excon::Response.new
  response.status = 200
  response.body = invalidation
  response
end

#get_invalidation_list(distribution_id, options = {}) ⇒ Object



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
# File 'lib/fog/aws/requests/cdn/get_invalidation_list.rb', line 38

def get_invalidation_list(distribution_id, options = {})
  distribution = self.data[:distributions][distribution_id]
  unless distribution
    Fog::CDN::AWS::Mock.error(:no_such_distribution)
  end

  invalidations = (self.data[:invalidations][distribution_id] || {}).values

  invalidations.each do |invalidation|
    if invalidation['Status'] == 'InProgress' && (Time.now - Time.parse(invalidation['CreateTime']) >= Fog::Mock.delay * 2)
      invalidation['Status'] = 'Completed'
      distribution['InProgressInvalidationBatches'] -= 1
    end
  end

  response = Excon::Response.new
  response.status = 200

  response.body = {
    'Marker' => Fog::Mock.random_hex(16),
    'IsTruncated' => false,
    'MaxItems' => 100,
    'InvalidationSummary' => invalidations.map { |i| to_invalidation_summary(i) }
  }
  response
end

#get_streaming_distribution(distribution_id) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fog/aws/requests/cdn/get_streaming_distribution.rb', line 45

def get_streaming_distribution(distribution_id)
  response = Excon::Response.new

  distribution = self.data[:streaming_distributions][distribution_id]
  unless distribution
    Fog::CDN::AWS::Mock.error(:no_such_streaming_distribution)
  end

  if distribution['Status'] == 'InProgress' && (Time.now - Time.parse(distribution['LastModifiedTime']) >= Fog::Mock.delay * 2)
    distribution['Status'] = 'Deployed'
  end

  etag = Fog::CDN::AWS::Mock.generic_id
  response.status = 200
  response.body = distribution.reject { |k,v| k == 'ETag' }

  response.headers['ETag'] = etag
  distribution['ETag'] = etag

  response
end

#get_streaming_distribution_list(options = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fog/aws/requests/cdn/get_streaming_distribution_list.rb', line 53

def get_streaming_distribution_list(options = {})
  response = Excon::Response.new
  response.status = 200

  distributions = self.data[:streaming_distributions].values

  response.body = {
    'Marker' => Fog::Mock.random_hex(16),
    'IsTruncated' => false,
    'MaxItems' => 100,
    'StreamingDistributionSummary' => distributions.map { |d| to_streaming_distribution_summary(d) }
  }

  response
end

#post_distribution(options = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fog/aws/requests/cdn/post_distribution.rb', line 89

def post_distribution(options = {})
  if self.data[:distributions].values.any? { |d| (d['CNAME'] & (options['CNAME']||[])).empty? }
    Fog::CDN::AWS::Mock.error(:invalid_argument, 'CNAME is already in use')
  end

  response = Excon::Response.new

  response.status = 201
  options['CallerReference'] = Time.now.to_i.to_s

  dist_id = Fog::CDN::AWS::Mock.distribution_id

  distribution = {
    'DomainName' => Fog::CDN::AWS::Mock.domain_name,
    'Id' => dist_id,
    'Status' => 'InProgress',
    'LastModifiedTime' => Time.now.utc.iso8601,
    'InProgressInvalidationBatches' => 0,
    'DistributionConfig' => {
      'CallerReference' => options['CallerReference'],
      'CNAME' => options['CNAME'] || [],
      'Comment' => options['Comment'],
      'Enabled' => options['Enabled'],
      'Logging' => {
        'Bucket' => options['Bucket'],
        'Prefix' => options['Prefix']
      },
      'S3Origin' => options['S3Origin'],
      'CustomOrigin' => options['CustomOrigin'],
      'TrustedSigners' => options['TrustedSigners'] || []
    }
  }

  self.data[:distributions][dist_id] = distribution

  response.body = distribution
  response
end

#post_invalidation(distribution_id, paths, caller_reference = Time.now.to_i.to_s) ⇒ Object



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
# File 'lib/fog/aws/requests/cdn/post_invalidation.rb', line 45

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::CDN::AWS::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::CDN::AWS::Mock.error(:no_such_distribution)
  end
end

#post_streaming_distribution(options = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/fog/aws/requests/cdn/post_streaming_distribution.rb', line 76

def post_streaming_distribution(options = {})
  if self.data[:streaming_distributions].values.any? { |d| (d['CNAME'] & (options['CNAME']||[])).empty? }
    Fog::CDN::AWS::Mock.error(:invalid_argument, 'CNAME is already in use')
  end

  response = Excon::Response.new

  response.status = 201
  options['CallerReference'] = Time.now.to_i.to_s

  dist_id = Fog::CDN::AWS::Mock.distribution_id

  distribution = {
    'DomainName' => Fog::CDN::AWS::Mock.domain_name,
    'Id' => dist_id,
    'Status' => 'InProgress',
    'LastModifiedTime' => Time.now.utc.iso8601,
    'StreamingDistributionConfig' => {
      'CallerReference' => options['CallerReference'],
      'CNAME' => options['CNAME'] || [],
      'Comment' => options['Comment'],
      'Enabled' => options['Enabled'],
      'Logging' => {
        'Bucket' => options['Bucket'],
        'Prefix' => options['Prefix']
      },
      'S3Origin' => options['S3Origin'],
      'TrustedSigners' => options['TrustedSigners'] || []
    }
  }

  self.data[:streaming_distributions][dist_id] = distribution

  response.body = distribution
  response
end

#put_distribution_config(distribution_id, etag, options = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fog/aws/requests/cdn/put_distribution_config.rb', line 90

def put_distribution_config(distribution_id, etag, options = {})
  distribution = self.data[:distributions][distribution_id]

  if distribution
    if distribution['ETag'] != etag
      Fog::CDN::AWS::Mock.error(:invalid_if_match_version)
    end
    unless distribution['DistributionConfig']['CallerReference']
      Fog::CDN::AWS::Mock.error(:illegal_update)
    end

    distribution['DistributionConfig'].merge!(options)
    distribution['Status'] = 'InProgress'

    response = Excon::Response.new
    response.status = 200
    response.headers['ETag'] = Fog::CDN::AWS::Mock.generic_id
    response.body = distribution.merge({ 'LastModifiedTime' => Time.now.utc.iso8601 }).reject{ |k,v| k == 'ETag' }
    response
  else
    Fog::CDN::AWS::Mock.error(:no_such_distribution)
  end
end

#put_streaming_distribution_config(distribution_id, etag, options = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/fog/aws/requests/cdn/put_streaming_distribution_config.rb', line 79

def put_streaming_distribution_config(distribution_id, etag, options = {})
  distribution = self.data[:streaming_distributions][distribution_id]

  if distribution
    if distribution['ETag'] != etag
      Fog::CDN::AWS::Mock.error(:invalid_if_match_version)
    end
    unless distribution['StreamingDistributionConfig']['CallerReference']
      Fog::CDN::AWS::Mock.error(:illegal_update)
    end

    distribution['StreamingDistributionConfig'].merge!(options)
    distribution['Status'] = 'InProgress'

    response = Excon::Response.new
    response.status = 200
    response.headers['ETag'] = Fog::CDN::AWS::Mock.generic_id
    response.body = distribution.merge({ 'LastModifiedTime' => Time.now.utc.iso8601 }).reject{ |k,v| k == 'ETag' }
    response
  else
    Fog::CDN::AWS::Mock.error(:no_such_streaming_distribution)
  end
end

#reset_dataObject



56
57
58
# File 'lib/fog/aws/cdn.rb', line 56

def reset_data
  self.class.data.delete(@aws_access_key_id)
end

#setup_credentials(options = {}) ⇒ Object



64
65
66
# File 'lib/fog/aws/cdn.rb', line 64

def setup_credentials(options={})
  @aws_access_key_id  = options[:aws_access_key_id]
end

#signature(params) ⇒ Object



60
61
62
# File 'lib/fog/aws/cdn.rb', line 60

def signature(params)
  "foo"
end