openstack() public method

This method ensures that openstack instance is always from the current environment scope
public openstack ( OpenStackConfig | string $platform, string $region = null ) : OpenStack
$platform Scalr\Service\OpenStack\OpenStackConfig | string The platform name or Openstack config
$region string optional The region
return Scalr\Service\OpenStack\OpenStack Returns openstack instance from DI container
コード例 #1
0
 /**
  * {@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;
 }
コード例 #2
0
 /**
  * {@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;
     }
 }
コード例 #3
0
 /**
  * {@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;
 }
コード例 #4
0
 /**
  * {@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;
 }
コード例 #5
0
ファイル: Openstack.php プロジェクト: recipe/scalr
 /**
  * @return \Scalr\Service\OpenStack\OpenStack
  */
 public function getOsClient(Scalr_Environment $environment, $cloudLocation)
 {
     return $environment->openstack($this->platform, $cloudLocation);
 }
コード例 #6
0
 /**
  * {@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] : [];
 }