Skip to content

Commit

Permalink
Migrate audit suite to newer syntax.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiasco committed Nov 5, 2023
1 parent d70b711 commit b10ed16
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 172 deletions.
17 changes: 12 additions & 5 deletions src/Audit/AppInfoAnalysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

namespace Drutiny\Acquia\Audit;

use Drutiny\Sandbox\Sandbox;
use Drutiny\Acquia\Api\CloudApi;
use Drutiny\Attribute\DataProvider;
use Drutiny\Attribute\Deprecated;

/**
* Ensure an environment has custom domains set.
*/
#[Deprecated]
class AppInfoAnalysis extends CloudApiAnalysis {

/**
* @inheritdoc
*/
public function gather(Sandbox $sandbox) {
#[DataProvider(-1)]
public function gatherAppInfo() {
$calls['teams'] = [
'path' => "/applications/{acquia.cloud.application.uuid}/teams",
];
Expand All @@ -25,11 +29,15 @@ public function gather(Sandbox $sandbox) {

$this->setParameter('calls', $calls);
$this->setParameter('is_legacy', true);
parent::gather($sandbox);

}

#[DataProvider(1)]
public function processAppInfo(CloudApi $api) {

$members = [];
foreach ($this->get('teams')['_embedded']['items'] as $team) {
$team_members = $this->call(path: "/teams/{$team->uuid}/members", options: [
$team_members = $this->call($api, path: "/teams/{$team->uuid}/members", options: [
'query' => ['limit' => 100]
]);

Expand All @@ -45,5 +53,4 @@ public function gather(Sandbox $sandbox) {

$this->set('members', $members);
}

}
17 changes: 5 additions & 12 deletions src/Audit/ApplicationNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@

namespace Drutiny\Acquia\Audit;

use Drutiny\Sandbox\Sandbox;
use Drutiny\Attribute\DataProvider;
use Drutiny\Attribute\Deprecated;

/**
* Check to ensure Production Mode is enabled on Acquia Cloud.
*/
#[Deprecated]
class ApplicationNotifications extends CloudApiAnalysis {

public function configure():void
{
parent::configure();
$this->setDeprecated();
}

/**
* @inheritdoc
*/
public function gather(Sandbox $sandbox) {
#[DataProvider(-1)]
public function gatherNotifications() {
$filters = [
'created_at>=' . $this->reportingPeriodStart->format('c'),
'created_at<=' . $this->reportingPeriodEnd->format('c'),
Expand All @@ -32,6 +26,5 @@ public function gather(Sandbox $sandbox) {
];
$this->setParameter('calls', $calls);
$this->setParameter('is_legacy', true);
parent::gather($sandbox);
}
}
29 changes: 13 additions & 16 deletions src/Audit/ApplicationViewsAndVisits.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,28 @@

namespace Drutiny\Acquia\Audit;

use Drutiny\Acquia\Api\CloudApi;
use Drutiny\Sandbox\Sandbox;
use Drutiny\Attribute\DataProvider;
use Drutiny\Attribute\Parameter;
use Drutiny\Attribute\Deprecated;
use Drutiny\Attribute\Type;

/**
* Supply data around application views and visits.
*/
#[Parameter(
name: 'from',
description: 'A relative interval from the reporting-period-end to calculate the ApplicationUsageData from.',
default: '-1 month',
type: Type::STRING,
)]
#[Deprecated]
class ApplicationViewsAndVisits extends CloudApiAnalysis {

public function configure():void
{
$this->setDeprecated();
$this->addParameter(
'from',
static::PARAMETER_OPTIONAL,
'A relative interval from the reporting-period-end to calculate the ApplicationUsageData from.',
'-1 month'
);
parent::configure();
}

/**
* @inheritdoc
*/
public function gather(Sandbox $sandbox) {
#[DataProvider(-1)]
public function gatherViewsAndVisits() {
// Use the from parameter if present, otherwise base the from date based
// on the reporting period start time.
$from = $this->get('from') ? date('c', strtotime($this->get('from'))) : $this->reportingPeriodStart->format('c');
Expand All @@ -45,6 +43,5 @@ public function gather(Sandbox $sandbox) {
];
$this->setParameter('calls', $calls);
$this->setParameter('is_legacy', true);
parent::gather($sandbox);
}
}
44 changes: 11 additions & 33 deletions src/Audit/CloudApiAnalysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,18 @@

use AcquiaCloudApi\Connector\Client;
use Drutiny\Acquia\Api\CloudApi;
use Drutiny\Attribute\UseService;
use Drutiny\Attribute\DataProvider;
use Drutiny\Attribute\Parameter;
use Drutiny\Attribute\Type;
use Drutiny\Audit\AbstractAnalysis;
use Drutiny\Sandbox\Sandbox;
use Psr\Cache\CacheItemInterface;

#[UseService(CloudApi::class, 'setCloudApi')]
#[Parameter('calls','An array of API calls to make to the Cloud API.', mode: Parameter::REQUIRED, type: Type::HASH, default: [])]
#[Parameter('is_legacy', 'A boolean flag indicating if the policy is legacy.', mode: Parameter::OPTIONAL, default: false, type: Type::BOOLEAN)]
class CloudApiAnalysis extends AbstractAnalysis {

protected CloudApi $api;

public function setCloudApi(CloudApi $api)
{
$this->api = $api;
}

public function configure():void
{
parent::configure();

$this->addParameter(
'is_legacy',
static::PARAMETER_OPTIONAL,
'A boolean flag indicating if the policy is legacy.',
false
);
$this->addParameter(
'calls',
static::PARAMETER_IS_ARRAY | static::PARAMETER_OPTIONAL,
'An array of API calls to make to the Cloud API.',
[]
);
}

protected function gather(Sandbox $sandbox)
#[DataProvider]
protected function gather(CloudApi $api)
{
$this->set('drush', $this->target['drush']->export());
$this->set('app', $this->target['acquia.cloud.application']->export());
Expand All @@ -47,7 +25,7 @@ protected function gather(Sandbox $sandbox)
$this->logger->error("$name should be an array containing a path and optionally a verb (e.g. GET) or an options array. Skipping.");
continue;
}
$response = $this->call(...$call);
$response = $this->call($api, ...$call);

if ($this->getParameter('is_legacy')) {
$response = ['_embedded' => ['items' => (array) $response]];
Expand All @@ -60,14 +38,14 @@ protected function gather(Sandbox $sandbox)
/**
* Make a call to Acquia Cloud API.
*/
protected function call(string $path, string $verb = 'get', array $options = []) {
protected function call(CloudApi $api, string $path, string $verb = 'get', array $options = []) {
// Allow variable replacement inside of path calls.
$path = $this->interpolate($path);
$call = new CloudApiAnalysisCall(verb: $verb, path: $path, options: $options);

return $this->cache->get($call->getCacheKey(), function (CacheItemInterface $cache) use ($call) {
return $this->cache->get($call->getCacheKey(), function (CacheItemInterface $cache) use ($call, $api) {
$cache->expiresAfter(120);
$client = $this->api->getApiClient();
$client = $api->getApiClient();
$call->addQuery($client);
return $client->request($call->verb, $call->path, $call->options);
});
Expand Down
13 changes: 4 additions & 9 deletions src/Audit/DatabaseAnalysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@

namespace Drutiny\Acquia\Audit;

use Drutiny\Sandbox\Sandbox;
use Drutiny\Attribute\DataProvider;

/**
* Adds the database size to the database result set.
*/
class DatabaseAnalysis extends EnvironmentAnalysis {

/**
* @inheritdoc
*/
public function gather(Sandbox $sandbox) {
parent::gather($sandbox);

#[DataProvider(1)]
public function gatherTableData() {
$data = $this->get('databases');

$sql = "SELECT CONCAT(TABLE_SCHEMA, ':', ROUND(SUM(data_length + index_length) / 1024 / 1024, 1)) as size
Expand All @@ -32,7 +28,7 @@ public function gather(Sandbox $sandbox) {
$db_sizes[$db[0]] = $db[1];
}

$this->set('databases', array_map(function ($database) use ($sandbox, $db_sizes) {
$this->set('databases', array_map(function ($database) use ($db_sizes) {
// Extract the machine_name from the db_url.
$strArray = explode('/',$database->url);
$db_machine_name = end($strArray);
Expand All @@ -41,7 +37,6 @@ public function gather(Sandbox $sandbox) {
$database->size = array_key_exists($db_machine_name, $db_sizes) ? $db_sizes[$db_machine_name] : 0;
return $database;
}, $data['_embedded']['items']));

}

}
9 changes: 3 additions & 6 deletions src/Audit/EnvironmentAnalysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Drutiny\Acquia\Audit;

use Drutiny\Sandbox\Sandbox;
use Drutiny\Attribute\DataProvider;

/**
* Check to ensure Production Mode is enabled on Acquia Cloud.
Expand All @@ -15,10 +15,8 @@ public function configure():void
$this->setDeprecated();
}

/**
* @inheritdoc
*/
public function gather(Sandbox $sandbox) {
#[DataProvider(-1)]
public function gatherAllData() {

$calls['cron'] = [
'path' => '/environments/{acquia.cloud.environment.uuid}/crons'
Expand Down Expand Up @@ -66,6 +64,5 @@ public function gather(Sandbox $sandbox) {
$this->set('environment', $this->target['acquia.cloud.environment']);
$this->setParameter('is_legacy', true);
$this->setParameter('calls', $calls);
parent::gather($sandbox);
}
}
8 changes: 5 additions & 3 deletions src/Audit/EnvironmentVariableAnalysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

namespace Drutiny\Acquia\Audit;

use Drutiny\Sandbox\Sandbox;
use Drutiny\Attribute\DataProvider;
use Drutiny\Attribute\Deprecated;

/**
* Retrieve all custom environment variables for a particular Acquia Cloud environment.
*/
#[Deprecated]
class EnvironmentVariableAnalysis extends EnvironmentAnalysis {

/**
* @inheritdoc
*/
public function gather(Sandbox $sandbox) {
parent::gather($sandbox);
#[DataProvider(1)]
public function gatherEnvVars() {

// Grab the environment and sitegroup name
$app = $this->target['acquia.cloud.application']->export();
Expand Down
43 changes: 13 additions & 30 deletions src/Audit/FilesystemAnalysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,27 @@

namespace Drutiny\Acquia\Audit;

use Drutiny\Sandbox\Sandbox;
use Drutiny\Credential\Manager;
use Drutiny\Acquia\CloudApiDrushAdaptor;
use Drutiny\Acquia\CloudApiV2;

use Drutiny\Attribute\DataProvider;
use Drutiny\Attribute\Parameter;
use Drutiny\Attribute\Type;

/**
* Audit the usage of the filesystem.
*/
#[Parameter(
name: 'unit',
description: 'the unit of measurement to describe the volume usage in. E.g. B,M,G,T.',
mode: Parameter::OPTIONAL,
default: 'G',
type: Type::STRING,
enums: ['B', 'M', 'G', 'T'],
)]
class FilesystemAnalysis extends EnvironmentAnalysis
{


public function configure():void
{

$this->addParameter(
'unit',
static::PARAMETER_OPTIONAL,
'the unit of measurement to describe the volume usage in. E.g. B,M,G,T.',
'G'
);
$this->addParameter(
'filesystem',
static::PARAMETER_OPTIONAL,
'the storage usage information for both disk and inodes.',
''
);
parent::configure();
}

/**
* @inheritdoc
*/
public function gather(Sandbox $sandbox)
#[DataProvider(1)]
public function gatherFileSystemData()
{
parent::gather($sandbox);

$unit = $this->getParameter('unit', "G");

$app = $this->get('app');
Expand Down
Loading

0 comments on commit b10ed16

Please sign in to comment.