esx_vi.h 17.1 KB
Newer Older
1 2 3 4

/*
 * esx_vi.h: client for the VMware VI API 2.5 to manage ESX hosts
 *
5
 * Copyright (C) 2009-2010 Matthias Bolte <matthias.bolte@googlemail.com>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 */

#ifndef __ESX_VI_H__
24
# define __ESX_VI_H__
25

26 27 28
# include <libxml/tree.h>
# include <libxml/xpath.h>
# include <curl/curl.h>
29

30
# include "internal.h"
31
# include "virterror_internal.h"
32 33
# include "datatypes.h"
# include "esx_vi_types.h"
M
Matthias Bolte 已提交
34
# include "esx_util.h"
35

36 37 38 39 40 41 42 43


# define ESX_VI_ERROR(code, ...)                                              \
    virReportErrorHelper(NULL, VIR_FROM_ESX, code, __FILE__, __FUNCTION__,    \
                         __LINE__, __VA_ARGS__)



E
Eric Blake 已提交
44
# define ESX_VI__SOAP__REQUEST_HEADER                                         \
45 46 47 48 49 50 51 52 53 54
    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"                            \
    "<soapenv:Envelope\n"                                                     \
    " xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n"          \
    " xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\"\n"          \
    " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"              \
    " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n"                      \
    "<soapenv:Body>\n"



E
Eric Blake 已提交
55
# define ESX_VI__SOAP__REQUEST_FOOTER                                         \
56 57 58 59 60
    "</soapenv:Body>\n"                                                       \
    "</soapenv:Envelope>"



E
Eric Blake 已提交
61
# define ESV_VI__XML_TAG__OPEN(_buffer, _element, _type)                      \
62 63 64 65 66 67 68 69 70 71
    do {                                                                      \
        virBufferAddLit(_buffer, "<");                                        \
        virBufferAdd(_buffer, _element, -1);                                  \
        virBufferAddLit(_buffer, " xmlns=\"urn:vim25\" xsi:type=\"");         \
        virBufferAdd(_buffer, _type, -1);                                     \
        virBufferAddLit(_buffer, "\">");                                      \
    } while (0)



E
Eric Blake 已提交
72
# define ESV_VI__XML_TAG__CLOSE(_buffer, _element)                            \
73 74 75 76 77 78 79 80
    do {                                                                      \
        virBufferAddLit(_buffer, "</");                                       \
        virBufferAdd(_buffer, _element, -1);                                  \
        virBufferAddLit(_buffer, ">");                                        \
    } while (0)



81 82
typedef enum _esxVI_APIVersion esxVI_APIVersion;
typedef enum _esxVI_ProductVersion esxVI_ProductVersion;
M
Matthias Bolte 已提交
83
typedef enum _esxVI_Occurrence esxVI_Occurrence;
84
typedef struct _esxVI_ParsedHostCpuIdInfo esxVI_ParsedHostCpuIdInfo;
85
typedef struct _esxVI_Context esxVI_Context;
86
typedef struct _esxVI_Response esxVI_Response;
87 88 89 90 91 92
typedef struct _esxVI_Enumeration esxVI_Enumeration;
typedef struct _esxVI_EnumerationValue esxVI_EnumerationValue;
typedef struct _esxVI_List esxVI_List;



93 94 95 96
enum _esxVI_APIVersion {
    esxVI_APIVersion_Undefined = 0,
    esxVI_APIVersion_Unknown,
    esxVI_APIVersion_25,
M
Matthias Bolte 已提交
97 98 99
    esxVI_APIVersion_40,
    esxVI_APIVersion_41,
    esxVI_APIVersion_4x /* > 4.1 */
100 101
};

102 103 104 105
/*
 * AAAABBBB: where AAAA0000 is the product and BBBB the version. this format
 * allows simple bitmask testing for a product independent of the version
 */
106 107
enum _esxVI_ProductVersion {
    esxVI_ProductVersion_Undefined = 0,
108 109 110 111 112 113 114

