esx_vi.h 14.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 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 97 98 99
enum _esxVI_APIVersion {
    esxVI_APIVersion_Undefined = 0,
    esxVI_APIVersion_Unknown,
    esxVI_APIVersion_25,
    esxVI_APIVersion_40
};

100 101 102 103
/*
 * AAAABBBB: where AAAA0000 is the product and BBBB the version. this format
 * allows simple bitmask testing for a product independent of the version
 */
104 105
enum _esxVI_ProductVersion {
    esxVI_ProductVersion_Undefined = 0,
106 107 108 109 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,

    esxVI_ProductVersion_VPX   = (1 << 2) << 16,
    esxVI_ProductVersion_VPX25 = esxVI_ProductVersion_VPX | 1,
    esxVI_ProductVersion_VPX40 = esxVI_ProductVersion_VPX | 2
117 118
};

M
Matthias Bolte 已提交
119 120 121
enum _esxVI_Occurrence {
    esxVI_Occurrence_Undefined = 0,
    esxVI_Occurrence_RequiredItem,
122
    esxVI_Occurrence_RequiredList,
M
Matthias Bolte 已提交
123
    esxVI_Occurrence_OptionalItem,
124
    esxVI_Occurrence_OptionalList,
125
    esxVI_Occurrence_None
126 127
};

128 129 130 131 132 133 134 135
struct _esxVI_ParsedHostCpuIdInfo {
    int level;
    char eax[32];
    char ebx[32];
    char ecx[32];
    char edx[32];
};

136 137


138 139 140 141 142 143
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Context
 */

struct _esxVI_Context {
    char *url;
M
Matthias Bolte 已提交
144
    char *ipAddress;
145 146
    CURL *curl_handle;
    struct curl_slist *curl_headers;
147
    char curl_error[CURL_ERROR_SIZE];
148 149 150 151
    virMutex curl_lock;
    char *username;
    char *password;
    esxVI_ServiceContent *service;
152 153
    esxVI_APIVersion apiVersion;
    esxVI_ProductVersion productVersion;
154 155 156 157 158 159 160
    esxVI_UserSession *session;
    esxVI_ManagedObjectReference *datacenter;
    esxVI_ManagedObjectReference *vmFolder;
    esxVI_ManagedObjectReference *hostFolder;
    esxVI_SelectionSpec *fullTraversalSpecList;
};

161
int esxVI_Context_Alloc(esxVI_Context **ctx);
162
void esxVI_Context_Free(esxVI_Context **ctx);
163 164
int esxVI_Context_Connect(esxVI_Context *ctx, const char *ipAddress,
                          const char *url, const char *username,
M
Matthias Bolte 已提交
165 166
                          const char *password,
                          esxUtil_ParsedQuery *parsedQuery);
167 168 169 170 171 172 173
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);
174 175 176 177



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
178
 * Response
179 180
 */

181
struct _esxVI_Response {
182
    int responseCode;                                 /* required */
183
    char *content;                                    /* required */
184
    xmlDocPtr document;                               /* optional */
185
    xmlNodePtr node;                                  /* optional, list */
186 187
};

188
int esxVI_Response_Alloc(esxVI_Response **response);
189
void esxVI_Response_Free(esxVI_Response **response);
190 191 192 193 194 195 196 197 198 199 200 201 202



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

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

struct _esxVI_Enumeration {
203
    esxVI_Type type;
204 205 206
    esxVI_EnumerationValue values[10];
};

207
int esxVI_Enumeration_CastFromAnyType(const esxVI_Enumeration *enumeration,
208
                                      esxVI_AnyType *anyType, int *value);
209
int esxVI_Enumeration_Serialize(const esxVI_Enumeration *enumeration,
210
                                int value, const char *element,
211
                                virBufferPtr output);
212
int esxVI_Enumeration_Deserialize(const esxVI_Enumeration *enumeration,
213 214 215 216 217 218 219 220 221 222 223 224 225
                                  xmlNodePtr node, int *value);



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

struct _esxVI_List {
    esxVI_List *_next;
};

typedef int (*esxVI_List_FreeFunc) (esxVI_List **item);
226 227
typedef int (*esxVI_List_DeepCopyFunc) (esxVI_List **dest, esxVI_List *src);
typedef int (*esxVI_List_CastFromAnyTypeFunc) (esxVI_AnyType *anyType,
228
                                               esxVI_List **item);
229
typedef int (*esxVI_List_SerializeFunc) (esxVI_List *item, const char *element,
230
                                         virBufferPtr output);
231
typedef int (*esxVI_List_DeserializeFunc) (xmlNodePtr node, esxVI_List **item);
232

233 234
int esxVI_List_Append(esxVI_List **list, esxVI_List *item);
int esxVI_List_DeepCopy(esxVI_List **destList, esxVI_List *srcList,
235 236
                        esxVI_List_DeepCopyFunc deepCopyFunc,
                        esxVI_List_FreeFunc freeFunc);
237
int esxVI_List_CastFromAnyType(esxVI_AnyType *anyType, esxVI_List **list,
238 239
                               esxVI_List_CastFromAnyTypeFunc castFromAnyTypeFunc,
                               esxVI_List_FreeFunc freeFunc);
240
int esxVI_List_Serialize(esxVI_List *list, const char *element,
241
                         virBufferPtr output,
242
                         esxVI_List_SerializeFunc serializeFunc);
