This method ensures that openstack instance is always from the current
environment scope
public openstack ( |
||
$platform | The platform name or Openstack config | |
$region | string | optional The region |
리턴 | Returns openstack instance from DI container |
/** * {@inheritdoc} * @see \Scalr\Modules\PlatformModuleInterface::getLocations() */ public function getLocations(\Scalr_Environment $environment = null) { if ($environment === null || !$environment->isPlatformEnabled($this->platform)) { return array(); } try { $client = $environment->openstack($this->platform, "fakeRegion"); foreach ($client->listZones() as $zone) { $retval[$zone->name] = ucfirst($this->platform) . " / {$zone->name}"; } } catch (\Exception $e) { return array(); } return $retval; }
/** * {@inheritdoc} * @see \Scalr\Modules\Platforms\Openstack\OpenstackPlatformModule::getLocations() */ public function getLocations(\Scalr_Environment $environment = null) { if (!$environment) { return array('ORD' => 'Rackspace US / ORD', 'DFW' => 'Rackspace US / DFW', 'IAD' => 'Rackspace US / IAD', 'SYD' => 'Rackspace US / SYD'); } else { try { $client = $environment->openstack($this->platform, "fakeRegion"); $zones = $client->listZones(); $endpoints = $client->getConfig()->getAuthToken()->getRegionEndpoints(); foreach ($zones as $zone) { if (isset($endpoints['compute'][$zone->name])) { $retval[$zone->name] = "Rackspace US / {$zone->name}"; } } } catch (\Exception $e) { return array(); } return $retval; } }
/** * {@inheritdoc} * @see \Scalr\Modules\PlatformModuleInterface::getInstanceTypes() */ public function getInstanceTypes(\Scalr_Environment $env = null, $cloudLocation = null, $details = false) { if (!$env instanceof \Scalr_Environment || empty($cloudLocation)) { throw new \InvalidArgumentException(sprintf("Method %s requires both environment object and cloudLocation to be specified.", __METHOD__)); } $ret = array(); $client = $env->openstack($this->platform, $cloudLocation); foreach ($client->servers->listFlavors() as $flavor) { if (!$details) { $ret[(string) $flavor->id] = (string) $flavor->name; } else { $ret[(string) $flavor->id] = array('name' => (string) $flavor->name, 'ram' => (string) $flavor->ram, 'vcpus' => (string) $flavor->vcpus, 'disk' => (string) $flavor->disk, 'type' => 'HDD'); } } return $ret; }
/** * {@inheritdoc} * @see \Scalr\Modules\PlatformModuleInterface::getInstanceTypes() */ public function getInstanceTypes(\Scalr_Environment $env = null, $cloudLocation = null, $details = false) { if (!$env instanceof \Scalr_Environment || empty($cloudLocation)) { throw new \InvalidArgumentException(sprintf("Method %s requires both environment object and cloudLocation to be specified.", __METHOD__)); } $ret = []; $detailed = []; //Trying to retrieve instance types from the cache $url = $env->cloudCredentials($this->platform)->properties[Entity\CloudCredentialsProperty::OPENSTACK_KEYSTONE_URL]; $collection = $this->getCachedInstanceTypes($this->platform, $url, $cloudLocation); if ($collection === false || $collection->count() == 0) { //No cache. Fetching data from the cloud $client = $env->openstack($this->platform, $cloudLocation); foreach ($client->servers->listFlavors() as $flavor) { $detailed[(string) $flavor->id] = array('name' => (string) $flavor->name, 'ram' => (string) $flavor->ram, 'vcpus' => (string) $flavor->vcpus, 'disk' => (string) $flavor->disk, 'type' => 'HDD'); if (!$details) { $ret[(string) $flavor->id] = (string) $flavor->name; } else { $ret[(string) $flavor->id] = $detailed[(string) $flavor->id]; } } //Refreshes/creates a cache CloudLocation::updateInstanceTypes($this->platform, $url, $cloudLocation, $detailed); } else { //Takes data from cache foreach ($collection as $cloudInstanceType) { /* @var $cloudInstanceType \Scalr\Model\Entity\CloudInstanceType */ if (!$details) { $ret[$cloudInstanceType->instanceTypeId] = $cloudInstanceType->name; } else { $ret[$cloudInstanceType->instanceTypeId] = $cloudInstanceType->getProperties(); } } } return $ret; }
/** * @return \Scalr\Service\OpenStack\OpenStack */ public function getOsClient(Scalr_Environment $environment, $cloudLocation) { return $environment->openstack($this->platform, $cloudLocation); }
/** * {@inheritdoc} * @see PlatformModuleInterface::getImageInfo() */ public function getImageInfo(\Scalr_Environment $environment, $cloudLocation, $imageId) { $snap = $environment->openstack($this->platform, $cloudLocation)->servers->getImage($imageId); return $snap ? ["name" => $snap->name, "size" => $snap->metadata->instance_type_root_gb] : []; }