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.
This commit is contained in:
Matthias Bolte 2010-06-03 20:04:15 +02:00
parent cd2b18968d
commit 070f61002f
5 changed files with 87 additions and 78 deletions

View File

@ -314,12 +314,11 @@ esxOpen(virConnectPtr conn, virConnectAuthPtr auth, int flags ATTRIBUTE_UNUSED)
{ {
virDrvOpenStatus result = VIR_DRV_OPEN_ERROR; virDrvOpenStatus result = VIR_DRV_OPEN_ERROR;
esxPrivate *priv = NULL; esxPrivate *priv = NULL;
esxUtil_ParsedQuery *parsedQuery = NULL;
char hostIpAddress[NI_MAXHOST] = ""; char hostIpAddress[NI_MAXHOST] = "";
char vCenterIpAddress[NI_MAXHOST] = ""; char vCenterIpAddress[NI_MAXHOST] = "";
char *url = NULL; char *url = NULL;
char *vCenter = NULL; char *vCenter = NULL;
int noVerify = 0; // boolean
int autoAnswer = 0; // boolean
char *username = NULL; char *username = NULL;
char *password = NULL; char *password = NULL;
esxVI_String *propertyNameList = NULL; esxVI_String *propertyNameList = NULL;
@ -349,20 +348,19 @@ esxOpen(virConnectPtr conn, virConnectAuthPtr auth, int flags ATTRIBUTE_UNUSED)
goto cleanup; goto cleanup;
} }
priv->maxVcpus = -1; if (esxUtil_ParseQuery(&parsedQuery, conn->uri) < 0) {
priv->supportsVMotion = esxVI_Boolean_Undefined;
priv->supportsLongMode = esxVI_Boolean_Undefined;
priv->autoAnswer = esxVI_Boolean_False;
priv->usedCpuTimeCounterId = -1;
if (esxUtil_ParseQuery(conn->uri, &priv->transport, &vCenter, &noVerify,
&autoAnswer) < 0) {
goto cleanup; goto cleanup;
} }
if (autoAnswer) { priv->transport = parsedQuery->transport;
priv->autoAnswer = esxVI_Boolean_True; parsedQuery->transport = NULL;
}
priv->maxVcpus = -1;
priv->supportsVMotion = esxVI_Boolean_Undefined;
priv->supportsLongMode = esxVI_Boolean_Undefined;
priv->autoAnswer = parsedQuery->autoAnswer ? esxVI_Boolean_True
: esxVI_Boolean_False;
priv->usedCpuTimeCounterId = -1;
/* /*
* Set the port dependent on the transport protocol if no port is * Set the port dependent on the transport protocol if no port is
@ -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); password = virRequestPassword(auth, username, conn->uri->server);
if (password == NULL) { if (password == NULL) {
@ -425,8 +419,9 @@ esxOpen(virConnectPtr conn, virConnectAuthPtr auth, int flags ATTRIBUTE_UNUSED)
goto cleanup; goto cleanup;
} }
if (esxVI_Context_Connect(priv->host, url, hostIpAddress, username, if (esxVI_Context_Alloc(&priv->host) < 0 ||
password, noVerify) < 0) { esxVI_Context_Connect(priv->host, url, hostIpAddress, username,
password, parsedQuery->noVerify) < 0) {
goto cleanup; goto cleanup;
} }
@ -559,7 +554,8 @@ esxOpen(virConnectPtr conn, virConnectAuthPtr auth, int flags ATTRIBUTE_UNUSED)
} }
if (esxVI_Context_Connect(priv->vCenter, url, vCenterIpAddress, if (esxVI_Context_Connect(priv->vCenter, url, vCenterIpAddress,
username, password, noVerify) < 0) { username, password,
parsedQuery->noVerify) < 0) {
goto cleanup; goto cleanup;
} }
@ -594,6 +590,8 @@ esxOpen(virConnectPtr conn, virConnectAuthPtr auth, int flags ATTRIBUTE_UNUSED)
VIR_FREE(priv); VIR_FREE(priv);
} }
esxUtil_FreeParsedQuery(&parsedQuery);
VIR_FREE(url); VIR_FREE(url);
VIR_FREE(vCenter); VIR_FREE(vCenter);
VIR_FREE(password); VIR_FREE(password);
@ -2961,14 +2959,14 @@ esxDomainMigratePrepare(virConnectPtr dconn,
unsigned long resource ATTRIBUTE_UNUSED) unsigned long resource ATTRIBUTE_UNUSED)
{ {
int result = -1; int result = -1;
char *transport = NULL; esxUtil_ParsedQuery *parsedQuery = NULL;
if (uri_in == NULL) { if (uri_in == NULL) {
if (esxUtil_ParseQuery(dconn->uri, &transport, NULL, NULL, NULL) < 0) { if (esxUtil_ParseQuery(&parsedQuery, dconn->uri) < 0) {
return -1; 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) { dconn->uri->server, dconn->uri->port) < 0) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
@ -2978,7 +2976,7 @@ esxDomainMigratePrepare(virConnectPtr dconn,
result = 0; result = 0;
cleanup: cleanup:
VIR_FREE(transport); esxUtil_FreeParsedQuery(&parsedQuery);
return result; return result;
} }

View File

@ -39,29 +39,25 @@
#define VIR_FROM_THIS VIR_FROM_ESX #define VIR_FROM_THIS VIR_FROM_ESX
int int
esxUtil_ParseQuery(xmlURIPtr uri, char **transport, char **vCenter, esxUtil_ParseQuery(esxUtil_ParsedQuery **parsedQuery, xmlURIPtr uri)
int *noVerify, int *autoAnswer)
{ {
int result = -1; int result = -1;
int i;
struct qparam_set *queryParamSet = NULL; struct qparam_set *queryParamSet = NULL;
struct qparam *queryParam = NULL; struct qparam *queryParam = NULL;
int i;
int noVerify;
int autoAnswer;
if (transport != NULL) { if (parsedQuery == NULL || *parsedQuery != NULL) {
*transport = NULL; ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
return -1;
} }
if (vCenter != NULL) { if (VIR_ALLOC(*parsedQuery) < 0) {
*vCenter = NULL; virReportOOMError();
} return -1;
if (noVerify != NULL) {
*noVerify = 0;
}
if (autoAnswer != NULL) {
*autoAnswer = 0;
} }
#ifdef HAVE_XMLURI_QUERY_RAW #ifdef HAVE_XMLURI_QUERY_RAW
@ -71,75 +67,69 @@ esxUtil_ParseQuery(xmlURIPtr uri, char **transport, char **vCenter,
#endif #endif
if (queryParamSet == NULL) { if (queryParamSet == NULL) {
return -1; goto cleanup;
} }
for (i = 0; i < queryParamSet->n; i++) { for (i = 0; i < queryParamSet->n; i++) {
queryParam = &queryParamSet->p[i]; queryParam = &queryParamSet->p[i];
if (STRCASEEQ(queryParam->name, "transport")) { if (STRCASEEQ(queryParam->name, "transport")) {
if (transport == NULL) { VIR_FREE((*parsedQuery)->transport);
continue;
}
*transport = strdup(queryParam->value); (*parsedQuery)->transport = strdup(queryParam->value);
if (*transport == NULL) { if ((*parsedQuery)->transport == NULL) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }
if (STRNEQ(*transport, "http") && STRNEQ(*transport, "https")) { if (STRNEQ((*parsedQuery)->transport, "http") &&
STRNEQ((*parsedQuery)->transport, "https")) {
ESX_ERROR(VIR_ERR_INVALID_ARG, ESX_ERROR(VIR_ERR_INVALID_ARG,
_("Query parameter 'transport' has unexpected value " _("Query parameter 'transport' has unexpected value "
"'%s' (should be http|https)"), *transport); "'%s' (should be http|https)"),
(*parsedQuery)->transport);
goto cleanup; goto cleanup;
} }
} else if (STRCASEEQ(queryParam->name, "vcenter")) { } else if (STRCASEEQ(queryParam->name, "vcenter")) {
if (vCenter == NULL) { VIR_FREE((*parsedQuery)->vCenter);
continue;
}
*vCenter = strdup(queryParam->value); (*parsedQuery)->vCenter = strdup(queryParam->value);
if (*vCenter == NULL) { if ((*parsedQuery)->vCenter == NULL) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }
} else if (STRCASEEQ(queryParam->name, "no_verify")) { } else if (STRCASEEQ(queryParam->name, "no_verify")) {
if (noVerify == NULL) { if (virStrToLong_i(queryParam->value, NULL, 10, &noVerify) < 0 ||
continue; (noVerify != 0 && noVerify != 1)) {
}
if (virStrToLong_i(queryParam->value, NULL, 10, noVerify) < 0 ||
(*noVerify != 0 && *noVerify != 1)) {
ESX_ERROR(VIR_ERR_INVALID_ARG, ESX_ERROR(VIR_ERR_INVALID_ARG,
_("Query parameter 'no_verify' has unexpected value " _("Query parameter 'no_verify' has unexpected value "
"'%s' (should be 0 or 1)"), queryParam->value); "'%s' (should be 0 or 1)"), queryParam->value);
goto cleanup; goto cleanup;
} }
} else if (STRCASEEQ(queryParam->name, "auto_answer")) {
if (autoAnswer == NULL) {
continue;
}
if (virStrToLong_i(queryParam->value, NULL, 10, autoAnswer) < 0 || (*parsedQuery)->noVerify = noVerify != 0;
(*autoAnswer != 0 && *autoAnswer != 1)) { } 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, ESX_ERROR(VIR_ERR_INVALID_ARG,
_("Query parameter 'auto_answer' has unexpected " _("Query parameter 'auto_answer' has unexpected "
"value '%s' (should be 0 or 1)"), queryParam->value); "value '%s' (should be 0 or 1)"), queryParam->value);
goto cleanup; goto cleanup;
} }
(*parsedQuery)->autoAnswer = autoAnswer != 0;
} else { } else {
VIR_WARN("Ignoring unexpected query parameter '%s'", VIR_WARN("Ignoring unexpected query parameter '%s'",
queryParam->name); queryParam->name);
} }
} }
if (transport != NULL && *transport == NULL) { if ((*parsedQuery)->transport == NULL) {
*transport = strdup("https"); (*parsedQuery)->transport = strdup("https");
if (*transport == NULL) { if ((*parsedQuery)->transport == NULL) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }
@ -149,13 +139,7 @@ esxUtil_ParseQuery(xmlURIPtr uri, char **transport, char **vCenter,
cleanup: cleanup:
if (result < 0) { if (result < 0) {
if (transport != NULL) { esxUtil_FreeParsedQuery(parsedQuery);
VIR_FREE(*transport);
}
if (vCenter != NULL) {
VIR_FREE(*vCenter);
}
} }
if (queryParamSet != NULL) { 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 int
esxUtil_ParseVirtualMachineIDString(const char *id_string, int *id) esxUtil_ParseVirtualMachineIDString(const char *id_string, int *id)
{ {

View File

@ -23,13 +23,24 @@
#ifndef __ESX_UTIL_H__ #ifndef __ESX_UTIL_H__
# define __ESX_UTIL_H__ # define __ESX_UTIL_H__
# include <stdbool.h>
# include <libxml/uri.h> # include <libxml/uri.h>
# include "internal.h" # include "internal.h"
# include "conf.h" # include "conf.h"
int esxUtil_ParseQuery(xmlURIPtr uri, char **transport, char **vCenter, typedef struct _esxUtil_ParsedQuery esxUtil_ParsedQuery;
int *noVerify, int *autoAnswer);
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); int esxUtil_ParseVirtualMachineIDString(const char *id_string, int *id);

View File

@ -277,7 +277,7 @@ esxVI_CURL_Perform(esxVI_Context *ctx, const char *url)
int int
esxVI_Context_Connect(esxVI_Context *ctx, const char *url, esxVI_Context_Connect(esxVI_Context *ctx, const char *url,
const char *ipAddress, const char *username, const char *ipAddress, const char *username,
const char *password, int noVerify) const char *password, bool noVerify)
{ {
int result = -1; int result = -1;
esxVI_String *propertyNameList = NULL; esxVI_String *propertyNameList = NULL;

View File

@ -161,7 +161,7 @@ int esxVI_Context_Alloc(esxVI_Context **ctx);
void esxVI_Context_Free(esxVI_Context **ctx); void esxVI_Context_Free(esxVI_Context **ctx);
int esxVI_Context_Connect(esxVI_Context *ctx, const char *ipAddress, int esxVI_Context_Connect(esxVI_Context *ctx, const char *ipAddress,
const char *url, const char *username, 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, int esxVI_Context_DownloadFile(esxVI_Context *ctx, const char *url,
char **content); char **content);
int esxVI_Context_UploadFile(esxVI_Context *ctx, const char *url, int esxVI_Context_UploadFile(esxVI_Context *ctx, const char *url,