From 836fa83ab4d55fc5f16fd324f7e8f82ad59d762a Mon Sep 17 00:00:00 2001 From: Shawna Date: Fri, 6 Sep 2019 06:07:29 -0400 Subject: [PATCH 1/9] added a new setupoptions model and a new post method to the resource class because no existing methods made sense to use in the context of the need --- src/Model/Resource.php | 27 +++++++++++++++++++++++++++ src/Model/SetupOptions.php | 12 ++++++++++++ src/PlatformClient.php | 27 +++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 src/Model/SetupOptions.php diff --git a/src/Model/Resource.php b/src/Model/Resource.php index 710196f..d5d9f1b 100644 --- a/src/Model/Resource.php +++ b/src/Model/Resource.php @@ -180,6 +180,33 @@ public static function get($id, $collectionUrl = null, ClientInterface $client) } } + /** + * Post request for returned data. + * + * @param string $body array of the data to send in the post. + * @param string $collectionUrl The URL of the collection. + * @param ClientInterface $client A suitably configured Guzzle + * client. + * + * @return array The resource object, or false if the resource is + * not found. + */ + public static function post(array $body, $collectionUrl = null, ClientInterface $client) + { + try { + $request = $client->createRequest('post', $collectionUrl, ['json' => $body]); + $data = self::send($request, $client); + return $data; + } catch (BadResponseException $e) { + $response = $e->getResponse(); + if ($response && $response->getStatusCode() === 404) { + return false; + } + throw $e; + } + } + + /** * Create a resource. * diff --git a/src/Model/SetupOptions.php b/src/Model/SetupOptions.php new file mode 100644 index 0000000..8851cad --- /dev/null +++ b/src/Model/SetupOptions.php @@ -0,0 +1,12 @@ +accountsEndpoint . 'regions', 0, [], $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::post($options, $url, $this->connector->getClient()); + } } From de7f74a3fa67fbd0554804fb0556da6e069b4fc2 Mon Sep 17 00:00:00 2001 From: Shawna Date: Mon, 5 Aug 2019 09:21:01 -0400 Subject: [PATCH 2/9] library updates to allow for the project create command in the cli to handle a template and initialize flag --- src/Model/Catalog copy.php | 37 +++++++++++++++++++++++++++++++++++++ src/Model/Subscription.php | 1 + src/PlatformClient.php | 19 +++++++++++++++++-- 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/Model/Catalog copy.php diff --git a/src/Model/Catalog copy.php b/src/Model/Catalog copy.php new file mode 100644 index 0000000..f9c5ce6 --- /dev/null +++ b/src/Model/Catalog copy.php @@ -0,0 +1,37 @@ +data = $data; + } + + /** + * @inheritdoc + */ + public static function wrapCollection(array $data, $baseUrl, ClientInterface $client) + { + $data = isset($data['info']) ? $data['info'] : []; + + return parent::wrapCollection($data, $baseUrl, $client); + } +} diff --git a/src/Model/Subscription.php b/src/Model/Subscription.php index 47f0971..071eca5 100644 --- a/src/Model/Subscription.php +++ b/src/Model/Subscription.php @@ -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 diff --git a/src/PlatformClient.php b/src/PlatformClient.php index 94dac42..932b88b 100644 --- a/src/PlatformClient.php +++ b/src/PlatformClient.php @@ -9,6 +9,7 @@ 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\SshKey; use Platformsh\Client\Model\Subscription; @@ -226,6 +227,7 @@ protected function cleanRequest(array $request) /** * Create a new Platform.sh subscription. * + * @param string $catalog The catalog item url. See getCatalog(). * @param string $region The region ID. See getRegions(). * @param string $plan The plan. See Subscription::$availablePlans. * @param string $title The project title. @@ -233,6 +235,7 @@ protected function cleanRequest(array $request) * @param int $environments The number of available environments. * @param array $activationCallback An activation callback for the subscription. * + * @see PlatformClient::getCatalog() * @see PlatformClient::getRegions() * @see Subscription::wait() * @@ -241,8 +244,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($catalog, $region, $plan = 'development', $title = null, $storage = null, $environments = null, array $activationCallback = null) { + $url = $this->accountsEndpoint . 'subscriptions'; $values = $this->cleanRequest([ 'project_region' => $region, @@ -250,9 +254,10 @@ public function createSubscription($region, $plan = 'development', $title = null 'project_title' => $title, 'storage' => $storage, 'environments' => $environments, + 'options_url' => $catalog, 'activation_callback' => $activationCallback, ]); - + return Subscription::create($values, $url, $this->connector->getClient()); } @@ -333,4 +338,14 @@ 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()); + } } From d67ea8e05f96b8e794d925fc67815bbdb162e558 Mon Sep 17 00:00:00 2001 From: Shawna Date: Wed, 7 Aug 2019 08:31:10 -0400 Subject: [PATCH 3/9] fixing my local --- src/Model/{Catalog copy.php => Catalog.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/Model/{Catalog copy.php => Catalog.php} (100%) diff --git a/src/Model/Catalog copy.php b/src/Model/Catalog.php similarity index 100% rename from src/Model/Catalog copy.php rename to src/Model/Catalog.php From b683d81159c7f13a6034f812891c9fcb46135164 Mon Sep 17 00:00:00 2001 From: Patrick Dawkins Date: Thu, 8 Aug 2019 13:03:02 +0100 Subject: [PATCH 4/9] Allow the bitbucket_server integration type --- src/Model/Integration.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Model/Integration.php b/src/Model/Integration.php index c7fc74d..3b34be6 100644 --- a/src/Model/Integration.php +++ b/src/Model/Integration.php @@ -19,6 +19,7 @@ class Integration extends Resource /** @var array */ protected static $types = [ 'bitbucket', + 'bitbucket_server', 'hipchat', 'github', 'gitlab', From ea87c0499930dc62e48a48030eb9f226c043e3df Mon Sep 17 00:00:00 2001 From: Patrick Dawkins Date: Mon, 26 Aug 2019 14:28:34 -0400 Subject: [PATCH 5/9] Mark environments as $full directly from the collection --- src/Model/Environment.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Model/Environment.php b/src/Model/Environment.php index 7bd5277..ee29c9b 100644 --- a/src/Model/Environment.php +++ b/src/Model/Environment.php @@ -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; @@ -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. * From 471b880608e4037736a3ea15907dc2d74a628790 Mon Sep 17 00:00:00 2001 From: Shawna Date: Fri, 30 Aug 2019 07:10:51 -0400 Subject: [PATCH 6/9] moving catalog for bw --- src/PlatformClient.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/PlatformClient.php b/src/PlatformClient.php index 932b88b..c834422 100644 --- a/src/PlatformClient.php +++ b/src/PlatformClient.php @@ -227,13 +227,13 @@ protected function cleanRequest(array $request) /** * Create a new Platform.sh subscription. * - * @param string $catalog The catalog item url. See getCatalog(). * @param string $region The region ID. See getRegions(). * @param string $plan The plan. See Subscription::$availablePlans. * @param string $title The project title. * @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() @@ -244,7 +244,7 @@ protected function cleanRequest(array $request) * similar code to wait for the subscription's project to be provisioned * and activated. */ - public function createSubscription($catalog, $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'; @@ -254,8 +254,8 @@ public function createSubscription($catalog, $region, $plan = 'development', $ti 'project_title' => $title, 'storage' => $storage, 'environments' => $environments, - 'options_url' => $catalog, 'activation_callback' => $activationCallback, + 'options_url' => $catalog, ]); return Subscription::create($values, $url, $this->connector->getClient()); From 57b035760f861a95c56aa220a75dceaf43e7ddb7 Mon Sep 17 00:00:00 2001 From: Patrick Dawkins Date: Wed, 11 Sep 2019 16:56:21 +0100 Subject: [PATCH 7/9] Do not mess up activity whitespace during wait() --- src/Model/Activity.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Model/Activity.php b/src/Model/Activity.php index 60cba2c..601991c 100644 --- a/src/Model/Activity.php +++ b/src/Model/Activity.php @@ -54,10 +54,10 @@ class Activity extends Resource public function wait(callable $onPoll = null, callable $onLog = null, $pollInterval = 1) { $log = $this->getProperty('log'); - if ($onLog !== null && strlen(trim($log))) { - $onLog(trim($log) . "\n"); - } $length = strlen($log); + if ($onLog !== null && $length > 0) { + $onLog($log); + } $retries = 0; while (!$this->isComplete()) { usleep($pollInterval * 1000000); @@ -67,7 +67,7 @@ public function wait(callable $onPoll = null, callable $onLog = null, $pollInter $onPoll($this); } if ($onLog !== null && ($new = substr($this->getProperty('log'), $length))) { - $onLog(trim($new) . "\n"); + $onLog($new); $length += strlen($new); } } catch (ConnectException $e) { From 1c363a79e67679711fe5f3740431df8b3caab545 Mon Sep 17 00:00:00 2001 From: Shawna Date: Fri, 6 Sep 2019 06:07:29 -0400 Subject: [PATCH 8/9] added a new setupoptions model and a new post method to the resource class because no existing methods made sense to use in the context of the need --- src/Model/Resource.php | 27 +++++++++++++++++++++++++++ src/Model/SetupOptions.php | 12 ++++++++++++ src/PlatformClient.php | 27 +++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 src/Model/SetupOptions.php diff --git a/src/Model/Resource.php b/src/Model/Resource.php index 710196f..d5d9f1b 100644 --- a/src/Model/Resource.php +++ b/src/Model/Resource.php @@ -180,6 +180,33 @@ public static function get($id, $collectionUrl = null, ClientInterface $client) } } + /** + * Post request for returned data. + * + * @param string $body array of the data to send in the post. + * @param string $collectionUrl The URL of the collection. + * @param ClientInterface $client A suitably configured Guzzle + * client. + * + * @return array The resource object, or false if the resource is + * not found. + */ + public static function post(array $body, $collectionUrl = null, ClientInterface $client) + { + try { + $request = $client->createRequest('post', $collectionUrl, ['json' => $body]); + $data = self::send($request, $client); + return $data; + } catch (BadResponseException $e) { + $response = $e->getResponse(); + if ($response && $response->getStatusCode() === 404) { + return false; + } + throw $e; + } + } + + /** * Create a resource. * diff --git a/src/Model/SetupOptions.php b/src/Model/SetupOptions.php new file mode 100644 index 0000000..8851cad --- /dev/null +++ b/src/Model/SetupOptions.php @@ -0,0 +1,12 @@ +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::post($options, $url, $this->connector->getClient()); + } } From b9158099b75859c4365559fca4e3f9dcb54d2f2a Mon Sep 17 00:00:00 2001 From: Shawna Date: Mon, 23 Sep 2019 07:51:22 -0400 Subject: [PATCH 9/9] rebasing to bring everything up to date --- src/Model/Resource.php | 27 ------------------------- src/Model/SetupOptions.php | 40 +++++++++++++++++++++++++++++++++++--- src/PlatformClient.php | 4 ++-- 3 files changed, 39 insertions(+), 32 deletions(-) diff --git a/src/Model/Resource.php b/src/Model/Resource.php index d5d9f1b..710196f 100644 --- a/src/Model/Resource.php +++ b/src/Model/Resource.php @@ -180,33 +180,6 @@ public static function get($id, $collectionUrl = null, ClientInterface $client) } } - /** - * Post request for returned data. - * - * @param string $body array of the data to send in the post. - * @param string $collectionUrl The URL of the collection. - * @param ClientInterface $client A suitably configured Guzzle - * client. - * - * @return array The resource object, or false if the resource is - * not found. - */ - public static function post(array $body, $collectionUrl = null, ClientInterface $client) - { - try { - $request = $client->createRequest('post', $collectionUrl, ['json' => $body]); - $data = self::send($request, $client); - return $data; - } catch (BadResponseException $e) { - $response = $e->getResponse(); - if ($response && $response->getStatusCode() === 404) { - return false; - } - throw $e; - } - } - - /** * Create a resource. * diff --git a/src/Model/SetupOptions.php b/src/Model/SetupOptions.php index 8851cad..9da1782 100644 --- a/src/Model/SetupOptions.php +++ b/src/Model/SetupOptions.php @@ -2,11 +2,45 @@ namespace Platformsh\Client\Model; +use GuzzleHttp\ClientInterface; +use GuzzleHttp\Exception\BadResponseException; + /** - * Represents Platform.sh setup options return data. + * Represents Platform.sh setup options api requests. * */ -class SetupOptions extends Resource + +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; + } + } } diff --git a/src/PlatformClient.php b/src/PlatformClient.php index 3b6bd90..5939d49 100644 --- a/src/PlatformClient.php +++ b/src/PlatformClient.php @@ -349,7 +349,7 @@ public function getCatalog() { return Catalog::create([], $this->accountsEndpoint . 'setup/catalog', $this->getConnector()->getClient()); } - + /** * Get the setup options file for a user. * @@ -373,6 +373,6 @@ public function getSetupOptions($vendor = NULL, $plan = NULL, $options_url = NUL 'organization' => $organization ]); - return SetupOptions::post($options, $url, $this->connector->getClient()); + return SetupOptions::getList($options, $url, $this->connector->getClient()); } }