|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OpenStack\Compute\v2\Models; |
| 6 | + |
| 7 | +use OpenStack\Common\Resource\Creatable; |
| 8 | +use OpenStack\Common\Resource\Deletable; |
| 9 | +use OpenStack\Common\Resource\Listable; |
| 10 | +use OpenStack\Common\Resource\OperatorResource; |
| 11 | +use OpenStack\Common\Resource\Retrievable; |
| 12 | +use OpenStack\Common\Transport\Utils; |
| 13 | + |
| 14 | +/** |
| 15 | + * Represents a Compute v2 Server Group. |
| 16 | + * |
| 17 | + * @property \OpenStack\Compute\v2\Api $api |
| 18 | + */ |
| 19 | +class ServerGroup extends OperatorResource implements Listable, Retrievable, Creatable, Deletable |
| 20 | +{ |
| 21 | + /** @var string */ |
| 22 | + public $id; |
| 23 | + |
| 24 | + /** @var array */ |
| 25 | + public $members = []; |
| 26 | + |
| 27 | + /** @var array */ |
| 28 | + public $metadata = []; |
| 29 | + |
| 30 | + /** @var string */ |
| 31 | + public $name; |
| 32 | + |
| 33 | + /** @var string */ |
| 34 | + public $policy; |
| 35 | + |
| 36 | + /** @var array */ |
| 37 | + public $policies = []; |
| 38 | + |
| 39 | + /** @var string */ |
| 40 | + public $projectId; |
| 41 | + |
| 42 | + /** @var array */ |
| 43 | + public $rules = []; |
| 44 | + |
| 45 | + /** @var string */ |
| 46 | + public $userId; |
| 47 | + |
| 48 | + protected $aliases = [ |
| 49 | + 'project_id' => 'projectId', |
| 50 | + 'user_id' => 'userId', |
| 51 | + ]; |
| 52 | + |
| 53 | + protected $resourceKey = 'server_group'; |
| 54 | + protected $resourcesKey = 'server_groups'; |
| 55 | + |
| 56 | + public function retrieve() |
| 57 | + { |
| 58 | + $response = $this->execute($this->api->getServerGroup(), ['id' => (string) $this->id]); |
| 59 | + $this->populateFromResponse($response); |
| 60 | + } |
| 61 | + |
| 62 | + public function create(array $userOptions): Creatable |
| 63 | + { |
| 64 | + if (isset($userOptions['policy'], $userOptions['policies'])) { |
| 65 | + throw new \RuntimeException('Provide either "policy" or "policies", not both.'); |
| 66 | + } |
| 67 | + |
| 68 | + if (isset($userOptions['rules']) && !isset($userOptions['policy'])) { |
| 69 | + throw new \RuntimeException('"rules" requires "policy".'); |
| 70 | + } |
| 71 | + |
| 72 | + if (!isset($userOptions['policy']) && !isset($userOptions['policies'])) { |
| 73 | + throw new \RuntimeException('"policy" or "policies" must be set.'); |
| 74 | + } |
| 75 | + |
| 76 | + $definition = isset($userOptions['policy']) |
| 77 | + ? $this->api->postServerGroupWithPolicy() |
| 78 | + : $this->api->postServerGroup(); |
| 79 | + |
| 80 | + $response = $this->execute($definition, $userOptions); |
| 81 | + |
| 82 | + return $this->populateFromResponse($response); |
| 83 | + } |
| 84 | + |
| 85 | + public function delete() |
| 86 | + { |
| 87 | + $this->execute($this->api->deleteServerGroup(), ['id' => (string) $this->id]); |
| 88 | + } |
| 89 | + |
| 90 | + public function populateFromArray(array $array): self |
| 91 | + { |
| 92 | + $array = Utils::flattenJson($array, $this->resourceKey); |
| 93 | + |
| 94 | + if (isset($array['policy']) && !isset($array['policies'])) { |
| 95 | + $array['policies'] = [$array['policy']]; |
| 96 | + } |
| 97 | + |
| 98 | + if (isset($array['policies']) && is_array($array['policies']) && !isset($array['policy']) && count($array['policies']) > 0) { |
| 99 | + $array['policy'] = reset($array['policies']); |
| 100 | + } |
| 101 | + |
| 102 | + $array += [ |
| 103 | + 'members' => [], |
| 104 | + 'metadata' => [], |
| 105 | + 'policies' => [], |
| 106 | + 'rules' => [], |
| 107 | + ]; |
| 108 | + |
| 109 | + return parent::populateFromArray($array); |
| 110 | + } |
| 111 | +} |
0 commit comments