Skip to content
Closed
37 changes: 37 additions & 0 deletions src/Model/Catalog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Platformsh\Client\Model;

use GuzzleHttp\ClientInterface;

/**
* Represents a Platform.sh catalog.
*
* @property-read int $id
* @property-read string $label
* @property-read bool $available
* @property-read bool $private
* @property-read string $zone
* @property-read string $endpoint
*/
class Catalog extends Resource
{
/**
* @inheritdoc
*/
protected function setData(array $data)
{
$data = isset($data['info']) ? $data['info'] : $data;
$this->data = $data;
}

/**
* @inheritdoc
*/
public static function wrapCollection(array $data, $baseUrl, ClientInterface $client)
{
$data = isset($data['info']) ? $data['info'] : [];

return parent::wrapCollection($data, $baseUrl, $client);
}
}
16 changes: 16 additions & 0 deletions src/Model/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Platformsh\Client\Model;

use Cocur\Slugify\Slugify;
use GuzzleHttp\ClientInterface;
use Platformsh\Client\Exception\EnvironmentStateException;
use Platformsh\Client\Exception\OperationUnavailableException;
use Platformsh\Client\Model\Backups\BackupConfig;
Expand Down Expand Up @@ -368,6 +369,21 @@ public function synchronize($data = false, $code = false, $rebase = false)
return $this->runLongOperation('synchronize', 'post', $body);
}

/**
* {@inheritDoc}
*/
public static function wrapCollection(array $data, $baseUrl, ClientInterface $client)
{
// The environments collection contains full information about each
// environment, so set $full to true when initializing.
$resources = [];
foreach ($data as $item) {
$resources[] = new static($item, $baseUrl, $client, true);
}

return $resources;
}

/**
* Create a backup of the environment.
*
Expand Down
46 changes: 46 additions & 0 deletions src/Model/SetupOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Platformsh\Client\Model;

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\BadResponseException;

/**
* Represents Platform.sh setup options api requests.
*
*/

class SetupOptions
{

/** @var ClientInterface */
protected $client;

/**
* Post request for setup options data.
*
* @param string $body array of the data to send in the post.
* @param string $url The URL of the setup options api.
* @param ClientInterface $client A suitably configured Guzzle
* client.
*
* @return array The returned setup options api data in array format or an
* error if one is returned.
*/
public static function getList(array $body, $url = null, ClientInterface $client)
{
try {
$request = $client->createRequest('post', $url, ['json' => $body]);
$response = $client->send($request);
$body = $response->getBody()->getContents();
$data = [];
if ($body) {
$response->getBody()->seek(0);
$data = $response->json();
}
return $data;
} catch (BadResponseException $e) {
throw $e;
}
}
}
1 change: 1 addition & 0 deletions src/Model/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* @property-read int $user_licenses Number of users.
* @property-read string $project_id
* @property-read string $project_title
* @property-read string $project_options
* @property-read string $project_region
* @property-read string $project_region_label
* @property-read string $project_ui
Expand Down
46 changes: 44 additions & 2 deletions src/PlatformClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use Platformsh\Client\Model\Plan;
use Platformsh\Client\Model\Project;
use Platformsh\Client\Model\Region;
use Platformsh\Client\Model\Catalog;
use Platformsh\Client\Model\Result;
use Platformsh\Client\Model\SetupOptions;
use Platformsh\Client\Model\SshKey;
use Platformsh\Client\Model\Subscription;

Expand Down Expand Up @@ -232,7 +234,9 @@ protected function cleanRequest(array $request)
* @param int $storage The storage of each environment, in MiB.
* @param int $environments The number of available environments.
* @param array $activationCallback An activation callback for the subscription.
* @param string $catalog The catalog item url. See getCatalog().
*
* @see PlatformClient::getCatalog()
* @see PlatformClient::getRegions()
* @see Subscription::wait()
*
Expand All @@ -241,8 +245,9 @@ protected function cleanRequest(array $request)
* similar code to wait for the subscription's project to be provisioned
* and activated.
*/
public function createSubscription($region, $plan = 'development', $title = null, $storage = null, $environments = null, array $activationCallback = null)
public function createSubscription($region, $plan = 'development', $title = null, $storage = null, $environments = null, array $activationCallback = null, $catalog = null)
{

$url = $this->accountsEndpoint . 'subscriptions';
$values = $this->cleanRequest([
'project_region' => $region,
Expand All @@ -251,8 +256,9 @@ public function createSubscription($region, $plan = 'development', $title = null
'storage' => $storage,
'environments' => $environments,
'activation_callback' => $activationCallback,
'options_url' => $catalog,
]);

return Subscription::create($values, $url, $this->connector->getClient());
}

Expand Down Expand Up @@ -333,4 +339,40 @@ public function getRegions()
{
return Region::getCollection($this->accountsEndpoint . 'regions', 0, [], $this->getConnector()->getClient());
}

/**
* Get the project options catalog.
*
* @return Catalog[]
*/
public function getCatalog()
{
return Catalog::create([], $this->accountsEndpoint . 'setup/catalog', $this->getConnector()->getClient());
}

/**
* Get the setup options file for a user.
*
* @param string $vendor The query string containing the vendor machine name.
* @param string $plan The machine name of the plan which has been selected during the project setup process.
* @param string $options_url The URL of a project options file which has been selected as a setup template.
* @param string $username The name of the account for which the project is to be created.
* @param string $organization The name of the organization for which the project is to be created.
*
*
* @return SetupOptions[]
*/
public function getSetupOptions($vendor = NULL, $plan = NULL, $options_url = NULL, $username = NULL, $organization = NULL)
{
$url = $this->accountsEndpoint . 'setup/options';
$options = $this->cleanRequest([
'vendor' => $vendor,
'plan' => $plan,
'options_url' => $options_url,
'username' => $username,
'organization' => $organization
]);

return SetupOptions::getList($options, $url, $this->connector->getClient());
}
}