[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH] esx: Add esxVI_LookupVirtualMachineByName



Used in esxDomainLookupByName and to be used in esxDomainDefineXML later.
---
 src/esx/esx_driver.c |   66 +++++++++++++++++----------------------------
 src/esx/esx_vi.c     |   72 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/esx/esx_vi.h     |    5 +++
 3 files changed, 102 insertions(+), 41 deletions(-)

diff --git a/src/esx/esx_driver.c b/src/esx/esx_driver.c
index b97945d..30a1adb 100644
--- a/src/esx/esx_driver.c
+++ b/src/esx/esx_driver.c
@@ -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 (virtualMachine == NULL) {
+        ESX_ERROR(VIR_ERR_NO_DOMAIN, "No domain with name '%s'", name);
+        goto failure;
+    }
 
-        if (esxVI_GetVirtualMachinePowerState(virtualMachine,
-                                              &powerState) < 0) {
-            goto failure;
-        }
 
-        domain = virGetDomain(conn, name_candidate, uuid_candidate);
+    if (esxVI_GetVirtualMachineIdentity(virtualMachine, &id, NULL, uuid) < 0) {
+        goto failure;
+    }
 
-        if (domain == NULL) {
-            goto failure;
-        }
+    if (esxVI_GetVirtualMachinePowerState(virtualMachine,
+                                          &powerState) < 0) {
+        goto failure;
+    }
 
-        /* Only running/suspended virtual machines have an ID != -1 */
-        if (powerState != esxVI_VirtualMachinePowerState_PoweredOff) {
-            domain->id = id_candidate;
-        } else {
-            domain->id = -1;
-        }
+    domain = virGetDomain(conn, name, uuid);
 
-        break;
+    if (domain == NULL) {
+        goto failure;
     }
 
-    if (domain == NULL) {
-        ESX_ERROR(VIR_ERR_NO_DOMAIN, "No domain with name '%s'", name);
+    /* 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;
 
diff --git a/src/esx/esx_vi.c b/src/esx/esx_vi.c
index 63ddaa4..326add7 100644
--- a/src/esx/esx_vi.c
+++ b/src/esx/esx_vi.c
@@ -1877,6 +1877,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,
    esxVI_String *propertyNameList, esxVI_ObjectContent **virtualMachine,
diff --git a/src/esx/esx_vi.h b/src/esx/esx_vi.h
index a57406c..1349a1b 100644
--- a/src/esx/esx_vi.h
+++ b/src/esx/esx_vi.h
@@ -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,
-- 
1.6.3.3


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]