    esxVI_ProductVersion_GSX   = (1 << 0) << 16,
    esxVI_ProductVersion_GSX20 = esxVI_ProductVersion_GSX | 1,

    esxVI_ProductVersion_ESX   = (1 << 1) << 16,
    esxVI_ProductVersion_ESX35 = esxVI_ProductVersion_ESX | 1,
    esxVI_ProductVersion_ESX40 = esxVI_ProductVersion_ESX | 2,
M
Matthias Bolte 已提交
115 116
    esxVI_ProductVersion_ESX41 = esxVI_ProductVersion_ESX | 3,
    esxVI_ProductVersion_ESX4x = esxVI_ProductVersion_ESX | 4, /* > 4.1 */
117 118 119

    esxVI_ProductVersion_VPX   = (1 << 2) << 16,
    esxVI_ProductVersion_VPX25 = esxVI_ProductVersion_VPX | 1,
M
Matthias Bolte 已提交
120 121 122
    esxVI_ProductVersion_VPX40 = esxVI_ProductVersion_VPX | 2,
    esxVI_ProductVersion_VPX41 = esxVI_ProductVersion_VPX | 3,
    esxVI_ProductVersion_VPX4x = esxVI_ProductVersion_VPX | 4  /* > 4.1 */
123 124
};

M
Matthias Bolte 已提交
125 126 127
enum _esxVI_Occurrence {
    esxVI_Occurrence_Undefined = 0,
    esxVI_Occurrence_RequiredItem,
128
    esxVI_Occurrence_RequiredList,
M
Matthias Bolte 已提交
129
    esxVI_Occurrence_OptionalItem,
130
    esxVI_Occurrence_OptionalList,
131
    esxVI_Occurrence_None
132 133
};

134 135 136 137 138 139 140 141
struct _esxVI_ParsedHostCpuIdInfo {
    int level;
    char eax[32];
    char ebx[32];
    char ecx[32];
    char edx[32];
};

142 143


144 145 146 147 148 149
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Context
 */

struct _esxVI_Context {
    char *url;
M
Matthias Bolte 已提交
150
    char *ipAddress;
151 152
    CURL *curl_handle;
    struct curl_slist *curl_headers;
153
    char curl_error[CURL_ERROR_SIZE];
154 155 156 157
    virMutex curl_lock;
    char *username;
    char *password;
    esxVI_ServiceContent *service;
158 159
    esxVI_APIVersion apiVersion;
    esxVI_ProductVersion productVersion;
160
    esxVI_UserSession *session;
161 162 163
    esxVI_Datacenter *datacenter;
    esxVI_ComputeResource *computeResource;
    esxVI_HostSystem *hostSystem;
164 165 166 167 168 169
    esxVI_SelectionSpec *selectSet_folderToChildEntity;
    esxVI_SelectionSpec *selectSet_hostSystemToParent;
    esxVI_SelectionSpec *selectSet_hostSystemToVm;
    esxVI_SelectionSpec *selectSet_hostSystemToDatastore;
    esxVI_SelectionSpec *selectSet_computeResourceToHost;
    esxVI_SelectionSpec *selectSet_computeResourceToParentToParent;
170 171
};

172
int esxVI_Context_Alloc(esxVI_Context **ctx);
173
void esxVI_Context_Free(esxVI_Context **ctx);
174 175
int esxVI_Context_Connect(esxVI_Context *ctx, const char *ipAddress,
                          const char *url, const char *username,
M
Matthias Bolte 已提交
176
                          const char *password, esxUtil_ParsedUri *parsedUri);
177 178 179 180
int esxVI_Context_LookupObjectsByPath(esxVI_Context *ctx,
                                      esxUtil_ParsedUri *parsedUri);
int esxVI_Context_LookupObjectsByHostSystemIp(esxVI_Context *ctx,
                                              const char *hostSystemIpAddress);
181 182 183 184 185 186 187
int esxVI_Context_DownloadFile(esxVI_Context *ctx, const char *url,
                               char **content);
