Scalr\Model\Entity\FarmRole::delete PHP Method

delete() public method

See also: AbstractEntity::delete()
public delete ( )
    public function delete()
    {
        $db = $this->db();
        try {
            // we should set scaling to manual to prevent starting new instances while we are deleting FarmRole
            $frs = new FarmRoleSetting();
            $db->Execute("\n                 UPDATE {$frs->table()}\n                 SET {$frs->columnValue} = ?\n                 WHERE {$frs->columnFarmRoleId} = ?\n                 AND {$frs->columnName} = ?\n            ", [0, $this->id, FarmRoleSetting::SCALING_ENABLED]);
            $this->terminateServers();
            $db->BeginTrans();
            // Clear farm role options & scripts
            $db->Execute("DELETE FROM farm_role_service_config_presets WHERE farm_roleid=?", [$this->id]);
            $db->Execute("DELETE FROM farm_role_scaling_times WHERE farm_roleid=?", [$this->id]);
            $db->Execute("DELETE FROM farm_role_service_config_presets WHERE farm_roleid=?", [$this->id]);
            $db->Execute("DELETE FROM farm_role_scripting_targets WHERE `target`=? AND `target_type` = 'farmrole'", [$this->id]);
            $db->Execute("DELETE FROM ec2_ebs WHERE farm_roleid=?", [$this->id]);
            $db->Execute("DELETE FROM elastic_ips WHERE farm_roleid=?", [$this->id]);
            $db->Execute("DELETE FROM storage_volumes WHERE farm_roleid=?", [$this->id]);
            // Clear apache vhosts and update DNS zones
            $db->Execute("UPDATE apache_vhosts SET farm_roleid='0', farm_id='0' WHERE farm_roleid=?", [$this->id]);
            $db->Execute("UPDATE dns_zones SET farm_roleid='0' WHERE farm_roleid=?", [$this->id]);
            $this->deleteScheduled();
            $db->Execute("DELETE FROM farm_role_scripts WHERE farm_roleid=?", [$this->id]);
            parent::delete();
            $db->CommitTrans();
        } catch (Exception $e) {
            $db->RollbackTrans();
            throw $e;
        }
    }

Usage Example

Example #1
0
 public function FarmRemoveRole($FarmID, $FarmRoleID)
 {
     try {
         $DBFarm = DBFarm::LoadByID($FarmID);
         if ($DBFarm->EnvID != $this->Environment->id) {
             throw new Exception("N");
         }
     } catch (Exception $e) {
         throw new Exception(sprintf("Farm #%s not found", $FarmID));
     }
     $this->user->getPermissions()->validate($DBFarm);
     $this->restrictFarmAccess($DBFarm, Acl::PERM_FARMS_UPDATE);
     $DBFarm->isLocked(true);
     try {
         $DBFarmRole = DBFarmRole::LoadByID($FarmRoleID);
         if ($DBFarm->ID != $DBFarmRole->FarmID) {
             throw new Exception("N");
         }
     } catch (Exception $e) {
         throw new Exception(sprintf("FarmRole ID #%s not found", $FarmRoleID));
     }
     $this->user->getPermissions()->validate($DBFarm);
     $farmRole = new Entity\FarmRole();
     $farmRole->id = $FarmRoleID;
     $farmRole->delete();
     $response = $this->CreateInitialResponse();
     $response->Result = true;
     return $response;
 }