esx: Move occurrence check into esxVI_LookupObjectContentByType

This simplifies the callers of esxVI_LookupObjectContentByType.
This commit is contained in:
Matthias Bolte 2010-12-30 13:30:44 +01:00
parent 3d4f6eeeae
commit dd1f59a9e5
3 changed files with 69 additions and 81 deletions

View File

@ -3886,7 +3886,8 @@ esxNodeGetFreeMemory(virConnectPtr conn)
esxVI_LookupObjectContentByType(priv->primary, esxVI_LookupObjectContentByType(priv->primary,
priv->primary->computeResource->resourcePool, priv->primary->computeResource->resourcePool,
"ResourcePool", propertyNameList, "ResourcePool", propertyNameList,
&resourcePool) < 0) { &resourcePool,
esxVI_Occurrence_RequiredItem) < 0) {
goto cleanup; goto cleanup;
} }

View File

@ -499,13 +499,8 @@ esxVI_Context_LookupObjectsByPath(esxVI_Context *ctx,
"hostFolder\0") < 0 || "hostFolder\0") < 0 ||
esxVI_LookupObjectContentByType(ctx, ctx->service->rootFolder, esxVI_LookupObjectContentByType(ctx, ctx->service->rootFolder,
"Datacenter", propertyNameList, "Datacenter", propertyNameList,
&datacenterList) < 0) { &datacenterList,
goto cleanup; esxVI_Occurrence_RequiredList) < 0) {
}
if (datacenterList == NULL) {
ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
_("Could not retrieve datacenter list"));
goto cleanup; goto cleanup;
} }
@ -548,13 +543,8 @@ esxVI_Context_LookupObjectsByPath(esxVI_Context *ctx,
"resourcePool\0") < 0 || "resourcePool\0") < 0 ||
esxVI_LookupObjectContentByType(ctx, ctx->datacenter->hostFolder, esxVI_LookupObjectContentByType(ctx, ctx->datacenter->hostFolder,
"ComputeResource", propertyNameList, "ComputeResource", propertyNameList,
&computeResourceList) < 0) { &computeResourceList,
goto cleanup; esxVI_Occurrence_RequiredList) < 0) {
}
if (computeResourceList == NULL) {
ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
_("Could not retrieve compute resource list"));
goto cleanup; goto cleanup;
} }
@ -610,13 +600,8 @@ esxVI_Context_LookupObjectsByPath(esxVI_Context *ctx,
"configManager\0") < 0 || "configManager\0") < 0 ||
esxVI_LookupObjectContentByType(ctx, ctx->computeResource->_reference, esxVI_LookupObjectContentByType(ctx, ctx->computeResource->_reference,
"HostSystem", propertyNameList, "HostSystem", propertyNameList,
&hostSystemList) < 0) { &hostSystemList,
goto cleanup; esxVI_Occurrence_RequiredList) < 0) {
}
if (hostSystemList == NULL) {
ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
_("Could not retrieve host system list"));
goto cleanup; goto cleanup;
} }
@ -687,17 +672,9 @@ esxVI_Context_LookupObjectsByHostSystemIp(esxVI_Context *ctx,
&managedObjectReference) < 0 || &managedObjectReference) < 0 ||
esxVI_LookupObjectContentByType(ctx, managedObjectReference, esxVI_LookupObjectContentByType(ctx, managedObjectReference,
"HostSystem", propertyNameList, "HostSystem", propertyNameList,
&hostSystem) < 0) { &hostSystem,
goto cleanup; esxVI_Occurrence_RequiredItem) < 0 ||
} esxVI_HostSystem_CastFromObjectContent(hostSystem,
if (hostSystem == NULL) {
ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
_("Could not retrieve host system"));
goto cleanup;
}
if (esxVI_HostSystem_CastFromObjectContent(hostSystem,
&ctx->hostSystem) < 0) { &ctx->hostSystem) < 0) {
goto cleanup; goto cleanup;
} }
@ -711,17 +688,9 @@ esxVI_Context_LookupObjectsByHostSystemIp(esxVI_Context *ctx,
"resourcePool\0") < 0 || "resourcePool\0") < 0 ||
esxVI_LookupObjectContentByType(ctx, hostSystem->obj, esxVI_LookupObjectContentByType(ctx, hostSystem->obj,
"ComputeResource", propertyNameList, "ComputeResource", propertyNameList,
&computeResource) < 0) { &computeResource,
goto cleanup; esxVI_Occurrence_RequiredItem) < 0 ||
} esxVI_ComputeResource_CastFromObjectContent(computeResource,
if (computeResource == NULL) {
ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
_("Could not retrieve compute resource of host system"));
goto cleanup;
}
if (esxVI_ComputeResource_CastFromObjectContent(computeResource,
&ctx->computeResource) < 0) { &ctx->computeResource) < 0) {
goto cleanup; goto cleanup;
} }
@ -735,17 +704,9 @@ esxVI_Context_LookupObjectsByHostSystemIp(esxVI_Context *ctx,
"hostFolder\0") < 0 || "hostFolder\0") < 0 ||
esxVI_LookupObjectContentByType(ctx, computeResource->obj, esxVI_LookupObjectContentByType(ctx, computeResource->obj,
"Datacenter", propertyNameList, "Datacenter", propertyNameList,
&datacenter) < 0) { &datacenter,
goto cleanup; esxVI_Occurrence_RequiredItem) < 0 ||
} esxVI_Datacenter_CastFromObjectContent(datacenter,
if (datacenter == NULL) {
ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
_("Could not retrieve datacenter of compute resource"));
goto cleanup;
}
if (esxVI_Datacenter_CastFromObjectContent(datacenter,
&ctx->datacenter) < 0) { &ctx->datacenter) < 0) {
goto cleanup; goto cleanup;
} }
@ -1586,7 +1547,8 @@ esxVI_EnsureSession(esxVI_Context *ctx)
"currentSession") < 0 || "currentSession") < 0 ||
esxVI_LookupObjectContentByType(ctx, ctx->service->sessionManager, esxVI_LookupObjectContentByType(ctx, ctx->service->sessionManager,
"SessionManager", propertyNameList, "SessionManager", propertyNameList,
&sessionManager) < 0) { &sessionManager,
esxVI_Occurrence_RequiredItem) < 0) {
goto cleanup; goto cleanup;
} }
@ -1636,7 +1598,8 @@ esxVI_LookupObjectContentByType(esxVI_Context *ctx,
esxVI_ManagedObjectReference *root, esxVI_ManagedObjectReference *root,
const char *type, const char *type,
esxVI_String *propertyNameList, esxVI_String *propertyNameList,
esxVI_ObjectContent **objectContentList) esxVI_ObjectContent **objectContentList,
esxVI_Occurrence occurrence)
{ {
int result = -1; int result = -1;
esxVI_ObjectSpec *objectSpec = NULL; esxVI_ObjectSpec *objectSpec = NULL;
@ -1710,12 +1673,41 @@ esxVI_LookupObjectContentByType(esxVI_Context *ctx,
esxVI_PropertySpec_AppendToList(&propertyFilterSpec->propSet, esxVI_PropertySpec_AppendToList(&propertyFilterSpec->propSet,
propertySpec) < 0 || propertySpec) < 0 ||
esxVI_ObjectSpec_AppendToList(&propertyFilterSpec->objectSet, esxVI_ObjectSpec_AppendToList(&propertyFilterSpec->objectSet,
objectSpec) < 0) { objectSpec) < 0 ||
esxVI_RetrieveProperties(ctx, propertyFilterSpec,
objectContentList) < 0) {
goto cleanup; goto cleanup;
} }
result = esxVI_RetrieveProperties(ctx, propertyFilterSpec, if (objectContentList == NULL) {
objectContentList); switch (occurrence) {
case esxVI_Occurrence_OptionalItem:
case esxVI_Occurrence_OptionalList:
result = 0;
break;
case esxVI_Occurrence_RequiredItem:
ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,
_("Could not lookup '%s' from '%s'"),
type, root->type);
break;
case esxVI_Occurrence_RequiredList:
ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,
_("Could not lookup '%s' list from '%s'"),
type, root->type);
break;
default:
ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
_("Invalid occurrence value"));
break;
}
goto cleanup;
}
result = 0;
cleanup: cleanup:
/* /*
@ -2276,7 +2268,8 @@ esxVI_LookupHostSystemProperties(esxVI_Context *ctx,
{ {
return esxVI_LookupObjectContentByType(ctx, ctx->hostSystem->_reference, return esxVI_LookupObjectContentByType(ctx, ctx->hostSystem->_reference,
"HostSystem", propertyNameList, "HostSystem", propertyNameList,
hostSystem); hostSystem,
esxVI_Occurrence_RequiredItem);
} }
@ -2290,7 +2283,8 @@ esxVI_LookupVirtualMachineList(esxVI_Context *ctx,
* for cluster support */ * for cluster support */
return esxVI_LookupObjectContentByType(ctx, ctx->hostSystem->_reference, return esxVI_LookupObjectContentByType(ctx, ctx->hostSystem->_reference,
"VirtualMachine", propertyNameList, "VirtualMachine", propertyNameList,
virtualMachineList); virtualMachineList,
esxVI_Occurrence_OptionalList);
} }
@ -2332,7 +2326,8 @@ esxVI_LookupVirtualMachineByUuid(esxVI_Context *ctx, const unsigned char *uuid,
if (esxVI_LookupObjectContentByType(ctx, managedObjectReference, if (esxVI_LookupObjectContentByType(ctx, managedObjectReference,
"VirtualMachine", propertyNameList, "VirtualMachine", propertyNameList,
virtualMachine) < 0) { virtualMachine,
esxVI_Occurrence_RequiredItem) < 0) {
goto cleanup; goto cleanup;
} }
@ -2475,7 +2470,8 @@ esxVI_LookupDatastoreList(esxVI_Context *ctx, esxVI_String *propertyNameList,
* support */ * support */
return esxVI_LookupObjectContentByType(ctx, ctx->hostSystem->_reference, return esxVI_LookupObjectContentByType(ctx, ctx->hostSystem->_reference,
"Datastore", propertyNameList, "Datastore", propertyNameList,
datastoreList); datastoreList,
esxVI_Occurrence_OptionalList);
} }
@ -2654,7 +2650,8 @@ esxVI_LookupDatastoreHostMount(esxVI_Context *ctx,
if (esxVI_String_AppendValueToList(&propertyNameList, "host") < 0 || if (esxVI_String_AppendValueToList(&propertyNameList, "host") < 0 ||
esxVI_LookupObjectContentByType(ctx, datastore, "Datastore", esxVI_LookupObjectContentByType(ctx, datastore, "Datastore",
propertyNameList, &objectContent) < 0) { propertyNameList, &objectContent,
esxVI_Occurrence_RequiredItem) < 0) {
goto cleanup; goto cleanup;
} }
@ -2719,7 +2716,8 @@ esxVI_LookupTaskInfoByTask(esxVI_Context *ctx,
if (esxVI_String_AppendValueToList(&propertyNameList, "info") < 0 || if (esxVI_String_AppendValueToList(&propertyNameList, "info") < 0 ||
esxVI_LookupObjectContentByType(ctx, task, "Task", propertyNameList, esxVI_LookupObjectContentByType(ctx, task, "Task", propertyNameList,
&objectContent) < 0) { &objectContent,
esxVI_Occurrence_RequiredItem) < 0) {
goto cleanup; goto cleanup;
} }
@ -3400,13 +3398,7 @@ esxVI_LookupAutoStartDefaults(esxVI_Context *ctx,
esxVI_LookupObjectContentByType esxVI_LookupObjectContentByType
(ctx, ctx->hostSystem->configManager->autoStartManager, (ctx, ctx->hostSystem->configManager->autoStartManager,
"HostAutoStartManager", propertyNameList, "HostAutoStartManager", propertyNameList,
&hostAutoStartManager) < 0) { &hostAutoStartManager, esxVI_Occurrence_RequiredItem) < 0) {
goto cleanup;
}
if (hostAutoStartManager == NULL) {
ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
_("Could not retrieve the HostAutoStartManager object"));
goto cleanup; goto cleanup;
} }
@ -3463,13 +3455,7 @@ esxVI_LookupAutoStartPowerInfoList(esxVI_Context *ctx,
esxVI_LookupObjectContentByType esxVI_LookupObjectContentByType
(ctx, ctx->hostSystem->configManager->autoStartManager, (ctx, ctx->hostSystem->configManager->autoStartManager,
"HostAutoStartManager", propertyNameList, "HostAutoStartManager", propertyNameList,
&hostAutoStartManager) < 0) { &hostAutoStartManager, esxVI_Occurrence_RequiredItem) < 0) {
goto cleanup;
}
if (hostAutoStartManager == NULL) {
ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
_("Could not retrieve the HostAutoStartManager object"));
goto cleanup; goto cleanup;
} }

View File

@ -284,7 +284,8 @@ int esxVI_LookupObjectContentByType(esxVI_Context *ctx,
esxVI_ManagedObjectReference *root, esxVI_ManagedObjectReference *root,
const char *type, const char *type,
esxVI_String *propertyNameList, esxVI_String *propertyNameList,
esxVI_ObjectContent **objectContentList); esxVI_ObjectContent **objectContentList,
esxVI_Occurrence occurrence);
int esxVI_GetManagedEntityStatus int esxVI_GetManagedEntityStatus
(esxVI_ObjectContent *objectContent, const char *propertyName, (esxVI_ObjectContent *objectContent, const char *propertyName,