Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/turtle0x1/php-lxd
Browse files Browse the repository at this point in the history
  • Loading branch information
turtle0x1 committed Jan 5, 2019
2 parents 28f5058 + 190eaee commit b0bdc02
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 38 deletions.
25 changes: 23 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,17 @@ class Client
/**
* Create a new lxd client Instance
*/
public function __construct(HttpClient $httpClient = null, $apiVersion = null, $url = null)
{
public function __construct(
HttpClient $httpClient = null,
$apiVersion = null,
$url = null,
string $projectName = "default"
) {
$this->httpClient = $httpClient ?: HttpClientDiscovery::find();
$this->messageFactory = MessageFactoryDiscovery::find();
$this->apiVersion = $apiVersion ?: '1.0';
$this->url = $url ?: 'https://127.0.0.1:8443';
$this->projectName = $projectName;

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

Expand Down Expand Up @@ -213,4 +218,20 @@ private function pushBackCachePlugin()
}
}
}
/**
* Set the project to use on the server
* @param string $projectName The project name to use
*/
public function setProject(string $projectName)
{
$this->project = $projectName;
}
/**
* Get the project using on the client
* @return string The current project
*/
public function getProject()
{
return $this->project;
}
}
68 changes: 56 additions & 12 deletions src/Endpoint/Containers.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ public function all()
{
$containers = [];

foreach ($this->get($this->getEndpoint()) as $container) {
$config = [
"project"=>$this->client->getProject()
];

foreach ($this->get($this->getEndpoint(), $config) as $container) {
$containers[] = str_replace('/'.$this->client->getApiVersion().$this->getEndpoint(), '', $container);
}

Expand All @@ -39,7 +43,11 @@ public function all()
*/
public function info($name)
{
return $this->get($this->getEndpoint().$name);
$config = [
"project"=>$this->client->getProject()
];

return $this->get($this->getEndpoint().$name, $config);
}

/**
Expand All @@ -50,7 +58,11 @@ public function info($name)
*/
public function state($name)
{
return $this->get($this->getEndpoint().$name.'/state');
$config = [
"project"=>$this->client->getProject()
];

return $this->get($this->getEndpoint().$name.'/state', $config);
}

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

$response = $this->put($this->getEndpoint().$name.'/state', $opts);
$config = [
"project"=>$this->client->getProject()
];

$response = $this->put($this->getEndpoint().$name.'/state', $opts, $config);

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

$response = $this->post($this->getEndpoint(), $opts);
$config = [
"project"=>$this->client->getProject()
];

$response = $this->post($this->getEndpoint(), $opts, $config);

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

$response = $this->post($this->getEndpoint(), $opts);
$config = [
"project"=>$this->client->getProject()
];

$response = $this->post($this->getEndpoint(), $opts, $config);

if ($wait) {
$response = $this->client->operations->wait($response['id']);
Expand Down Expand Up @@ -381,7 +405,11 @@ public function initMigration($name)
*/
public function replace($name, $container, $wait = false)
{
$response = $this->put($this->getEndpoint().$name, $container);
$config = [
"project"=>$this->client->getProject()
];

$response = $this->put($this->getEndpoint().$name, $container, $config);

if ($wait) {
$response = $this->client->operations->wait($response['id']);
Expand Down Expand Up @@ -413,7 +441,11 @@ public function replace($name, $container, $wait = false)
*/
public function update($name, $config, $wait = false)
{
$response = $this->patch($this->getEndpoint().$name, $config);
$options = [
"project"=>$this->client->getProject()
];

$response = $this->patch($this->getEndpoint().$name, $config, $options);

if ($wait) {
$response = $this->client->operations->wait($response['id']);
Expand All @@ -433,7 +465,12 @@ public function update($name, $config, $wait = false)
public function rename($name, $newName, $wait = false)
{
$opts['name'] = $newName;
$response = $this->post($this->getEndpoint().$name, $opts);

$config = [
"project"=>$this->client->getProject()
];

$response = $this->post($this->getEndpoint().$name, $opts, $config);

if ($wait) {
$response = $this->client->operations->wait($response['id']);
Expand All @@ -451,7 +488,11 @@ public function rename($name, $newName, $wait = false)
*/
public function remove($name, $wait = false)
{
$response = $this->delete($this->getEndpoint().$name);
$config = [
"project"=>$this->client->getProject()
];

$response = $this->delete($this->getEndpoint().$name, $config);

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

$response = $this->post($this->getEndpoint().$name.'/exec', $opts);
$config = [
"project"=>$this->client->getProject()
];

$response = $this->post($this->getEndpoint().$name.'/exec', $opts, $config);

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

$logs = [];
$output = $response['metadata']['output'];
$return = $response['metadata']['return'];
Expand Down
31 changes: 25 additions & 6 deletions src/Endpoint/Images.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ public function all()
{
$images = [];

foreach ($this->get($this->getEndpoint()) as $image) {
$config = [
"project"=>$this->client->getProject()
];

foreach ($this->get($this->getEndpoint(), $config) as $image) {
$images[] = str_replace('/'.$this->client->getApiVersion().$this->getEndpoint(), '', $image);
}

Expand All @@ -39,12 +43,15 @@ public function all()
public function info($fingerprint, $secret = null)
{
$endpoint = $this->getEndpoint().$fingerprint;

if (!empty($secret)) {
$endpoint .= '?secret='.$secret;
}

return $this->get($endpoint);
$config = [
"project"=>$this->client->getProject()
];

return $this->get($endpoint, $config);
}

/**
Expand All @@ -62,7 +69,11 @@ public function info($fingerprint, $secret = null)
*/
public function create(array $options, $headers = [], $wait = false)
{
$response = $this->post($this->getEndpoint(), $options, $headers);
$config = [
"project"=>$this->client->getProject()
];

$response = $this->post($this->getEndpoint(), $options, $config, $headers);

if ($wait) {
$response = $this->client->operations->wait($response['id']);
Expand Down Expand Up @@ -231,7 +242,11 @@ public function createFromSnapshot($container, $snapshot, array $options, $wait
*/
public function replace($fingerprint, $options, $wait = false)
{
$response = $this->put($this->getEndpoint().$fingerprint, $options);
$config = [
"project"=>$this->client->getProject()
];

$response = $this->put($this->getEndpoint().$fingerprint, $options, $config);

if ($wait) {
$response = $this->client->operations->wait($response['id']);
Expand All @@ -249,7 +264,11 @@ public function replace($fingerprint, $options, $wait = false)
*/
public function remove($fingerprint, $wait = false)
{
$response = $this->delete($this->getEndpoint().$fingerprint);
$config = [
"project"=>$this->client->getProject()
];

$response = $this->delete($this->getEndpoint().$fingerprint, $config);

if ($wait) {
$response = $this->client->operations->wait($response['id']);
Expand Down
36 changes: 30 additions & 6 deletions src/Endpoint/Images/Aliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ public function all()
{
$aliases = [];

foreach ($this->get($this->getEndpoint()) as $alias) {
$config = [
"project"=>$this->client->getProject()
];

foreach ($this->get($this->getEndpoint(), $config) as $alias) {
$aliases[] = str_replace('/'.$this->client->getApiVersion().$this->getEndpoint(), '', $alias);
}

Expand All @@ -35,7 +39,11 @@ public function all()
*/
public function info($name)
{
return $this->get($this->getEndpoint().$name);
$config = [
"project"=>$this->client->getProject()
];

return $this->get($this->getEndpoint().$name, $config);
}

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

return $this->post($this->getEndpoint(), $opts);
$config = [
"project"=>$this->client->getProject()
];

return $this->post($this->getEndpoint(), $opts, $config);
}

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

return $this->put($this->getEndpoint().$name, $opts);
$config = [
"project"=>$this->client->getProject()
];

return $this->put($this->getEndpoint().$name, $opts, $config);
}

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

return $this->post($this->getEndpoint().$name, $opts);
$config = [
"project"=>$this->client->getProject()
];

return $this->post($this->getEndpoint().$name, $opts, $config);
}

/**
Expand All @@ -99,6 +119,10 @@ public function rename($name, $newName)
*/
public function remove($name)
{
return $this->delete($this->getEndpoint().$name);
$config = [
"project"=>$this->client->getProject()
];

return $this->delete($this->getEndpoint().$name, $config);
}
}
23 changes: 19 additions & 4 deletions src/Endpoint/Operations.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ public function all()
{
$operations = [];

foreach ($this->get($this->getEndpoint()) as $key => $operation) {
$config = [
"project"=>$this->client->getProject()
];

foreach ($this->get($this->getEndpoint(), $config) as $key => $operation) {
$operations[$key] = str_replace('/'.$this->client->getApiVersion().$this->getEndpoint(), '', $operation);
}

Expand All @@ -37,7 +41,11 @@ public function all()
*/
public function info($uuid)
{
return $this->get($this->getEndpoint().$uuid);
$config = [
"project"=>$this->client->getProject()
];

return $this->get($this->getEndpoint().$uuid, $config);
}

/**
Expand All @@ -50,7 +58,11 @@ public function info($uuid)
*/
public function cancel($uuid)
{
return $this->delete($this->getEndpoint().$uuid);
$config = [
"project"=>$this->client->getProject()
];

return $this->delete($this->getEndpoint().$uuid, $config);
}

/**
Expand All @@ -62,13 +74,16 @@ public function cancel($uuid)
*/
public function wait($uuid, $timeout = null)
{
$config = [
"project"=>$this->client->getProject()
];

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

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

return $this->get($endpoint);
return $this->get($endpoint, $config);
}
}
Loading

0 comments on commit b0bdc02

Please sign in to comment.