int esxVI_Context_UploadFile(esxVI_Context *ctx, const char *url,
                             const char *content);
int esxVI_Context_Execute(esxVI_Context *ctx, const char *methodName,
                          const char *request, esxVI_Response **response,
                          esxVI_Occurrence occurrence);
188 189 190 191



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
192
 * Response
193 194
 */

195
struct _esxVI_Response {
196
    int responseCode;                                 /* required */
197
    char *content;                                    /* required */
198
    xmlDocPtr document;                               /* optional */
199
    xmlNodePtr node;                                  /* optional, list */
200 201
};

202
int esxVI_Response_Alloc(esxVI_Response **response);
203
void esxVI_Response_Free(esxVI_Response **response);
204 205 206 207 208 209 210 211 212 213 214 215 216



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Enumeration
 */

struct _esxVI_EnumerationValue {
    const char *name;
    int value;
};

struct _esxVI_Enumeration {
217
    esxVI_Type type;
218 219 220
    esxVI_EnumerationValue values[10];
};

221
int esxVI_Enumeration_CastFromAnyType(const esxVI_Enumeration *enumeration,
222
                                      esxVI_AnyType *anyType, int *value);
223
int esxVI_Enumeration_Serialize(const esxVI_Enumeration *enumeration,
224
                                int value, const char *element,
225
                                virBufferPtr output);
226
int esxVI_Enumeration_Deserialize(const esxVI_Enumeration *enumeration,
227 228 229 230 231 232 233 234 235 236 237 238 239
                                  xmlNodePtr node, int *value);



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * List
 */

struct _esxVI_List {
    esxVI_List *_next;
};

typedef int (*esxVI_List_FreeFunc) (esxVI_List **item);
240 241
typedef int (*esxVI_List_DeepCopyFunc) (esxVI_List **dest, esxVI_List *src);
typedef int (*esxVI_List_CastFromAnyTypeFunc) (esxVI_AnyType *anyType,
242
                                               esxVI_List **item);
243
typedef int (*esxVI_List_SerializeFunc) (esxVI_List *item, const char *element,
244
                                         virBufferPtr output);
245
typedef int (*esxVI_List_DeserializeFunc) (xmlNodePtr node, esxVI_List **item);
246

247 248
int esxVI_List_Append(esxVI_List **list, esxVI_List *item);
int esxVI_List_DeepCopy(esxVI_List **destList, esxVI_List *srcList,
249 250
                        esxVI_List_DeepCopyFunc deepCopyFunc,
                        esxVI_List_FreeFunc freeFunc);
251
int esxVI_List_CastFromAnyType(esxVI_AnyType *anyType, esxVI_List **list,
252 253
                               esxVI_List_CastFromAnyTypeFunc castFromAnyTypeFunc,
                               esxVI_List_FreeFunc freeFunc);
254
int esxVI_List_Serialize(esxVI_List *list, const char *element,
255
                         virBufferPtr output,
256
                         esxVI_List_SerializeFunc serializeFunc);
257
int esxVI_List_Deserialize(xmlNodePtr node, esxVI_List **list,
258 259 260 261 262 263 264
                           esxVI_List_DeserializeFunc deserializeFunc,
                           esxVI_List_FreeFunc freeFunc);



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Utility and Convenience Functions
265 266 267 268
 *
 * Function naming scheme:
 *  - 'lookup' functions query the ESX or vCenter for information
 *  - 'get' functions get information from a local object
269 270
 */

271
int esxVI_Alloc(void **ptrptr, size_t size);
272

273 274
int esxVI_BuildSelectSet
      (esxVI_SelectionSpec **selectSet, const char *name,
275
       const char *type, const char *path, const char *selectSetNames);
276

277
int esxVI_BuildSelectSetCollection(esxVI_Context *ctx);
278

279
int esxVI_EnsureSession(esxVI_Context *ctx);
280

281
int esxVI_LookupObjectContentByType(esxVI_Context *ctx,
282 283 284 285
                                    esxVI_ManagedObjectReference *root,
                                    const char *type,
                                    esxVI_String *propertyNameList,
                                    esxVI_ObjectContent **objectContentList);
