﻿<?PHP

    //====== custom parameters ==========================
    
    $hierarchyRootName = "rootName";
    $hierarchyObjName = "objName";
    $hierarchyObjType = "objType";
    $login = "login";
    $password = "password";
    $jobName = "jobName";
    
    //=========================================

	require_once('Common.php');
	require_once('util\http.php');
	
	use XmlSerialization\XmlSerializer;	
    use \com\veeam\www\ent\v1_0 as restApi;
    
    
	$http = new HttpContext('127.0.0.1', 9399);
	$restApiInteraction = new RestApiInteraction($http);
	
	// log in using basic authentification
	$session = $restApiInteraction->LogInByBasicAuthentication($login, $password);
	if ($session->SessionId == null) { echo 'Login failed'; exit; }

	echo 'Logged in successfully, sessionId : ' . $session->SessionId . "\r\n";
    
    $queryResult = $restApiInteraction -> GetObject(
                        "/api/query?type=HierarchyRoot&format=References&filter=Name==\"" . urlencode($hierarchyRootName) . "\"",
                        ClassHelper::CombineWithNamespace("QueryResultType")
                    );

    if (!isset($queryResult) || !isset($queryResult->Refs) 
        || !isset($queryResult->Refs->Ref)) { echo "Cannot find hierarchy root with the following name: '" . $hierarchyRootName . "'";exit; }
        

    $hRootRef = $queryResult -> Refs -> Ref[0];
    
    echo "Getting hierarchy object info: object name '$hierarchyObjName', type $hierarchyObjType, hierarchy root '$hierarchyRootName'\r\n";
    $vmLookupRes = $restApiInteraction -> GetObject(
                        "/api/lookup?host=" . urlencode($hRootRef->UID) . "&name=$hierarchyObjName&type=$hierarchyObjType",
                        ClassHelper::CombineWithNamespace("HierarchyItemListType")
                    );
    if (!isset($vmLookupRes)) { echo "Cannot find hierarchy root with the following name: '" . $hierarchyRootName . "'";exit; }

    $vmInfo = $vmLookupRes->HierarchyItem[0];
    
    $createOijSpec = new restApi\CreateObjectInJobSpecType();
    $createOijSpec->HierarchyObjRef = $vmInfo->ObjectRef;
    $createOijSpec->HierarchyObjName = $vmInfo->ObjectName;
    $createOijSpec->Order = 0;
    $createOijSpec->GuestProcessingOptions = new restApi\GuestProcessingOptionsType();
    $createOijSpec->GuestProcessingOptions->AppAwareProcessingMode = "RequireSuccess";
    $createOijSpec->GuestProcessingOptions->FileSystemIndexingMode = "ExceptSpecifiedFolders";
    $createOijSpec->GuestProcessingOptions->ExcludedIndexingFolders = new restApi\StringListType();
    $createOijSpec->GuestProcessingOptions->ExcludedIndexingFolders->Path = array("%windir%", "%ProgramFiles%", "%TEMP%");
    
    echo "Getting job \"$jobName\" info\n";
    $jobQueryResult = $restApiInteraction -> GetObject(
                        "/api/query?type=Job&format=Entities&filter=Name==\"" . urlencode($jobName) . "\"",
                        ClassHelper::CombineWithNamespace("QueryResultType")
                    );
    if (!isset($jobQueryResult) || !isset($jobQueryResult->Entities) 
        || !isset($jobQueryResult->Entities->Jobs) || !isset($jobQueryResult->Entities->Jobs->Job)) { echo "Cannot find job with the following name: '" . $jobName . "'";exit; }
        
    $jobToEdit = $jobQueryResult->Entities->Jobs->Job[0];
    $createOibLink = LinkHelper::FindLink($jobToEdit->Links->Link, LinkRelations::Create);
    echo "Start editing task\n";
    $createOijTask = $restApiInteraction->PostObject (
                            $createOibLink->Href,
                            $createOijSpec,
                            ClassHelper::CombineWithNamespace("TaskType")
                            );
                            
    if (!$createOijTask) { echo 'Failed to edit job'; exit; }
	
    echo "Waiting for the task to finish\n";
	if (strcmp($createOijTask->State, 'Running') == 0) {
		$createOijTask = $restApiInteraction->WaitForTaskResult($createOijTask->Href, 1);
	}
	
	// check if clone task is succeeded
	if (!$createOijTask->Result->Success) { echo "Job editing failed\r\n\r\n"; var_dump($createOijTask); exit; }
	
	echo "Job edited successfully\n";
    echo "Removing tasks\n";
    $restApiInteraction->DeleteTask($createOijTask);
    echo "Logout";
    $restApiInteraction->Logout($session);
    
?>