esx_vi.h 19.3 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) 2011 Red Hat, Inc.
6
 * Copyright (C) 2009-2010 Matthias Bolte <matthias.bolte@googlemail.com>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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__
25
# define __ESX_VI_H__
26

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

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

37 38 39


# define ESX_VI_ERROR(code, ...)                                              \
40
    virReportErrorHelper(VIR_FROM_ESX, code, __FILE__, __FUNCTION__,          \
41 42 43 44
                         __LINE__, __VA_ARGS__)



E
Eric Blake 已提交
45
# define ESX_VI__SOAP__REQUEST_HEADER                                         \
46 47 48 49 50 51 52 53 54 55
    "<?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 已提交
56
# define ESX_VI__SOAP__REQUEST_FOOTER                                         \
57 58 59 60 61
    "</soapenv:Body>\n"                                                       \
    "</soapenv:Envelope>"



E
Eric Blake 已提交
62
# define ESV_VI__XML_TAG__OPEN(_buffer, _element, _type)                      \
63 64 65 66 67 68 69 70 71 72
    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 已提交
73
# define ESV_VI__XML_TAG__CLOSE(_buffer, _element)                            \
74 75 76 77 78 79 80 81
    do {                                                                      \
        virBufferAddLit(_buffer, "</");                                       \
        virBufferAdd(_buffer, _element, -1);                                  \
        virBufferAddLit(_buffer, ">");                                        \
    } while (0)



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



96 97 98 99
enum _esxVI_APIVersion {
    esxVI_APIVersion_Undefined = 0,
    esxVI_APIVersion_Unknown,
    esxVI_APIVersion_25,
M
Matthias Bolte 已提交
100 101
    esxVI_APIVersion_40,
    esxVI_APIVersion_41,
P
Patrice LACHANCE 已提交
102 103 104
    esxVI_APIVersion_4x, /* > 4.1 */
    esxVI_APIVersion_50,
    esxVI_APIVersion_5x  /* > 5.0 */
105 106
};

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

    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 已提交
120 121
    esxVI_ProductVersion_ESX41 = esxVI_ProductVersion_ESX | 3,
    esxVI_ProductVersion_ESX4x = esxVI_ProductVersion_ESX | 4, /* > 4.1 */
P
Patrice LACHANCE 已提交
122 123
    esxVI_ProductVersion_ESX50 = esxVI_ProductVersion_ESX | 5,
    esxVI_ProductVersion_ESX5x = esxVI_ProductVersion_ESX | 6, /* > 5.0 */
124 125 126

    esxVI_ProductVersion_VPX   = (1 << 2) << 16,
    esxVI_ProductVersion_VPX25 = esxVI_ProductVersion_VPX | 1,
M
Matthias Bolte 已提交
127 128
    esxVI_ProductVersion_VPX40 = esxVI_ProductVersion_VPX | 2,
    esxVI_ProductVersion_VPX41 = esxVI_ProductVersion_VPX | 3,
P
Patrice LACHANCE 已提交
129 130 131
    esxVI_ProductVersion_VPX4x = esxVI_ProductVersion_VPX | 4, /* > 4.1 */
    esxVI_ProductVersion_VPX50 = esxVI_ProductVersion_VPX | 5,
    esxVI_ProductVersion_VPX5x = esxVI_ProductVersion_VPX | 6  /* > 5.0 */
132 133
};

M
Matthias Bolte 已提交
134 135 136
enum _esxVI_Occurrence {
    esxVI_Occurrence_Undefined = 0,
    esxVI_Occurrence_RequiredItem,
137
    esxVI_Occurrence_RequiredList,
M
Matthias Bolte 已提交
138
    esxVI_Occurrence_OptionalItem,
139
    esxVI_Occurrence_OptionalList,
140
    esxVI_Occurrence_None
141 142
};

143 144 145 146 147 148 149 150
struct _esxVI_ParsedHostCpuIdInfo {
    int level;
    char eax[32];
    char ebx[32];
    char ecx[32];
    char edx[32];
};

151 152


153 154 155 156 157 158 159 160 161
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * CURL
 */

struct _esxVI_CURL {
    CURL *handle;
    virMutex lock;
    struct curl_slist *headers;
    char error[CURL_ERROR_SIZE];
162
    esxVI_SharedCURL *shared;
163 164 165 166 167 168 169 170 171 172
};

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);



