Skip to content

Instantly share code, notes, and snippets.

@mxr576
Last active July 6, 2018 11:10
Show Gist options
  • Save mxr576/3eca575976d36fc8fd44f21780c1b92b to your computer and use it in GitHub Desktop.
Save mxr576/3eca575976d36fc8fd44f21780c1b92b to your computer and use it in GitHub Desktop.
The Ultimate Apigee Edge clean up script in PHP - WARNING: Use it on your own risk!!!
<?php
require_once "vendor/autoload.php";
use GuzzleHttp\Client;
use GuzzleHttp\Promise;
$endpoint = getenv('APIGEE_EDGE_ENDPOINT') ?: 'https://api.enterprise.apigee.com/v1';
$org = getenv('APIGEE_EDGE_ORGANIZATION');
$client = new Client(['base_uri' => "{$endpoint}/o/{$org}/", 'auth' => [getenv('APIGEE_EDGE_USER'), getenv('APIGEE_EDGE_PASSWORD')]]);
function getEntityIds(Client $client, string $endpoint) : array {
$ids = json_decode((string) $client->get($endpoint)->getBody());
$lastId = end($ids);
if ($lastId) {
do {
$tmp = json_decode((string) $client->get("{$endpoint}?startKey={$lastId}")->getBody());
$ids = array_merge($ids, $tmp);
// Apigee Edge response always contains the requested entity id (startKey).
if (count($tmp) > 1) {
$lastId = end($tmp);
}
else {
$lastId = FALSE;
}
} while ($lastId);
}
return $ids;
}
function buildPromises(Client $client, string $endpoint, array $ids) : array {
$promises = [];
$pos = 0;
while($emails = array_slice($ids, $pos, 100)) {
$tmp = [];
foreach($emails as $id) {
$tmp[] = $client->deleteAsync("{$endpoint}/{$id}");
$pos++;
}
$promises[] = $tmp;
}
return $promises;
}
function processPromises(array $promises) : void {
foreach ($promises as $batch) {
try {
// Wait for the requests to complete, even if some of them fail.
$results = Promise\settle($batch)->wait();
$success = 0;
$failures = [];
foreach ($results as $result) {
if ($result['state'] === Promise\Promise::FULFILLED) {
$success++;
}
else {
$failures[] = method_exists($result['reason'], 'getMessage') ? $result['reason']->getMessage() : (string) $result['reason'];
}
}
printf("Successfully deleted %d.\n", $success);
if ($failures) {
printf("There were %s failures.\n", count($failures));
foreach ($failures as $failure) {
echo $failure . PHP_EOL;
}
}
} catch (\Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
}
}
echo "This will PERMANENTLY REMOVE all developers and api products from {$org} organization. Are you sure about this? Type 'yes' to continue:".PHP_EOL;
$handle = fopen("php://stdin", "r");
$line = fgets($handle);
fclose($handle);
if (trim($line) !== 'yes') {
echo "ABORTING!\n";
exit;
}
echo "DESTROY!!!! DESTROY!!!! DESTROY!!!" . PHP_EOL;
$developers = getEntityIds($client, 'developers');
printf("Number of developers to be removed: %d.\n", count($developers));
processPromises(buildPromises($client, 'developers', $developers));
$products = getEntityIds($client, 'apiproducts');
printf("Number of API Products to be removed: %d.\n", count($products));
processPromises(buildPromises($client, 'apiproducts', $products));
@mxr576
Copy link
Author

mxr576 commented Jul 6, 2018

Requirements:

  • PHP 7.1
  • Composer
wget https://gist.githubusercontent.com/mxr576/3eca575976d36fc8fd44f21780c1b92b/raw -O apigee_edge_cleanup.php
composer init -n
composer require guzzlehttp/guzzle:^6.0
composer require guzzlehttp/promises:^1.3
# Save your Apigee Edge credentials to environment variables that you can see above.
php apigee_edge_cleanup.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment