Example #1
0
File: Elb.php Project: mheydt/scalr
 /**
  * @param string     $cloudLocation                     Ec2 Region
  * @param JsonData   $listeners                         Listeners list
  * @param bool       $crossLoadBalancing                Enable Cross balancing
  * @param JsonData   $healthcheck                       Health check data
  * @param string     $scheme                            optional Scheme
  * @param JsonData   $securityGroups                    optional Security groups
  * @param string     $vpcId                             optional Vpc id
  * @param JsonData   $zones                             optional Availability zones
  * @param JsonData   $subnets                           optional Subnets
  * @param string     $name                              optional Name
  * @throws Exception
  */
 public function xCreateAction($cloudLocation, JsonData $listeners, $crossLoadBalancing, JsonData $healthcheck, $scheme = null, JsonData $securityGroups = null, $vpcId = null, JsonData $zones = null, JsonData $subnets = null, $name = null)
 {
     $this->request->restrictAccess(Acl::RESOURCE_AWS_ELB, Acl::PERM_AWS_ELB_MANAGE);
     $elb = $this->environment->aws($cloudLocation)->elb;
     //prepare listeners
     $listenersList = new ListenerList();
     foreach ($listeners as $listener) {
         $listener_chunks = explode("#", $listener);
         $listenersList->append(new ListenerData(trim($listener_chunks[1]), trim($listener_chunks[2]), trim($listener_chunks[0]), null, trim($listener_chunks[3])));
     }
     $zones = !empty($zones) ? (array) $zones : null;
     $subnets = !empty($subnets) ? (array) $subnets : null;
     if (empty($name)) {
         $name = sprintf("scalr-%s-%s", CryptoTool::sault(10), rand(100, 999));
     } else {
         if (!preg_match('/^[-a-zA-Z0-9]+$/', $name)) {
             throw new Exception('Load Balancer names must only contain alphanumeric characters or dashes.');
         }
     }
     $healthCheckType = new HealthCheckData();
     $healthCheckType->target = $healthcheck['target'];
     $healthCheckType->healthyThreshold = $healthcheck['healthyThreshold'];
     $healthCheckType->interval = $healthcheck['interval'];
     $healthCheckType->timeout = $healthcheck['timeout'];
     $healthCheckType->unhealthyThreshold = $healthcheck['unhealthyThreshold'];
     $securityGroupIds = [];
     foreach ($securityGroups as $securityGroup) {
         $securityGroupIds[] = $securityGroup['id'];
     }
     $result = self::loadController('Aws', 'Scalr_UI_Controller_Tools')->checkSecurityGroupsPolicy($securityGroups, Aws::SERVICE_INTERFACE_ELB);
     if ($result === true) {
         $result = self::loadController('Aws', 'Scalr_UI_Controller_Tools')->checkVpcPolicy($vpcId, $subnets, $cloudLocation);
     }
     if ($result !== true) {
         throw new Exception($result);
     }
     //Creates a new ELB
     $dnsName = $elb->loadBalancer->create($name, $listenersList, $zones, $subnets, !empty($securityGroupIds) ? $securityGroupIds : null, !empty($scheme) ? $scheme : null);
     if ($crossLoadBalancing) {
         $attributes = new AttributesData();
         $attributes->setCrossZoneLoadBalancing(new CrossZoneLoadBalancingData($crossLoadBalancing));
         $requestData = new ModifyLoadBalancerAttributes($name, $attributes);
         $elb->loadBalancer->modifyAttributes($requestData);
     }
     $elb->loadBalancer->addTags($name, $this->getEnvironment()->getAwsTags());
     try {
         $elb->loadBalancer->configureHealthCheck($name, $healthCheckType);
     } catch (Exception $e) {
         $elb->loadBalancer->delete($name);
         throw $e;
     }
     $lb = $elb->loadBalancer->describe($name)->get(0);
     // return all as in xListElb
     $this->response->data(['elb' => ['name' => $name, 'dnsName' => $dnsName, 'dtcreated' => $lb->createdTime->format('c'), 'subnets' => $lb->subnets]]);
 }
Example #2
0
 /**
  * Handles Attributes Result.
  *
  * Loads Instance list from Result and updates load balancer entity if it does exist.
  *
  * @param   \SimpleXMLElement $attributesResult   Result that contains Attributes obejct
  * @return  AttributesData      Returns an updated list of attributes for the LoadBalancer.
  * @throws  ClientException
  */
 private function handleAttributesResultForLoadBalancer(\SimpleXMLElement $attributesResult)
 {
     $result = new AttributesData();
     $result->setElb($this->elb)->setLoadBalancerName($attributesResult->LoadBalancerName);
     if (!empty($attributesResult->LoadBalancerAttributes->AdditionalAttributes->member)) {
         $attrList = new AdditionalAttributesList();
         foreach ($attributesResult->LoadBalancerAttributes->AdditionalAttributes->member as $v) {
             $attrData = new AdditionalAttributesData();
             $attrData->value = (string) $v->Value;
             $attrData->key = (string) $v->Key;
             $attrList->append($attrData);
             unset($attrData);
         }
         $result->setAdditionalAttributes($attrList);
     }
     if (!empty($attributesResult->LoadBalancerAttributes->AccessLog)) {
         $sxml = $attributesResult->LoadBalancerAttributes->AccessLog;
         $accessLog = new AccessLogData((string) $sxml->Enabled == 'true');
         $accessLog->emitInterval = isset($sxml->EmitInterval) ? (int) $sxml->EmitInterval : null;
         $accessLog->s3BucketName = isset($sxml->S3BucketName) ? (string) $sxml->S3BucketName : null;
         $accessLog->s3BucketPrefix = isset($sxml->S3BucketPrefix) ? (string) $sxml->S3BucketPrefix : null;
         $result->setAccessLog($accessLog);
     }
     if (!empty($attributesResult->LoadBalancerAttributes->ConnectionDraining)) {
         $sxml = $attributesResult->LoadBalancerAttributes->ConnectionDraining;
         $connectionDraining = new ConnectionDrainingData((string) $sxml->Enabled == 'true');
         $connectionDraining->timeout = isset($sxml->Timeout) ? (int) $sxml->Timeout : null;
         $result->setConnectionDraining($connectionDraining);
     }
     if (!empty($attributesResult->LoadBalancerAttributes->ConnectionSettings)) {
         $sxml = $attributesResult->LoadBalancerAttributes->ConnectionSettings;
         $connectionSettings = new ConnectionSettingsData((int) $sxml->IdleTimeout);
         $result->setConnectionSettings($connectionSettings);
     }
     if (!empty($attributesResult->LoadBalancerAttributes->CrossZoneLoadBalancing)) {
         $sxml = $attributesResult->LoadBalancerAttributes->CrossZoneLoadBalancing;
         $crossZoneLoadBalancing = new CrossZoneLoadBalancingData((string) $sxml->Enabled == 'true');
         $result->setCrossZoneLoadBalancing($crossZoneLoadBalancing);
     }
     return $result;
 }