Overview
  • Namespace
  • Class

Namespaces

  • OpenCloud
    • Autoscale
      • 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
      • Exception
      • Resource
      • Upload
    • Orchestration
      • Resource
    • Queues
      • Collection
      • Exception
      • Resource
    • Volume
      • Resource

Classes

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