173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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);



190 191 192 193 194
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Context
 */

struct _esxVI_Context {
195
    /* All members are used read-only after esxVI_Context_Connect ... */
196
    esxVI_CURL *curl;
197
    char *url;
M
Matthias Bolte 已提交
198
    char *ipAddress;
199 200 201
    char *username;
    char *password;
    esxVI_ServiceContent *service;
202 203
    esxVI_APIVersion apiVersion;
    esxVI_ProductVersion productVersion;
204 205
    esxVI_UserSession *session; /* ... except the session ... */
    virMutexPtr sessionLock; /* ... that is protected by this mutex */
206
    esxVI_Datacenter *datacenter;
207
    char *datacenterPath; /* including folders */
208
    esxVI_ComputeResource *computeResource;
209
    char *computeResourcePath; /* including folders */
210
    esxVI_HostSystem *hostSystem;
211
    char *hostSystemName;
212 213 214 215 216 217
    esxVI_SelectionSpec *selectSet_folderToChildEntity;
    esxVI_SelectionSpec *selectSet_hostSystemToParent;
    esxVI_SelectionSpec *selectSet_hostSystemToVm;
    esxVI_SelectionSpec *selectSet_hostSystemToDatastore;
    esxVI_SelectionSpec *selectSet_computeResourceToHost;
    esxVI_SelectionSpec *selectSet_computeResourceToParentToParent;
218
    bool hasQueryVirtualDiskUuid;
219
    bool hasSessionIsActive;
220 221
};

222
int esxVI_Context_Alloc(esxVI_Context **ctx);
223
void esxVI_Context_Free(esxVI_Context **ctx);
224 225
int esxVI_Context_Connect(esxVI_Context *ctx, const char *ipAddress,
                          const char *url, const char *username,
M
Matthias Bolte 已提交
226
                          const char *password, esxUtil_ParsedUri *parsedUri);
227 228 229 230
int esxVI_Context_LookupManagedObjects(esxVI_Context *ctx);
int esxVI_Context_LookupManagedObjectsByPath(esxVI_Context *ctx, const char *path);
int esxVI_Context_LookupManagedObjectsByHostSystemIp(esxVI_Context *ctx,
                                                     const char *hostSystemIpAddress);
231 232 233
int esxVI_Context_Execute(esxVI_Context *ctx, const char *methodName,
                          const char *request, esxVI_Response **response,
                          esxVI_Occurrence occurrence);
234 235 236 237



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
238
 * Response
239 240
 */

241
struct _esxVI_Response {
242
    int responseCode;                                 /* required */
243
    char *content;                                    /* required */
244
    xmlDocPtr document;                               /* optional */
245
    xmlNodePtr node;                                  /* optional, list */
246 247
};

248
int esxVI_Response_Alloc(esxVI_Response **response);
249
void esxVI_Response_Free(esxVI_Response **response);
250 251 252 253 254 255 256 257 258 259 260 261 262



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

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

struct _esxVI_Enumeration {
263
    esxVI_Type type;
264 265 266
    esxVI_EnumerationValue values[10];
};

267
int esxVI_Enumeration_CastFromAnyType(const esxVI_Enumeration *enumeration,
268
                                      esxVI_AnyType *anyType, int *value);
269
int esxVI_Enumeration_Serialize(const esxVI_Enumeration *enumeration,
270
                                int value, const char *element,
271
                                virBufferPtr output);
272
int esxVI_Enumeration_Deserialize(const esxVI_Enumeration *enumeration,
273 274 275 276 277 278 279 280 281 282 283 284 285
                                  xmlNodePtr node, int *value);



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

struct _esxVI_List {
    esxVI_List *_next;
};

typedef int (*esxVI_List_FreeFunc) (esxVI_List **item);
286 287
typedef int (*esxVI_List_DeepCopyFunc) (esxVI_List **dest, esxVI_List *src);
typedef int (*esxVI_List_CastFromAnyTypeFunc) (esxVI_AnyType *anyType,
288
                                               esxVI_List **item);
289
typedef int (*esxVI_List_SerializeFunc) (esxVI_List *item, const char *element,
290
                                         virBufferPtr output);
291
typedef int (*esxVI_List_DeserializeFunc) (xmlNodePtr node, esxVI_List **item);
292

