Skip to content

Commit b0bdc02

Browse files
committed
Merge branch 'master' of https://github.com/turtle0x1/php-lxd
2 parents 28f5058 + 190eaee commit b0bdc02

File tree

6 files changed

+192
-38
lines changed

6 files changed

+192
-38
lines changed

src/Client.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,17 @@ class Client
6868
/**
6969
* Create a new lxd client Instance
7070
*/
71-
public function __construct(HttpClient $httpClient = null, $apiVersion = null, $url = null)
72-
{
71+
public function __construct(
72+
HttpClient $httpClient = null,
73+
$apiVersion = null,
74+
$url = null,
75+
string $projectName = "default"
76+
) {
7377
$this->httpClient = $httpClient ?: HttpClientDiscovery::find();
7478
$this->messageFactory = MessageFactoryDiscovery::find();
7579
$this->apiVersion = $apiVersion ?: '1.0';
7680
$this->url = $url ?: 'https://127.0.0.1:8443';
81+
$this->projectName = $projectName;
7782

7883
$this->addPlugin(new LxdExceptionThower());
7984

@@ -213,4 +218,20 @@ private function pushBackCachePlugin()
213218
}
214219
}
215220
}
221+
/**
222+
* Set the project to use on the server
223+
* @param string $projectName The project name to use
224+
*/
225+
public function setProject(string $projectName)
226+
{
227+
$this->project = $projectName;
228+
}
229+
/**
230+
* Get the project using on the client
231+
* @return string The current project
232+
*/
233+
public function getProject()
234+
{
235+
return $this->project;
236+
}
216237
}

