esx_vi.h 18.7 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


# define ESX_VI_ERROR(code, ...)                                              \
39
    virReportErrorHelper(VIR_FROM_ESX, code, __FILE__, __FUNCTION__,          \
40 41 42 43
                         __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_CURL esxVI_CURL;
86
typedef struct _esxVI_SharedCURL esxVI_SharedCURL;
87
typedef struct _esxVI_Context esxVI_Context;
88
typedef struct _esxVI_Response esxVI_Response;
89 90 91 92 93 94
typedef struct _esxVI_Enumeration esxVI_Enumeration;
typedef struct _esxVI_EnumerationValue esxVI_EnumerationValue;
typedef struct _esxVI_List esxVI_List;



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

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

    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 已提交
117 118
    esxVI_ProductVersion_ESX41 = esxVI_ProductVersion_ESX | 3,
    esxVI_ProductVersion_ESX4x = esxVI_ProductVersion_ESX | 4, /* > 4.1 */
119 120 121

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

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

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

144 145


146 147 148 149 150 151 152 153 154
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * CURL
 */

struct _esxVI_CURL {
    CURL *handle;
    virMutex lock;
    struct curl_slist *headers;
    char error[CURL_ERROR_SIZE];
155
    esxVI_SharedCURL *shared;
156 157 158 159 160 161 162 163 164 165
};

int esxVI_CURL_Alloc(esxVI_CURL **curl);
void esxVI_CURL_Free(esxVI_CURL **curl);
int esxVI_CURL_Connect(esxVI_CURL *curl, esxUtil_ParsedUri *parsedUri);
int esxVI_CURL_Download(esxVI_CURL *curl, const char *url, char **content);
int esxVI_CURL_Upload(esxVI_CURL *curl, const char *url, const char *content);



166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * SharedCURL
 */

struct _esxVI_SharedCURL {
    CURLSH *handle;
    virMutex locks[3]; /* share, cookie, dns */
    size_t count;
};

int esxVI_SharedCURL_Alloc(esxVI_SharedCURL **shared);
void esxVI_SharedCURL_Free(esxVI_SharedCURL **shared);
int esxVI_SharedCURL_Add(esxVI_SharedCURL *shared, esxVI_CURL *curl);
int esxVI_SharedCURL_Remove(esxVI_SharedCURL *shared, esxVI_CURL *curl);



183 184 185 186 187
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Context
 */

struct _esxVI_Context {
188
    /* All members are used read-only after esxVI_Context_Connect ... */
189
    esxVI_CURL *curl;
190
    char *url;
M
Matthias Bolte 已提交
191
    char *ipAddress;
192 193 194
    char *username;
    char *password;
    esxVI_ServiceContent *service;
195 196
    esxVI_APIVersion apiVersion;
    esxVI_ProductVersion productVersion;
197 198
    esxVI_UserSession *session; /* ... except the session ... */
    virMutexPtr sessionLock; /* ... that is protected by this mutex */
199 200 201
    esxVI_Datacenter *datacenter;
    esxVI_ComputeResource *computeResource;
    esxVI_HostSystem *hostSystem;
202 203 204 205 206 207
    esxVI_SelectionSpec *selectSet_folderToChildEntity;
    esxVI_SelectionSpec *selectSet_hostSystemToParent;
    esxVI_SelectionSpec *selectSet_hostSystemToVm;
    esxVI_SelectionSpec *selectSet_hostSystemToDatastore;
    esxVI_SelectionSpec *selectSet_computeResourceToHost;
    esxVI_SelectionSpec *selectSet_computeResourceToParentToParent;
208
    bool hasQueryVirtualDiskUuid;
209
    bool hasSessionIsActive;
210 211
};

212
int esxVI_Context_Alloc(esxVI_Context **ctx);
213
void esxVI_Context_Free(esxVI_Context **ctx);
214 215
int esxVI_Context_Connect(esxVI_Context *ctx, const char *ipAddress,
                          const char *url, const char *username,
M
Matthias Bolte 已提交
216
                          const char *password, esxUtil_ParsedUri *parsedUri);
217 218 219 220
int esxVI_Context_LookupObjectsByPath(esxVI_Context *ctx,
                                      esxUtil_ParsedUri *parsedUri);
int esxVI_Context_LookupObjectsByHostSystemIp(esxVI_Context *ctx,
                                              const char *hostSystemIpAddress);
221 222 223
int esxVI_Context_Execute(esxVI_Context *ctx, const char *methodName,
                          const char *request, esxVI_Response **response,
                          esxVI_Occurrence occurrence);
224 225 226 227



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
228
 * Response
229 230
 */

231
struct _esxVI_Response {
232
    int responseCode;                                 /* required */
233
    char *content;                                    /* required */
234
    xmlDocPtr document;                               /* optional */
235
    xmlNodePtr node;                                  /* optional, list */
236 237
};

238
int esxVI_Response_Alloc(esxVI_Response **response);
239
void esxVI_Response_Free(esxVI_Response **response);
240 241 242 243 244 245 246 247 248 249 250 251 252



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

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

struct _esxVI_Enumeration {
253
    esxVI_Type type;
254 255 256
    esxVI_EnumerationValue values[10];
};

257
int esxVI_Enumeration_CastFromAnyType(const esxVI_Enumeration *enumeration,
258
                                      esxVI_AnyType *anyType, int *value);
259
int esxVI_Enumeration_Serialize(const esxVI_Enumeration *enumeration,
260
                                int value, const char *element,
261
                                virBufferPtr output);
262
int esxVI_Enumeration_Deserialize(const esxVI_Enumeration *enumeration,
263 264 265 266 267 268 269 270 271 272 273 274 275
                                  xmlNodePtr node, int *value);



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

struct _esxVI_List {
    esxVI_List *_next;
};

typedef int (*esxVI_List_FreeFunc) (esxVI_List **item);
276 277
typedef int (*esxVI_List_DeepCopyFunc) (esxVI_List **dest, esxVI_List *src);
typedef int (*esxVI_List_CastFromAnyTypeFunc) (esxVI_AnyType *anyType,
278
                                               esxVI_List **item);
279
typedef int (*esxVI_List_SerializeFunc) (esxVI_List *item, const char *element,
280
                                         virBufferPtr output);
281
typedef int (*esxVI_List_DeserializeFunc) (xmlNodePtr node, esxVI_List **item);
282

283 284
int esxVI_List_Append(esxVI_List **list, esxVI_List *item);
int esxVI_List_DeepCopy(esxVI_List **destList, esxVI_List *srcList,
285 286
                        esxVI_List_DeepCopyFunc deepCopyFunc,
                        esxVI_List_FreeFunc freeFunc);
287
int esxVI_List_CastFromAnyType(esxVI_AnyType *anyType, esxVI_List **list,
288 289
                               esxVI_List_CastFromAnyTypeFunc castFromAnyTypeFunc,
                               esxVI_List_FreeFunc freeFunc);
290
int esxVI_List_Serialize(esxVI_List *list, const char *element,
291
                         virBufferPtr output,
292
                         esxVI_List_SerializeFunc serializeFunc);
293
int esxVI_List_Deserialize(xmlNodePtr node, esxVI_List **list,
294 295 296 297 298 299 300
                           esxVI_List_DeserializeFunc deserializeFunc,
                           esxVI_List_FreeFunc freeFunc);



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Utility and Convenience Functions
301 302 303 304
 *
 * Function naming scheme:
 *  - 'lookup' functions query the ESX or vCenter for information
 *  - 'get' functions get information from a local object
305 306
 */

307
int esxVI_Alloc(void **ptrptr, size_t size);
308

309 310
int esxVI_BuildSelectSet
      (esxVI_SelectionSpec **selectSet, const char *name,
311
       const char *type, const char *path, const char *selectSetNames);
312

313
int esxVI_BuildSelectSetCollection(esxVI_Context *ctx);
314

315
int esxVI_EnsureSession(esxVI_Context *ctx);
316

317
int esxVI_LookupObjectContentByType(esxVI_Context *ctx,
318 319 320
                                    esxVI_ManagedObjectReference *root,
                                    const char *type,
                                    esxVI_String *propertyNameList,
321 322
                                    esxVI_ObjectContent **objectContentList,
                                    esxVI_Occurrence occurrence);
323

324
int esxVI_GetManagedEntityStatus
325
      (esxVI_ObjectContent *objectContent, const char *propertyName,
326 327
       esxVI_ManagedEntityStatus *managedEntityStatus);

328
int esxVI_GetVirtualMachinePowerState
329
      (esxVI_ObjectContent *virtualMachine,
330 331
       esxVI_VirtualMachinePowerState *powerState);

332
int esxVI_GetVirtualMachineQuestionInfo
333
      (esxVI_ObjectContent *virtualMachine,
334 335
       esxVI_VirtualMachineQuestionInfo **questionInfo);

336 337
int esxVI_GetBoolean(esxVI_ObjectContent *objectContent,
                     const char *propertyName,
M
Matthias Bolte 已提交
338
                     esxVI_Boolean *value, esxVI_Occurrence occurrence);
339

340
int esxVI_GetLong(esxVI_ObjectContent *objectContent, const char *propertyName,
M
Matthias Bolte 已提交
341
                  esxVI_Long **value, esxVI_Occurrence occurrence);
342

343 344
int esxVI_GetStringValue(esxVI_ObjectContent *objectContent,
                         const char *propertyName,
M
Matthias Bolte 已提交
345
                         char **value, esxVI_Occurrence occurrence);
346 347 348 349

int esxVI_GetManagedObjectReference(esxVI_ObjectContent *objectContent,
                                    const char *propertyName,
                                    esxVI_ManagedObjectReference **value,
M
Matthias Bolte 已提交
350
                                    esxVI_Occurrence occurrence);
351

352
int esxVI_LookupNumberOfDomainsByPowerState
353
      (esxVI_Context *ctx, esxVI_VirtualMachinePowerState powerState,
354
       bool inverse);
355

356
int esxVI_GetVirtualMachineIdentity(esxVI_ObjectContent *virtualMachine,
357 358
                                    int *id, char **name, unsigned char *uuid);

359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
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);

