|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Opensaucesystems\Lxd\Endpoint; |
| 4 | + |
| 5 | +class Projects extends AbstructEndpoint |
| 6 | +{ |
| 7 | + protected function getEndpoint() |
| 8 | + { |
| 9 | + return '/projects/'; |
| 10 | + } |
| 11 | + |
| 12 | + /** |
| 13 | + * List all projects on the server |
| 14 | + * |
| 15 | + * |
| 16 | + * @return array |
| 17 | + */ |
| 18 | + public function all() |
| 19 | + { |
| 20 | + $projects = []; |
| 21 | + |
| 22 | + foreach ($this->get($this->getEndpoint()) as $project) { |
| 23 | + $x = str_replace('/'.$this->client->getApiVersion().$this->getEndpoint(), '', $project); |
| 24 | + $x = str_replace('?project=' . $this->client->getProject(), '', $x); |
| 25 | + |
| 26 | + $projects[] = $x; |
| 27 | + } |
| 28 | + |
| 29 | + return $projects; |
| 30 | + } |
| 31 | + |
| 32 | + public function create(string $name, string $description = "", array $config = []) |
| 33 | + { |
| 34 | + $project = []; |
| 35 | + $project["name"] = $name; |
| 36 | + $project["description"] = $description; |
| 37 | + $project["config"] = empty($config) ? $this->defaultProjectConfig() : $config; |
| 38 | + |
| 39 | + return $this->post($this->getEndpoint(), $project); |
| 40 | + } |
| 41 | + |
| 42 | + public function info(string $name) |
| 43 | + { |
| 44 | + return $this->get($this->getEndpoint() . "/" . $name); |
| 45 | + } |
| 46 | + |
| 47 | + public function replace(string $name, string $description = "", array $config = []) |
| 48 | + { |
| 49 | + $project = []; |
| 50 | + $project["description"] = $description; |
| 51 | + $project["config"] = empty($config) ? $this->defaultProjectConfig() : $config; |
| 52 | + |
| 53 | + return $this->put($this->getEndpoint() . $name, $project); |
| 54 | + } |
| 55 | + |
| 56 | + public function update(string $name, string $description = "", array $config = []) |
| 57 | + { |
| 58 | + $project = []; |
| 59 | + if(!empty($description)){ |
| 60 | + $project["description"] = $description; |
| 61 | + } |
| 62 | + $project["config"] = empty($config) ? $this->defaultProjectConfig() : $config; |
| 63 | + return $this->patch($this->getEndpoint() . $name, $project); |
| 64 | + } |
| 65 | + |
| 66 | + public function rename(string $name, string $newName) |
| 67 | + { |
| 68 | + $config = ["name"=>$newName]; |
| 69 | + return $this->post($this->getEndpoint() . $name, $config); |
| 70 | + } |
| 71 | + |
| 72 | + public function remove(string $name) |
| 73 | + { |
| 74 | + return $this->delete($this->getEndpoint() . $name); |
| 75 | + } |
| 76 | + |
| 77 | + private function defaultProjectConfig() |
| 78 | + { |
| 79 | + return [ |
| 80 | + "features.images"=>"true", |
| 81 | + "features.profiles"=>"true", |
| 82 | + ]; |
| 83 | + } |
| 84 | +} |
0 commit comments