      1: <?php
      2: /**
      3:  * Copyright 2012-2014 Rackspace US, Inc.
      4:  *
      5:  * Licensed under the Apache License, Version 2.0 (the "License");
      6:  * you may not use this file except in compliance with the License.
      7:  * You may obtain a copy of the License at
      8:  *
      9:  * http://www.apache.org/licenses/LICENSE-2.0
     10:  *
     11:  * Unless required by applicable law or agreed to in writing, software
     12:  * distributed under the License is distributed on an "AS IS" BASIS,
     13:  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14:  * See the License for the specific language governing permissions and
     15:  * limitations under the License.
     16:  */
     17:
     18: namespace OpenCloud\Image;
     19:
     20: use OpenCloud\Common\Constants\Header;
     21: use OpenCloud\Common\Service\CatalogService;
     22: use OpenCloud\Image\Resource\Image;
     23: use OpenCloud\Image\Resource\Schema\Schema;
     24:
     25: /**
     26:  * Service class that represents OpenStack Glance / Rackspace Cloud Images
     27:  *
     28:  * @package OpenCloud\Images
     29:  */
     30: class Service extends CatalogService
     31: {
     32:     const DEFAULT_TYPE = 'image';
     33:     const DEFAULT_NAME = 'cloudImages';
     34:
     35:     const PATCH_CONTENT_TYPE = 'application/openstack-images-v2.1-json-patch';
     36:
     37:     /**
     38:      * Get the default headers to send for PATCH requests
     39:      *
     40:      * @return array
     41:      */
     42:     public function getPatchHeaders()
     43:     {
     44:         return array(Header::CONTENT_TYPE => self::PATCH_CONTENT_TYPE);
     45:     }
     46:
     47:     /**
     48:      * This operation returns images you created, shared images that you accepted, and standard images.
     49:      *
     50:      * @param array $params
     51:      * @return \OpenCloud\Common\Collection\PaginatedIterator
     52:      */
     53:     public function listImages(array $params = array())
     54:     {
     55:         $url = clone $this->getUrl();
     56:         $url->addPath(Image::resourceName())->setQuery($params);
     57:
     58:         return $this->resourceList('Image', $url);
     59:     }
     60:
     61:     /**
     62:      * Returns details for a specific image.
     63:      *
     64:      * @param $imageId
     65:      * @return object
     66:      */
     67:     public function getImage($imageId)
     68:     {
     69:         $image = $this->resource('Image');
     70:         $image->setId($imageId);
     71:         $image->refresh();
     72:
     73:         return $image;
     74:     }
     75:
     76:     /**
     77:      * For iterator use only.
     78:      *
     79:      * @param $data
     80:      * @return object
     81:      */
     82:     public function image($data)
     83:     {
     84:         $image = $this->resource('Image');
     85:         $image->setData((array) $data);
     86:
     87:         return $image;
     88:     }
     89:
     90:     /**
     91:      * A convenience method which returns the URL needed to retrieve schemas.
     92:      *
     93:      * @param $path
     94:      * @return \Guzzle\Http\Url
     95:      */
     96:     protected function getSchemaUrl($path)
     97:     {
     98:         $url = clone $this->getUrl();
     99:
    100:         return $url->addPath('schemas')->addPath($path);
    101:     }
    102:
    103:     /**
    104:      * Return a JSON schema for a collection of image resources
    105:      *
    106:      * @return Schema
    107:      */
    108:     public function getImagesSchema()
    109:     {
    110:         $data = $this->getClient()->get($this->getSchemaUrl('images'))->send()->json();
    111:
    112:         return Schema::factory($data);
    113:     }
    114:
    115:     /**
    116:      * Return a JSON schema for an individual image resource
    117:      *
    118:      * @return Schema
    119:      */
    120:     public function getImageSchema()
    121:     {
    122:         $data = $this->getClient()->get($this->getSchemaUrl('image'))->send()->json();
    123:
    124:         return Schema::factory($data);
    125:     }
    126:
    127:     /**
    128:      * Return a JSON schema for a collection of member resources
    129:      *
    130:      * @return Schema
    131:      */
    132:     public function getMembersSchema()
    133:     {
    134:         $data = $this->getClient()->get($this->getSchemaUrl('members'))->send()->json();
    135:
    136:         return Schema::factory($data);
    137:     }
    138:
    139:     /**
    140:      * Return a JSON schema for an individual member resource
    141:      *
    142:      * @return Schema
    143:      */
    144:     public function getMemberSchema()
    145:     {
    146:         $data = $this->getClient()->get($this->getSchemaUrl('member'))->send()->json();
    147:
    148:         return Schema::factory($data);
    149:     }
    150: }
    151: