Class: Fog::CDN::Rackspace::Real

Inherits:
Rackspace::Service show all
Includes:
Base
Defined in:
lib/fog/rackspace/cdn.rb,
lib/fog/rackspace/requests/cdn/delete_object.rb,
lib/fog/rackspace/requests/cdn/put_container.rb,
lib/fog/rackspace/requests/cdn/get_containers.rb,
lib/fog/rackspace/requests/cdn/head_container.rb,
lib/fog/rackspace/requests/cdn/post_container.rb

Constant Summary

Constants included from Base

Base::URI_HEADERS

Instance Method Summary collapse

Methods included from Base

#endpoint_uri, #publish_container, #region, #service_name, #urls

Methods inherited from Rackspace::Service

#authenticate, #endpoint_uri, #region, #service_name

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/fog/rackspace/cdn.rb', line 103

def initialize(options={})
  @connection_options = options[:connection_options] || {}
  @rackspace_auth_url = options[:rackspace_auth_url]
  @rackspace_cdn_url = options[:rackspace_cdn_url]
  @rackspace_region = options[:rackspace_region] || :dfw
  authenticate(options)
  @enabled = false
  @persistent = options[:persistent] || false

  if endpoint_uri
    @connection = Fog::Connection.new(endpoint_uri.to_s, @persistent, @connection_options)
    @enabled = true
  end
end

Instance Method Details

#delete_object(container, object) ⇒ Object

Delete an existing object

Parameters

  • container<~String> - Name of container to delete

  • object<~String> - Name of object to delete



12
13
14
15
16
17
18
# File 'lib/fog/rackspace/requests/cdn/delete_object.rb', line 12

def delete_object(container, object)
  request(
    :expects  => 204,
    :method   => 'DELETE',
    :path     => "#{Fog::Rackspace.escape(container)}/#{Fog::Rackspace.escape(object)}"
  )
end

#enabled?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/fog/rackspace/cdn.rb', line 118

def enabled?
  @enabled
end

#get_containers(options = {}) ⇒ Object

List existing cdn-enabled storage containers

Parameters

  • options<~Hash>:

    • ‘enabled_only’<~Boolean> - Set to true to limit results to cdn enabled containers

    • ‘limit’<~Integer> - Upper limit to number of results returned

    • ‘marker’<~String> - Only return objects with name greater than this value

Returns

  • response<~Excon::Response>:

    • body<~Array>:

      • container<~String>: Name of container



18
19
20
21
22
23
24
25
26
# File 'lib/fog/rackspace/requests/cdn/get_containers.rb', line 18

def get_containers(options = {})
  response = request(
    :expects  => [200, 204],
    :method   => 'GET',
    :path     => '',
    :query    => {'format' => 'json'}.merge!(options)
  )
  response
end

#head_container(container) ⇒ Object

List cdn properties for a container

Parameters

  • container<~String> - Name of container to retrieve info for

Returns

  • response<~Excon::Response>:

    • headers<~Hash>:

      • ‘X-CDN-Enabled’<~Boolean> - cdn status for container

      • ‘X-CDN-URI’<~String> - cdn url for this container

      • ‘X-TTL’<~String> - integer seconds before data expires, defaults to 86400 (1 day)

      • ‘X-Log-Retention’<~Boolean> - ?

      • ‘X-User-Agent-ACL’<~String> - ?

      • ‘X-Referrer-ACL’<~String> - ?



20
21
22
23
24
25
26
27
28
# File 'lib/fog/rackspace/requests/cdn/head_container.rb', line 20

def head_container(container)
  response = request(
    :expects  => 204,
    :method   => 'HEAD',
    :path     => container,
    :query    => {'format' => 'json'}
  )
  response
end

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

modify CDN properties for a container

Parameters

  • name<~String> - Name for container, should be < 256 bytes and must not contain ‘/’

# options<~Hash>:

* 'X-CDN-Enabled'<~Boolean> - cdn status for container
* 'X-CDN-URI'<~String> - cdn url for this container
* 'X-TTL'<~String> - integer seconds before data expires, defaults to 86400 (1 day), in 3600..259200
* 'X-Log-Retention'<~Boolean> - ?
* 'X-User-Agent-ACL'<~String> - ?
* 'X-Referrer-ACL'<~String> - ?


17
18
19
20
21
22
23
24
25
# File 'lib/fog/rackspace/requests/cdn/post_container.rb', line 17

def post_container(name, options = {})
  response = request(
    :expects  => [201, 202],
    :headers  => options,
    :method   => 'POST',
    :path     => CGI.escape(name)
  )
  response
end

#purge(file) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/fog/rackspace/cdn.rb', line 126

def purge(file)
  unless file.is_a? Fog::Storage::Rackspace::File
    raise Fog::Errors::NotImplemented.new("#{object.class} does not support CDN purging")  if object
  end
  
  delete_object file.directory.key, file.key
  true
end

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

enable CDN for a container

Parameters

  • name<~String> - Name for container, should be < 256 bytes and must not contain ‘/’

# options<~Hash>:

* 'X-CDN-Enabled'<~Boolean> - cdn status for container
* 'X-CDN-URI'<~String> - cdn url for this container
* 'X-TTL'<~String> - integer seconds before data expires, defaults to 86400 (1 day), in 3600..259200
* 'X-Log-Retention'<~Boolean> - ?
* 'X-User-Agent-ACL'<~String> - ?
* 'X-Referrer-ACL'<~String> - ?


17
18
19
20
21
22
23
24
25
# File 'lib/fog/rackspace/requests/cdn/put_container.rb', line 17

def put_container(name, options = {})
  response = request(
    :expects  => [201, 202],
    :headers  => options,
    :method   => 'PUT',
    :path     => CGI.escape(name)
  )
  response
end

#reloadObject



122
123
124
# File 'lib/fog/rackspace/cdn.rb', line 122

def reload
  @cdn_connection.reset
end

#request(params, parse_json = true) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/fog/rackspace/cdn.rb', line 135

def request(params, parse_json = true)
  begin
    response = @connection.request(params.merge!({
      :headers  => {
        'Content-Type' => 'application/json',
        'Accept' => 'application/json',
        'X-Auth-Token' => auth_token
      }.merge!(params[:headers] || {}),
      :host     => endpoint_uri.host,
      :path     => "#{endpoint_uri.path}/#{params[:path]}",
    }))
  rescue Excon::Errors::HTTPStatusError => error
    raise case error
    when Excon::Errors::NotFound
      Fog::Storage::Rackspace::NotFound.slurp(error)
    else
      error
    end
  end
  if !response.body.empty? && parse_json && response.headers['Content-Type'] =~ %r{application/json}
    response.body = Fog::JSON.decode(response.body)
  end
  response
end