243
int esxVI_List_Deserialize(xmlNodePtr node, esxVI_List **list,
244 245 246 247 248 249 250
                           esxVI_List_DeserializeFunc deserializeFunc,
                           esxVI_List_FreeFunc freeFunc);



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Utility and Convenience Functions
251 252 253 254
 *
 * Function naming scheme:
 *  - 'lookup' functions query the ESX or vCenter for information
 *  - 'get' functions get information from a local object
255 256
 */

257
int esxVI_Alloc(void **ptrptr, size_t size);
258 259

int esxVI_BuildFullTraversalSpecItem
260 261
      (esxVI_SelectionSpec **fullTraversalSpecList, const char *name,
       const char *type, const char *path, const char *selectSetNames);
262

263
int esxVI_BuildFullTraversalSpecList
264
      (esxVI_SelectionSpec **fullTraversalSpecList);
265

266
int esxVI_EnsureSession(esxVI_Context *ctx);
267

268
int esxVI_LookupObjectContentByType(esxVI_Context *ctx,
269 270 271 272 273
                                    esxVI_ManagedObjectReference *root,
                                    const char *type,
                                    esxVI_String *propertyNameList,
                                    esxVI_Boolean recurse,
                                    esxVI_ObjectContent **objectContentList);
274

275
int esxVI_GetManagedEntityStatus
276
      (esxVI_ObjectContent *objectContent, const char *propertyName,
277 278
       esxVI_ManagedEntityStatus *managedEntityStatus);

279
int esxVI_GetVirtualMachinePowerState
280
      (esxVI_ObjectContent *virtualMachine,
281 282
       esxVI_VirtualMachinePowerState *powerState);

283
int esxVI_GetVirtualMachineQuestionInfo
284
      (esxVI_ObjectContent *virtualMachine,
285 286
       esxVI_VirtualMachineQuestionInfo **questionInfo);

287 288 289 290 291 292 293 294 295 296 297 298 299
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);

300
int esxVI_LookupNumberOfDomainsByPowerState
301 302
      (esxVI_Context *ctx, esxVI_VirtualMachinePowerState powerState,
       esxVI_Boolean inverse);
303

304
int esxVI_GetVirtualMachineIdentity(esxVI_ObjectContent *virtualMachine,
305 306
                                    int *id, char **name, unsigned char *uuid);

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
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);

325
int esxVI_LookupResourcePoolByHostSystem
326
      (esxVI_Context *ctx, esxVI_ObjectContent *hostSystem,
327
       esxVI_ManagedObjectReference **resourcePool);
M
Matthias Bolte 已提交
328

329
int esxVI_LookupHostSystemByIp(esxVI_Context *ctx, const char *ipAddress,
M
Matthias Bolte 已提交
330
                               esxVI_String *propertyNameList,
331 332
                               esxVI_ObjectContent **hostSystem);

333
int esxVI_LookupVirtualMachineByUuid(esxVI_Context *ctx,
334 335
                                     const unsigned char *uuid,
                                     esxVI_String *propertyNameList,
336
                                     esxVI_ObjectContent **virtualMachine,
M
Matthias Bolte 已提交
337
                                     esxVI_Occurrence occurrence);
338

339 340 341 342 343
int esxVI_LookupVirtualMachineByName(esxVI_Context *ctx, const char *name,
                                     esxVI_String *propertyNameList,
                                     esxVI_ObjectContent **virtualMachine,
                                     esxVI_Occurrence occurrence);

344
int esxVI_LookupVirtualMachineByUuidAndPrepareForTask
345
      (esxVI_Context *ctx, const unsigned char *uuid,
346 347 348
       esxVI_String *propertyNameList, esxVI_ObjectContent **virtualMachine,
       esxVI_Boolean autoAnswer);

349
int esxVI_LookupDatastoreByName(esxVI_Context *ctx, const char *name,
M
Matthias Bolte 已提交
350 351
                                esxVI_String *propertyNameList,
                                esxVI_ObjectContent **datastore,
M
Matthias Bolte 已提交
352
                                esxVI_Occurrence occurrence);
M
Matthias Bolte 已提交
353

354
int esxVI_LookupTaskInfoByTask(esxVI_Context *ctx,
355 356 357 358
                               esxVI_ManagedObjectReference *task,
                               esxVI_TaskInfo **taskInfo);

int esxVI_LookupPendingTaskInfoListByVirtualMachine
359
      (esxVI_Context *ctx, esxVI_ObjectContent *virtualMachine,
360 361
       esxVI_TaskInfo **pendingTaskInfoList);

362
int esxVI_LookupAndHandleVirtualMachineQuestion(esxVI_Context *ctx,
363 364 365
                                                const unsigned char *uuid,
                                                esxVI_Boolean autoAnswer);

366 367 368 369 370 371 372 373 374
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);

375
int esxVI_HandleVirtualMachineQuestion
376
      (esxVI_Context *ctx,
377 378 379 380
       esxVI_ManagedObjectReference *virtualMachine,
       esxVI_VirtualMachineQuestionInfo *questionInfo,
       esxVI_Boolean autoAnswer);

381
int esxVI_WaitForTaskCompletion(esxVI_Context *ctx,
382
                                esxVI_ManagedObjectReference *task,
383 384
                                const unsigned char *virtualMachineUuid,
                                esxVI_Boolean autoAnswer,
385 386
                                esxVI_TaskInfoState *finalState);

387 388 389
int esxVI_ParseHostCpuIdInfo(esxVI_ParsedHostCpuIdInfo *parsedHostCpuIdInfo,
                             esxVI_HostCpuIdInfo *hostCpuIdInfo);

390
#endif /* __ESX_VI_H__ */