1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 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: 27: 28: 29: 30: 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: 42: 43: 44: 45: 46: 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:
56: if (!($property = $schema->getProperty($propertyName))) {
57:
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:
66: $property->setName($propertyName);
67: $property->setValue($value);
68: $property->validate();
69:
70:
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:
80: $operation = JsonOperation::factory($schema, $property, $operationType);
81:
82:
83: $document->addOperation($operation);
84: }
85:
86:
87: $body = $document->toString();
88:
89: return $this->getClient()
90: ->patch($this->getUrl(), $this->getPatchHeaders(), $body)
91: ->send();
92: }
93:
94: 95: 96: 97: 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: 110: 111: 112:
113: public function delete()
114: {
115: return $this->getClient()->delete($this->getUrl())->send();
116: }
117:
118: 119: 120: 121: 122: 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: 134: 135: 136: 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: 154: 155: 156: 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: 171: 172: 173: 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: 186: 187: 188: 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: 201: 202: 203: 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: 215: 216: 217: 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: