Skip to content

Commit

Permalink
Various updates to the code and more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
typhonius committed Mar 7, 2020
1 parent 7865e08 commit fc85c1c
Show file tree
Hide file tree
Showing 30 changed files with 694 additions and 305 deletions.
21 changes: 14 additions & 7 deletions README.md
Expand Up @@ -92,28 +92,35 @@ Some of the following commands have aliases for simplicity e.g. `environment:inf
[![asciicast](https://asciinema.org/a/178427.png)](https://asciinema.org/a/178427)

## Command Parameters
If a command takes an application UUID as a parameter, it can be provided in one of two manners:
* The Acquia hosting ID (without hosting realm) e.g. acquia
* The full Acquia hosting realm and ID e.g. prod:acquia
* The application UUID
*Application UUID*
If a command takes an application UUID as a parameter, it can be provided in one of three ways - see below for a description of hosting realm:
* The Acquia hosting ID on its own e.g. myacquiasite
* The full Acquia hosting realm and ID e.g. prod:myacquiasite
* The application UUID e.g. 8ff6c046-ec64-4ce4-bea6-27845ec18600

Both the UUID and the hosting ID (with realm) for your applications may be discovered by running `./bin/acquiacli application:list`
*Hosting Realms*
Acquia uses the concept of a 'realm' to differentiate between customers on the two tiers of hosting offered:
* prod: The 'prod' realm is exclusively for Acquia Cloud Enterprise (ACE) customers.
* devcloud: The 'devcloud' realm is exclusively for Acquia Cloud Professional (ACP) customers.

If no hosting realm is provided, prod is used by default. This can be overridden in the command by specifying a realm e.g. `--realm=devcloud`

Both the UUID and the hosting ID (with realm) for your applications may be discovered by running `./bin/acquiacli application:list`

*Other parameters*
Environment parameters take the label name of the environment e.g. dev
Organization parameters take the label name of the organization e.g. mycompany

All other parameters are currently provided in the UUID form, including but not limited to:
* Role ID
* Team ID
* Organization ID

Commands using the following parameters will be automatically converted by the Acquia Cli tool using the SDK. This is achieved in the `validateUuidHook` method in the `AcquiaCommand` class using a `@hook validate` [annotation](https://github.com/consolidation/annotated-command).
* `$uuid` is converted to the UUID of the application
* `$environment` is converted into an EnvironmentResponse object
* `$environmentFrom` is converted into an EnvironmentResponse object
* `$environmentTo` is converted into an EnvironmentResponse object

* `$organization` is converted into an OrganizationResponse object

## Creating a Phar
A phar archive can be created to run Acquia Cli instead of utilising the entire codebase. Because some of Acquia Cli relies on user configuration of email/password, it is currently most appropriate to allow users to generate their own phar files inclusive of their own configuration.
Expand Down
1 change: 1 addition & 0 deletions VERSION
@@ -0,0 +1 @@
2.0.0-dev
47 changes: 23 additions & 24 deletions bin/acquiacli-robo.php
Expand Up @@ -2,44 +2,43 @@

use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Robo\Config\Config;
use Consolidation\Config\Loader\ConfigProcessor;
use Consolidation\Config\Loader\YamlConfigLoader;

use AcquiaCli\Config;
use AcquiaCli\AcquiaCli;
use AcquiaCli\CloudApi;

use AcquiaCloudApi\Connector\Connector;
use AcquiaCloudApi\Connector\Client;

if (strpos(basename(__FILE__), 'phar')) {
$pharPath = \Phar::running(true);
if ($pharPath) {
$root = __DIR__;
require_once 'phar://acquiacli.phar/vendor/autoload.php';
$autoloaderPath = "$pharPath/vendor/autoload.php";
} else {
if (file_exists(dirname(__DIR__).'/vendor/autoload.php')) {
$root = dirname(__DIR__);
require_once dirname(__DIR__) . '/vendor/autoload.php';
} elseif (file_exists(dirname(__DIR__) . '/../../autoload.php')) {
$autoloaderPath = dirname(__DIR__).'/vendor/autoload.php';
} elseif (file_exists(dirname(__DIR__).'/../../autoload.php')) {
$root = dirname(__DIR__) . '/../../..';
require_once dirname(__DIR__) . '/../../autoload.php';
$autoloaderPath = dirname(__DIR__) . '/../../autoload.php';
} else {
$root = __DIR__;
require_once 'phar://acquiacli.phar/vendor/autoload.php';
die("Could not find autoloader. Run 'composer install'.");
}
}
$classLoader = require $autoloaderPath;

$config = new Config();
$loader = new YamlConfigLoader();
$processor = new ConfigProcessor();

$globalConfig = getenv('HOME') . '/.acquiacli/acquiacli.yml';
$projectConfig = $root . '/acquiacli.yml';
$config = new Config($root);

$processor->extend($loader->load(dirname(__DIR__) . '/default.acquiacli.yml'));
$processor->extend($loader->load($globalConfig));
$processor->extend($loader->load($projectConfig));

$config->import($processor->export());
$config->set('config.project', $projectConfig);
$config->set('config.global', $globalConfig);
// Instantiate CloudApi client
$cloudapi = new CloudApi($config);
$client = $cloudapi->createClient();

// Set up input and output parameters
$argv = $_SERVER['argv'];
$input = new ArgvInput($argv);
$output = new ConsoleOutput();
$app = new AcquiaCli($config, $input, $output);

// Create and run AcquiaCli instance
$app = new AcquiaCli($config, $client, $input, $output);
$statusCode = $app->run($input, $output);
exit($statusCode);
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 25 additions & 7 deletions src/AcquiaCli.php
Expand Up @@ -13,6 +13,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Consolidation\AnnotatedCommand\CommandFileDiscovery;
use AcquiaCloudApi\Connector\Client;

/**
* Class AcquiaCli
Expand All @@ -27,20 +28,19 @@ class AcquiaCli

const NAME = 'AcquiaCli';

const VERSION = '2.0.0-dev';

/**
* AcquiaCli constructor.
* @param Config $config
* @param InputInterface|null $input
* @param OutputInterface|null $output
*/
public function __construct(Config $config, InputInterface $input = null, OutputInterface $output = null)
public function __construct(Config $config, Client $client, InputInterface $input = null, OutputInterface $output = null)
{
$version = trim(file_get_contents(dirname(__DIR__) . '/VERSION'));

// Create application.
$this->setConfig($config);
$application = new Application(self::NAME, self::VERSION);
$application = new Application(self::NAME, $version);

$application->getDefinition()->addOptions([
new InputOption(
Expand All @@ -65,9 +65,7 @@ public function __construct(Config $config, InputInterface $input = null, Output
]);

// Create and configure container.
$container = Robo::createDefaultContainer($input, $output, $application, $config);
$container->add('cloudApi', \AcquiaCli\CloudApi::class)
->withArgument('config');
$container = $this->getContainer($input, $output, $application, $config, $client);

$discovery = new CommandFileDiscovery();
$discovery->setSearchPattern('*Command.php');
Expand All @@ -79,6 +77,26 @@ public function __construct(Config $config, InputInterface $input = null, Output
$this->runner->registerCommandClasses($application, $commandClasses);
}

public function getContainer($input, $output, $application, $config, $client)
{
$container = Robo::createDefaultContainer($input, $output, $application, $config);
$container->add('client', $client);

$container->add('cloudApi', \AcquiaCli\CloudApi::class)
->withArgument('config')
->withArgument('client');

$parameterInjection = $container->get('parameterInjection');
$parameterInjection->register('AcquiaCli\CloudApi', new \AcquiaCli\Injector\AcquiaCliInjector);
$parameterInjection->register('AcquiaCloudApi\Endpoints\Applications', new \AcquiaCli\Injector\AcquiaCliInjector);
$parameterInjection->register('AcquiaCloudApi\Endpoints\Environments', new \AcquiaCli\Injector\AcquiaCliInjector);
$parameterInjection->register('AcquiaCloudApi\Endpoints\Databases', new \AcquiaCli\Injector\AcquiaCliInjector);
$parameterInjection->register('AcquiaCloudApi\Endpoints\Servers', new \AcquiaCli\Injector\AcquiaCliInjector);
$parameterInjection->register('AcquiaCloudApi\Endpoints\Domains', new \AcquiaCli\Injector\AcquiaCliInjector);

return $container;
}

/**
* @param InputInterface $input
* @param OutputInterface $output
Expand Down
66 changes: 49 additions & 17 deletions src/CloudApi.php
Expand Up @@ -6,47 +6,79 @@
use AcquiaCloudApi\Connector\Connector;
use AcquiaCloudApi\Endpoints\Applications;
use AcquiaCloudApi\Endpoints\Environments;
use Robo\Config\Config;


/**
* Class CloudApi
* @package AcquiaCli
*/
final class CloudApi
class CloudApi
{

private $cloudapi;
protected $client;

private $extraConfig;
protected $extraConfig;

public function __construct($config)
{
protected $acquia;

public function __construct(Config $config)
{
$this->extraConfig = $config->get('extraconfig');
$acquia = $config->get('acquia');
$this->acquia = $config->get('acquia');
}

public function createClient()
{

if (getenv('ACQUIACLI_KEY') && getenv('ACQUIACLI_SECRET')) {
$acquia['key'] = getenv('ACQUIACLI_KEY');
$acquia['secret'] = getenv('ACQUIACLI_SECRET');
$this->acquia['key'] = getenv('ACQUIACLI_KEY');
$this->acquia['secret'] = getenv('ACQUIACLI_SECRET');
}

$connector = new Connector([
'key' => $acquia['key'],
'secret' => $acquia['secret'],
'key' => $this->acquia['key'],
'secret' => $this->acquia['secret'],
]);
/** @var \AcquiaCloudApi\Connector\Client $cloudapi */
$cloudapi = Client::factory($connector);
$client = Client::factory($connector);

$this->setClient($client);

$this->setCloudApi($cloudapi);
return $client;
}

public function getCloudApi()
/**
* @param string $uuid
* @param string $environment
* @return EnvironmentResponse
* @throws Exception
*/
public function getEnvironment($uuid, $environment)
{
return $this->cloudapi;
$environmentsAdapter = new Environments($this->client);
$environments = $environmentsAdapter->getAll($uuid);

foreach ($environments as $e) {
if ($environment === $e->name) {
return $e;
}
}

throw new Exception('Unable to find ID for environment');
}

public function getClient()
{
if (!$this->client) {
$this->createClient();
}
return $this->client;
}

public function setCloudApi($cloudapi)
protected function setClient($client)
{
$this->cloudapi = $cloudapi;
$this->client = $client;
}

public function getExtraConfig()
Expand Down

0 comments on commit fc85c1c

Please sign in to comment.