293 294
int esxVI_List_Append(esxVI_List **list, esxVI_List *item);
int esxVI_List_DeepCopy(esxVI_List **destList, esxVI_List *srcList,
295 296
                        esxVI_List_DeepCopyFunc deepCopyFunc,
                        esxVI_List_FreeFunc freeFunc);
297
int esxVI_List_CastFromAnyType(esxVI_AnyType *anyType, esxVI_List **list,
298 299
                               esxVI_List_CastFromAnyTypeFunc castFromAnyTypeFunc,
                               esxVI_List_FreeFunc freeFunc);
300
int esxVI_List_Serialize(esxVI_List *list, const char *element,
301
                         virBufferPtr output,
302
                         esxVI_List_SerializeFunc serializeFunc);
303
int esxVI_List_Deserialize(xmlNodePtr node, esxVI_List **list,
304 305 306 307 308 309 310
                           esxVI_List_DeserializeFunc deserializeFunc,
                           esxVI_List_FreeFunc freeFunc);



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Utility and Convenience Functions
311 312 313 314
 *
 * Function naming scheme:
 *  - 'lookup' functions query the ESX or vCenter for information
 *  - 'get' functions get information from a local object
315 316
 */

317
int esxVI_Alloc(void **ptrptr, size_t size);
318

319 320
int esxVI_BuildSelectSet
      (esxVI_SelectionSpec **selectSet, const char *name,
321
       const char *type, const char *path, const char *selectSetNames);
322

323
int esxVI_BuildSelectSetCollection(esxVI_Context *ctx);
324

325
int esxVI_EnsureSession(esxVI_Context *ctx);
326

327
int esxVI_LookupObjectContentByType(esxVI_Context *ctx,
328 329 330
                                    esxVI_ManagedObjectReference *root,
                                    const char *type,
                                    esxVI_String *propertyNameList,
331 332
                                    esxVI_ObjectContent **objectContentList,
                                    esxVI_Occurrence occurrence);
333

334
int esxVI_GetManagedEntityStatus
335
      (esxVI_ObjectContent *objectContent, const char *propertyName,
336 337
       esxVI_ManagedEntityStatus *managedEntityStatus);

338
int esxVI_GetVirtualMachinePowerState
339
      (esxVI_ObjectContent *virtualMachine,
340 341
       esxVI_VirtualMachinePowerState *powerState);

342
int esxVI_GetVirtualMachineQuestionInfo
343
      (esxVI_ObjectContent *virtualMachine,
344 345
       esxVI_VirtualMachineQuestionInfo **questionInfo);

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

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

353 354
int esxVI_GetStringValue(esxVI_ObjectContent *objectContent,
                         const char *propertyName,
M
Matthias Bolte 已提交
355
                         char **value, esxVI_Occurrence occurrence);
356 357 358 359

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

362
int esxVI_LookupNumberOfDomainsByPowerState
363
      (esxVI_Context *ctx, esxVI_VirtualMachinePowerState powerState,
364
       bool inverse);
365

366
int esxVI_GetVirtualMachineIdentity(esxVI_ObjectContent *virtualMachine,
367 368
                                    int *id, char **name, unsigned char *uuid);

369
int esxVI_GetNumberOfSnapshotTrees
370
      (esxVI_VirtualMachineSnapshotTree *snapshotTreeList,
371
       bool recurse, bool leaves);
372 373 374

int esxVI_GetSnapshotTreeNames
      (esxVI_VirtualMachineSnapshotTree *snapshotTreeList, char **names,
375
       int nameslen, bool recurse, bool leaves);
376 377 378 379 380 381 382 383 384 385 386 387

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);

388 389 390
int esxVI_LookupHostSystemProperties(esxVI_Context *ctx,
                                     esxVI_String *propertyNameList,
                                     esxVI_ObjectContent **hostSystem);
M
Matthias Bolte 已提交
391

392 393 394
int esxVI_LookupVirtualMachineList(esxVI_Context *ctx,
                                   esxVI_String *propertyNameList,
                                   esxVI_ObjectContent **virtualMachineList);
395

396
int esxVI_LookupVirtualMachineByUuid(esxVI_Context *ctx,
397 398
                                     const unsigned char *uuid,
                                     esxVI_String *propertyNameList,
399
                                     esxVI_ObjectContent **virtualMachine,
M
Matthias Bolte 已提交
400
                                     esxVI_Occurrence occurrence);
