1 /**
  2  * @fileOverview Base Container.
  3  */
  4 
  5 (function () {
  6   var utils = require("../../utils"),
  7     CloudError = require("../../errors").CloudError,
  8     Container;
  9 
 10   /**
 11    * Container class.
 12    *
 13    * @param {base.blob.Container} conn  Connection object.
 14    * @param {Object}      attrs     Attributes.
 15    * @config {string}     name      Name.
 16    * @config {string}     created   Creation date.
 17    * @exports Container as base.blob.Container
 18    * @constructor
 19    */
 20   Container = function (conn, attrs) {
 21     // Validation.
 22     if (!conn) { throw new Error("No connection object."); }
 23     if (!attrs || !attrs.name) { throw new Error("No container name."); }
 24 
 25     this._conn = conn;
 26     this._name = attrs.name;
 27     this._created = attrs.created || null;
 28   };
 29 
 30   Object.defineProperties(Container.prototype, {
 31     /**
 32      * Connection object.
 33      *
 34      * @name Container#connection
 35      * @type base.Connection
 36      */
 37     connection: {
 38       get: function () {
 39         return this._conn;
 40       }
 41     },
 42 
 43     /**
 44      * Container name.
 45      *
 46      * @name Container#name
 47      * @type string
 48      */
 49     name: {
 50       get: function () {
 51         return this._name;
 52       }
 53     }
 54   });
 55 
 56   /**
 57    * Create blob object.
 58    * @private
 59    */
 60   Container.prototype._createBlob = function (name) {
 61     throw new Error("Not implemented.");
 62   };
 63 
 64   /**
 65    * Completion event ('``end``').
 66    *
 67    * @name base.blob.Container#get_end
 68    * @event
 69    * @param  {Object}                results  Results object.
 70    * @config {base.blob.Container} container  Container object.
 71    * @config {boolean}        alreadyCreated  True if object already exists.
 72    *                                          Note: Not all providers can tell.
 73    * @param  {Object}                   meta  Headers, meta object.
 74    * @config {Object}              [headers]  HTTP headers.
 75    * @config {Object}         [cloudHeaders]  Cloud provider headers.
 76    * @config {Object}             [metadata]  Cloud metadata.
 77    */
 78   /**
 79    * Error event ('``error``').
 80    *
 81    * @name base.blob.Container#get_error
 82    * @event
 83    * @param   {Error|errors.CloudError} err Error object.
 84    */
 85   /**
 86    * GET (or PUT) Container from cloud.
 87    *
 88    * ## Events
 89    *  - [``end(results, meta)``](#get_end)
 90    *  - [``error(err)``](#get_error)
 91    *
 92    * ## Note - Unvalidated GET's
 93    * If both ``validate`` and ``create`` are false, there is typically
 94    * no actual network operation, just a dummy (and immediate) callback.
 95    * Subsequent code cannot assume the container actually exists in the
 96    * cloud and must handle 404's, etc.
 97    *
 98    * @param   {Object}  [options]         Options object.
 99    * @config  {bool}    [validate=false]  Validate?
100    * @config  {bool}    [create=false]    Create (PUT) if doesn't exist?
101    * @config  {Object}  [headers]         Raw headers to add.
102    * @config  {Object}  [cloudHeaders]    Cloud provider headers to add.
103    * @returns {request.AuthenticatedRequest} Request object.
104    */
105   Container.prototype.get = function (options) {
106     throw new Error("Not implemented.");
107   };
108 
109   /**
110    * PUT container in cloud.
111    *
112    * Alias of ``get()`` with '``create=true``' option.
113    * @see base.blob.Container#get
114    */
115   Container.prototype.put = function (options) {
116     options = utils.extend(options, { create: true });
117     return this.get(options);
118   };
119 
120   /**
121    * Completion event ('``end``').
122    *
123    * Event emission / callback indicates the object no longer exists.
124    *
125    * @name base.blob.Container#del_end
126    * @event
127    * @param  {Object}                results  Results object.
128    * @config {base.blob.Container} container  Container object.
129    * @config {boolean}              notFound  True if object was not found.
130    * @param  {Object}                   meta  Headers, meta object.
131    * @config {Object}              [headers]  HTTP headers.
132    * @config {Object}         [cloudHeaders]  Cloud provider headers.
133    * @config {Object}             [metadata]  Cloud metadata.
134    */
135   /**
136    * Error event ('``error``').
137    *
138    * @name base.blob.Container#del_error
139    * @event
140    * @param   {Error|errors.CloudError} err Error object.
141    */
142   /**
143    * DELETE a container.
144    *
145    * ## Events
146    *  - [``end(results, meta)``](#del_end)
147    *  - [``error(err)``](#del_error)
148    *
149    * @param   {Object}  [options]         Options object.
150    * @config  {Object}  [headers]         Raw headers to add.
151    * @config  {Object}  [cloudHeaders]    Cloud provider headers to add.
152    * @config  {Object}  [metadata]        Cloud metadata to add.
153    * @returns {request.AuthenticatedRequest} Request object.
154    */
155   Container.prototype.del = function (options) {
156     throw new Error("Not implemented.");
157   };
158 
159   /**
160    * Completion event ('``end``').
161    *
162    * @name base.blob.Container#getBlobs_end
163    * @event
164    * @param  {Object}         results  Results object.
165    * @config {Array}            blobs  List of blob objects.
166    * @config {Array}         dirNames  List of pseudo-directory name strings.
167    * @config {boolean}        hasNext  True if more results are available.
168    * @param  {Object}            meta  Headers, meta object.
169    * @config {Object}       [headers]  HTTP headers.
170    * @config {Object}  [cloudHeaders]  Cloud provider headers.
171    * @config {Object}      [metadata]  Cloud metadata.
172    */
173   /**
174    * Error event ('``error``').
175    *
176    * @name base.blob.Container#getBlobs_error
177    * @event
178    * @param   {Error|errors.CloudError} err Error object.
179    */
180   /**
181    * GET a list of blob objects.
182    *
183    * ## Events
184    *  - [``end(results, meta)``](#getBlobs_end)
185    *  - [``error(err)``](#getBlobs_error)
186    *
187    * @param   {Object}  [options]         Options object.
188    * @config  {string}  [prefix]          Prefix of blob namespace to filter.
189    * @config  {string}  [delimiter]       Implied directory character.
190    * @config  {string}  [marker]          Starting blob name for next results.
191    * @config  {number}  [maxResults=1000] Max. blobs to return.
192    * @config  {Object}  [headers]         Raw headers to add.
193    * @config  {Object}  [cloudHeaders]    Cloud provider headers to add.
194    * @config  {Object}  [metadata]        Cloud metadata to add.
195    * @returns {request.AuthenticatedRequest} Request object.
196    */
197   Container.prototype.getBlobs = function (options) {
198     throw new Error("Not implemented.");
199   };
200 
201   /**
202    * Create blob object and GET data.
203    *
204    * @see base.blob.Blob#get
205    */
206   Container.prototype.getBlob = function (name, options) {
207     return this._createBlob(name).get(options);
208   };
209 
210   /**
211    * Create blob object and GET data to file.
212    *
213    * @see base.blob.Blob#getToFile
214    */
215   Container.prototype.getBlobToFile = function (name, filePath, options) {
216     return this._createBlob(name).getToFile(filePath, options);
217   };
218 
219   /**
220    * Create blob object and HEAD headers, metadata.
221    *
222    * @see base.blob.Blob#head
223    */
224   Container.prototype.headBlob = function (name, options) {
225     return this._createBlob(name).head(options);
226   };
227 
228   /**
229    * Create blob object and PUT data.
230    *
231    * @see base.blob.Blob#put
232    */
233   Container.prototype.putBlob = function (name, options) {
234     return this._createBlob(name).put(options);
235   };
236 
237   /**
238    * Create blob object and PUT data from file.
239    *
240    * @see base.blob.Blob#putFromFile
241    */
242   Container.prototype.putBlobFromFile = function (name, filePath, options) {
243     return this._createBlob(name).putFromFile(filePath, options);
244   };
245 
246   /**
247    * Create blob object and DELETE.
248    *
249    * @see base.blob.Blob#del
250    */
251   Container.prototype.delBlob = function (name, options) {
252     return this._createBlob(name).del(options);
253   };
254 
255   module.exports.Container = Container;
256 }());
257