esx_vi.h 17.8 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_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
    bool hasQueryVirtualDiskUuid;
171
    bool hasSessionIsActive;
172 173
};

174
int esxVI_Context_Alloc(esxVI_Context **ctx);
175
void esxVI_Context_Free(esxVI_Context **ctx);
176 177
int esxVI_Context_Connect(esxVI_Context *ctx, const char *ipAddress,
                          const char *url, const char *username,
M
Matthias Bolte 已提交
178
                          const char *password, esxUtil_ParsedUri *parsedUri);
179 180 181 182
int esxVI_Context_LookupObjectsByPath(esxVI_Context *ctx,
                                      esxUtil_ParsedUri *parsedUri);
int esxVI_Context_LookupObjectsByHostSystemIp(esxVI_Context *ctx,
                                              const char *hostSystemIpAddress);
183 184 185 186 187 188 189
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);
190 191 192 193



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
194
 * Response
195 196
 */

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

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



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

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

struct _esxVI_Enumeration {
219
    esxVI_Type type;
220 221 222
    esxVI_EnumerationValue values[10];
};

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



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

struct _esxVI_List {
    esxVI_List *_next;
};

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

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



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

273
int esxVI_Alloc(void **ptrptr, size_t size);
274

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

279
int esxVI_BuildSelectSetCollection(esxVI_Context *ctx);
280

281
int esxVI_EnsureSession(esxVI_Context *ctx);
282

283
int esxVI_LookupObjectContentByType(esxVI_Context *ctx,
284 285 286
                                    esxVI_ManagedObjectReference *root,
                                    const char *type,
                                    esxVI_String *propertyNameList,
287 288
                                    esxVI_ObjectContent **objectContentList,
                                    esxVI_Occurrence occurrence);
289

290
int esxVI_GetManagedEntityStatus
291
      (esxVI_ObjectContent *objectContent, const char *propertyName,
292 293
       esxVI_ManagedEntityStatus *managedEntityStatus);

294
int esxVI_GetVirtualMachinePowerState
295
      (esxVI_ObjectContent *virtualMachine,
296 297
       esxVI_VirtualMachinePowerState *powerState);

298
int esxVI_GetVirtualMachineQuestionInfo
299
      (esxVI_ObjectContent *virtualMachine,
300 301
       esxVI_VirtualMachineQuestionInfo **questionInfo);

302 303
int esxVI_GetBoolean(esxVI_ObjectContent *objectContent,
                     const char *propertyName,
M
Matthias Bolte 已提交
304
                     esxVI_Boolean *value, esxVI_Occurrence occurrence);
305

306
int esxVI_GetLong(esxVI_ObjectContent *objectContent, const char *propertyName,
M
Matthias Bolte 已提交
307
                  esxVI_Long **value, esxVI_Occurrence occurrence);
308

309 310
int esxVI_GetStringValue(esxVI_ObjectContent *objectContent,
                         const char *propertyName,
M
Matthias Bolte 已提交
311
                         char **value, esxVI_Occurrence occurrence);
312 313 314 315

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

318
int esxVI_LookupNumberOfDomainsByPowerState
319 320
      (esxVI_Context *ctx, esxVI_VirtualMachinePowerState powerState,
       esxVI_Boolean inverse);
321

322
int esxVI_GetVirtualMachineIdentity(esxVI_ObjectContent *virtualMachine,
323 324
                                    int *id, char **name, unsigned char *uuid);

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
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);

343 344 345
int esxVI_LookupHostSystemProperties(esxVI_Context *ctx,
                                     esxVI_String *propertyNameList,
                                     esxVI_ObjectContent **hostSystem);
M
Matthias Bolte 已提交
346

347 348 349
int esxVI_LookupVirtualMachineList(esxVI_Context *ctx,
                                   esxVI_String *propertyNameList,
                                   esxVI_ObjectContent **virtualMachineList);
350

351
int esxVI_LookupVirtualMachineByUuid(esxVI_Context *ctx,
352 353
                                     const unsigned char *uuid,
                                     esxVI_String *propertyNameList,
354
                                     esxVI_ObjectContent **virtualMachine,
M
Matthias Bolte 已提交
355
                                     esxVI_Occurrence occurrence);
356

