AwsInspector\Command\CloudwatchLogs\SetRetentionCommand::execute PHP Method

execute() protected method

protected execute ( Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output )
$input Symfony\Component\Console\Input\InputInterface
$output Symfony\Component\Console\Output\OutputInterface
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // TODO: refactor this to use \AwsInspector\Model\CloudWatchLogs\Repository
        $days = $input->getArgument('days');
        $days = intval($days);
        if ($days == 0) {
            throw new \Exception('Invalid retention period');
        }
        $groupPattern = $input->getArgument('group');
        $cloudwatchLogsClient = \AwsInspector\SdkFactory::getClient('cloudwatchlogs');
        /* @var $cloudwatchLogsClient \Aws\CloudWatchLogs\CloudWatchLogsClient */
        $totalBytes = 0;
        $nextToken = null;
        do {
            $params = ['limit' => 50];
            if ($nextToken) {
                $params['nextToken'] = $nextToken;
            }
            $result = $cloudwatchLogsClient->describeLogGroups($params);
            foreach ($result->get('logGroups') as $logGroup) {
                $name = $logGroup['logGroupName'];
                if (preg_match('/' . $groupPattern . '/', $name)) {
                    $retention = isset($logGroup['retentionInDays']) ? $logGroup['retentionInDays'] : 'never';
                    if ($retention != $days) {
                        $output->writeln('Updating ' . $logGroup['logGroupName']);
                        $cloudwatchLogsClient->putRetentionPolicy(['logGroupName' => $name, 'retentionInDays' => $days]);
                    } else {
                        $output->writeln('Skipping ' . $logGroup['logGroupName']);
                    }
                } else {
                    $output->writeln('Does not match pattern: ' . $logGroup['logGroupName']);
                }
            }
            $nextToken = $result->get("nextToken");
        } while ($nextToken);
    }
SetRetentionCommand