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 Guzzle\Http\Exception\BadResponseException;
21: use OpenCloud\Common\Exceptions\ForbiddenOperationException;
22: use OpenCloud\Common\Exceptions\ResourceNotFoundException;
23: use OpenCloud\Image\Enum\MemberStatus;
24:
25: /**
26: * Class that represents a Member which belongs to a Glance Image. In more general terms, an image member is a user who
27: * has been granted access to an image. Normally, if an image is not public, only the owner can boot from the image.
28: *
29: * @package OpenCloud\Images\Resource
30: */
31: class Member extends AbstractSchemaResource
32: {
33: protected static $url_resource = 'members';
34: protected static $json_name = '';
35: protected static $json_collection_name = 'members';
36:
37: /**
38: * @var array Enumerated types
39: */
40: private $allowedStates = array(
41: MemberStatus::ACCEPTED,
42: MemberStatus::PENDING,
43: MemberStatus::REJECTED
44: );
45:
46: /**
47: * Update the status of a member, allowing them to fully access an image after being invited
48: *
49: * @param $status The eventual status this member wants.
50: * @return \Guzzle\Http\Message\Response
51: * @throws \OpenCloud\Common\Exceptions\ForbiddenOperationException
52: * @throws \OpenCloud\Common\Exceptions\ResourceNotFoundException
53: * @throws \Guzzle\Http\Exception\BadResponseException
54: * @throws \InvalidArgumentException
55: */
56: public function updateStatus($status)
57: {
58: if (!in_array($status, $this->allowedStates)) {
59: throw new \InvalidArgumentException(
60: sprintf('Status must be one of these defined types: %s', implode($this->allowedStates, ','))
61: );
62: }
63:
64: $json = json_encode(array('status' => $status));
65:
66: $request = $this->getClient()->put($this->getUrl(), self::getJsonHeader(), $json);
67:
68: try {
69: return $request->send();
70: } catch (BadResponseException $e) {
71: $response = $e->getResponse();
72:
73: switch ($response->getStatusCode()) {
74: case 403:
75: $exception = ForbiddenOperationException::factory($e);
76: break;
77: case 404:
78: $exception = ResourceNotFoundException::factory($e);
79: break;
80: }
81:
82: throw isset($exception) ? $exception : $e;
83: }
84: }
85:
86: /**
87: * @return \Guzzle\Http\Message\Response
88: */
89: public function delete()
90: {
91: return $this->getClient()->delete($this->getUrl())->send();
92: }
93: }
94: