-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CORE-1576] Commands to set and unset primary domain. (#2011)
* Commands to set and unset primary domain. WIP: needs tests. * Tests for setting/resetting primary domain and its commands. * Rename command to remove primary domain to `domain:primary:reset` for consistency. * Refactor set_primary_domain workflow maker out of a Command to Models\Environment class. * CS fix * Filter pantheonsite.io domains from interactive choice list. While the platform supports setting these domains as primary, it seems highly unlikely that a user would do this intentionally, and more likely that if it were chosen from a multiple choice list, that it was just fat-fingered. For better UX, they are therefore excluded. Users that really mean to do this can do so by typing/passing the full pantheonsite.io domain non-interactively to the domain:primary:set command. * Add primary domain to Domain model and domain:list command. * UX review: Change command to domain:primary:unset, log completion. * Changes that are clear from @TeslaDethray's requests. * Rename domain:primary:set to domain:primary:add per consensus of product and technical leadership. * Best guess at new model refactor request * Fix failing tests in older minor versions of php 7.0 - 7.3. It seems substr_compare issues a warning iff the first parameter is the empty string in these earlier minor releases; manual states it was fixed in 7.2.18. Since a) it's been fixed and b) empty strings are not expected real-world input, I've simply removed that input from the test coverage. * Behat tests for add and remove primary domain * Make behat.yml a bit more yaml-compliant (% not allowed as beginning of string) * Restore Consolidation\\Filter\\Hooks\\FilterHooks to command list * Changelog for 2.2.0 * Fix stated version in changelog
- Loading branch information
1 parent
15b9174
commit 6bddde1
Showing
15 changed files
with
656 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
|
||
namespace Pantheon\Terminus\Commands\Domain\Primary; | ||
|
||
use Consolidation\AnnotatedCommand\AnnotationData; | ||
use Pantheon\Terminus\Commands\TerminusCommand; | ||
use Pantheon\Terminus\Commands\WorkflowProcessingTrait; | ||
use Pantheon\Terminus\Models\Environment; | ||
use Pantheon\Terminus\Models\Site; | ||
use Pantheon\Terminus\Site\SiteAwareInterface; | ||
use Pantheon\Terminus\Site\SiteAwareTrait; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* Class AddCommand | ||
* @package Pantheon\Terminus\Commands\Domain\Primary | ||
*/ | ||
class AddCommand extends TerminusCommand implements SiteAwareInterface | ||
{ | ||
use SiteAwareTrait; | ||
use WorkflowProcessingTrait; | ||
|
||
const PLATFORM_DOMAIN = '.pantheonsite.io'; | ||
|
||
/** | ||
* Sets a domain associated to the environment as primary, causing all traffic to redirect to it. | ||
* | ||
* @authorize | ||
* | ||
* @command domain:primary:add | ||
* | ||
* @param string $site_env Site & environment in the format `site-name.env` | ||
* @param string $domain A domain that has been associated to your site. Optional when running interactively. | ||
* | ||
* @usage domain:primary:add <site_env> | ||
*/ | ||
public function add($site_env, $domain) | ||
{ | ||
/** | ||
* @var $site Site | ||
* @var $env Environment | ||
*/ | ||
list($site, $env) = $this->getSiteEnv($site_env); | ||
|
||
// The primary domain is set via a workflow so as to use workflow logging to track changes & update policy docs. | ||
$workflow = $env->getPrimaryDomainModel()->setPrimaryDomain($domain); | ||
$this->processWorkflow($workflow); | ||
$this->log()->notice( | ||
'Set {domain} as primary for {site}.{env}', | ||
['domain' => $domain, 'site' => $site->get('name'), 'env' => $env->id,] | ||
); | ||
} | ||
|
||
/** | ||
* Prompt the user for the domain, if it was not specified. | ||
* | ||
* n.b. This hook is not called in --no-interaction mode. | ||
* | ||
* @param InputInterface $input | ||
* @param OutputInterface $output | ||
* @param AnnotationData $annotationData | ||
* | ||
* @hook interact domain:primary:add | ||
*/ | ||
public function interact(InputInterface $input, OutputInterface $output, AnnotationData $annotationData) | ||
{ | ||
$domain = $input->getArgument('domain'); | ||
if (empty($domain)) { | ||
/** | ||
* @var $site Site | ||
* @var $env Environment | ||
*/ | ||
list($site, $env) = $this->getSiteEnv($input->getArgument('site_env')); | ||
$domains = $this->filterPlatformDomains($env->getDomains()->ids()); | ||
sort($domains); | ||
|
||
if (!empty($domains)) { | ||
$domain = $this->io()->choice('Select the primary domain for this site', $domains); | ||
$input->setArgument('domain', $domain); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Filters strings ending in the platform domain from an array. | ||
* | ||
* @param $domains | ||
* @return array | ||
*/ | ||
public function filterPlatformDomains($domains) | ||
{ | ||
return array_filter($domains, function ($domain) { | ||
return substr_compare($domain, self::PLATFORM_DOMAIN, -strlen(self::PLATFORM_DOMAIN)) !== 0; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
|
||
namespace Pantheon\Terminus\Commands\Domain\Primary; | ||
|
||
use Pantheon\Terminus\Commands\TerminusCommand; | ||
use Pantheon\Terminus\Commands\WorkflowProcessingTrait; | ||
use Pantheon\Terminus\Models\Environment; | ||
use Pantheon\Terminus\Models\Site; | ||
use Pantheon\Terminus\Site\SiteAwareInterface; | ||
use Pantheon\Terminus\Site\SiteAwareTrait; | ||
|
||
class RemoveCommand extends TerminusCommand implements SiteAwareInterface | ||
{ | ||
use SiteAwareTrait; | ||
use WorkflowProcessingTrait; | ||
|
||
/** | ||
* Removes the primary designation from the primary domain in the site and environment. | ||
* | ||
* @authorize | ||
* | ||
* @command domain:primary:remove | ||
* @aliases domain:primary:rm | ||
* | ||
* @param string $site_env Site & environment in the format `site-name.env` | ||
*/ | ||
public function remove($site_env) | ||
{ | ||
/** | ||
* @var $site Site | ||
* @var $env Environment | ||
*/ | ||
list($site, $env) = $this->getSiteEnv($site_env); | ||
|
||
$workflow = $env->getPrimaryDomainModel()->removePrimaryDomain(); | ||
$this->processWorkflow($workflow); | ||
$this->log()->notice( | ||
'Primary domain has been removed from {site}.{env}', | ||
['site' => $site->get('name'), 'env' => $env->id,] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
|
||
namespace Pantheon\Terminus\Models; | ||
|
||
use Pantheon\Terminus\Friends\EnvironmentInterface; | ||
use Pantheon\Terminus\Friends\EnvironmentTrait; | ||
|
||
class PrimaryDomain implements EnvironmentInterface | ||
{ | ||
use EnvironmentTrait; | ||
|
||
public function __construct(Environment $environment) | ||
{ | ||
$this->setEnvironment($environment); | ||
} | ||
|
||
/** | ||
* Builds a Workflow to set the primary domain for this environment. | ||
* | ||
* @param string $domain A domain name attached to this environment. | ||
* | ||
* @return Workflow | ||
*/ | ||
public function setPrimaryDomain($domain) | ||
{ | ||
return $this->workflowFactory($domain); | ||
} | ||
|
||
/** | ||
* Builds a workflow to remove the primary domain from this environment. | ||
* | ||
* @return Workflow | ||
*/ | ||
public function removePrimaryDomain() | ||
{ | ||
return $this->workflowFactory(null); | ||
} | ||
|
||
/** | ||
* @param string $domain | ||
* @return Workflow | ||
*/ | ||
protected function workflowFactory($domain) | ||
{ | ||
return $this->getEnvironment()->getWorkflows()->create( | ||
'set_primary_domain', | ||
[ | ||
'environment' => $this->getEnvironment()->id, | ||
'params' => ['primary_domain' => $domain] | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
Feature: Adding a primary domain to an environment | ||
In order to redirect all visitors to my preferred domain | ||
As a user | ||
I need to be able to manage primary domains attached to my site's environments | ||
|
||
Background: I am authenticated and have a site named [[test_site_name]] | ||
Given I am authenticated | ||
And a site named "[[test_site_name]]" | ||
|
||
@vcr domain-primary-add.yml | ||
Scenario: Adding a primary domain to an environment | ||
When I run "terminus domain:primary:add [[test_site_name]].live testdomain.com" | ||
Then I should get "." | ||
And I should get "." | ||
Then I should get: | ||
""" | ||
Set testdomain.com as primary for [[test_site_name]].live | ||
""" | ||
|
||
@vcr domain-primary-add.yml | ||
Scenario: Removing a primary domain from an environment | ||
When I run "terminus domain:primary:remove [[test_site_name]].live" | ||
Then I should get "." | ||
And I should get "." | ||
Then I should get: | ||
""" | ||
Primary domain has been removed from [[test_site_name]].live | ||
""" |
Large diffs are not rendered by default.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
tests/unit_tests/Commands/Domain/Primary/AddCommandTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
|
||
namespace Pantheon\Terminus\UnitTests\Commands\Domain\Primary; | ||
|
||
use Pantheon\Terminus\Commands\Domain\Primary\AddCommand; | ||
use Pantheon\Terminus\UnitTests\Commands\Domain\Primary\PrimaryDomainCommandsTestBase; | ||
|
||
/** | ||
* Class AddCommandTest | ||
* Test suite class for Pantheon\Terminus\Commands\Domain\Primary\AddCommand | ||
* @package Pantheon\Terminus\UnitTests\Commands\Domain\Primary | ||
*/ | ||
class AddCommandTest extends PrimaryDomainCommandsTestBase | ||
{ | ||
protected function getSystemUnderTest() | ||
{ | ||
return new AddCommand(); | ||
} | ||
|
||
public function testAdd() | ||
{ | ||
$site_name = 'site_name'; | ||
$domain = 'some.domain'; | ||
$this->prepareTestSetReset( | ||
$domain, | ||
'Set {domain} as primary for {site}.{env}', | ||
['domain' => $domain, 'site' => $this->site->get('name'), 'env' => $this->environment->id] | ||
); | ||
|
||
$out = $this->command->add("$site_name.{$this->environment->id}", $domain); | ||
$this->assertNull($out); | ||
} | ||
|
||
public function testFilterPlatformDomains() | ||
{ | ||
$sys_under_test = new AddCommand(); | ||
$this->assertEquals( | ||
array_values([ | ||
'x', | ||
'something.com', | ||
'averylong.domain.ofsomeosrt.tld', | ||
]), | ||
array_values($sys_under_test->filterPlatformDomains( | ||
[ | ||
'x', | ||
'something.com', | ||
'dev-mikes-testsite.pantheonsite.io', | ||
'averylong.domain.ofsomeosrt.tld', | ||
] | ||
)) | ||
); | ||
} | ||
} |
Oops, something went wrong.