Class: Fog::CDN::HP::Mock

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/fog/hp/requests/cdn/put_container.rb,
lib/fog/hp/cdn.rb,
lib/fog/hp/requests/cdn/get_containers.rb,
lib/fog/hp/requests/cdn/head_container.rb,
lib/fog/hp/requests/cdn/post_container.rb,
lib/fog/hp/requests/cdn/delete_container.rb

Overview

:nodoc:all

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fog/hp/cdn.rb', line 42

def initialize(options={})
  # deprecate hp_account_id
  if options[:hp_account_id]
    Fog::Logger.deprecation(":hp_account_id is deprecated, please use :hp_access_key instead.")
    @hp_access_key = options.delete(:hp_account_id)
  end
  @hp_access_key = options[:hp_access_key]
  unless @hp_access_key
    raise ArgumentError.new("Missing required arguments: hp_access_key. :hp_account_id is deprecated, please use :hp_access_key instead.")
  end
end

Class Method Details

.dataObject



30
31
32
33
34
35
36
# File 'lib/fog/hp/cdn.rb', line 30

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

.resetObject



38
39
40
# File 'lib/fog/hp/cdn.rb', line 38

def self.reset
  @data = nil
end

Instance Method Details

#dataObject



54
55
56
# File 'lib/fog/hp/cdn.rb', line 54

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

#delete_container(name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fog/hp/requests/cdn/delete_container.rb', line 21

def delete_container(name)
  response = Excon::Response.new
  if self.data[:cdn_containers][name]
    self.data[:cdn_containers].delete(name)
    response.status = 204
    response.body = ""
    response
  else
    raise Fog::CDN::HP::NotFound
  end
end

#get_containers(options = {}) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/fog/hp/requests/cdn/get_containers.rb', line 29

def get_containers(options = {})
  response = Excon::Response.new
  data = self.data[:cdn_containers].map {|_,v| v}
  response.body = data
  response.status = 200
  response
end

#head_container(name) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fog/hp/requests/cdn/head_container.rb', line 29

def head_container(name)
  response = Excon::Response.new
  headers = {}
  if data = self.data[:cdn_containers][name]
    data.each do |k,_|
      headers[k] = data[k] if data[k]
    end
    response.headers = headers
    response.status = 204
    response.body = ""
    response
  else
    raise Fog::CDN::HP::NotFound
  end
end

#post_container(name, options = {}) ⇒ Object



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

def post_container(name, options = {})
  response = Excon::Response.new
  container_id = Fog::Mock.random_hex(33)
  if data = self.data[:cdn_containers][name]
    options.each do |k,v|
      data[k] = options[k] if options[k]
    end
    response.headers = {
      "X-Cdn-Ssl-Uri" => "https://a111.cdn.net/cdn-test.net/#{container_id}/abc",
      "X-Cdn-Uri"     => "http://#{container_id}.cdn-test.net",
      "X-Trans-Id"    => Fog::Mock.random_hex(34)
    }
    response.status = 202
    response.body = "202 Accepted\n\nThe request is accepted for processing.\n\n   "
    self.data[:cdn_containers][name] = data
    response
  else
    raise Fog::CDN::HP::NotFound
  end
end

#put_container(name, options = {}) ⇒ Object



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

def put_container(name, options = {})
  response = Excon::Response.new
  container_id = Fog::Mock.random_hex(33)
  data = {
    'x-cdn-ssl-uri' => "https://a111.cdn.net/cdn-test.net/#{container_id}/abc",
    'cdn_enabled'   => true,
    'name'          => name,
    'x-cdn-uri'     => "http://#{container_id}.cdn-test.net",
    'ttl'           => 86400,
    'log_retention' => false
  }
  response.headers = {
    "X-Cdn-Ssl-Uri" => "https://a111.cdn.net/cdn-test.net/#{container_id}/abc",
    "X-Cdn-Uri"     => "http://#{container_id}.cdn-test.net",
    "X-Trans-Id"    => Fog::Mock.random_hex(34)
  }
  response.status = 201
  response.body = "201 Created\n\n\n\n   "
  self.data[:cdn_containers][name] = data
  response
end

#reset_dataObject



58
59
60
# File 'lib/fog/hp/cdn.rb', line 58

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