377 378 379
int esxVI_LookupHostSystemProperties(esxVI_Context *ctx,
                                     esxVI_String *propertyNameList,
                                     esxVI_ObjectContent **hostSystem);
M
Matthias Bolte 已提交
380

381 382 383
int esxVI_LookupVirtualMachineList(esxVI_Context *ctx,
                                   esxVI_String *propertyNameList,
                                   esxVI_ObjectContent **virtualMachineList);
384

385
int esxVI_LookupVirtualMachineByUuid(esxVI_Context *ctx,
386 387
                                     const unsigned char *uuid,
                                     esxVI_String *propertyNameList,
388
                                     esxVI_ObjectContent **virtualMachine,
M
Matthias Bolte 已提交
389
                                     esxVI_Occurrence occurrence);
390

391 392 393 394 395
int esxVI_LookupVirtualMachineByName(esxVI_Context *ctx, const char *name,
                                     esxVI_String *propertyNameList,
                                     esxVI_ObjectContent **virtualMachine,
                                     esxVI_Occurrence occurrence);

396
int esxVI_LookupVirtualMachineByUuidAndPrepareForTask
397
      (esxVI_Context *ctx, const unsigned char *uuid,
398
       esxVI_String *propertyNameList, esxVI_ObjectContent **virtualMachine,
399
       bool autoAnswer);