286

287
int esxVI_GetManagedEntityStatus
288
      (esxVI_ObjectContent *objectContent, const char *propertyName,
289 290
       esxVI_ManagedEntityStatus *managedEntityStatus);

291
int esxVI_GetVirtualMachinePowerState
292
      (esxVI_ObjectContent *virtualMachine,
293 294
       esxVI_VirtualMachinePowerState *powerState);

295
int esxVI_GetVirtualMachineQuestionInfo
296
      (esxVI_ObjectContent *virtualMachine,
297 298
       esxVI_VirtualMachineQuestionInfo **questionInfo);

299 300 301 302 303 304 305 306 307 308 309 310 311
int esxVI_GetBoolean(esxVI_ObjectContent *objectContent,
                     const char *propertyName,
                     esxVI_Boolean *value, esxVI_Occurrence occurence);

int esxVI_GetStringValue(esxVI_ObjectContent *objectContent,
                         const char *propertyName,
                         char **value, esxVI_Occurrence occurence);

int esxVI_GetManagedObjectReference(esxVI_ObjectContent *objectContent,
                                    const char *propertyName,
                                    esxVI_ManagedObjectReference **value,
                                    esxVI_Occurrence occurence);

312
int esxVI_LookupNumberOfDomainsByPowerState
313 314
      (esxVI_Context *ctx, esxVI_VirtualMachinePowerState powerState,
       esxVI_Boolean inverse);
315

316
int esxVI_GetVirtualMachineIdentity(esxVI_ObjectContent *virtualMachine,
317 318
                                    int *id, char **name, unsigned char *uuid);

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
int esxVI_GetNumberOfSnapshotTrees
      (esxVI_VirtualMachineSnapshotTree *snapshotTreeList);

int esxVI_GetSnapshotTreeNames
      (esxVI_VirtualMachineSnapshotTree *snapshotTreeList, char **names,
       int nameslen);

int esxVI_GetSnapshotTreeByName
      (esxVI_VirtualMachineSnapshotTree *snapshotTreeList, const char *name,
       esxVI_VirtualMachineSnapshotTree **snapshotTree,
       esxVI_VirtualMachineSnapshotTree **snapshotTreeParent,
       esxVI_Occurrence occurrence);

int esxVI_GetSnapshotTreeBySnapshot
      (esxVI_VirtualMachineSnapshotTree *snapshotTreeList,
       esxVI_ManagedObjectReference *snapshot,
       esxVI_VirtualMachineSnapshotTree **snapshotTree);

337 338 339
int esxVI_LookupHostSystemProperties(esxVI_Context *ctx,
                                     esxVI_String *propertyNameList,
                                     esxVI_ObjectContent **hostSystem);
M
Matthias Bolte 已提交
340

341 342 343
int esxVI_LookupVirtualMachineList(esxVI_Context *ctx,
                                   esxVI_String *propertyNameList,
                                   esxVI_ObjectContent **virtualMachineList);
344

345
int esxVI_LookupVirtualMachineByUuid(esxVI_Context *ctx,
346 347
                                     const unsigned char *uuid,
                                     esxVI_String *propertyNameList,
348
                                     esxVI_ObjectContent **virtualMachine,
M
Matthias Bolte 已提交
349
                                     esxVI_Occurrence occurrence);
350

351 352 353 354 355
int esxVI_LookupVirtualMachineByName(esxVI_Context *ctx, const char *name,
                                     esxVI_String *propertyNameList,
                                     esxVI_ObjectContent **virtualMachine,
                                     esxVI_Occurrence occurrence);

356
int esxVI_LookupVirtualMachineByUuidAndPrepareForTask
357
      (esxVI_Context *ctx, const unsigned char *uuid,
358 359 360
       esxVI_String *propertyNameList, esxVI_ObjectContent **virtualMachine,
       esxVI_Boolean autoAnswer);

