azure() 공개 메소드

This method ensures that azure instance is always from the current environment scope
public azure ( ) : Azure
리턴 Scalr\Service\Azure Returns azure instance from DI container
예제 #1
0
파일: AzureTest.php 프로젝트: mheydt/scalr
 /**
  * Set test names for objects
  */
 protected function setUp()
 {
     parent::setUp();
     if ($this->isSkipFunctionalTests()) {
         $this->markTestSkipped();
     }
     $testEnvId = \Scalr::config('scalr.phpunit.envid');
     try {
         $this->testEnv = \Scalr_Environment::init()->loadById($testEnvId);
     } catch (Exception $e) {
         $this->markTestSkipped('Test Environment does not exist.');
     }
     if (!$this->testEnv || !$this->testEnv->isPlatformEnabled(\SERVER_PLATFORMS::AZURE)) {
         $this->markTestSkipped('Azure platform is not enabled.');
     }
     $this->azure = $this->testEnv->azure();
     $this->subscriptionId = $this->azure->getEnvironment()->cloudCredentials(SERVER_PLATFORMS::AZURE)->properties[Entity\CloudCredentialsProperty::AZURE_SUBSCRIPTION_ID];
     $this->resourceGroupName = 'test3-resource-group-' . $this->getInstallationId();
     $this->availabilitySetName = 'test3-availability-set-' . $this->getInstallationId();
     $this->vmName = 'test3-virtual-machine-' . $this->getInstallationId();
     $this->vnName = 'test3-virtual-network-' . $this->getInstallationId();
     $this->nicName = 'test3-network-interface' . $this->getInstallationId();
     $this->publicIpName = 'myPublicIP3';
     $this->storageName = 'teststorage3' . $this->getInstallationId();
     $this->sgName = 'test3-security-group' . $this->getInstallationId();
 }
