Overview

Namespaces

  • OpenCloud
    • Autoscale
      • Resource
    • CDN
      • Resource
    • CloudMonitoring
      • Collection
      • Exception
      • Resource
    • Common
      • Collection
      • Constants
      • Exceptions
      • Http
        • Message
      • Log
      • Resource
      • Service
    • Compute
      • Constants
      • Exception
      • Resource
    • Database
      • Resource
    • DNS
      • Collection
      • Resource
    • Identity
      • Constants
      • Resource
    • Image
      • Enum
      • Resource
        • JsonPatch
        • Schema
    • LoadBalancer
      • Collection
      • Enum
      • Resource
    • Networking
      • Resource
    • ObjectStore
      • Constants
      • Enum
      • Exception
      • Resource
      • Upload
    • Orchestration
      • Resource
    • Queues
      • Collection
      • Exception
      • Resource
    • Volume
      • Resource
  • PHP

Classes

  • AbstractSchemaResource
  • Image
  • Member

Interfaces

  • ImageInterface
  • Overview
  • Namespace
  • Class
  • Tree
  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\Resource;
 19: 
 20: use OpenCloud\Image\Enum\OperationType;
 21: use OpenCloud\Image\Resource\JsonPatch\Document as JsonDocument;
 22: use OpenCloud\Image\Resource\JsonPatch\Operation as JsonOperation;
 23: use OpenCloud\Image\Resource\Schema\Schema;
 24: 
 25: /**
 26:  * Class that represents a Glance Image. In more general terms, a virtual machine image is a single file which contains
 27:  * a virtual disk that has an installed bootable operating system. In the Cloud Images API, an image is represented by
 28:  * a JSON-encoded data structure (the image schema) and its raw binary data (the image file).
 29:  *
 30:  * @package OpenCloud\Images\Resource
 31:  */
 32: class Image extends AbstractSchemaResource implements ImageInterface
 33: {
 34:     protected static $url_resource = 'images';
 35:     protected static $json_name = '';
 36:     protected static $json_collection_name = 'images';
 37: 
 38:     const PATCH_CONTENT_TYPE = 'application/openstack-images-v2.1-json-patch';
 39: 
 40:     /**
 41:      * Update this resource
 42:      *
 43:      * @param array  $params
 44:      * @param Schema $schema
 45:      * @return \Guzzle\Http\Message\Response
 46:      * @throws \RuntimeException
 47:      */
 48:     public function update(array $params, Schema $schema = null)
 49:     {
 50:         $schema = $schema ?: $this->getService()->getImageSchema();
 51: 
 52:         $document = new JsonDocument();
 53: 
 54:         foreach ($params as $propertyName => $value) {
 55:             // find property object
 56:             if (!($property = $schema->getProperty($propertyName))) {
 57:                 // check whether additional properties are found
 58:                 if (false === ($property = $schema->validateAdditionalProperty($value))) {
 59:                     throw new \RuntimeException(
 60:                         'If a property does not exist in the schema, the `additionalProperties` property must be set'
 61:                     );
 62:                 }
 63:             }
 64: 
 65:             // do validation checks
 66:             $property->setName($propertyName);
 67:             $property->setValue($value);
 68:             $property->validate();
 69: 
 70:             // decide operation type
 71:             if (!$value) {
 72:                 $operationType = OperationType::REMOVE;
 73:             } elseif ($this->offsetExists($propertyName)) {
 74:                 $operationType = OperationType::REPLACE;
 75:             } else {
 76:                 $operationType = $schema->decideOperationType($property);
 77:             }
 78: 
 79:             // create JSON-patch operation
 80:             $operation = JsonOperation::factory($schema, $property, $operationType);
 81: 
 82:             // add to JSON document
 83:             $document->addOperation($operation);
 84:         }
 85: 
 86:         // create request
 87:         $body = $document->toString();
 88: 
 89:         return $this->getClient()
 90:             ->patch($this->getUrl(), $this->getPatchHeaders(), $body)
 91:             ->send();
 92:     }
 93: 
 94:     /**
 95:      * Refresh this resource
 96:      *
 97:      * @return \Guzzle\Http\Message\Response
 98:      */
 99:     public function refresh()
100:     {
101:         $response = $this->getClient()->get($this->getUrl())->send();
102: 
103:         $this->setData($response->json());
104: 
105:         return $response;
106:     }
107: 
108:     /**
109:      * Delete this resource
110:      *
111:      * @return \Guzzle\Http\Message\Response
112:      */
113:     public function delete()
114:     {
115:         return $this->getClient()->delete($this->getUrl())->send();
116:     }
117: 
118:     /**
119:      * List the members of this image
120:      *
121:      * @param array $params
122:      * @return mixed
123:      */
124:     public function listMembers(array $params = array())
125:     {
126:         $url = clone $this->getUrl();
127:         $url->addPath(Member::resourceName())->setQuery($params);
128: 
129:         return $this->getService()->resourceList('Member', $url, $this);
130:     }
131: 
132:     /**
133:      * Iterator use only
134:      *
135:      * @param $data
136:      * @return mixed
137:      */
138:     public function member($data)
139:     {
140:         $data = (array) $data;
141: 
142:         $member = $this->getService()->resource('Member', null, $this);
143:         $member->setData($data);
144: 
145:         if (isset($data['member_id'])) {
146:             $member->setId($data['member_id']);
147:         }
148: 
149:         return $member;
150:     }
151: 
152:     /**
153:      * Get a member belonging to this image
154:      *
155:      * @param $memberId
156:      * @return mixed
157:      */
158:     public function getMember($memberId)
159:     {
160:         $url = clone $this->getUrl();
161:         $url->addPath('members');
162:         $url->addPath((string) $memberId);
163: 
164:         $data = $this->getClient()->get($url)->send()->json();
165: 
166:         return $this->member($data);
167:     }
168: 
169:     /**
170:      * Add a member to this image
171:      *
172:      * @param $tenantId
173:      * @return \Guzzle\Http\Message\Response
174:      */
175:     public function createMember($tenantId)
176:     {
177:         $url = $this->getUrl();
178:         $url->addPath('members');
179: 
180:         $json = json_encode(array('member' => $tenantId));
181:         return $this->getClient()->post($url, self::getJsonHeader(), $json)->send();
182:     }
183: 
184:     /**
185:      * Delete a member from this image
186:      *
187:      * @param $tenantId
188:      * @return \Guzzle\Http\Message\Response
189:      */
190:     public function deleteMember($tenantId)
191:     {
192:         $url = $this->getUrl();
193:         $url->addPath('members');
194:         $url->addPath((string)$tenantId);
195: 
196:         return $this->getClient()->delete($url)->send();
197:     }
198: 
199:     /**
200:      * Add a tag to this image
201:      *
202:      * @param string $tag
203:      * @return \Guzzle\Http\Message\Response
204:      */
205:     public function addTag($tag)
206:     {
207:         $url = clone $this->getUrl();
208:         $url->addPath('tags')->addPath((string) $tag);
209: 
210:         return $this->getClient()->put($url)->send();
211:     }
212: 
213:     /**
214:      * Delete a tag from this image
215:      *
216:      * @param $tag
217:      * @return \Guzzle\Http\Message\Response
218:      */
219:     public function deleteTag($tag)
220:     {
221:         $url = clone $this->getUrl();
222:         $url->addPath('tags')->addPath((string) $tag);
223: 
224:         return $this->getClient()->delete($url)->send();
225:     }
226: }
227: 
API documentation generated by ApiGen 2.8.0