src/Endpoint/Containers.php

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ public function all()
2424
{
2525
$containers = [];
2626

27-
foreach ($this->get($this->getEndpoint()) as $container) {
27+
$config = [
28+
"project"=>$this->client->getProject()
29+
];
30+
31+
foreach ($this->get($this->getEndpoint(), $config) as $container) {
2832
$containers[] = str_replace('/'.$this->client->getApiVersion().$this->getEndpoint(), '', $container);
2933
}
3034

@@ -39,7 +43,11 @@ public function all()
3943
*/
4044
public function info($name)
4145
{
42-
return $this->get($this->getEndpoint().$name);
46+
$config = [
47+
"project"=>$this->client->getProject()
48+
];
49+
50+
return $this->get($this->getEndpoint().$name, $config);
4351
}
4452

4553
/**
@@ -50,7 +58,11 @@ public function info($name)
5058
*/
5159
public function state($name)
5260
{
53-
return $this->get($this->getEndpoint().$name.'/state');
61+
$config = [
62+
"project"=>$this->client->getProject()
63+
];
64+
65+
return $this->get($this->getEndpoint().$name.'/state', $config);
5466
}
5567

5668
/**
@@ -71,7 +83,11 @@ public function setState($name, $state, $timeout = 30, $force = true, $stateful
7183
$opts['force'] = $force;
7284
$opts['stateful'] = $stateful;
7385

74-
$response = $this->put($this->getEndpoint().$name.'/state', $opts);
86+
$config = [
87+
"project"=>$this->client->getProject()
88+
];
89+
90+
$response = $this->put($this->getEndpoint().$name.'/state', $opts, $config);
7591

7692
if ($wait) {
7793
$response = $this->client->operations->wait($response['id']);
@@ -262,7 +278,11 @@ public function create($name, array $options, $wait = false)
262278
$opts = $this->getLocalImageOptions($name, $source, $options);
263279
}
264280

265-
$response = $this->post($this->getEndpoint(), $opts);
281+
$config = [
282+
"project"=>$this->client->getProject()
283+
];
284+
285+
$response = $this->post($this->getEndpoint(), $opts, $config);
266286

267287
if ($wait) {
268288
$response = $this->client->operations->wait($response['id']);
@@ -297,7 +317,11 @@ public function copy($name, $copyName, array $options = [], $wait = false)
297317
$opts['source']['type'] = 'copy';
298318
$opts['source']['source'] = $name;
299319

300-
$response = $this->post($this->getEndpoint(), $opts);
320+
$config = [
321+
"project"=>$this->client->getProject()
322+
];
323+
324+
$response = $this->post($this->getEndpoint(), $opts, $config);
301325

302326
if ($wait) {
303327
$response = $this->client->operations->wait($response['id']);
@@ -381,7 +405,11 @@ public function initMigration($name)
381405
*/
382406
public function replace($name, $container, $wait = false)
383407
{
384-
$response = $this->put($this->getEndpoint().$name, $container);
408+
$config = [
409+
"project"=>$this->client->getProject()
410+
];
411+
412+
$response = $this->put($this->getEndpoint().$name, $container, $config);
385413

386414
if ($wait) {
387415
$response = $this->client->operations->wait($response['id']);
@@ -413,7 +441,11 @@ public function replace($name, $container, $wait = false)
413441
*/
414442
public function update($name, $config, $wait = false)
415443
{
416-
$response = $this->patch($this->getEndpoint().$name, $config);
444+
$options = [
445+
"project"=>$this->client->getProject()
446+
];
447+
448+
$response = $this->patch($this->getEndpoint().$name, $config, $options);
417449

418450
if ($wait) {
419451
$response = $this->client->operations->wait($response['id']);
@@ -433,7 +465,12 @@ public function update($name, $config, $wait = false)
433465
public function rename($name, $newName, $wait = false)
434466
{
435467
$opts['name'] = $newName;
436-
$response = $this->post($this->getEndpoint().$name, $opts);
468+
469+
$config = [
470+
"project"=>$this->client->getProject()
471+
];
472+
473+
$response = $this->post($this->getEndpoint().$name, $opts, $config);
437474

438475
if ($wait) {
439476
$response = $this->client->operations->wait($response['id']);
@@ -451,7 +488,11 @@ public function rename($name, $newName, $wait = false)
451488
*/
452489
public function remove($name, $wait = false)
453490
{
454-
$response = $this->delete($this->getEndpoint().$name);
491+
$config = [
492+
"project"=>$this->client->getProject()
493+
];
494+
495+
$response = $this->delete($this->getEndpoint().$name, $config);
455496

456497
if ($wait) {
457498
$response = $this->client->operations->wait($response['id']);
@@ -489,11 +530,14 @@ public function execute($name, $command, $record = false, array $environment = [
489530
$opts['wait-for-websocket'] = false;
490531
$opts['interactive'] = false;
491532

492-
$response = $this->post($this->getEndpoint().$name.'/exec', $opts);
533+
$config = [
534+
"project"=>$this->client->getProject()
535+
];
536+
537+
$response = $this->post($this->getEndpoint().$name.'/exec', $opts, $config);
493538

494539
if ($wait) {
495540
$response = $this->client->operations->wait($response['id']);
496-
497541
$logs = [];
498542
$output = $response['metadata']['output'];
499543
$return = $response['metadata']['return'];

src/Endpoint/Images.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ public function all()
2222
{
2323
$images = [];
2424

25-
foreach ($this->get($this->getEndpoint()) as $image) {
25+
$config = [
26+
"project"=>$this->client->getProject()
27+
];
28+
29+
foreach ($this->get($this->getEndpoint(), $config) as $image) {
2630
$images[] = str_replace('/'.$this->client->getApiVersion().$this->getEndpoint(), '', $image);
2731
}
2832

@@ -39,12 +43,15 @@ public function all()
3943
public function info($fingerprint, $secret = null)
4044
{
4145
$endpoint = $this->getEndpoint().$fingerprint;
42-
4346
if (!empty($secret)) {
4447
$endpoint .= '?secret='.$secret;
4548
}
4649

47-
return $this->get($endpoint);
50+
$config = [
51+
"project"=>$this->client->getProject()
52+
];
53+
54+
return $this->get($endpoint, $config);
4855
}
4956

5057
/**
@@ -62,7 +69,11 @@ public function info($fingerprint, $secret = null)
6269
*/
6370
public function create(array $options, $headers = [], $wait = false)
6471
{
65-
$response = $this->post($this->getEndpoint(), $options, $headers);
72+
$config = [
73+
"project"=>$this->client->getProject()
74+
];
75+
76+
$response = $this->post($this->getEndpoint(), $options, $config, $headers);
6677

6778
if ($wait) {
6879
$response = $this->client->operations->wait($response['id']);
@@ -231,7 +242,11 @@ public function createFromSnapshot($container, $snapshot, array $options, $wait
231242
*/
232243
public function replace($fingerprint, $options, $wait = false)
233244
{
234-
$response = $this->put($this->getEndpoint().$fingerprint, $options);
245+
$config = [
246+
"project"=>$this->client->getProject()
247+
];
248+
249+
$response = $this->put($this->getEndpoint().$fingerprint, $options, $config);
235250

236251
if ($wait) {
237252
$response = $this->client->operations->wait($response['id']);
@@ -249,7 +264,11 @@ public function replace($fingerprint, $options, $wait = false)
249264
*/
250265
public function remove($fingerprint, $wait = false)
251266
{
252-
$response = $this->delete($this->getEndpoint().$fingerprint);
267+
$config = [
268+
"project"=>$this->client->getProject()
269+
];
270+
271+
$response = $this->delete($this->getEndpoint().$fingerprint, $config);
253272

254273
if ($wait) {
255274
$response = $this->client->operations->wait($response['id']);

src/Endpoint/Images/Aliases.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ public function all()
2020
{
2121
$aliases = [];
2222

23-
foreach ($this->get($this->getEndpoint()) as $alias) {
23+
$config = [
24+
"project"=>$this->client->getProject()
25+
];
26+
27+
foreach ($this->get($this->getEndpoint(), $config) as $alias) {
2428
$aliases[] = str_replace('/'.$this->client->getApiVersion().$this->getEndpoint(), '', $alias);
2529
}
2630

@@ -35,7 +39,11 @@ public function all()
3539
*/
3640
public function info($name)
3741
{
38-
return $this->get($this->getEndpoint().$name);
42+
$config = [
43+
"project"=>$this->client->getProject()
44+
];
45+
46+
return $this->get($this->getEndpoint().$name, $config);
3947
}
4048

4149
/**
@@ -51,7 +59,11 @@ public function create($fingerprint, $aliasName, $description = '')
5159
$opts['name'] = $aliasName;
5260
$opts['description'] = $description;
5361

54-
return $this->post($this->getEndpoint(), $opts);
62+
$config = [
63+
"project"=>$this->client->getProject()
64+
];
65+
66+
return $this->post($this->getEndpoint(), $opts, $config);
5567
}
5668

5769
/**
@@ -74,7 +86,11 @@ public function replace($name, $fingerprint, $description = '')
7486
$opts['target'] = $fingerprint;
7587
$opts['description'] = $description;
7688

77-
return $this->put($this->getEndpoint().$name, $opts);
89+
$config = [
90+
"project"=>$this->client->getProject()
91+
];
92+
93+
return $this->put($this->getEndpoint().$name, $opts, $config);
7894
}
7995

8096
/**
@@ -88,7 +104,11 @@ public function rename($name, $newName)
88104
{
89105
$opts['name'] = $newName;
90106

91-
return $this->post($this->getEndpoint().$name, $opts);
107+
$config = [
108+
"project"=>$this->client->getProject()
109+
];
110+
111+
return $this->post($this->getEndpoint().$name, $opts, $config);
92112
}
93113

94114
/**
@@ -99,6 +119,10 @@ public function rename($name, $newName)
99119
*/
100120
public function remove($name)
101121
{
102-
return $this->delete($this->getEndpoint().$name);
122+
$config = [
123+
"project"=>$this->client->getProject()
124+
];
125+
126+
return $this->delete($this->getEndpoint().$name, $config);
103127
}
104128
}

src/Endpoint/Operations.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ public function all()
2222
{
2323
$operations = [];
2424

25-
foreach ($this->get($this->getEndpoint()) as $key => $operation) {
25+
$config = [
26+
"project"=>$this->client->getProject()
27+
];
28+
29+
foreach ($this->get($this->getEndpoint(), $config) as $key => $operation) {
2630
$operations[$key] = str_replace('/'.$this->client->getApiVersion().$this->getEndpoint(), '', $operation);
2731
}
2832

@@ -37,7 +41,11 @@ public function all()
3741
*/
3842
public function info($uuid)
3943
{
40-
return $this->get($this->getEndpoint().$uuid);
44+
$config = [
45+
"project"=>$this->client->getProject()
46+
];
47+
48+
return $this->get($this->getEndpoint().$uuid, $config);
4149
}
4250

4351
/**
@@ -50,7 +58,11 @@ public function info($uuid)
5058
*/
5159
public function cancel($uuid)
5260
{
53-
return $this->delete($this->getEndpoint().$uuid);
61+
$config = [
62+
"project"=>$this->client->getProject()
63+
];
64+
65+
return $this->delete($this->getEndpoint().$uuid, $config);
5466
}
5567

5668
/**
@@ -62,13 +74,16 @@ public function cancel($uuid)
6274
*/
6375
public function wait($uuid, $timeout = null)
6476
{
77+
$config = [
78+
"project"=>$this->client->getProject()
79+
];
6580

6681
$endpoint = $this->getEndpoint().$uuid.'/wait';
6782

6883
if (is_numeric($timeout) && $timeout > 0) {
6984
$endpoint .= '?timeout='.$timeout;
7085
}
7186

72-
return $this->get($endpoint);
87+
return $this->get($endpoint, $config);
7388
}
7489
}

0 commit comments

Comments
 (0)