예제 #2
0
파일: Clouds.php 프로젝트: scalr/scalr
 private function saveAzure()
 {
     if (Scalr::isHostedScalr() && !$this->request->getHeaderVar('Interface-Beta')) {
         $this->response->failure('Azure support available only for Scalr Enterprise Edition.');
         return;
     }
     $enabled = false;
     $currentCloudCredentials = $this->env->keychain(SERVER_PLATFORMS::AZURE);
     if (empty($currentCloudCredentials->id)) {
         $currentCloudCredentials = $this->makeCloudCredentials(SERVER_PLATFORMS::AZURE, [], Entity\CloudCredentials::STATUS_DISABLED);
     }
     /* @var $ccProps Scalr\Model\Collections\SettingsCollection */
     $ccProps = $currentCloudCredentials->properties;
     if ($this->getParam('azure.is_enabled')) {
         $enabled = true;
         $tenantName = $this->checkVar(Entity\CloudCredentialsProperty::AZURE_TENANT_NAME, 'string', "Azure Tenant name is required", SERVER_PLATFORMS::AZURE);
         $ccProps->saveSettings([Entity\CloudCredentialsProperty::AZURE_AUTH_STEP => 0]);
         if (!count($this->checkVarError)) {
             $oldTenantName = $ccProps[Entity\CloudCredentialsProperty::AZURE_TENANT_NAME];
             $ccProps->saveSettings([Entity\CloudCredentialsProperty::AZURE_TENANT_NAME => $tenantName]);
             $azure = $this->env->azure();
             $ccProps->saveSettings([Entity\CloudCredentialsProperty::AZURE_AUTH_STEP => 1]);
             $authorizationCode = $ccProps[Entity\CloudCredentialsProperty::AZURE_AUTH_CODE];
             $accessToken = $ccProps[Entity\CloudCredentialsProperty::AZURE_ACCESS_TOKEN];
             if (empty($authorizationCode) && empty($accessToken) || $oldTenantName != $ccProps[Entity\CloudCredentialsProperty::AZURE_TENANT_NAME]) {
                 $ccProps->saveSettings([Entity\CloudCredentialsProperty::AZURE_AUTH_CODE => false, Entity\CloudCredentialsProperty::AZURE_SUBSCRIPTION_ID => false]);
                 $location = $azure->getAuthorizationCodeLocation();
                 $this->response->data(['authLocation' => $location]);
                 return;
             }
             $ccProps->saveSettings([Entity\CloudCredentialsProperty::AZURE_AUTH_STEP => 0]);
             $subscriptionId = trim($this->checkVar(Entity\CloudCredentialsProperty::AZURE_SUBSCRIPTION_ID, 'string', "Azure Subscription id is required", SERVER_PLATFORMS::AZURE));
             $params[Entity\CloudCredentialsProperty::AZURE_SUBSCRIPTION_ID] = $subscriptionId;
             if (!count($this->checkVarError)) {
                 $oldSubscriptionId = $ccProps[Entity\CloudCredentialsProperty::AZURE_SUBSCRIPTION_ID];
                 if ($subscriptionId != $oldSubscriptionId) {
                     $azure->getClientToken();
                     $objectId = $azure->getAppObjectId();
                     $params[Entity\CloudCredentialsProperty::AZURE_CLIENT_OBJECT_ID] = $objectId;
                     $contributorRoleId = $azure->getContributorRoleId($subscriptionId);
                     $params[Entity\CloudCredentialsProperty::AZURE_CONTRIBUTOR_ID] = $contributorRoleId;
                     $roleAssignment = $azure->getContributorRoleAssignmentInfo($subscriptionId, $objectId, $contributorRoleId);
                     if (empty($roleAssignment)) {
                         $roleAssignmentId = \Scalr::GenerateUID();
                         $azure->assignContributorRoleToApp($subscriptionId, $roleAssignmentId, $objectId, $contributorRoleId);
                     } else {
                         $roleAssignmentId = $roleAssignment->name;
                     }
                     $params[Entity\CloudCredentialsProperty::AZURE_ROLE_ASSIGNMENT_ID] = $roleAssignmentId;
                     $ccProps->saveSettings([Entity\CloudCredentialsProperty::AZURE_CLIENT_TOKEN => false, Entity\CloudCredentialsProperty::AZURE_CLIENT_TOKEN_EXPIRE => false]);
                     $azure->getClientToken(Azure::URL_CORE_WINDOWS);
                     $ccProps->saveSettings($params);
                     $providersList = $azure->getProvidersList($subscriptionId);
                     $requiredProviders = ProviderData::getRequiredProviders();
                     foreach ($providersList as $providerData) {
                         /* @var $providerData ProviderData */
                         if (in_array($providerData->namespace, $requiredProviders) && $providerData->registrationState == ProviderData::REGISTRATION_STATE_NOT_REGISTERED) {
                             $registerResponse = $azure->registerSubscription($subscriptionId, $providerData->namespace);
                         }
                     }
                     if (!empty($registerResponse)) {
                         do {
                             sleep(5);
                             $provider = $azure->getLocationsList($registerResponse->namespace);
                         } while ($provider->registrationState != ProviderData::REGISTRATION_STATE_REGISTERED);
                     }
                 }
                 $ccProps[Entity\CloudCredentialsProperty::AZURE_AUTH_STEP] = 3;
             } else {
                 $this->response->failure();
                 $this->response->data(['errors' => $this->checkVarError]);
                 return;
             }
         } else {
             $this->response->failure();
             $this->response->data(['errors' => $this->checkVarError]);
             return;
         }
     }
     $this->db->BeginTrans();
     try {
         $this->env->enablePlatform(SERVER_PLATFORMS::AZURE, $enabled);
         if ($enabled) {
             $currentCloudCredentials->status = Entity\CloudCredentials::STATUS_ENABLED;
             $currentCloudCredentials->save();
         }
         if (!$this->user->getAccount()->getSetting(Scalr_Account::SETTING_DATE_ENV_CONFIGURED)) {
             $this->user->getAccount()->setSetting(Scalr_Account::SETTING_DATE_ENV_CONFIGURED, time());
         }
         $this->response->success('Environment saved');
         $this->response->data(['enabled' => $enabled]);
     } catch (Exception $e) {
         $this->db->RollbackTrans();
         throw new Exception(_("Failed to save Azure settings: {$e->getMessage()}"));
     }
     $this->db->CommitTrans();
 }
예제 #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__));
     }
     $collection = $this->getCachedInstanceTypes(\SERVER_PLATFORMS::AZURE, '', $cloudLocation);
     if ($collection === false || $collection->count() == 0) {
         $instanceTypesResult = $env->azure()->compute->location->getInstanceTypesList($env->keychain(SERVER_PLATFORMS::AZURE)->properties[CloudCredentialsProperty::AZURE_SUBSCRIPTION_ID], $cloudLocation);
         $ret = [];
         foreach ($instanceTypesResult as $instanceType) {
             $detailed[$instanceType->name] = ['name' => $instanceType->name, 'ram' => $instanceType->memoryInMB, 'vcpus' => $instanceType->numberOfCores, 'disk' => $instanceType->resourceDiskSizeInMB / 1024, 'type' => '', 'maxdatadiskcount' => $instanceType->maxDataDiskCount, 'rootdevicesize' => $instanceType->osDiskSizeInMB / 1024];
             if (!$details) {
                 $ret[$instanceType->name] = array($instanceType->name => $instanceType->name);
             } else {
                 $ret[$instanceType->name] = $detailed[$instanceType->name];
             }
         }
         CloudLocation::updateInstanceTypes(\SERVER_PLATFORMS::AZURE, '', $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;
 }