Scalr\Api\Service\User\V1beta0\Controller\FarmRoles::getFarm PHP Méthode

getFarm() public méthode

Gets farm from database using User's Environment
public getFarm ( integer $farmId, boolean $modify = false ) : Farm | null
$farmId integer The identifier of the Role
$modify boolean optional Modification flag
Résultat Scalr\Model\Entity\Farm | null Returns specified Farm
    public function getFarm($farmId, $modify = false)
    {
        if (empty($this->farmController)) {
            $this->farmController = $this->getContainer()->api->controller(static::$farmControllerClass);
        }
        return $this->farmController->getFarm($farmId, $modify);
    }

Usage Example

Exemple #1
0
 /**
  * {@inheritdoc}
  * @see ApiEntityAdapter::validateEntity()
  */
 public function validateEntity($entity)
 {
     if (!$entity instanceof FarmRole) {
         throw new InvalidArgumentException(sprintf("First argument must be instance of Scalr\\Model\\Entity\\FarmRole class"));
     }
     if ($entity->id !== null) {
         if (!FarmRole::findPk($entity->id)) {
             throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, sprintf("Could not find out the Farm with ID: %d", $entity->id));
         }
     }
     if (empty($entity->farmId)) {
         throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property farm.id");
     } else {
         $farm = $this->controller->getFarm($entity->farmId, true);
     }
     if (empty($entity->roleId)) {
         throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property role.id");
     }
     if (empty($entity->platform)) {
         throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property platform");
     }
     switch ($entity->platform) {
         case SERVER_PLATFORMS::EC2:
             if (empty($entity->settings[FarmRoleSetting::AWS_INSTANCE_TYPE])) {
                 throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Missed property instance.type");
             }
             /* @var $platform Ec2PlatformModule */
             $platform = PlatformFactory::NewPlatform(SERVER_PLATFORMS::EC2);
             if (!in_array($entity->settings[FarmRoleSetting::AWS_INSTANCE_TYPE], $platform->getInstanceTypes())) {
                 throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Wrong instance type");
             }
             $gov = new Scalr_Governance($this->controller->getEnvironment()->id);
             $allowGovernanceIns = $gov->getValue(SERVER_PLATFORMS::EC2, Scalr_Governance::AWS_INSTANCE_TYPE);
             if (isset($allowGovernanceIns) && !in_array($entity->settings[FarmRoleSetting::AWS_INSTANCE_TYPE], $allowGovernanceIns)) {
                 throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, sprintf("Only %s %s allowed according to governance settings", ...count($allowGovernanceIns) > 1 ? [implode(', ', $allowGovernanceIns), 'instances are'] : [array_shift($allowGovernanceIns), 'instance is']));
             }
             if (!in_array($entity->cloudLocation, Aws::getCloudLocations())) {
                 throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, "Unknown region");
             }
             $vpcGovernanceRegions = $gov->getValue(SERVER_PLATFORMS::EC2, Scalr_Governance::AWS_VPC, 'regions');
             if (isset($vpcGovernanceRegions) && !array_key_exists($entity->cloudLocation, $vpcGovernanceRegions)) {
                 $regions = array_keys($vpcGovernanceRegions);
                 throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, sprintf("Only %s %s allowed according to governance settings", ...count($regions) > 1 ? [implode(', ', $regions), 'regions are'] : [array_shift($regions), 'region is']));
             }
             $env = Scalr_Environment::init()->loadById($this->controller->getEnvironment()->id);
             $aws = $this->controller->getContainer()->aws($entity->cloudLocation, $env);
             if (!empty($entity->settings[FarmRoleSetting::AWS_AVAIL_ZONE]) && $entity->settings[FarmRoleSetting::AWS_AVAIL_ZONE] !== 'x-scalr-diff') {
                 $availZones = explode(":", str_replace("x-scalr-custom=", '', $entity->settings[FarmRoleSetting::AWS_AVAIL_ZONE]));
                 $ec2availabilityZones = [];
                 foreach ($aws->ec2->availabilityZone->describe() as $zone) {
                     /* @var $zone AvailabilityZoneData */
                     if (stristr($zone->zoneState, 'available')) {
                         $ec2availabilityZones[] = $zone->zoneName;
                     }
                 }
                 $diffZones = array_diff($availZones, $ec2availabilityZones);
                 if (!empty($diffZones)) {
                     throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, sprintf('%s %s available. Available zones are %s', ...count($diffZones) > 1 ? [implode(', ', $diffZones), 'zones are not', implode(', ', $ec2availabilityZones)] : [array_shift($diffZones), 'zone is not', implode(', ', $ec2availabilityZones)]));
                 }
             }
             if (!empty($entity->settings[FarmRoleSetting::AWS_VPC_SUBNET_ID])) {
                 $vpcId = $farm->settings[FarmSetting::EC2_VPC_ID];
                 $subnets = $platform->listSubnets($env, $entity->cloudLocation, $vpcId, true);
                 $vpcGovernanceIds = $gov->getValue(SERVER_PLATFORMS::EC2, Scalr_Governance::AWS_VPC, 'ids');
                 $subnetType = null;
                 foreach (json_decode($entity->settings[FarmRoleSetting::AWS_VPC_SUBNET_ID]) as $subnetId) {
                     $found = false;
                     foreach ($subnets as $subnet) {
                         if ($subnet['id'] == $subnetId) {
                             if ($subnetType == null) {
                                 $subnetType = $subnet['type'];
                             } else {
                                 if ($subnet['type'] != $subnetType) {
                                     throw new ApiErrorException(409, ErrorMessage::ERR_UNICITY_VIOLATION, "All subnets must be a same type");
                                 }
                             }
                             //check governance subnet settings
                             if (isset($vpcGovernanceIds[$vpcId])) {
                                 if (!empty($vpcGovernanceIds[$vpcId]) && is_array($vpcGovernanceIds[$vpcId]) && !in_array($subnetId, $vpcGovernanceIds[$vpcId])) {
                                     throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, sprintf("Only %s %s allowed by governance settings", ...count($vpcGovernanceIds[$vpcId]) > 1 ? [implode(', ', $vpcGovernanceIds[$vpcId]), 'subnets are'] : [array_shift($vpcGovernanceIds[$vpcId]), 'subnet is']));
                                 } elseif ($vpcGovernanceIds[$vpcId] == "outbound-only" && $subnetType != 'private') {
                                     throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Only private subnets allowed by governance settings");
                                 } elseif ($vpcGovernanceIds[$vpcId] == "full" && $subnetType != 'public') {
                                     throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Only public subnets allowed by governance settings");
                                 }
                             }
                             $found = true;
                         }
                     }
                     if (!$found) {
                         throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, "Subnet with id '{$subnetId}' not found");
                     }
                 }
                 if (!empty($entity->settings[Scalr_Role_Behavior_Router::ROLE_VPC_SCALR_ROUTER_ID])) {
                     $router = $this->controller->getFarmRole($entity->settings[Scalr_Role_Behavior_Router::ROLE_VPC_SCALR_ROUTER_ID]);
                     if (empty($router->settings[Scalr_Role_Behavior_Router::ROLE_VPC_NID])) {
                         throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Farm-role with id '{$router->id}' is not a valid router");
                     }
                 } else {
                     if ($subnetType == 'private') {
                         throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "You must describe a VPC Router");
                     }
                 }
             } else {
                 if ($farm->settings[FarmSetting::EC2_VPC_ID]) {
                     throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "VPC Subnet(s) should be described");
                 }
             }
             break;
         default:
             if (in_array($entity->platform, SERVER_PLATFORMS::GetList())) {
                 throw new ApiErrorException(501, ErrorMessage::ERR_NOT_IMPLEMENTED, "Platform '{$entity->platform}' is not supported yet");
             } else {
                 throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, "Unknown platform '{$entity->platform}'");
             }
     }
     if (!$this->controller->hasPermissions($entity, true)) {
         //Checks entity level write access permissions
         throw new ApiErrorException(403, ErrorMessage::ERR_PERMISSION_VIOLATION, "Insufficient permissions");
     }
 }
All Usage Examples Of Scalr\Api\Service\User\V1beta0\Controller\FarmRoles::getFarm