400

401 402 403
int esxVI_LookupDatastoreList(esxVI_Context *ctx, esxVI_String *propertyNameList,
                              esxVI_ObjectContent **datastoreList);

404
int esxVI_LookupDatastoreByName(esxVI_Context *ctx, const char *name,
M
Matthias Bolte 已提交
405 406
                                esxVI_String *propertyNameList,
                                esxVI_ObjectContent **datastore,
M
Matthias Bolte 已提交
407
                                esxVI_Occurrence occurrence);
M
Matthias Bolte 已提交
408

409 410 411 412 413 414
int esxVI_LookupDatastoreByAbsolutePath(esxVI_Context *ctx,
                                        const char *absolutePath,
                                        esxVI_String *propertyNameList,
                                        esxVI_ObjectContent **datastore,
                                        esxVI_Occurrence occurrence);

415 416 417 418
int esxVI_LookupDatastoreHostMount(esxVI_Context *ctx,
                                   esxVI_ManagedObjectReference *datastore,
                                   esxVI_DatastoreHostMount **hostMount);

419
int esxVI_LookupTaskInfoByTask(esxVI_Context *ctx,
420 421 422 423
                               esxVI_ManagedObjectReference *task,
                               esxVI_TaskInfo **taskInfo);

int esxVI_LookupPendingTaskInfoListByVirtualMachine
424
      (esxVI_Context *ctx, esxVI_ObjectContent *virtualMachine,
425 426
       esxVI_TaskInfo **pendingTaskInfoList);

427
int esxVI_LookupAndHandleVirtualMachineQuestion(esxVI_Context *ctx,
428
                                                const unsigned char *uuid,
429
                                                esxVI_Occurrence occurrence,
430
                                                bool autoAnswer, bool *blocked);
431

432 433 434 435 436 437 438 439 440
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);

441 442
int esxVI_LookupFileInfoByDatastorePath(esxVI_Context *ctx,
                                        const char *datastorePath,
443
                                        bool lookupFolder,
444 445 446 447 448 449 450
                                        esxVI_FileInfo **fileInfo,
                                        esxVI_Occurrence occurrence);

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

451 452 453 454
int esxVI_LookupStorageVolumeKeyByDatastorePath(esxVI_Context *ctx,
                                                const char *datastorePath,
                                                char **key);

455 456 457 458 459 460
int esxVI_LookupAutoStartDefaults(esxVI_Context *ctx,
                                  esxVI_AutoStartDefaults **defaults);

int esxVI_LookupAutoStartPowerInfoList(esxVI_Context *ctx,
                                       esxVI_AutoStartPowerInfo **powerInfoList);

461
int esxVI_HandleVirtualMachineQuestion
462 463 464
      (esxVI_Context *ctx, esxVI_ManagedObjectReference *virtualMachine,
       esxVI_VirtualMachineQuestionInfo *questionInfo, bool autoAnswer,
       bool *blocked);
465

466
int esxVI_WaitForTaskCompletion(esxVI_Context *ctx,
467
                                esxVI_ManagedObjectReference *task,
468
                                const unsigned char *virtualMachineUuid,
469
                                esxVI_Occurrence virtualMachineOccurrence,
470
                                bool autoAnswer,
471 472
                                esxVI_TaskInfoState *finalState,
                                char **errorMessage);
473

474 475 476
int esxVI_ParseHostCpuIdInfo(esxVI_ParsedHostCpuIdInfo *parsedHostCpuIdInfo,
                             esxVI_HostCpuIdInfo *hostCpuIdInfo);

477 478
int esxVI_ProductVersionToDefaultVirtualHWVersion(esxVI_ProductVersion productVersion);

479 480
# include "esx_vi.generated.h"

481
#endif /* __ESX_VI_H__ */