From 9381f8e094ca1eee66525cb35c778fe394856405 Mon Sep 17 00:00:00 2001 From: Shawna Date: Mon, 5 Aug 2019 05:49:39 -0400 Subject: [PATCH 01/16] update handling flag for catalog passing and initialization within the creation step --- src/Command/Project/ProjectCreateCommand.php | 104 +++++++++++++++---- 1 file changed, 86 insertions(+), 18 deletions(-) diff --git a/src/Command/Project/ProjectCreateCommand.php b/src/Command/Project/ProjectCreateCommand.php index ad4df76b9c..6a40e50a7b 100644 --- a/src/Command/Project/ProjectCreateCommand.php +++ b/src/Command/Project/ProjectCreateCommand.php @@ -27,11 +27,14 @@ protected function configure() ->setAliases(['create']) ->setDescription('Create a new project'); - $this->form = Form::fromArray($this->getFields()); - $this->form->configureInputDefinition($this->getDefinition()); $this->addOption('check-timeout', null, InputOption::VALUE_REQUIRED, 'The API timeout while checking the project status', 30) - ->addOption('timeout', null, InputOption::VALUE_REQUIRED, 'The total timeout for all API checks (0 to disable the timeout)', 900); + ->addOption('timeout', null, InputOption::VALUE_REQUIRED, 'The total timeout for all API checks (0 to disable the timeout)', 900) + ->addOption('template', null, InputOption::VALUE_NONE, 'Choose a template on which to build your project.') + ->addOption('initialize', null, InputOption::VALUE_NONE, 'Initialize the project after it has been created.') + ->addOption('current-repo', null, InputOption::VALUE_NONE, 'Automatically set remote after creation.') + ->addOption('region', null, InputOption::VALUE_REQUIRED, 'The region to assign the project to.'); + $this->setHelp(<<form = Form::fromArray($this->getFields($input->getOption('template'))); + $this->form->configureInputDefinition($this->getDefinition()); + /** @var \Platformsh\Cli\Service\QuestionHelper $questionHelper */ $questionHelper = $this->getService('question_helper'); @@ -63,6 +69,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $estimate = $this->api() ->getClient() ->getSubscriptionEstimate($options['plan'], $options['storage'], $options['environments'], 1); + $costConfirm = sprintf( 'The estimated monthly cost of this project is: %s', $estimate['total'] @@ -77,9 +84,15 @@ protected function execute(InputInterface $input, OutputInterface $output) if (!$questionHelper->confirm($costConfirm)) { return 1; } + + // Handle if no template is chosen look for a default one for the user. + if (empty($options['catalog'])) { + $options['catalog'] == NULL; + } $subscription = $this->api()->getClient() ->createSubscription( + $options['catalog'], $options['region'], $options['plan'], $options['title'], @@ -129,7 +142,6 @@ protected function execute(InputInterface $input, OutputInterface $output) $timedOut = $totalTimeout ? time() - $start > $totalTimeout : false; } $this->stdErr->writeln(''); - if (!$subscription->isActive()) { if ($timedOut) { $this->stdErr->writeln('The project failed to activate on time'); @@ -146,14 +158,30 @@ protected function execute(InputInterface $input, OutputInterface $output) return 1; } - $this->stdErr->writeln("The project is now ready!"); + if ($input->getOption('initialize')) { + // Use the existing initialize command. + $project = $this->selectProject($subscription->project_id); + $environment = $this->api()->getEnvironment('master', $project, null, true); + $environment->initialize($subscription->project_options['initialize']['profile'], $subscription->project_options['initialize']['repository']); + $this->api()->clearEnvironmentsCache($environment->project); + $this->stdErr->writeln("The project has been initialized and is ready!"); + } + else { + $this->stdErr->writeln("The project is now ready!"); + } + $output->writeln($subscription->project_id); $this->stdErr->writeln(''); + if (!empty($subscription->project_options['initialize'])) { + $this->stdErr->writeln(" Template: {$subscription->project_options[initialize][repository] }"); + } $this->stdErr->writeln(" Region: {$subscription->project_region}"); $this->stdErr->writeln(" Project ID: {$subscription->project_id}"); $this->stdErr->writeln(" Project title: {$subscription->project_title}"); $this->stdErr->writeln(" URL: {$subscription->project_ui}"); + + return 0; } @@ -216,29 +244,68 @@ protected function getAvailableRegions($runtime = false) return $regions; } + /** + * Return the catalog. + * + * The default list is in the config `service.catalog`. This is + * replaced at runtime by an API call. + * + * @param bool $runtime + * + * @return array + */ + protected function getAvailableCatalog($runtime = false) + { + if ($runtime) { + $catalog = []; + foreach ($this->api()->getClient()->getCatalog()->getData() as $item) { + if ($item['info'] && $item['template']) { + $catalog[$item['template']] = $item['info']['name']; + } + } + $catalog['empty'] = 'Empty Project'; + } else { + $catalog = (array) $this->config()->get('service.catalog'); + } + + return $catalog; + } + /** * Returns a list of ConsoleForm form fields for this command. * * @return Field[] */ - protected function getFields() + protected function getFields($template) { - return [ - 'title' => new Field('Project title', [ + $fields = []; + + $fields['title'] = new Field('Project title', [ 'optionName' => 'title', 'description' => 'The initial project title', 'questionLine' => '', 'default' => 'Untitled Project', - ]), - 'region' => new OptionsField('Region', [ + ]); + if ($template) { + $fields['catalog'] = new OptionsField('Base Template', [ + 'optionName' => 'catalog', + 'description' => 'The template from which to create your project or your own blank project.', + 'options' => $this->getAvailableCatalog(), + 'asChoice' => FALSE, + 'optionsCallback' => function () { + return $this->getAvailableCatalog(true); + }, + ]); + } + $fields['region'] = new OptionsField('Region', [ 'optionName' => 'region', 'description' => 'The region where the project will be hosted', 'options' => $this->getAvailableRegions(), 'optionsCallback' => function () { return $this->getAvailableRegions(true); }, - ]), - 'plan' => new OptionsField('Plan', [ + ]); + $fields['plan'] = new OptionsField('Plan', [ 'optionName' => 'plan', 'description' => 'The subscription plan', 'options' => $this->getAvailablePlans(), @@ -247,23 +314,24 @@ protected function getFields() }, 'default' => in_array('development', $this->getAvailablePlans()) ? 'development' : null, 'allowOther' => true, - ]), - 'environments' => new Field('Environments', [ + ]); + $fields['environments'] = new Field('Environments', [ 'optionName' => 'environments', 'description' => 'The number of environments', 'default' => 3, 'validator' => function ($value) { return is_numeric($value) && $value > 0 && $value < 50; }, - ]), - 'storage' => new Field('Storage', [ + ]); + $fields['storage'] = new Field('Storage', [ 'description' => 'The amount of storage per environment, in GiB', 'default' => 5, 'validator' => function ($value) { return is_numeric($value) && $value > 0 && $value < 1024; }, - ]), - ]; + ]); + + return $fields; } /** From 65a50279c4181b513eaecf4b7930b89efaceb0ed Mon Sep 17 00:00:00 2001 From: Shawna Date: Mon, 5 Aug 2019 08:03:58 -0400 Subject: [PATCH 02/16] handling values passed in via the flag for a template location --- src/Command/Project/ProjectCreateCommand.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Command/Project/ProjectCreateCommand.php b/src/Command/Project/ProjectCreateCommand.php index 6a40e50a7b..994233ef5e 100644 --- a/src/Command/Project/ProjectCreateCommand.php +++ b/src/Command/Project/ProjectCreateCommand.php @@ -30,7 +30,7 @@ protected function configure() $this->addOption('check-timeout', null, InputOption::VALUE_REQUIRED, 'The API timeout while checking the project status', 30) ->addOption('timeout', null, InputOption::VALUE_REQUIRED, 'The total timeout for all API checks (0 to disable the timeout)', 900) - ->addOption('template', null, InputOption::VALUE_NONE, 'Choose a template on which to build your project.') + ->addOption('template', null, InputOption::VALUE_OPTIONAL, 'Provide a template url or choose a template from the provided list on which to build your project.', false) ->addOption('initialize', null, InputOption::VALUE_NONE, 'Initialize the project after it has been created.') ->addOption('current-repo', null, InputOption::VALUE_NONE, 'Automatically set remote after creation.') ->addOption('region', null, InputOption::VALUE_REQUIRED, 'The region to assign the project to.'); @@ -58,7 +58,14 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { - $this->form = Form::fromArray($this->getFields($input->getOption('template'))); + // Process the template field to determine if it's a url or just the flag. + //@todo How do we want to handle bad urls? + $catalog = false; + if ($input->getOption('template') !== false && empty(parse_url($input->getOption('template'), PHP_URL_PATH))) { + $catalog = true; + } + + $this->form = Form::fromArray($this->getFields($catalog)); $this->form->configureInputDefinition($this->getDefinition()); /** @var \Platformsh\Cli\Service\QuestionHelper $questionHelper */ @@ -86,7 +93,10 @@ protected function execute(InputInterface $input, OutputInterface $output) } // Handle if no template is chosen look for a default one for the user. - if (empty($options['catalog'])) { + if ($catalog === false && !empty(parse_url($input->getOption('template'), PHP_URL_PATH))) { + $options['catalog'] = $input->getOption('template'); + } + else { $options['catalog'] == NULL; } From 9da78e2b9452e1ef41851738fb9220f4a65b90fe Mon Sep 17 00:00:00 2001 From: Shawna Date: Mon, 5 Aug 2019 14:45:43 -0400 Subject: [PATCH 03/16] switched out all the options for fields to allow consoleForm to work in all its glory --- src/Command/Project/ProjectCreateCommand.php | 86 +++++++++++++------- 1 file changed, 55 insertions(+), 31 deletions(-) diff --git a/src/Command/Project/ProjectCreateCommand.php b/src/Command/Project/ProjectCreateCommand.php index 994233ef5e..6a5fad6954 100644 --- a/src/Command/Project/ProjectCreateCommand.php +++ b/src/Command/Project/ProjectCreateCommand.php @@ -7,6 +7,8 @@ use Platformsh\Cli\Console\Bot; use Platformsh\ConsoleForm\Field\Field; use Platformsh\ConsoleForm\Field\OptionsField; +use Platformsh\ConsoleForm\Field\BooleanField; +use Platformsh\ConsoleForm\Field\UrlField; use Platformsh\ConsoleForm\Form; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -27,14 +29,11 @@ protected function configure() ->setAliases(['create']) ->setDescription('Create a new project'); + $this->form = Form::fromArray($this->getFields()); + $this->form->configureInputDefinition($this->getDefinition()); $this->addOption('check-timeout', null, InputOption::VALUE_REQUIRED, 'The API timeout while checking the project status', 30) - ->addOption('timeout', null, InputOption::VALUE_REQUIRED, 'The total timeout for all API checks (0 to disable the timeout)', 900) - ->addOption('template', null, InputOption::VALUE_OPTIONAL, 'Provide a template url or choose a template from the provided list on which to build your project.', false) - ->addOption('initialize', null, InputOption::VALUE_NONE, 'Initialize the project after it has been created.') - ->addOption('current-repo', null, InputOption::VALUE_NONE, 'Automatically set remote after creation.') - ->addOption('region', null, InputOption::VALUE_REQUIRED, 'The region to assign the project to.'); - + ->addOption('timeout', null, InputOption::VALUE_REQUIRED, 'The total timeout for all API checks (0 to disable the timeout)', 900); $this->setHelp(<<getOption('template') !== false && empty(parse_url($input->getOption('template'), PHP_URL_PATH))) { - $catalog = true; - } - - $this->form = Form::fromArray($this->getFields($catalog)); - $this->form->configureInputDefinition($this->getDefinition()); - /** @var \Platformsh\Cli\Service\QuestionHelper $questionHelper */ $questionHelper = $this->getService('question_helper'); @@ -91,15 +80,15 @@ protected function execute(InputInterface $input, OutputInterface $output) if (!$questionHelper->confirm($costConfirm)) { return 1; } - - // Handle if no template is chosen look for a default one for the user. - if ($catalog === false && !empty(parse_url($input->getOption('template'), PHP_URL_PATH))) { - $options['catalog'] = $input->getOption('template'); + + // Grab the url of the yaml file. + if (!empty($options['catalog_url'])) { + $options['catalog'] = $options['catalog_url']; } - else { - $options['catalog'] == NULL; + else if (!empty($options['catalog_url'])) { + $options['catalog'] = $options['template_url']; } - + $subscription = $this->api()->getClient() ->createSubscription( $options['catalog'], @@ -168,7 +157,7 @@ protected function execute(InputInterface $input, OutputInterface $output) return 1; } - if ($input->getOption('initialize')) { + if ($options['initialize']) { // Use the existing initialize command. $project = $this->selectProject($subscription->project_id); $environment = $this->api()->getEnvironment('master', $project, null, true); @@ -286,7 +275,7 @@ protected function getAvailableCatalog($runtime = false) * * @return Field[] */ - protected function getFields($template) + protected function getFields() { $fields = []; @@ -296,17 +285,41 @@ protected function getFields($template) 'questionLine' => '', 'default' => 'Untitled Project', ]); - if ($template) { - $fields['catalog'] = new OptionsField('Base Template', [ - 'optionName' => 'catalog', + + $fields['template_option'] = new OptionsField('Template Options', [ + 'optionName' => 'template_option', + 'options' => [ + 'Provide your own template url.', + 'Choose a template from the catalog.', + 'No template at this time.', + ], + 'description' => 'Choose a template, provide a url or choose not to use one.', + 'includeAsOption' => false, + ]); + $fields['catalog_url'] = new OptionsField('Catalog', [ + 'optionName' => 'catalog_url', + 'conditions' => [ + 'template_option' => [ + 'Choose a template from the catalog.' + ], + ], 'description' => 'The template from which to create your project or your own blank project.', 'options' => $this->getAvailableCatalog(), 'asChoice' => FALSE, 'optionsCallback' => function () { return $this->getAvailableCatalog(true); - }, - ]); - } + }, + ]); + $fields['template_url'] = new UrlField('Template URL', [ + 'optionName' => 'template_url', + 'conditions' => [ + 'template_option' => [ + 'Provide your own template url.' + ], + ], + 'description' => 'The template url', + 'questionLine' => 'What is the URL of the template?', + ]); $fields['region'] = new OptionsField('Region', [ 'optionName' => 'region', 'description' => 'The region where the project will be hosted', @@ -340,6 +353,17 @@ protected function getFields($template) return is_numeric($value) && $value > 0 && $value < 1024; }, ]); + $fields['initialize'] = new BooleanField('Initialize', [ + 'optionName' => 'initialized', + 'conditions' => [ + 'template_option' => [ + 'Provide your own template url.', + 'Choose a template from the catalog.', + ], + ], + 'description' => 'Initialize this environment?', + 'questionLine' => 'Initialize this environment?', + ]); return $fields; } From 95817f8463b6f1b3d2ebcbdd26402fe25084700f Mon Sep 17 00:00:00 2001 From: Shawna Date: Tue, 6 Aug 2019 07:31:43 -0400 Subject: [PATCH 04/16] possible solution using a secondary form --- src/Command/Project/ProjectCreateCommand.php | 172 ++++++++++++++----- 1 file changed, 125 insertions(+), 47 deletions(-) diff --git a/src/Command/Project/ProjectCreateCommand.php b/src/Command/Project/ProjectCreateCommand.php index 6a5fad6954..81148389ef 100644 --- a/src/Command/Project/ProjectCreateCommand.php +++ b/src/Command/Project/ProjectCreateCommand.php @@ -33,7 +33,8 @@ protected function configure() $this->form->configureInputDefinition($this->getDefinition()); $this->addOption('check-timeout', null, InputOption::VALUE_REQUIRED, 'The API timeout while checking the project status', 30) - ->addOption('timeout', null, InputOption::VALUE_REQUIRED, 'The total timeout for all API checks (0 to disable the timeout)', 900); + ->addOption('timeout', null, InputOption::VALUE_REQUIRED, 'The total timeout for all API checks (0 to disable the timeout)', 900) + ->addOption('template', null, InputOption::VALUE_OPTIONAL, 'Choose a starting template or provide a url of one.', false); $this->setHelp(<<form->resolveOptions($input, $output, $questionHelper); + if ($input->getOption('template') !== false) { + if (empty(parse_url($input->getOption('template'), PHP_URL_PATH))) { + $temp_provided = true; + } + else { + $temp_provided = false; + } + $this->template_form = Form::fromArray($this->getTemplateFields($temp_provided)); + $this->template_form->configureInputDefinition($this->getDefinition()); + $template_options = $this->template_form->resolveOptions($input, $output, $questionHelper); + + } + + $estimate = $this->api() ->getClient() ->getSubscriptionEstimate($options['plan'], $options['storage'], $options['environments'], 1); @@ -82,11 +97,11 @@ protected function execute(InputInterface $input, OutputInterface $output) } // Grab the url of the yaml file. - if (!empty($options['catalog_url'])) { - $options['catalog'] = $options['catalog_url']; + if (!empty($template_options['catalog_url'])) { + $options['catalog'] = $template_options['catalog_url']; } - else if (!empty($options['catalog_url'])) { - $options['catalog'] = $options['template_url']; + else if (!empty($template_options['catalog_url'])) { + $options['catalog'] = $template_options['template_url']; } $subscription = $this->api()->getClient() @@ -157,7 +172,7 @@ protected function execute(InputInterface $input, OutputInterface $output) return 1; } - if ($options['initialize']) { + if ($template_options['initialize']) { // Use the existing initialize command. $project = $this->selectProject($subscription->project_id); $environment = $this->api()->getEnvironment('master', $project, null, true); @@ -286,40 +301,40 @@ protected function getFields() 'default' => 'Untitled Project', ]); - $fields['template_option'] = new OptionsField('Template Options', [ - 'optionName' => 'template_option', - 'options' => [ - 'Provide your own template url.', - 'Choose a template from the catalog.', - 'No template at this time.', - ], - 'description' => 'Choose a template, provide a url or choose not to use one.', - 'includeAsOption' => false, - ]); - $fields['catalog_url'] = new OptionsField('Catalog', [ - 'optionName' => 'catalog_url', - 'conditions' => [ - 'template_option' => [ - 'Choose a template from the catalog.' - ], - ], - 'description' => 'The template from which to create your project or your own blank project.', - 'options' => $this->getAvailableCatalog(), - 'asChoice' => FALSE, - 'optionsCallback' => function () { - return $this->getAvailableCatalog(true); - }, - ]); - $fields['template_url'] = new UrlField('Template URL', [ - 'optionName' => 'template_url', - 'conditions' => [ - 'template_option' => [ - 'Provide your own template url.' - ], - ], - 'description' => 'The template url', - 'questionLine' => 'What is the URL of the template?', - ]); + // $fields['template_option'] = new OptionsField('Template Options', [ + // 'optionName' => 'template_option', + // 'options' => [ + // 'Provide your own template url.', + // 'Choose a template from the catalog.', + // 'No template at this time.', + // ], + // 'description' => 'Choose a template, provide a url or choose not to use one.', + // 'includeAsOption' => false, + // ]); + // $fields['catalog_url'] = new OptionsField('Catalog', [ + // 'optionName' => 'catalog_url', + // 'conditions' => [ + // 'template_option' => [ + // 'Choose a template from the catalog.' + // ], + // ], + // 'description' => 'The template from which to create your project or your own blank project.', + // 'options' => $this->getAvailableCatalog(), + // 'asChoice' => FALSE, + // 'optionsCallback' => function () { + // return $this->getAvailableCatalog(true); + // }, + // ]); + // $fields['template_url'] = new UrlField('Template URL', [ + // 'optionName' => 'template_url', + // 'conditions' => [ + // 'template_option' => [ + // 'Provide your own template url.' + // ], + // ], + // 'description' => 'The template url', + // 'questionLine' => 'What is the URL of the template?', + // ]); $fields['region'] = new OptionsField('Region', [ 'optionName' => 'region', 'description' => 'The region where the project will be hosted', @@ -353,17 +368,80 @@ protected function getFields() return is_numeric($value) && $value > 0 && $value < 1024; }, ]); - $fields['initialize'] = new BooleanField('Initialize', [ - 'optionName' => 'initialized', - 'conditions' => [ - 'template_option' => [ + // $fields['initialize'] = new BooleanField('Initialize', [ + // 'optionName' => 'initialized', + // 'conditions' => [ + // 'template_option' => [ + // 'Provide your own template url.', + // 'Choose a template from the catalog.', + // ], + // ], + // 'description' => 'Initialize this environment?', + // 'questionLine' => 'Initialize this environment?', + // ]); + + return $fields; + } + + /** + * Returns a list of ConsoleForm form fields for this command. + * + * @return Field[] + */ + protected function getTemplateFields($url_provided) + { + $fields = []; + + if (!$url_provided) { + $fields['template_option'] = new OptionsField('Template Options', [ + 'optionName' => 'template_option', + 'options' => [ 'Provide your own template url.', 'Choose a template from the catalog.', + 'No template at this time.', + ], + 'description' => 'Choose a template, provide a url or choose not to use one.', + 'includeAsOption' => false, + ]); + + $fields['catalog_url'] = new OptionsField('Catalog', [ + 'optionName' => 'catalog_url', + 'conditions' => [ + 'template_option' => [ + 'Choose a template from the catalog.' ], ], - 'description' => 'Initialize this environment?', - 'questionLine' => 'Initialize this environment?', - ]); + 'description' => 'The template from which to create your project or your own blank project.', + 'options' => $this->getAvailableCatalog(), + 'asChoice' => FALSE, + 'optionsCallback' => function () { + return $this->getAvailableCatalog(true); + }, + ]); + + $fields['template_url'] = new UrlField('Template URL', [ + 'optionName' => 'template_url', + 'conditions' => [ + 'template_option' => [ + 'Provide your own template url.' + ], + ], + 'description' => 'The template url', + 'questionLine' => 'What is the URL of the template?', + ]); + } + + $fields['initialize'] = new BooleanField('Initialize', [ + 'optionName' => 'initialized', + 'conditions' => [ + 'template_option' => [ + 'Provide your own template url.', + 'Choose a template from the catalog.', + ], + ], + 'description' => 'Initialize this environment?', + 'questionLine' => 'Initialize this environment?', + ]); return $fields; } From c78352032d766e3885856cf3f272897db7bb96f0 Mon Sep 17 00:00:00 2001 From: Shawna Date: Tue, 6 Aug 2019 09:01:03 -0400 Subject: [PATCH 05/16] cleaning up --- src/Command/Project/ProjectCreateCommand.php | 48 -------------------- 1 file changed, 48 deletions(-) diff --git a/src/Command/Project/ProjectCreateCommand.php b/src/Command/Project/ProjectCreateCommand.php index 81148389ef..2d8d932ab2 100644 --- a/src/Command/Project/ProjectCreateCommand.php +++ b/src/Command/Project/ProjectCreateCommand.php @@ -300,41 +300,6 @@ protected function getFields() 'questionLine' => '', 'default' => 'Untitled Project', ]); - - // $fields['template_option'] = new OptionsField('Template Options', [ - // 'optionName' => 'template_option', - // 'options' => [ - // 'Provide your own template url.', - // 'Choose a template from the catalog.', - // 'No template at this time.', - // ], - // 'description' => 'Choose a template, provide a url or choose not to use one.', - // 'includeAsOption' => false, - // ]); - // $fields['catalog_url'] = new OptionsField('Catalog', [ - // 'optionName' => 'catalog_url', - // 'conditions' => [ - // 'template_option' => [ - // 'Choose a template from the catalog.' - // ], - // ], - // 'description' => 'The template from which to create your project or your own blank project.', - // 'options' => $this->getAvailableCatalog(), - // 'asChoice' => FALSE, - // 'optionsCallback' => function () { - // return $this->getAvailableCatalog(true); - // }, - // ]); - // $fields['template_url'] = new UrlField('Template URL', [ - // 'optionName' => 'template_url', - // 'conditions' => [ - // 'template_option' => [ - // 'Provide your own template url.' - // ], - // ], - // 'description' => 'The template url', - // 'questionLine' => 'What is the URL of the template?', - // ]); $fields['region'] = new OptionsField('Region', [ 'optionName' => 'region', 'description' => 'The region where the project will be hosted', @@ -368,18 +333,6 @@ protected function getFields() return is_numeric($value) && $value > 0 && $value < 1024; }, ]); - // $fields['initialize'] = new BooleanField('Initialize', [ - // 'optionName' => 'initialized', - // 'conditions' => [ - // 'template_option' => [ - // 'Provide your own template url.', - // 'Choose a template from the catalog.', - // ], - // ], - // 'description' => 'Initialize this environment?', - // 'questionLine' => 'Initialize this environment?', - // ]); - return $fields; } @@ -430,7 +383,6 @@ protected function getTemplateFields($url_provided) 'questionLine' => 'What is the URL of the template?', ]); } - $fields['initialize'] = new BooleanField('Initialize', [ 'optionName' => 'initialized', 'conditions' => [ From fca2eeff3c6753b9d6805ba5d1516e8989664420 Mon Sep 17 00:00:00 2001 From: Shawna Date: Fri, 30 Aug 2019 07:04:36 -0400 Subject: [PATCH 06/16] moving catalog to end of create subscription for backwards compatibility --- src/Command/Project/ProjectCreateCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Command/Project/ProjectCreateCommand.php b/src/Command/Project/ProjectCreateCommand.php index 2d8d932ab2..c9349611a5 100644 --- a/src/Command/Project/ProjectCreateCommand.php +++ b/src/Command/Project/ProjectCreateCommand.php @@ -106,12 +106,12 @@ protected function execute(InputInterface $input, OutputInterface $output) $subscription = $this->api()->getClient() ->createSubscription( - $options['catalog'], $options['region'], $options['plan'], $options['title'], $options['storage'] * 1024, - $options['environments'] + $options['environments'], + $options['catalog'] ); $this->api()->clearProjectsCache(); From 6b3dca3775f00476ee56ac199b51bb757da33702 Mon Sep 17 00:00:00 2001 From: Shawna Date: Tue, 3 Sep 2019 05:29:45 -0400 Subject: [PATCH 07/16] some of the updates requested by Patrick still working on cleaning up this branch --- config.yaml | 2 + src/Command/Project/ProjectCreateCommand.php | 117 ++++++++++--------- 2 files changed, 61 insertions(+), 58 deletions(-) diff --git a/config.yaml b/config.yaml index 7a94c96b95..ce60f6627b 100644 --- a/config.yaml +++ b/config.yaml @@ -80,6 +80,8 @@ service: - standard - medium - large + catalog: + - https://github.com/platformsh/template-drupal7-vanilla/blob/master/.platform.template.yaml # Configuration relating to API calls. # This can be overridden in the user config file. diff --git a/src/Command/Project/ProjectCreateCommand.php b/src/Command/Project/ProjectCreateCommand.php index c9349611a5..9df1cb4956 100644 --- a/src/Command/Project/ProjectCreateCommand.php +++ b/src/Command/Project/ProjectCreateCommand.php @@ -60,23 +60,19 @@ protected function execute(InputInterface $input, OutputInterface $output) { /** @var \Platformsh\Cli\Service\QuestionHelper $questionHelper */ $questionHelper = $this->getService('question_helper'); - $options = $this->form->resolveOptions($input, $output, $questionHelper); - - if ($input->getOption('template') !== false) { - if (empty(parse_url($input->getOption('template'), PHP_URL_PATH))) { + $template = $input->getOption('template'); + if ($template !== false) { + if (empty(parse_url($template, PHP_URL_PATH))) { $temp_provided = true; } else { $temp_provided = false; } $this->template_form = Form::fromArray($this->getTemplateFields($temp_provided)); - $this->template_form->configureInputDefinition($this->getDefinition()); $template_options = $this->template_form->resolveOptions($input, $output, $questionHelper); - } - $estimate = $this->api() ->getClient() ->getSubscriptionEstimate($options['plan'], $options['storage'], $options['environments'], 1); @@ -174,8 +170,8 @@ protected function execute(InputInterface $input, OutputInterface $output) if ($template_options['initialize']) { // Use the existing initialize command. - $project = $this->selectProject($subscription->project_id); - $environment = $this->api()->getEnvironment('master', $project, null, true); + $project = $this->api()->getProject($subscription->project_id); + $environment = $this->api()->getEnvironment('master', $project); $environment->initialize($subscription->project_options['initialize']['profile'], $subscription->project_options['initialize']['repository']); $this->api()->clearEnvironmentsCache($environment->project); $this->stdErr->writeln("The project has been initialized and is ready!"); @@ -195,7 +191,6 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->stdErr->writeln(" Project title: {$subscription->project_title}"); $this->stdErr->writeln(" URL: {$subscription->project_ui}"); - return 0; } @@ -272,9 +267,12 @@ protected function getAvailableCatalog($runtime = false) { if ($runtime) { $catalog = []; - foreach ($this->api()->getClient()->getCatalog()->getData() as $item) { - if ($item['info'] && $item['template']) { - $catalog[$item['template']] = $item['info']['name']; + $catalog_items = $this->api()->getClient()->getCatalog()->getData(); + if (!empty($catalog_items)) { + foreach ($catalog_items as $item) { + if (isset($item['info']) && isset($item['template'])) { + $catalog[$item['template']] = $item['info']['name']; + } } } $catalog['empty'] = 'Empty Project'; @@ -282,6 +280,10 @@ protected function getAvailableCatalog($runtime = false) $catalog = (array) $this->config()->get('service.catalog'); } + if (empty($catalog)) { + // Should we throw an error here? Do we want to kill the process? + } + return $catalog; } @@ -292,48 +294,47 @@ protected function getAvailableCatalog($runtime = false) */ protected function getFields() { - $fields = []; - - $fields['title'] = new Field('Project title', [ - 'optionName' => 'title', - 'description' => 'The initial project title', - 'questionLine' => '', - 'default' => 'Untitled Project', - ]); - $fields['region'] = new OptionsField('Region', [ - 'optionName' => 'region', - 'description' => 'The region where the project will be hosted', - 'options' => $this->getAvailableRegions(), - 'optionsCallback' => function () { - return $this->getAvailableRegions(true); - }, - ]); - $fields['plan'] = new OptionsField('Plan', [ - 'optionName' => 'plan', - 'description' => 'The subscription plan', - 'options' => $this->getAvailablePlans(), - 'optionsCallback' => function () { - return $this->getAvailablePlans(true); - }, - 'default' => in_array('development', $this->getAvailablePlans()) ? 'development' : null, - 'allowOther' => true, - ]); - $fields['environments'] = new Field('Environments', [ - 'optionName' => 'environments', - 'description' => 'The number of environments', - 'default' => 3, - 'validator' => function ($value) { - return is_numeric($value) && $value > 0 && $value < 50; - }, - ]); - $fields['storage'] = new Field('Storage', [ - 'description' => 'The amount of storage per environment, in GiB', - 'default' => 5, - 'validator' => function ($value) { - return is_numeric($value) && $value > 0 && $value < 1024; - }, - ]); - return $fields; + return [ + 'title' => new Field('Project title', [ + 'optionName' => 'title', + 'description' => 'The initial project title', + 'questionLine' => '', + 'default' => 'Untitled Project', + ]), + 'region' => new OptionsField('Region', [ + 'optionName' => 'region', + 'description' => 'The region where the project will be hosted', + 'options' => $this->getAvailableRegions(), + 'optionsCallback' => function () { + return $this->getAvailableRegions(true); + }, + ]), + 'plan' => new OptionsField('Plan', [ + 'optionName' => 'plan', + 'description' => 'The subscription plan', + 'options' => $this->getAvailablePlans(), + 'optionsCallback' => function () { + return $this->getAvailablePlans(true); + }, + 'default' => in_array('development', $this->getAvailablePlans()) ? 'development' : null, + 'allowOther' => true, + ]), + 'environments' => new Field('Environments', [ + 'optionName' => 'environments', + 'description' => 'The number of environments', + 'default' => 3, + 'validator' => function ($value) { + return is_numeric($value) && $value > 0 && $value < 50; + }, + ]), + 'storage' => new Field('Storage', [ + 'description' => 'The amount of storage per environment, in GiB', + 'default' => 5, + 'validator' => function ($value) { + return is_numeric($value) && $value > 0 && $value < 1024; + }, + ]), + ]; } /** @@ -357,7 +358,7 @@ protected function getTemplateFields($url_provided) 'includeAsOption' => false, ]); - $fields['catalog_url'] = new OptionsField('Catalog', [ + $fields['template_url_from_catalog'] = new OptionsField('Template (from a catalog)', [ 'optionName' => 'catalog_url', 'conditions' => [ 'template_option' => [ @@ -391,8 +392,8 @@ protected function getTemplateFields($url_provided) 'Choose a template from the catalog.', ], ], - 'description' => 'Initialize this environment?', - 'questionLine' => 'Initialize this environment?', + 'description' => 'Initialize this project from a template?', + 'questionLine' => 'Initialize this project from a template?', ]); return $fields; From 422d38d8fa8a00d048f6b2b35b92dfc541c3803e Mon Sep 17 00:00:00 2001 From: Shawna Date: Tue, 3 Sep 2019 06:28:55 -0400 Subject: [PATCH 08/16] error handling around the initialize flag if required information is not present --- src/Command/Project/ProjectCreateCommand.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Command/Project/ProjectCreateCommand.php b/src/Command/Project/ProjectCreateCommand.php index 9df1cb4956..0ce458b196 100644 --- a/src/Command/Project/ProjectCreateCommand.php +++ b/src/Command/Project/ProjectCreateCommand.php @@ -19,6 +19,9 @@ class ProjectCreateCommand extends CommandBase /** @var Form */ protected $form; + /** @var Form */ + protected $template_form; + /** * {@inheritdoc} */ @@ -172,9 +175,15 @@ protected function execute(InputInterface $input, OutputInterface $output) // Use the existing initialize command. $project = $this->api()->getProject($subscription->project_id); $environment = $this->api()->getEnvironment('master', $project); - $environment->initialize($subscription->project_options['initialize']['profile'], $subscription->project_options['initialize']['repository']); - $this->api()->clearEnvironmentsCache($environment->project); - $this->stdErr->writeln("The project has been initialized and is ready!"); + if (isset($subscription->project_options['initialize']['profile']) && isset( $subscription->project_options['initialize']['repository'])) { + $environment->initialize($subscription->project_options['initialize']['profile'], $subscription->project_options['initialize']['repository']); + $this->api()->clearEnvironmentsCache($environment->project); + $this->stdErr->writeln("The project has been initialized and is ready!"); + } + else { + $this->stdErr->writeln("The project could not be initialized at this time due to missing profile and repository information."); + } + } else { $this->stdErr->writeln("The project is now ready!"); From 16b8b3c1df76e3ada8385c236a3307f2bd0bdb27 Mon Sep 17 00:00:00 2001 From: Shawna Date: Tue, 3 Sep 2019 06:31:02 -0400 Subject: [PATCH 09/16] function name change per patricks feedback --- src/Command/Project/ProjectCreateCommand.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Command/Project/ProjectCreateCommand.php b/src/Command/Project/ProjectCreateCommand.php index 0ce458b196..cee908c4ca 100644 --- a/src/Command/Project/ProjectCreateCommand.php +++ b/src/Command/Project/ProjectCreateCommand.php @@ -272,7 +272,7 @@ protected function getAvailableRegions($runtime = false) * * @return array */ - protected function getAvailableCatalog($runtime = false) + protected function getDefaultCatalog($runtime = false) { if ($runtime) { $catalog = []; @@ -375,10 +375,10 @@ protected function getTemplateFields($url_provided) ], ], 'description' => 'The template from which to create your project or your own blank project.', - 'options' => $this->getAvailableCatalog(), + 'options' => $this->getDefaultCatalog(), 'asChoice' => FALSE, 'optionsCallback' => function () { - return $this->getAvailableCatalog(true); + return $this->getDefaultCatalog(true); }, ]); From 400230144f04641a774d8393a811944741d4beac Mon Sep 17 00:00:00 2001 From: Shawna Date: Tue, 3 Sep 2019 06:36:17 -0400 Subject: [PATCH 10/16] some additional checking to make sure things dont error out for no reason --- src/Command/Project/ProjectCreateCommand.php | 23 +++++++++++--------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Command/Project/ProjectCreateCommand.php b/src/Command/Project/ProjectCreateCommand.php index cee908c4ca..80a1175018 100644 --- a/src/Command/Project/ProjectCreateCommand.php +++ b/src/Command/Project/ProjectCreateCommand.php @@ -65,6 +65,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $questionHelper = $this->getService('question_helper'); $options = $this->form->resolveOptions($input, $output, $questionHelper); $template = $input->getOption('template'); + if ($template !== false) { if (empty(parse_url($template, PHP_URL_PATH))) { $temp_provided = true; @@ -172,18 +173,20 @@ protected function execute(InputInterface $input, OutputInterface $output) } if ($template_options['initialize']) { - // Use the existing initialize command. + // Make sure nothing happens if the all important subscription info + // is not available for some reason. $project = $this->api()->getProject($subscription->project_id); - $environment = $this->api()->getEnvironment('master', $project); - if (isset($subscription->project_options['initialize']['profile']) && isset( $subscription->project_options['initialize']['repository'])) { - $environment->initialize($subscription->project_options['initialize']['profile'], $subscription->project_options['initialize']['repository']); - $this->api()->clearEnvironmentsCache($environment->project); - $this->stdErr->writeln("The project has been initialized and is ready!"); - } - else { - $this->stdErr->writeln("The project could not be initialized at this time due to missing profile and repository information."); + if (!empty($project)) { + $environment = $this->api()->getEnvironment('master', $project); + if (isset($subscription->project_options['initialize']['profile']) && isset( $subscription->project_options['initialize']['repository'])) { + $environment->initialize($subscription->project_options['initialize']['profile'], $subscription->project_options['initialize']['repository']); + $this->api()->clearEnvironmentsCache($environment->project); + $this->stdErr->writeln("The project has been initialized and is ready!"); + } + else { + $this->stdErr->writeln("The project could not be initialized at this time due to missing profile and repository information."); + } } - } else { $this->stdErr->writeln("The project is now ready!"); From 9a53654e03ed06e04a4cfd9591ed399fecf62c61 Mon Sep 17 00:00:00 2001 From: Shawna Date: Wed, 4 Sep 2019 10:34:07 -0400 Subject: [PATCH 11/16] cleaning up after a change per what Patrick noticed and making a small requested change --- src/Command/Project/ProjectCreateCommand.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/Command/Project/ProjectCreateCommand.php b/src/Command/Project/ProjectCreateCommand.php index 80a1175018..7be56eedbb 100644 --- a/src/Command/Project/ProjectCreateCommand.php +++ b/src/Command/Project/ProjectCreateCommand.php @@ -19,9 +19,6 @@ class ProjectCreateCommand extends CommandBase /** @var Form */ protected $form; - /** @var Form */ - protected $template_form; - /** * {@inheritdoc} */ @@ -97,10 +94,10 @@ protected function execute(InputInterface $input, OutputInterface $output) } // Grab the url of the yaml file. - if (!empty($template_options['catalog_url'])) { - $options['catalog'] = $template_options['catalog_url']; + if (!empty($template_options['template_url_from_catalog'])) { + $options['catalog'] = $template_options['template_url_from_catalog']; } - else if (!empty($template_options['catalog_url'])) { + else if (!empty($template_options['template_url'])) { $options['catalog'] = $template_options['template_url']; } @@ -371,7 +368,7 @@ protected function getTemplateFields($url_provided) ]); $fields['template_url_from_catalog'] = new OptionsField('Template (from a catalog)', [ - 'optionName' => 'catalog_url', + 'optionName' => 'template_url_from_catalog', 'conditions' => [ 'template_option' => [ 'Choose a template from the catalog.' From 11f3652e0134c3208dfb5543bd8de02acdef2c9b Mon Sep 17 00:00:00 2001 From: Shawna Date: Fri, 6 Sep 2019 06:06:14 -0400 Subject: [PATCH 12/16] plans and regions first looking to setup options api for a listing --- src/Command/Project/ProjectCreateCommand.php | 60 +++++++++++++------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/src/Command/Project/ProjectCreateCommand.php b/src/Command/Project/ProjectCreateCommand.php index ad4df76b9c..41f9ef86a4 100644 --- a/src/Command/Project/ProjectCreateCommand.php +++ b/src/Command/Project/ProjectCreateCommand.php @@ -170,23 +170,31 @@ protected function execute(InputInterface $input, OutputInterface $output) protected function getAvailablePlans($runtime = false) { static $plans; - if (is_array($plans)) { - return $plans; + // Check for setup options. + $account = $this->api()->getMyAccount(true); + $setupOptions = $this->api()->getClient()->getSetupOptions(NULL, NULL, NULL, $account['username'], NULL); + if (isset($setupOptions) && !empty($setupOptions['plans'])) { + $plans = $setupOptions['plans']; } - - if (!$runtime) { - return (array) $this->config()->get('service.available_plans'); - } - - $plans = []; - foreach ($this->api()->getClient()->getPlans() as $plan) { - if ($plan->hasProperty('price', false)) { - $plans[$plan->name] = sprintf('%s (%s)', $plan->label, $plan->price->__toString()); - } else { - $plans[$plan->name] = $plan->label; + else { + if (is_array($plans)) { + return $plans; + } + + if (!$runtime) { + return (array) $this->config()->get('service.available_plans'); + } + + $plans = []; + foreach ($this->api()->getClient()->getPlans() as $plan) { + if ($plan->hasProperty('price', false)) { + $plans[$plan->name] = sprintf('%s (%s)', $plan->label, $plan->price->__toString()); + } else { + $plans[$plan->name] = $plan->label; + } } + } - return $plans; } @@ -202,17 +210,25 @@ protected function getAvailablePlans($runtime = false) */ protected function getAvailableRegions($runtime = false) { - if ($runtime) { - $regions = []; - foreach ($this->api()->getClient()->getRegions() as $region) { - if ($region->available) { - $regions[$region->id] = $region->label; + // Check for setup options. + $account = $this->api()->getMyAccount(true); + print_r($account); + $setupOptions = $this->api()->getClient()->getSetupOptions(NULL, NULL, NULL, $account['username'], NULL); + if (isset($setupOptions) && !empty($setupOptions['regions'])) { + $regions = $setupOptions['regions']; + } + else { + if ($runtime) { + $regions = []; + foreach ($this->api()->getClient()->getRegions() as $region) { + if ($region->available) { + $regions[$region->id] = $region->label; + } } + } else { + $regions = (array) $this->config()->get('service.available_regions'); } - } else { - $regions = (array) $this->config()->get('service.available_regions'); } - return $regions; } From 69bab98313b6d97b8ebb226e4de66cd1b3c2b2a4 Mon Sep 17 00:00:00 2001 From: Shawna Date: Wed, 11 Sep 2019 13:47:50 -0400 Subject: [PATCH 13/16] removing a print --- src/Command/Project/ProjectCreateCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Command/Project/ProjectCreateCommand.php b/src/Command/Project/ProjectCreateCommand.php index 41f9ef86a4..57955547b8 100644 --- a/src/Command/Project/ProjectCreateCommand.php +++ b/src/Command/Project/ProjectCreateCommand.php @@ -212,7 +212,7 @@ protected function getAvailableRegions($runtime = false) { // Check for setup options. $account = $this->api()->getMyAccount(true); - print_r($account); + // print_r($account); $setupOptions = $this->api()->getClient()->getSetupOptions(NULL, NULL, NULL, $account['username'], NULL); if (isset($setupOptions) && !empty($setupOptions['regions'])) { $regions = $setupOptions['regions']; From d25b5b8db25932ab9c8af41768034730bdcf5b4d Mon Sep 17 00:00:00 2001 From: Shawna Date: Mon, 16 Sep 2019 07:45:50 -0400 Subject: [PATCH 14/16] removing the empty catalog option still not sure if we need a error messaging around an empty catalog --- config.yaml | 2 +- src/Command/Project/ProjectCreateCommand.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/config.yaml b/config.yaml index ce60f6627b..f1738e700d 100644 --- a/config.yaml +++ b/config.yaml @@ -81,7 +81,7 @@ service: - medium - large catalog: - - https://github.com/platformsh/template-drupal7-vanilla/blob/master/.platform.template.yaml + - http://accounts.platform.sh/sites/default/files/catalog/catalog-psh.yaml # Configuration relating to API calls. # This can be overridden in the user config file. diff --git a/src/Command/Project/ProjectCreateCommand.php b/src/Command/Project/ProjectCreateCommand.php index 7be56eedbb..13542827cf 100644 --- a/src/Command/Project/ProjectCreateCommand.php +++ b/src/Command/Project/ProjectCreateCommand.php @@ -284,7 +284,6 @@ protected function getDefaultCatalog($runtime = false) } } } - $catalog['empty'] = 'Empty Project'; } else { $catalog = (array) $this->config()->get('service.catalog'); } From 22e4abb94a6419e3ebbd4f333f18ebefa4748051 Mon Sep 17 00:00:00 2001 From: Shawna Date: Mon, 16 Sep 2019 15:31:03 -0400 Subject: [PATCH 15/16] merging in PR 852 and adding error checking on the yaml that is passed and automatically initializing projects that allow for it --- src/Command/Project/ProjectCreateCommand.php | 67 ++++++++------------ 1 file changed, 25 insertions(+), 42 deletions(-) diff --git a/src/Command/Project/ProjectCreateCommand.php b/src/Command/Project/ProjectCreateCommand.php index 526a4a4822..99f3214060 100644 --- a/src/Command/Project/ProjectCreateCommand.php +++ b/src/Command/Project/ProjectCreateCommand.php @@ -64,13 +64,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $template = $input->getOption('template'); if ($template !== false) { - if (empty(parse_url($template, PHP_URL_PATH))) { - $temp_provided = true; - } - else { - $temp_provided = false; - } - $this->template_form = Form::fromArray($this->getTemplateFields($temp_provided)); + $this->template_form = Form::fromArray($this->getTemplateFields($template)); $template_options = $this->template_form->resolveOptions($input, $output, $questionHelper); } @@ -94,13 +88,13 @@ protected function execute(InputInterface $input, OutputInterface $output) } // Grab the url of the yaml file. - if (!empty($template_options['template_url_from_catalog'])) { - $options['catalog'] = $template_options['template_url_from_catalog']; + if (isset($template_options['template_url_from_catalog'])) { + $options['template'] = $template_options['template_url_from_catalog']; } - else if (!empty($template_options['template_url'])) { - $options['catalog'] = $template_options['template_url']; + else if (isset($template)) { + $options['template'] = $template; } - + $subscription = $this->api()->getClient() ->createSubscription( $options['region'], @@ -108,7 +102,8 @@ protected function execute(InputInterface $input, OutputInterface $output) $options['title'], $options['storage'] * 1024, $options['environments'], - $options['catalog'] + $options['activationCallback'], + $options['template'] ); $this->api()->clearProjectsCache(); @@ -169,20 +164,16 @@ protected function execute(InputInterface $input, OutputInterface $output) return 1; } - if ($template_options['initialize']) { - // Make sure nothing happens if the all important subscription info - // is not available for some reason. + if (isset($subscription->project_options['initialize']['profile']) && isset($subscription->project_options['initialize']['repository'])) { $project = $this->api()->getProject($subscription->project_id); - if (!empty($project)) { - $environment = $this->api()->getEnvironment('master', $project); - if (isset($subscription->project_options['initialize']['profile']) && isset( $subscription->project_options['initialize']['repository'])) { - $environment->initialize($subscription->project_options['initialize']['profile'], $subscription->project_options['initialize']['repository']); - $this->api()->clearEnvironmentsCache($environment->project); - $this->stdErr->writeln("The project has been initialized and is ready!"); - } - else { - $this->stdErr->writeln("The project could not be initialized at this time due to missing profile and repository information."); - } + $environment = $this->api()->getEnvironment('master', $project); + if (isset($project) && isset($environment)) { + $environment->initialize($subscription->project_options['initialize']['profile'], $subscription->project_options['initialize']['repository']); + $this->api()->clearEnvironmentsCache($environment->project); + $this->stdErr->writeln("The project has been initialized and is ready!"); + } + else { + $this->stdErr->writeln("The project could not be initialized at this time due to missing profile and repository information."); } } else { @@ -258,7 +249,6 @@ protected function getAvailableRegions($runtime = false) { // Check for setup options. $account = $this->api()->getMyAccount(true); - // print_r($account); $setupOptions = $this->api()->getClient()->getSetupOptions(NULL, NULL, NULL, $account['username'], NULL); if (isset($setupOptions) && !empty($setupOptions['regions'])) { $regions = $setupOptions['regions']; @@ -296,7 +286,7 @@ protected function getDefaultCatalog($runtime = false) if (!empty($catalog_items)) { foreach ($catalog_items as $item) { if (isset($item['info']) && isset($item['template'])) { - $catalog[$item['template']] = $item['info']['name']; + $catalog[] = $item['template']; } } } @@ -361,16 +351,20 @@ protected function getFields() ]; } - /** + /** * Returns a list of ConsoleForm form fields for this command. * * @return Field[] */ - protected function getTemplateFields($url_provided) + protected function getTemplateFields($url) { $fields = []; - if (!$url_provided) { + // If provided, check the template is valid. + if ($url != false) { + $this->api()->getClient()->getSetupOptions(NULL, NULL, $url, NULL, NULL); + } + if ($url == false) { $fields['template_option'] = new OptionsField('Template Options', [ 'optionName' => 'template_option', 'options' => [ @@ -408,17 +402,6 @@ protected function getTemplateFields($url_provided) 'questionLine' => 'What is the URL of the template?', ]); } - $fields['initialize'] = new BooleanField('Initialize', [ - 'optionName' => 'initialized', - 'conditions' => [ - 'template_option' => [ - 'Provide your own template url.', - 'Choose a template from the catalog.', - ], - ], - 'description' => 'Initialize this project from a template?', - 'questionLine' => 'Initialize this project from a template?', - ]); return $fields; } From 3b39002f8c2aeb9472d104d49840347894dab1ab Mon Sep 17 00:00:00 2001 From: Shawna Date: Mon, 16 Sep 2019 15:42:27 -0400 Subject: [PATCH 16/16] cleaning the file up a little --- src/Command/Project/ProjectCreateCommand.php | 31 ++++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/Command/Project/ProjectCreateCommand.php b/src/Command/Project/ProjectCreateCommand.php index 99f3214060..3c4a1498bd 100644 --- a/src/Command/Project/ProjectCreateCommand.php +++ b/src/Command/Project/ProjectCreateCommand.php @@ -63,6 +63,8 @@ protected function execute(InputInterface $input, OutputInterface $output) $options = $this->form->resolveOptions($input, $output, $questionHelper); $template = $input->getOption('template'); + // This will present the user with an additional form that will allow + // them to choose from a catalog if they add only --template with a url. if ($template !== false) { $this->template_form = Form::fromArray($this->getTemplateFields($template)); $template_options = $this->template_form->resolveOptions($input, $output, $questionHelper); @@ -87,7 +89,6 @@ protected function execute(InputInterface $input, OutputInterface $output) return 1; } - // Grab the url of the yaml file. if (isset($template_options['template_url_from_catalog'])) { $options['template'] = $template_options['template_url_from_catalog']; } @@ -207,9 +208,10 @@ protected function execute(InputInterface $input, OutputInterface $output) protected function getAvailablePlans($runtime = false) { static $plans; - // Check for setup options. + // First look to the setup/options API for a plan list. $account = $this->api()->getMyAccount(true); $setupOptions = $this->api()->getClient()->getSetupOptions(NULL, NULL, NULL, $account['username'], NULL); + if (isset($setupOptions) && !empty($setupOptions['plans'])) { $plans = $setupOptions['plans']; } @@ -247,24 +249,27 @@ protected function getAvailablePlans($runtime = false) */ protected function getAvailableRegions($runtime = false) { - // Check for setup options. - $account = $this->api()->getMyAccount(true); - $setupOptions = $this->api()->getClient()->getSetupOptions(NULL, NULL, NULL, $account['username'], NULL); - if (isset($setupOptions) && !empty($setupOptions['regions'])) { - $regions = $setupOptions['regions']; - } - else { - if ($runtime) { + if ($runtime) { + // First look to the setup/options API for a region list. + $account = $this->api()->getMyAccount(true); + $setupOptions = $this->api()->getClient()->getSetupOptions(NULL, NULL, NULL, $account['username'], NULL); + if (isset($setupOptions) && !empty($setupOptions['regions'])) { + $regions = $setupOptions['regions']; + } + else { + // Fallback to the regions api. $regions = []; foreach ($this->api()->getClient()->getRegions() as $region) { if ($region->available) { $regions[$region->id] = $region->label; } } - } else { - $regions = (array) $this->config()->get('service.available_regions'); + } + } else { + $regions = (array) $this->config()->get('service.available_regions'); } + return $regions; } @@ -348,7 +353,7 @@ protected function getFields() return is_numeric($value) && $value > 0 && $value < 1024; }, ]), - ]; + ]; } /**