AwsInspector\Model\Ec2\Repository::findEc2InstancesByTags PHP Method

findEc2InstancesByTags() public method

public findEc2InstancesByTags ( array $tags = [] ) : Collection
$tags array
return AwsInspector\Model\Collection
    public function findEc2InstancesByTags(array $tags = array())
    {
        $filters = [['Name' => 'instance-state-name', "Values" => ['running']]];
        foreach ($tags as $tagName => $tagValue) {
            $filters[] = ['Name' => 'tag:' . $tagName, "Values" => [$tagValue]];
        }
        return $this->findEc2Instances($filters);
    }

Usage Example

Esempio n. 1
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $tags = $input->getOption('tag');
     $tags = $this->convertTags($tags);
     $mapping = ['InstanceId' => 'InstanceId', 'ImageId' => 'ImageId', 'State' => 'State.Name', 'SubnetId' => 'SubnetId', 'AZ' => 'Placement.AvailabilityZone', 'PublicIp' => 'PublicIpAddress', 'PrivateIp' => 'PrivateIpAddress', 'KeyName' => 'KeyName'];
     // dynamically add current tags
     foreach (array_keys($tags) as $tagName) {
         $mapping[$tagName] = 'Tags[?Key==`' . $tagName . '`].Value | [0]';
     }
     foreach ($input->getOption('column') as $tagName) {
         $mapping[$tagName] = 'Tags[?Key==`' . $tagName . '`].Value | [0]';
     }
     $repository = new Repository();
     $instanceCollection = $repository->findEc2InstancesByTags($tags);
     $rows = [];
     foreach ($instanceCollection as $instance) {
         /* @var $instance Instance */
         $rows[] = $instance->extractData($mapping);
     }
     if (count($rows)) {
         $table = new \Symfony\Component\Console\Helper\Table($output);
         $table->setHeaders(array_keys(end($rows)))->setRows($rows);
         $table->render();
     } else {
         $output->writeln('No matching instances found.');
     }
 }
All Usage Examples Of AwsInspector\Model\Ec2\Repository::findEc2InstancesByTags