Scalr\Modules\Platforms\Ec2\Ec2PlatformModule::GetServersList PHP Метод

GetServersList() публичный Метод

Gets the list of the EC2 instances for the specified environment and AWS location
public GetServersList ( Scalr_Environment $environment, string $region, boolean $skipCache = false ) : array
$environment Scalr_Environment Environment Object
$region string EC2 location name
$skipCache boolean optional Whether it should skip the cache.
Результат array Returns array looks like array(InstanceId => stateName)
    public function GetServersList(\Scalr_Environment $environment, $region, $skipCache = false)
    {
        if (!$region) {
            return [];
        }
        $aws = $environment->aws($region);
        $cacheKey = sprintf('%s:%s', $environment->id, $region);
        if (!isset($this->instancesListCache[$cacheKey]) || $skipCache) {
            $cacheValue = array();
            /* @var $results ReservationList */
            $results = null;
            do {
                try {
                    $results = $aws->ec2->instance->describe(null, null, isset($results) ? $results->getNextToken() : null, 1000);
                } catch (Exception $e) {
                    throw new Exception(sprintf("Cannot get list of servers for platform ec2: %s", $e->getMessage()));
                }
                if (count($results)) {
                    foreach ($results as $reservation) {
                        /* @var $reservation ReservationData */
                        foreach ($reservation->instancesSet as $instance) {
                            /* @var $instance InstanceData */
                            $cacheValue[$cacheKey][$instance->instanceId] = ['localIp' => $instance->privateIpAddress, 'remoteIp' => $instance->ipAddress, 'status' => $instance->instanceState->name, 'type' => $instance->instanceType, '_timestamp' => time()];
                        }
                    }
                }
            } while ($results->getNextToken());
            foreach ($cacheValue as $offset => $value) {
                $this->instancesListCache[$offset] = $value;
            }
        }
        return isset($this->instancesListCache[$cacheKey]) ? $this->instancesListCache[$cacheKey] : [];
    }

Usage Example

Пример #1
0
 /**
  * @test
  */
 public function testGetServersList()
 {
     $env = $this->getEnvironment();
     $servers = $this->module->GetServersList($env, self::REGION, true);
     $this->assertInternalType('array', $servers);
     $ref = new ReflectionClass('Scalr\\Service\\Aws\\Ec2\\DataType\\InstanceStateData');
     if (count($servers)) {
         foreach ($servers as $instanceid => $v) {
             $this->assertStringStartsWith('i-', $instanceid);
             $this->assertNotEmpty($v);
         }
     }
 }