From 070f61002f47b602c15d1e4950a122ac9edefe1b Mon Sep 17 00:00:00 2001 From: Matthias Bolte Date: Thu, 3 Jun 2010 20:04:15 +0200 Subject: [PATCH] esx: Refactor esxUtil_ParseQuery's parameter handling Pass a struct containing the parameters instead of passing each one individually. This make future extensions a bit simpler. --- src/esx/esx_driver.c | 44 +++++++++---------- src/esx/esx_util.c | 100 +++++++++++++++++++++---------------------- src/esx/esx_util.h | 15 ++++++- src/esx/esx_vi.c | 2 +- src/esx/esx_vi.h | 2 +- 5 files changed, 86 insertions(+), 77 deletions(-) diff --git a/src/esx/esx_driver.c b/src/esx/esx_driver.c index 7257d6a7a1..0c12a3192b 100644 --- a/src/esx/esx_driver.c +++ b/src/esx/esx_driver.c @@ -314,12 +314,11 @@ esxOpen(virConnectPtr conn, virConnectAuthPtr auth, int flags ATTRIBUTE_UNUSED) { virDrvOpenStatus result = VIR_DRV_OPEN_ERROR; esxPrivate *priv = NULL; + esxUtil_ParsedQuery *parsedQuery = NULL; char hostIpAddress[NI_MAXHOST] = ""; char vCenterIpAddress[NI_MAXHOST] = ""; char *url = NULL; char *vCenter = NULL; - int noVerify = 0; // boolean - int autoAnswer = 0; // boolean char *username = NULL; char *password = NULL; esxVI_String *propertyNameList = NULL; @@ -349,21 +348,20 @@ esxOpen(virConnectPtr conn, virConnectAuthPtr auth, int flags ATTRIBUTE_UNUSED) goto cleanup; } + if (esxUtil_ParseQuery(&parsedQuery, conn->uri) < 0) { + goto cleanup; + } + + priv->transport = parsedQuery->transport; + parsedQuery->transport = NULL; + priv->maxVcpus = -1; priv->supportsVMotion = esxVI_Boolean_Undefined; priv->supportsLongMode = esxVI_Boolean_Undefined; - priv->autoAnswer = esxVI_Boolean_False; + priv->autoAnswer = parsedQuery->autoAnswer ? esxVI_Boolean_True + : esxVI_Boolean_False; priv->usedCpuTimeCounterId = -1; - if (esxUtil_ParseQuery(conn->uri, &priv->transport, &vCenter, &noVerify, - &autoAnswer) < 0) { - goto cleanup; - } - - if (autoAnswer) { - priv->autoAnswer = esxVI_Boolean_True; - } - /* * Set the port dependent on the transport protocol if no port is * specified. This allows us to rely on the port parameter being @@ -414,10 +412,6 @@ esxOpen(virConnectPtr conn, virConnectAuthPtr auth, int flags ATTRIBUTE_UNUSED) } } - if (esxVI_Context_Alloc(&priv->host) < 0) { - goto cleanup; - } - password = virRequestPassword(auth, username, conn->uri->server); if (password == NULL) { @@ -425,8 +419,9 @@ esxOpen(virConnectPtr conn, virConnectAuthPtr auth, int flags ATTRIBUTE_UNUSED) goto cleanup; } - if (esxVI_Context_Connect(priv->host, url, hostIpAddress, username, - password, noVerify) < 0) { + if (esxVI_Context_Alloc(&priv->host) < 0 || + esxVI_Context_Connect(priv->host, url, hostIpAddress, username, + password, parsedQuery->noVerify) < 0) { goto cleanup; } @@ -559,7 +554,8 @@ esxOpen(virConnectPtr conn, virConnectAuthPtr auth, int flags ATTRIBUTE_UNUSED) } if (esxVI_Context_Connect(priv->vCenter, url, vCenterIpAddress, - username, password, noVerify) < 0) { + username, password, + parsedQuery->noVerify) < 0) { goto cleanup; } @@ -594,6 +590,8 @@ esxOpen(virConnectPtr conn, virConnectAuthPtr auth, int flags ATTRIBUTE_UNUSED) VIR_FREE(priv); } + + esxUtil_FreeParsedQuery(&parsedQuery); VIR_FREE(url); VIR_FREE(vCenter); VIR_FREE(password); @@ -2961,14 +2959,14 @@ esxDomainMigratePrepare(virConnectPtr dconn, unsigned long resource ATTRIBUTE_UNUSED) { int result = -1; - char *transport = NULL; + esxUtil_ParsedQuery *parsedQuery = NULL; if (uri_in == NULL) { - if (esxUtil_ParseQuery(dconn->uri, &transport, NULL, NULL, NULL) < 0) { + if (esxUtil_ParseQuery(&parsedQuery, dconn->uri) < 0) { return -1; } - if (virAsprintf(uri_out, "%s://%s:%d/sdk", transport, + if (virAsprintf(uri_out, "%s://%s:%d/sdk", parsedQuery->transport, dconn->uri->server, dconn->uri->port) < 0) { virReportOOMError(); goto cleanup; @@ -2978,7 +2976,7 @@ esxDomainMigratePrepare(virConnectPtr dconn, result = 0; cleanup: - VIR_FREE(transport); + esxUtil_FreeParsedQuery(&parsedQuery); return result; } diff --git a/src/esx/esx_util.c b/src/esx/esx_util.c index 1ffcc223f7..dba9bc3b8e 100644 --- a/src/esx/esx_util.c +++ b/src/esx/esx_util.c @@ -39,29 +39,25 @@ #define VIR_FROM_THIS VIR_FROM_ESX + int -esxUtil_ParseQuery(xmlURIPtr uri, char **transport, char **vCenter, - int *noVerify, int *autoAnswer) +esxUtil_ParseQuery(esxUtil_ParsedQuery **parsedQuery, xmlURIPtr uri) { int result = -1; - int i; struct qparam_set *queryParamSet = NULL; struct qparam *queryParam = NULL; + int i; + int noVerify; + int autoAnswer; - if (transport != NULL) { - *transport = NULL; - } - - if (vCenter != NULL) { - *vCenter = NULL; - } - - if (noVerify != NULL) { - *noVerify = 0; + if (parsedQuery == NULL || *parsedQuery != NULL) { + ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); + return -1; } - if (autoAnswer != NULL) { - *autoAnswer = 0; + if (VIR_ALLOC(*parsedQuery) < 0) { + virReportOOMError(); + return -1; } #ifdef HAVE_XMLURI_QUERY_RAW @@ -71,75 +67,69 @@ esxUtil_ParseQuery(xmlURIPtr uri, char **transport, char **vCenter, #endif if (queryParamSet == NULL) { - return -1; + goto cleanup; } for (i = 0; i < queryParamSet->n; i++) { queryParam = &queryParamSet->p[i]; if (STRCASEEQ(queryParam->name, "transport")) { - if (transport == NULL) { - continue; - } + VIR_FREE((*parsedQuery)->transport); - *transport = strdup(queryParam->value); + (*parsedQuery)->transport = strdup(queryParam->value); - if (*transport == NULL) { + if ((*parsedQuery)->transport == NULL) { virReportOOMError(); goto cleanup; } - if (STRNEQ(*transport, "http") && STRNEQ(*transport, "https")) { + if (STRNEQ((*parsedQuery)->transport, "http") && + STRNEQ((*parsedQuery)->transport, "https")) { ESX_ERROR(VIR_ERR_INVALID_ARG, _("Query parameter 'transport' has unexpected value " - "'%s' (should be http|https)"), *transport); + "'%s' (should be http|https)"), + (*parsedQuery)->transport); goto cleanup; } } else if (STRCASEEQ(queryParam->name, "vcenter")) { - if (vCenter == NULL) { - continue; - } + VIR_FREE((*parsedQuery)->vCenter); - *vCenter = strdup(queryParam->value); + (*parsedQuery)->vCenter = strdup(queryParam->value); - if (*vCenter == NULL) { + if ((*parsedQuery)->vCenter == NULL) { virReportOOMError(); goto cleanup; } } else if (STRCASEEQ(queryParam->name, "no_verify")) { - if (noVerify == NULL) { - continue; - } - - if (virStrToLong_i(queryParam->value, NULL, 10, noVerify) < 0 || - (*noVerify != 0 && *noVerify != 1)) { + if (virStrToLong_i(queryParam->value, NULL, 10, &noVerify) < 0 || + (noVerify != 0 && noVerify != 1)) { ESX_ERROR(VIR_ERR_INVALID_ARG, _("Query parameter 'no_verify' has unexpected value " "'%s' (should be 0 or 1)"), queryParam->value); goto cleanup; } - } else if (STRCASEEQ(queryParam->name, "auto_answer")) { - if (autoAnswer == NULL) { - continue; - } - if (virStrToLong_i(queryParam->value, NULL, 10, autoAnswer) < 0 || - (*autoAnswer != 0 && *autoAnswer != 1)) { + (*parsedQuery)->noVerify = noVerify != 0; + } else if (STRCASEEQ(queryParam->name, "auto_answer")) { + if (virStrToLong_i(queryParam->value, NULL, 10, &autoAnswer) < 0 || + (autoAnswer != 0 && autoAnswer != 1)) { ESX_ERROR(VIR_ERR_INVALID_ARG, _("Query parameter 'auto_answer' has unexpected " "value '%s' (should be 0 or 1)"), queryParam->value); goto cleanup; } + + (*parsedQuery)->autoAnswer = autoAnswer != 0; } else { VIR_WARN("Ignoring unexpected query parameter '%s'", queryParam->name); } } - if (transport != NULL && *transport == NULL) { - *transport = strdup("https"); + if ((*parsedQuery)->transport == NULL) { + (*parsedQuery)->transport = strdup("https"); - if (*transport == NULL) { + if ((*parsedQuery)->transport == NULL) { virReportOOMError(); goto cleanup; } @@ -149,13 +139,7 @@ esxUtil_ParseQuery(xmlURIPtr uri, char **transport, char **vCenter, cleanup: if (result < 0) { - if (transport != NULL) { - VIR_FREE(*transport); - } - - if (vCenter != NULL) { - VIR_FREE(*vCenter); - } + esxUtil_FreeParsedQuery(parsedQuery); } if (queryParamSet != NULL) { @@ -167,6 +151,22 @@ esxUtil_ParseQuery(xmlURIPtr uri, char **transport, char **vCenter, + +void +esxUtil_FreeParsedQuery(esxUtil_ParsedQuery **parsedQuery) +{ + if (parsedQuery == NULL || *parsedQuery == NULL) { + return; + } + + VIR_FREE((*parsedQuery)->transport); + VIR_FREE((*parsedQuery)->vCenter); + + VIR_FREE(*parsedQuery); +} + + + int esxUtil_ParseVirtualMachineIDString(const char *id_string, int *id) { diff --git a/src/esx/esx_util.h b/src/esx/esx_util.h index 5298184c78..ae6e38bdbd 100644 --- a/src/esx/esx_util.h +++ b/src/esx/esx_util.h @@ -23,13 +23,24 @@ #ifndef __ESX_UTIL_H__ # define __ESX_UTIL_H__ +# include # include # include "internal.h" # include "conf.h" -int esxUtil_ParseQuery(xmlURIPtr uri, char **transport, char **vCenter, - int *noVerify, int *autoAnswer); +typedef struct _esxUtil_ParsedQuery esxUtil_ParsedQuery; + +struct _esxUtil_ParsedQuery { + char *transport; + char *vCenter; + bool noVerify; + bool autoAnswer; +}; + +int esxUtil_ParseQuery(esxUtil_ParsedQuery **parsedQuery, xmlURIPtr uri); + +void esxUtil_FreeParsedQuery(esxUtil_ParsedQuery **parsedQuery); int esxUtil_ParseVirtualMachineIDString(const char *id_string, int *id); diff --git a/src/esx/esx_vi.c b/src/esx/esx_vi.c index c535dbbd06..4332f2b9c0 100644 --- a/src/esx/esx_vi.c +++ b/src/esx/esx_vi.c @@ -277,7 +277,7 @@ esxVI_CURL_Perform(esxVI_Context *ctx, const char *url) int esxVI_Context_Connect(esxVI_Context *ctx, const char *url, const char *ipAddress, const char *username, - const char *password, int noVerify) + const char *password, bool noVerify) { int result = -1; esxVI_String *propertyNameList = NULL; diff --git a/src/esx/esx_vi.h b/src/esx/esx_vi.h index e2687c4c35..521be0cfcc 100644 --- a/src/esx/esx_vi.h +++ b/src/esx/esx_vi.h @@ -161,7 +161,7 @@ int esxVI_Context_Alloc(esxVI_Context **ctx); void esxVI_Context_Free(esxVI_Context **ctx); int esxVI_Context_Connect(esxVI_Context *ctx, const char *ipAddress, const char *url, const char *username, - const char *password, int noVerify); + const char *password, bool noVerify); int esxVI_Context_DownloadFile(esxVI_Context *ctx, const char *url, char **content); int esxVI_Context_UploadFile(esxVI_Context *ctx, const char *url, -- GitLab