Obtaining a List of TemplatesA list of virtual machines and virtual machine templates are obtained from the server using the same function: PRL_RESULT GetTemplateList(const PRL_HANDLE &hServer) { PRL_HANDLE hJob = PRL_INVALID_HANDLE; PRL_HANDLE hJobResult = PRL_INVALID_HANDLE;
PRL_RESULT ret = PRL_ERR_UNINITIALIZED; PRL_RESULT nJobReturnCode = PRL_ERR_UNINITIALIZED;
// Get a list of the available virtual machines. hJob = PrlSrv_GetVmList(hServer);
// Wait for a maximum of 10 seconds for PrlSrv_GetVmList. ret = PrlJob_Wait(hJob, 10000); if (PRL_FAILED(ret)) { fprintf(stderr, "PrlJob_Wait for PrlSrv_GetVmList returned with error: %s\n", prl_result_to_string(ret)); PrlHandle_Free(hJob); return ret; }
// Check the results of PrlSrv_GetVmList. ret = PrlJob_GetRetCode(hJob, &nJobReturnCode); if (PRL_FAILED(ret)) { fprintf(stderr, "PrlJob_GetRetCode returned with error: %s\n", prl_result_to_string(ret)); PrlHandle_Free(hJob); return ret; }
if (PRL_FAILED(nJobReturnCode)) { fprintf(stderr, "PrlSrv_GetVmList returned with error: %s\n", prl_result_to_string(ret)); PrlHandle_Free(hJob); return ret; }
// Get the results of PrlSrv_GetVmList. ret = PrlJob_GetResult(hJob, &hJobResult); if (PRL_FAILED(ret)) { fprintf(stderr, "PrlJob_GetResult returned with error: %s\n", prl_result_to_string(ret)); PrlHandle_Free(hJob); return ret; }
// Handle to result object is available, // job handle is no longer needed, so free it. PrlHandle_Free(hJob);
// Iterate through the results (list of virtual machines returned). PRL_UINT32 nParamsCount = 0; ret = PrlResult_GetParamsCount(hJobResult, &nParamsCount); for (PRL_UINT32 i = 0; i < nParamsCount; ++i) { // Virtual machine handle PRL_HANDLE hVm = PRL_INVALID_HANDLE;
// Get a handle to result at index i. PrlResult_GetParamByIndex(hJobResult, i, &hVm);
// Obtain the PHT_VM_CONFIGURATION object. PRL_HANDLE hVmCfg = PRL_INVALID_HANDLE; ret = PrlVm_GetConfig(hVm, &hVmCfg);
// See if the handle contains information about a template. PRL_BOOL isTemplate = PRL_FALSE; PrlVmCfg_IsTemplate(hVmCfg, &isTemplate);
// If this is not a template, proceed to the next // virtual machine in the list. if (isTemplate == PRL_FALSE) { PrlHandle_Free(hVmCfg); PrlHandle_Free(hVm); continue; }
// Get the name of the template for result i. char szVmNameReturned[1024]; PRL_UINT32 nBufSize = sizeof(szVmNameReturned); ret = PrlVmCfg_GetName(hVmCfg, szVmNameReturned, &nBufSize); if (PRL_FAILED(ret)) { printf("PrlVmCfg_GetName returned with error (%s)\n", prl_result_to_string(ret)); } else { printf("Template name: '%s'.\n", szVmNameReturned); }
PrlHandle_Free(hVm); PrlHandle_Free(hVmCfg); }
return PRL_ERR_SUCCESS; } |
||||
|