protected function execute(InputInterface $input, OutputInterface $output) { $iam = new \AwsInspector\Model\Iam\Repository(); $accountId = $iam->findCurrentUser()->getAccountId(); $output->writeln('Owner: ' . $accountId); $ec2Client = SdkFactory::getClient('EC2'); /* @var $ec2Client \Aws\Ec2\Ec2Client */ $res = $ec2Client->describeImages(['Owners' => [$accountId]]); $activeImageIds = array_flip($res->search('Images[].ImageId')); $res = $ec2Client->describeSnapshots(['OwnerIds' => [$accountId]]); $orphanSnapshots = []; foreach ($res->get('Snapshots') as $snapshotData) { $description = $snapshotData["Description"]; // Created by CreateImage(i-ee0c7564) for ami-9945d0ea from vol-e4b6ff16 // if (preg_match('/^Created by CreateImage\(i-.*\) for \(ami-.*\) from \(vol-.*\)$/', $description)) { if (preg_match('/^Created by CreateImage\\(i-.*\\) for (ami-.+) from vol-.+/', $description, $matches)) { $amiId = $matches[1]; if (isset($activeImageIds[$amiId])) { $output->writeln('Found active AMI: ' . $amiId); } else { $output->writeln('AMI not found: ' . $amiId); $orphanSnapshots[] = $snapshotData['SnapshotId']; } } } foreach ($orphanSnapshots as $snapshotId) { $output->writeln('Deleting ' . $snapshotId); $result = $ec2Client->deleteSnapshot(['SnapshotId' => $snapshotId]); } }
public function getResourceName() { if (is_null($this->resourceName)) { // get account id from current user $iam = new \AwsInspector\Model\Iam\Repository(); $accountId = $iam->findCurrentUser()->getAccountId(); $parts = []; $parts['prefix'] = 'arn:aws:rds'; $parts['region'] = substr($this->getAvailabilityZone(), 0, -1); $parts['AccountId'] = $accountId; $parts['resourcetype'] = 'db'; $parts['name'] = $this->getDBInstanceIdentifier(); $this->resourceName = implode(':', $parts); } return $this->resourceName; }
public function getResourceName() { if (is_null($this->resourceName)) { // TODO: this should be changed! $region = getenv('HURRICANE_TEST_REGION'); if (empty($region)) { throw new \Exception('Region missing'); } // get account id from current user $iam = new \AwsInspector\Model\Iam\Repository(); $accountId = $iam->findCurrentUser()->getAccountId(); $parts = []; $parts['prefix'] = 'arn:aws:elasticache'; $parts['region'] = $region; $parts['AccountId'] = $accountId; $parts['resourcetype'] = 'cluster'; $parts['name'] = $this->getCacheClusterId(); $this->resourceName = implode(':', $parts); } return $this->resourceName; }