mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-03-07 17:28:15 +00:00
esx: Add esxVI_LookupVirtualMachineByName
Used in esxDomainLookupByName and to be used in esxDomainDefineXML later.
This commit is contained in:
parent
bba36f7fc7
commit
041a18be17
@ -1206,12 +1206,10 @@ esxDomainLookupByName(virConnectPtr conn, const char *name)
|
||||
{
|
||||
esxPrivate *priv = conn->privateData;
|
||||
esxVI_String *propertyNameList = NULL;
|
||||
esxVI_ObjectContent *virtualMachineList = NULL;
|
||||
esxVI_ObjectContent *virtualMachine = NULL;
|
||||
esxVI_VirtualMachinePowerState powerState;
|
||||
int id_candidate = -1;
|
||||
char *name_candidate = NULL;
|
||||
unsigned char uuid_candidate[VIR_UUID_BUFLEN];
|
||||
int id = -1;
|
||||
unsigned char uuid[VIR_UUID_BUFLEN];
|
||||
virDomainPtr domain = NULL;
|
||||
|
||||
if (esxVI_EnsureSession(priv->host) < 0) {
|
||||
@ -1220,59 +1218,45 @@ esxDomainLookupByName(virConnectPtr conn, const char *name)
|
||||
|
||||
if (esxVI_String_AppendValueListToList(&propertyNameList,
|
||||
"configStatus\0"
|
||||
"name\0"
|
||||
"runtime.powerState\0"
|
||||
"config.uuid\0") < 0 ||
|
||||
esxVI_LookupObjectContentByType(priv->host, priv->host->vmFolder,
|
||||
"VirtualMachine", propertyNameList,
|
||||
esxVI_Boolean_True,
|
||||
&virtualMachineList) < 0) {
|
||||
esxVI_LookupVirtualMachineByName(priv->host, name, propertyNameList,
|
||||
&virtualMachine,
|
||||
esxVI_Occurrence_OptionalItem) < 0) {
|
||||
goto failure;
|
||||
}
|
||||
|
||||
for (virtualMachine = virtualMachineList; virtualMachine != NULL;
|
||||
virtualMachine = virtualMachine->_next) {
|
||||
VIR_FREE(name_candidate);
|
||||
|
||||
if (esxVI_GetVirtualMachineIdentity(virtualMachine,
|
||||
&id_candidate, &name_candidate,
|
||||
uuid_candidate) < 0) {
|
||||
goto failure;
|
||||
}
|
||||
|
||||
if (STRNEQ(name, name_candidate)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (esxVI_GetVirtualMachinePowerState(virtualMachine,
|
||||
&powerState) < 0) {
|
||||
goto failure;
|
||||
}
|
||||
|
||||
domain = virGetDomain(conn, name_candidate, uuid_candidate);
|
||||
|
||||
if (domain == NULL) {
|
||||
goto failure;
|
||||
}
|
||||
|
||||
/* Only running/suspended virtual machines have an ID != -1 */
|
||||
if (powerState != esxVI_VirtualMachinePowerState_PoweredOff) {
|
||||
domain->id = id_candidate;
|
||||
} else {
|
||||
domain->id = -1;
|
||||
}
|
||||
|
||||
break;
|
||||
if (virtualMachine == NULL) {
|
||||
ESX_ERROR(VIR_ERR_NO_DOMAIN, "No domain with name '%s'", name);
|
||||
goto failure;
|
||||
}
|
||||
|
||||
|
||||
if (esxVI_GetVirtualMachineIdentity(virtualMachine, &id, NULL, uuid) < 0) {
|
||||
goto failure;
|
||||
}
|
||||
|
||||
if (esxVI_GetVirtualMachinePowerState(virtualMachine,
|
||||
&powerState) < 0) {
|
||||
goto failure;
|
||||
}
|
||||
|
||||
domain = virGetDomain(conn, name, uuid);
|
||||
|
||||
if (domain == NULL) {
|
||||
ESX_ERROR(VIR_ERR_NO_DOMAIN, "No domain with name '%s'", name);
|
||||
goto failure;
|
||||
}
|
||||
|
||||
/* Only running/suspended virtual machines have an ID != -1 */
|
||||
if (powerState != esxVI_VirtualMachinePowerState_PoweredOff) {
|
||||
domain->id = id;
|
||||
} else {
|
||||
domain->id = -1;
|
||||
}
|
||||
|
||||
cleanup:
|
||||
esxVI_String_Free(&propertyNameList);
|
||||
esxVI_ObjectContent_Free(&virtualMachineList);
|
||||
VIR_FREE(name_candidate);
|
||||
esxVI_ObjectContent_Free(&virtualMachine);
|
||||
|
||||
return domain;
|
||||
|
||||
|
@ -1876,6 +1876,78 @@ esxVI_LookupVirtualMachineByUuid(esxVI_Context *ctx, const unsigned char *uuid,
|
||||
|
||||
|
||||
|
||||
int
|
||||
esxVI_LookupVirtualMachineByName(esxVI_Context *ctx, const char *name,
|
||||
esxVI_String *propertyNameList,
|
||||
esxVI_ObjectContent **virtualMachine,
|
||||
esxVI_Occurrence occurrence)
|
||||
{
|
||||
int result = 0;
|
||||
esxVI_String *completePropertyNameList = NULL;
|
||||
esxVI_ObjectContent *virtualMachineList = NULL;
|
||||
esxVI_ObjectContent *candidate = NULL;
|
||||
char *name_candidate = NULL;
|
||||
|
||||
if (virtualMachine == NULL || *virtualMachine != NULL) {
|
||||
ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "Invalid argument");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (esxVI_String_DeepCopyList(&completePropertyNameList,
|
||||
propertyNameList) < 0 ||
|
||||
esxVI_String_AppendValueToList(&completePropertyNameList, "name") < 0 ||
|
||||
esxVI_LookupObjectContentByType(ctx, ctx->vmFolder, "VirtualMachine",
|
||||
completePropertyNameList,
|
||||
esxVI_Boolean_True,
|
||||
&virtualMachineList) < 0) {
|
||||
goto failure;
|
||||
}
|
||||
|
||||
for (candidate = virtualMachineList; candidate != NULL;
|
||||
candidate = candidate->_next) {
|
||||
VIR_FREE(name_candidate);
|
||||
|
||||
if (esxVI_GetVirtualMachineIdentity(candidate, NULL, &name_candidate,
|
||||
NULL) < 0) {
|
||||
goto failure;
|
||||
}
|
||||
|
||||
if (STRNEQ(name, name_candidate)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (esxVI_ObjectContent_DeepCopy(virtualMachine, candidate) < 0) {
|
||||
goto failure;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (*virtualMachine == NULL) {
|
||||
if (occurrence == esxVI_Occurrence_OptionalItem) {
|
||||
return 0;
|
||||
} else {
|
||||
ESX_VI_ERROR(VIR_ERR_NO_DOMAIN,
|
||||
"Could not find domain with name '%s'", name);
|
||||
goto failure;
|
||||
}
|
||||
}
|
||||
|
||||
cleanup:
|
||||
esxVI_String_Free(&completePropertyNameList);
|
||||
esxVI_ObjectContent_Free(&virtualMachineList);
|
||||
VIR_FREE(name_candidate);
|
||||
|
||||
return result;
|
||||
|
||||
failure:
|
||||
result = -1;
|
||||
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int
|
||||
esxVI_LookupVirtualMachineByUuidAndPrepareForTask
|
||||
(esxVI_Context *ctx, const unsigned char *uuid,
|
||||
|
@ -237,6 +237,11 @@ int esxVI_LookupVirtualMachineByUuid(esxVI_Context *ctx,
|
||||
esxVI_ObjectContent **virtualMachine,
|
||||
esxVI_Occurrence occurrence);
|
||||
|
||||
int esxVI_LookupVirtualMachineByName(esxVI_Context *ctx, const char *name,
|
||||
esxVI_String *propertyNameList,
|
||||
esxVI_ObjectContent **virtualMachine,
|
||||
esxVI_Occurrence occurrence);
|
||||
|
||||
int esxVI_LookupVirtualMachineByUuidAndPrepareForTask
|
||||
(esxVI_Context *ctx, const unsigned char *uuid,
|
||||
esxVI_String *propertyNameList, esxVI_ObjectContent **virtualMachine,
|
||||
|
Loading…
x
Reference in New Issue
Block a user