2010-10-04 14:02:08 -07:00
require 'fog/core/model'
2011-08-24 14:12:29 -05:00
require 'fog/rackspace/models/storage/files'
2013-02-13 09:11:10 -06:00
require 'fog/rackspace/models/storage/metadata'
2010-03-19 18:29:42 -07:00
2010-02-27 13:39:33 -08:00
module Fog
2011-06-15 14:26:43 -07:00
module Storage
class Rackspace
2010-02-27 13:39:33 -08:00
class Directory < Fog :: Model
2013-03-07 16:01:42 -06:00
# @!attribute [r] key
# @return [String] The name of the directory
2010-09-09 17:50:38 -07:00
identity :key , :aliases = > 'name'
2010-02-27 13:39:33 -08:00
2013-03-07 16:01:42 -06:00
# @!attribute [r] bytes
# @return [Integer] The number of bytes used by the directory
2013-02-13 09:11:10 -06:00
attribute :bytes , :aliases = > 'X-Container-Bytes-Used' , :type = > :integer
2013-03-07 16:01:42 -06:00
# @!attribute [r] count
# @return [Integer] The number of objects in the directory
2013-02-13 09:11:10 -06:00
attribute :count , :aliases = > 'X-Container-Object-Count' , :type = > :integer
2013-03-07 16:01:42 -06:00
# @!attribute [rw] cdn_name
# @return [String] The CDN CNAME to be used instead of the default CDN directory URI. The CDN CNAME will need to be setup setup in DNS and
# point to the default CDN directory URI.
# @note This value does not persist and will need to be specified each time a directory is created or retrieved
# @see Directories#get
2011-06-16 16:22:37 -04:00
attribute :cdn_cname
2013-02-04 09:02:30 -06:00
2013-03-07 16:01:42 -06:00
# @!attribute [w] public
# Required for compatibility with other Fog providers. Not Used.
attr_writer :public
# @!attribute [w] public_url
# Required for compatibility with other Fog providers. Not Used.
attr_writer :public_url
2010-02-27 13:39:33 -08:00
2013-03-07 16:01:42 -06:00
# Set directory metadata
# @param [Hash,Fog::Storage::Rackspace::Metadata] hash contains key value pairs
2013-02-13 09:11:10 -06:00
def metadata = ( hash )
if hash . is_a? Fog :: Storage :: Rackspace :: Metadata
2013-02-13 14:23:55 -06:00
attributes [ :metadata ] = hash
2013-02-13 09:11:10 -06:00
else
2013-02-13 14:23:55 -06:00
attributes [ :metadata ] = Fog :: Storage :: Rackspace :: Metadata . new ( self , hash )
2013-02-13 09:11:10 -06:00
end
2013-02-13 14:23:55 -06:00
attributes [ :metadata ]
2013-02-13 09:11:10 -06:00
end
2013-03-07 16:01:42 -06:00
# Retrieve directory metadata
# @return [Fog::Storage::Rackspace::Metadata] metadata key value pairs.
2013-02-13 09:11:10 -06:00
def metadata
2013-02-13 14:23:55 -06:00
unless attributes [ :metadata ]
2013-02-14 09:56:57 -06:00
response = service . head_container ( key )
2013-02-13 14:23:55 -06:00
attributes [ :metadata ] = Fog :: Storage :: Rackspace :: Metadata . from_headers ( self , response . headers )
2013-02-14 09:56:57 -06:00
end
2013-02-13 14:23:55 -06:00
attributes [ :metadata ]
2013-02-13 09:11:10 -06:00
end
2013-03-07 16:01:42 -06:00
# Destroy the directory and remove it from CDN
# @return [Boolean] returns true if directory was deleted
2013-04-15 13:12:14 -05:00
# @raise [Fog::Storage::Rackspace::NotFound] - HTTP 404
# @raise [Fog::Storage::Rackspace::BadRequest] - HTTP 400
# @raise [Fog::Storage::Rackspace::InternalServerError] - HTTP 500
# @raise [Fog::Storage::Rackspace::ServiceError]
2013-03-07 16:01:42 -06:00
# @note Directory must be empty before it is destroyed.
# @see http://docs.rackspace.com/files/api/v1/cf-devguide/content/Delete_Container-d1e1765.html
2010-02-27 13:39:33 -08:00
def destroy
2010-05-02 19:59:15 -07:00
requires :key
2012-12-22 23:24:03 +00:00
service . delete_container ( key )
2013-02-15 13:01:17 -06:00
service . cdn . publish_container ( self , false ) if cdn_enabled?
2010-02-27 13:39:33 -08:00
true
rescue Excon :: Errors :: NotFound
false
end
2013-03-07 16:01:42 -06:00
# Returns collection of files in directory
# @return [Fog::Storage::Rackspace::Files] collection of files in directory
2013-04-15 13:12:14 -05:00
# @raise [Fog::Storage::Rackspace::NotFound] - HTTP 404
# @raise [Fog::Storage::Rackspace::BadRequest] - HTTP 400
# @raise [Fog::Storage::Rackspace::InternalServerError] - HTTP 500
# @raise [Fog::Storage::Rackspace::ServiceError]
2010-02-27 13:39:33 -08:00
def files
@files || = begin
2011-06-15 14:26:43 -07:00
Fog :: Storage :: Rackspace :: Files . new (
2010-02-27 13:39:33 -08:00
:directory = > self ,
2012-12-22 23:24:03 +00:00
:service = > service
2010-02-27 13:39:33 -08:00
)
end
end
2013-03-07 16:01:42 -06:00
# Is directory published to CDN
# @return [Boolean] return true if published to CDN
2013-04-15 13:12:14 -05:00
# @raise [Fog::Storage::Rackspace::NotFound] - HTTP 404
# @raise [Fog::Storage::Rackspace::BadRequest] - HTTP 400
# @raise [Fog::Storage::Rackspace::InternalServerError] - HTTP 500
# @raise [Fog::Storage::Rackspace::ServiceError]
2013-02-01 16:16:37 -06:00
def public?
2013-02-11 14:39:48 -06:00
if @public . nil?
@public || = ( key && public_url ) ? true : false
end
@public
2010-11-05 11:37:12 -07:00
end
2013-02-01 16:16:37 -06:00
2013-03-07 16:01:42 -06:00
# Reload directory with latest data from Cloud Files
# @return [Fog::Storage::Rackspace::Directory] returns itself
2013-04-15 13:12:14 -05:00
# @raise [Fog::Storage::Rackspace::NotFound] - HTTP 404
# @raise [Fog::Storage::Rackspace::BadRequest] - HTTP 400
# @raise [Fog::Storage::Rackspace::InternalServerError] - HTTP 500
# @raise [Fog::Storage::Rackspace::ServiceError]
2013-02-11 14:39:48 -06:00
def reload
@public = nil
2013-02-15 13:01:17 -06:00
@urls = nil
2013-02-11 14:39:48 -06:00
@files = nil
super
2013-02-01 16:16:37 -06:00
end
2013-02-15 13:01:17 -06:00
2013-03-07 16:01:42 -06:00
# Returns the public url for the directory.
2013-03-11 10:05:23 -05:00
# If the directory has not been published to the CDN, this method will return nil as it is not publically accessible. This method will return the approprate
2013-03-07 16:01:42 -06:00
# url in the following order:
#
2013-03-27 09:50:30 -05:00
# 1. If the service used to access this directory was created with the option :rackspace_cdn_ssl => true, this method will return the SSL-secured URL.
# 2. If the cdn_cname attribute is populated this method will return the cname.
# 3. return the default CDN url.
2013-03-07 16:01:42 -06:00
#
# @return [String] public url for directory
2013-04-15 13:12:14 -05:00
# @raise [Fog::Storage::Rackspace::NotFound] - HTTP 404
# @raise [Fog::Storage::Rackspace::BadRequest] - HTTP 400
# @raise [Fog::Storage::Rackspace::InternalServerError] - HTTP 500
# @raise [Fog::Storage::Rackspace::ServiceError]
2013-03-07 16:01:42 -06:00
def public_url
2013-02-15 13:01:17 -06:00
return nil if urls . empty?
2013-03-07 16:01:42 -06:00
return urls [ :ssl_uri ] if service . ssl?
2013-02-15 13:01:17 -06:00
cdn_cname || urls [ :uri ]
end
2013-03-07 16:01:42 -06:00
# URL used to stream video to iOS devices. Cloud Files will auto convert to the approprate format.
# @return [String] iOS URL
2013-04-15 13:12:14 -05:00
# @raise [Fog::Storage::Rackspace::NotFound] - HTTP 404
# @raise [Fog::Storage::Rackspace::BadRequest] - HTTP 400
# @raise [Fog::Storage::Rackspace::InternalServerError] - HTTP 500
# @raise [Fog::Storage::Rackspace::ServiceError]
2013-03-07 16:01:42 -06:00
# @see http://docs.rackspace.com/files/api/v1/cf-devguide/content/iOS-Streaming-d1f3725.html
2013-02-15 13:01:17 -06:00
def ios_url
urls [ :ios_uri ]
end
2013-03-07 16:01:42 -06:00
# URL used to stream resources
# @return [String] streaming url
2013-04-15 13:12:14 -05:00
# @raise [Fog::Storage::Rackspace::NotFound] - HTTP 404
# @raise [Fog::Storage::Rackspace::BadRequest] - HTTP 400
# @raise [Fog::Storage::Rackspace::InternalServerError] - HTTP 500
# @raise [Fog::Storage::Rackspace::ServiceError]
2013-03-07 16:01:42 -06:00
# @see http://docs.rackspace.com/files/api/v1/cf-devguide/content/Streaming-CDN-Enabled_Containers-d1f3721.html
2013-02-15 13:01:17 -06:00
def streaming_url
urls [ :streaming_uri ]
2010-11-05 11:37:12 -07:00
end
2013-03-07 16:01:42 -06:00
# Create or update directory and associated metadata
# @return [Boolean] returns true if directory was saved
2013-04-15 13:12:14 -05:00
# @raise [Fog::Storage::Rackspace::NotFound] - HTTP 404
# @raise [Fog::Storage::Rackspace::BadRequest] - HTTP 400
# @raise [Fog::Storage::Rackspace::InternalServerError] - HTTP 500
# @raise [Fog::Storage::Rackspace::ServiceError]
2013-03-07 16:01:42 -06:00
# @note If public attribute is true, directory will be CDN enabled
# @see http://docs.rackspace.com/files/api/v1/cf-devguide/content/Create_Container-d1e1694.html
2010-02-27 13:39:33 -08:00
def save
2010-05-02 19:59:15 -07:00
requires :key
2013-03-07 16:01:42 -06:00
create_or_update_container
2013-02-15 13:01:17 -06:00
raise Fog :: Storage :: Rackspace :: Error . new ( " Directory can not be set as :public without a CDN provided " ) if public ? && ! cdn_enabled?
@urls = service . cdn . publish_container ( self , public ?)
2010-02-27 13:39:33 -08:00
true
end
2013-02-05 10:41:59 -06:00
2013-02-15 13:01:17 -06:00
private
def cdn_enabled?
service . cdn && service . cdn . enabled?
end
def urls
requires :key
return { } unless cdn_enabled?
@urls || = service . cdn . urls ( self )
end
2013-03-07 16:01:42 -06:00
def create_or_update_container
2013-02-25 08:26:15 -06:00
headers = attributes [ :metadata ] . nil? ? { } : metadata . to_headers
2013-02-22 09:07:27 -06:00
service . put_container ( key , headers )
2013-02-05 10:41:59 -06:00
end
2010-02-27 13:39:33 -08:00
end
end
end
end