361 362 363
int esxVI_LookupDatastoreList(esxVI_Context *ctx, esxVI_String *propertyNameList,
                              esxVI_ObjectContent **datastoreList);

364
int esxVI_LookupDatastoreByName(esxVI_Context *ctx, const char *name,
M
Matthias Bolte 已提交
365 366
                                esxVI_String *propertyNameList,
                                esxVI_ObjectContent **datastore,
M
Matthias Bolte 已提交
367
                                esxVI_Occurrence occurrence);
M
Matthias Bolte 已提交
368

369 370 371 372 373 374
int esxVI_LookupDatastoreByAbsolutePath(esxVI_Context *ctx,
                                        const char *absolutePath,
                                        esxVI_String *propertyNameList,
                                        esxVI_ObjectContent **datastore,
                                        esxVI_Occurrence occurrence);

375 376 377 378
int esxVI_LookupDatastoreHostMount(esxVI_Context *ctx,
                                   esxVI_ManagedObjectReference *datastore,
                                   esxVI_DatastoreHostMount **hostMount);

379
int esxVI_LookupTaskInfoByTask(esxVI_Context *ctx,
380 381 382 383
                               esxVI_ManagedObjectReference *task,
                               esxVI_TaskInfo **taskInfo);

int esxVI_LookupPendingTaskInfoListByVirtualMachine
384
      (esxVI_Context *ctx, esxVI_ObjectContent *virtualMachine,
385 386
       esxVI_TaskInfo **pendingTaskInfoList);

387
int esxVI_LookupAndHandleVirtualMachineQuestion(esxVI_Context *ctx,
388
                                                const unsigned char *uuid,
389 390 391
                                                esxVI_Occurrence occurrence,
                                                esxVI_Boolean autoAnswer,
                                                esxVI_Boolean *blocked);
392

393 394 395 396 397 398 399 400 401
int esxVI_LookupRootSnapshotTreeList
      (esxVI_Context *ctx, const unsigned char *virtualMachineUuid,
       esxVI_VirtualMachineSnapshotTree **rootSnapshotTreeList);

int esxVI_LookupCurrentSnapshotTree
      (esxVI_Context *ctx, const unsigned char *virtualMachineUuid,
       esxVI_VirtualMachineSnapshotTree **currentSnapshotTree,
       esxVI_Occurrence occurrence);

402 403
int esxVI_LookupFileInfoByDatastorePath(esxVI_Context *ctx,
                                        const char *datastorePath,
404
                                        bool lookupFolder,
405 406 407 408 409 410 411
                                        esxVI_FileInfo **fileInfo,
                                        esxVI_Occurrence occurrence);

int esxVI_LookupDatastoreContentByDatastoreName
      (esxVI_Context *ctx, const char *datastoreName,
       esxVI_HostDatastoreBrowserSearchResults **searchResultsList);

412 413 414 415
int esxVI_LookupStorageVolumeKeyByDatastorePath(esxVI_Context *ctx,
                                                const char *datastorePath,
                                                char **key);

416
int esxVI_HandleVirtualMachineQuestion
417
      (esxVI_Context *ctx,
418 419
       esxVI_ManagedObjectReference *virtualMachine,
       esxVI_VirtualMachineQuestionInfo *questionInfo,
420
       esxVI_Boolean autoAnswer, esxVI_Boolean *blocked);
421

422
int esxVI_WaitForTaskCompletion(esxVI_Context *ctx,
423
                                esxVI_ManagedObjectReference *task,
424
                                const unsigned char *virtualMachineUuid,
425
                                esxVI_Occurrence virtualMachineOccurrence,
426
                                esxVI_Boolean autoAnswer,
427 428
                                esxVI_TaskInfoState *finalState);

429 430 431
int esxVI_ParseHostCpuIdInfo(esxVI_ParsedHostCpuIdInfo *parsedHostCpuIdInfo,
                             esxVI_HostCpuIdInfo *hostCpuIdInfo);

432
#endif /* __ESX_VI_H__ */