401

402 403 404 405 406
int esxVI_LookupVirtualMachineByName(esxVI_Context *ctx, const char *name,
                                     esxVI_String *propertyNameList,
                                     esxVI_ObjectContent **virtualMachine,
                                     esxVI_Occurrence occurrence);

407
int esxVI_LookupVirtualMachineByUuidAndPrepareForTask
408
      (esxVI_Context *ctx, const unsigned char *uuid,
409
       esxVI_String *propertyNameList, esxVI_ObjectContent **virtualMachine,
410
       bool autoAnswer);
411

412 413 414
int esxVI_LookupDatastoreList(esxVI_Context *ctx, esxVI_String *propertyNameList,
                              esxVI_ObjectContent **datastoreList);

415
int esxVI_LookupDatastoreByName(esxVI_Context *ctx, const char *name,
M
Matthias Bolte 已提交
416 417
                                esxVI_String *propertyNameList,
                                esxVI_ObjectContent **datastore,
M
Matthias Bolte 已提交
418
                                esxVI_Occurrence occurrence);
M
Matthias Bolte 已提交
419

420 421 422 423 424 425
int esxVI_LookupDatastoreByAbsolutePath(esxVI_Context *ctx,
                                        const char *absolutePath,
                                        esxVI_String *propertyNameList,
                                        esxVI_ObjectContent **datastore,
                                        esxVI_Occurrence occurrence);

426 427 428 429
int esxVI_LookupDatastoreHostMount(esxVI_Context *ctx,
                                   esxVI_ManagedObjectReference *datastore,
                                   esxVI_DatastoreHostMount **hostMount);

430
int esxVI_LookupTaskInfoByTask(esxVI_Context *ctx,
431 432 433 434
                               esxVI_ManagedObjectReference *task,
                               esxVI_TaskInfo **taskInfo);

int esxVI_LookupPendingTaskInfoListByVirtualMachine
435
      (esxVI_Context *ctx, esxVI_ObjectContent *virtualMachine,
436 437
       esxVI_TaskInfo **pendingTaskInfoList);

438
int esxVI_LookupAndHandleVirtualMachineQuestion(esxVI_Context *ctx,
439
                                                const unsigned char *uuid,
440
                                                esxVI_Occurrence occurrence,
441
                                                bool autoAnswer, bool *blocked);
442

443 444 445 446 447 448 449 450 451
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);

452 453
int esxVI_LookupFileInfoByDatastorePath(esxVI_Context *ctx,
                                        const char *datastorePath,
454
                                        bool lookupFolder,
455 456 457 458 459 460 461
                                        esxVI_FileInfo **fileInfo,
                                        esxVI_Occurrence occurrence);

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

462 463 464 465
int esxVI_LookupStorageVolumeKeyByDatastorePath(esxVI_Context *ctx,
                                                const char *datastorePath,
                                                char **key);

466 467 468 469 470 471
int esxVI_LookupAutoStartDefaults(esxVI_Context *ctx,
                                  esxVI_AutoStartDefaults **defaults);

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

472
int esxVI_HandleVirtualMachineQuestion
473 474 475
      (esxVI_Context *ctx, esxVI_ManagedObjectReference *virtualMachine,
       esxVI_VirtualMachineQuestionInfo *questionInfo, bool autoAnswer,
       bool *blocked);
476

477
int esxVI_WaitForTaskCompletion(esxVI_Context *ctx,
478
                                esxVI_ManagedObjectReference *task,
479
                                const unsigned char *virtualMachineUuid,
480
                                esxVI_Occurrence virtualMachineOccurrence,
481
                                bool autoAnswer,
482 483
                                esxVI_TaskInfoState *finalState,
                                char **errorMessage);
484

485 486 487
int esxVI_ParseHostCpuIdInfo(esxVI_ParsedHostCpuIdInfo *parsedHostCpuIdInfo,
                             esxVI_HostCpuIdInfo *hostCpuIdInfo);

488 489
int esxVI_ProductVersionToDefaultVirtualHWVersion(esxVI_ProductVersion productVersion);

490 491
# include "esx_vi.generated.h"

492
#endif /* __ESX_VI_H__ */