357 358 359 360 361
int esxVI_LookupVirtualMachineByName(esxVI_Context *ctx, const char *name,
                                     esxVI_String *propertyNameList,
                                     esxVI_ObjectContent **virtualMachine,
                                     esxVI_Occurrence occurrence);

362
int esxVI_LookupVirtualMachineByUuidAndPrepareForTask
363
      (esxVI_Context *ctx, const unsigned char *uuid,
364 365 366
       esxVI_String *propertyNameList, esxVI_ObjectContent **virtualMachine,
       esxVI_Boolean autoAnswer);

367 368 369
int esxVI_LookupDatastoreList(esxVI_Context *ctx, esxVI_String *propertyNameList,
                              esxVI_ObjectContent **datastoreList);

370
int esxVI_LookupDatastoreByName(esxVI_Context *ctx, const char *name,
M
Matthias Bolte 已提交
371 372
                                esxVI_String *propertyNameList,
                                esxVI_ObjectContent **datastore,
M
Matthias Bolte 已提交
373
                                esxVI_Occurrence occurrence);
M
Matthias Bolte 已提交
374

375 376 377 378 379 380
int esxVI_LookupDatastoreByAbsolutePath(esxVI_Context *ctx,
                                        const char *absolutePath,
                                        esxVI_String *propertyNameList,
                                        esxVI_ObjectContent **datastore,
                                        esxVI_Occurrence occurrence);

381 382 383 384
int esxVI_LookupDatastoreHostMount(esxVI_Context *ctx,
                                   esxVI_ManagedObjectReference *datastore,
                                   esxVI_DatastoreHostMount **hostMount);

385
int esxVI_LookupTaskInfoByTask(esxVI_Context *ctx,
386 387 388 389
                               esxVI_ManagedObjectReference *task,
                               esxVI_TaskInfo **taskInfo);

int esxVI_LookupPendingTaskInfoListByVirtualMachine
390
      (esxVI_Context *ctx, esxVI_ObjectContent *virtualMachine,
391 392
       esxVI_TaskInfo **pendingTaskInfoList);

393
int esxVI_LookupAndHandleVirtualMachineQuestion(esxVI_Context *ctx,
394
                                                const unsigned char *uuid,
395 396 397
                                                esxVI_Occurrence occurrence,
                                                esxVI_Boolean autoAnswer,
                                                esxVI_Boolean *blocked);
398

399 400 401 402 403 404 405 406 407
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);

408 409
int esxVI_LookupFileInfoByDatastorePath(esxVI_Context *ctx,
                                        const char *datastorePath,
410
                                        bool lookupFolder,
411 412 413 414 415 416 417
                                        esxVI_FileInfo **fileInfo,
                                        esxVI_Occurrence occurrence);

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

418 419 420 421
int esxVI_LookupStorageVolumeKeyByDatastorePath(esxVI_Context *ctx,
                                                const char *datastorePath,
                                                char **key);

422 423 424 425 426 427
int esxVI_LookupAutoStartDefaults(esxVI_Context *ctx,
                                  esxVI_AutoStartDefaults **defaults);

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

428
int esxVI_HandleVirtualMachineQuestion
429
      (esxVI_Context *ctx,
430 431
       esxVI_ManagedObjectReference *virtualMachine,
       esxVI_VirtualMachineQuestionInfo *questionInfo,
432
       esxVI_Boolean autoAnswer, esxVI_Boolean *blocked);
433

434
int esxVI_WaitForTaskCompletion(esxVI_Context *ctx,
435
                                esxVI_ManagedObjectReference *task,
436
                                const unsigned char *virtualMachineUuid,
437
                                esxVI_Occurrence virtualMachineOccurrence,
438
                                esxVI_Boolean autoAnswer,
439 440
                                esxVI_TaskInfoState *finalState,
                                char **errorMessage);
441

442 443 444
int esxVI_ParseHostCpuIdInfo(esxVI_ParsedHostCpuIdInfo *parsedHostCpuIdInfo,
                             esxVI_HostCpuIdInfo *hostCpuIdInfo);

445 446
int esxVI_ProductVersionToDefaultVirtualHWVersion(esxVI_ProductVersion productVersion);

447 448
# include "esx_vi.generated.h"

449
#endif /* __ESX_VI_H__ */