esx_vi.h 15.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) 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 161 162 163 164 165 166
    esxVI_UserSession *session;
    esxVI_ManagedObjectReference *datacenter;
    esxVI_ManagedObjectReference *vmFolder;
    esxVI_ManagedObjectReference *hostFolder;
    esxVI_SelectionSpec *fullTraversalSpecList;
};

167
int esxVI_Context_Alloc(esxVI_Context **ctx);
168
void esxVI_Context_Free(esxVI_Context **ctx);
169 170
int esxVI_Context_Connect(esxVI_Context *ctx, const char *ipAddress,
                          const char *url, const char *username,
M
Matthias Bolte 已提交
171 172
                          const char *password,
                          esxUtil_ParsedQuery *parsedQuery);
173 174 175 176 177 178 179
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);
180 181 182 183



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
184
 * Response
185 186
 */

187
struct _esxVI_Response {
188
    int responseCode;                                 /* required */
189
    char *content;                                    /* required */
190
    xmlDocPtr document;                               /* optional */
191
    xmlNodePtr node;                                  /* optional, list */
192 193
};

194
int esxVI_Response_Alloc(esxVI_Response **response);
195
void esxVI_Response_Free(esxVI_Response **response);
196 197 198 199 200 201 202 203 204 205 206 207 208



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

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

struct _esxVI_Enumeration {
209
    esxVI_Type type;
210 211 212
    esxVI_EnumerationValue values[10];
};

213
int esxVI_Enumeration_CastFromAnyType(const esxVI_Enumeration *enumeration,
214
                                      esxVI_AnyType *anyType, int *value);
215
int esxVI_Enumeration_Serialize(const esxVI_Enumeration *enumeration,
216
                                int value, const char *element,
217
                                virBufferPtr output);
218
int esxVI_Enumeration_Deserialize(const esxVI_Enumeration *enumeration,
219 220 221 222 223 224 225 226 227 228 229 230 231
                                  xmlNodePtr node, int *value);



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

struct _esxVI_List {
    esxVI_List *_next;
};

typedef int (*esxVI_List_FreeFunc) (esxVI_List **item);
232 233
typedef int (*esxVI_List_DeepCopyFunc) (esxVI_List **dest, esxVI_List *src);
typedef int (*esxVI_List_CastFromAnyTypeFunc) (esxVI_AnyType *anyType,
234
                                               esxVI_List **item);
235
typedef int (*esxVI_List_SerializeFunc) (esxVI_List *item, const char *element,
236
                                         virBufferPtr output);
237
typedef int (*esxVI_List_DeserializeFunc) (xmlNodePtr node, esxVI_List **item);
238

239 240
int esxVI_List_Append(esxVI_List **list, esxVI_List *item);
int esxVI_List_DeepCopy(esxVI_List **destList, esxVI_List *srcList,
241 242
                        esxVI_List_DeepCopyFunc deepCopyFunc,
                        esxVI_List_FreeFunc freeFunc);
243
int esxVI_List_CastFromAnyType(esxVI_AnyType *anyType, esxVI_List **list,
244 245
                               esxVI_List_CastFromAnyTypeFunc castFromAnyTypeFunc,
                               esxVI_List_FreeFunc freeFunc);
246
int esxVI_List_Serialize(esxVI_List *list, const char *element,
247
                         virBufferPtr output,
248
                         esxVI_List_SerializeFunc serializeFunc);
249
int esxVI_List_Deserialize(xmlNodePtr node, esxVI_List **list,
250 251 252 253 254 255 256
                           esxVI_List_DeserializeFunc deserializeFunc,
                           esxVI_List_FreeFunc freeFunc);



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Utility and Convenience Functions
257 258 259 260
 *
 * Function naming scheme:
 *  - 'lookup' functions query the ESX or vCenter for information
 *  - 'get' functions get information from a local object
261 262
 */

263
int esxVI_Alloc(void **ptrptr, size_t size);
264 265

int esxVI_BuildFullTraversalSpecItem
266 267
      (esxVI_SelectionSpec **fullTraversalSpecList, const char *name,
       const char *type, const char *path, const char *selectSetNames);
268

269
int esxVI_BuildFullTraversalSpecList
270
      (esxVI_SelectionSpec **fullTraversalSpecList);
271

272
int esxVI_EnsureSession(esxVI_Context *ctx);
273

274
int esxVI_LookupObjectContentByType(esxVI_Context *ctx,
275 276 277 278 279
                                    esxVI_ManagedObjectReference *root,
                                    const char *type,
                                    esxVI_String *propertyNameList,
                                    esxVI_Boolean recurse,
                                    esxVI_ObjectContent **objectContentList);
280

281
int esxVI_GetManagedEntityStatus
282
      (esxVI_ObjectContent *objectContent, const char *propertyName,
283 284
       esxVI_ManagedEntityStatus *managedEntityStatus);

285
int esxVI_GetVirtualMachinePowerState
286
      (esxVI_ObjectContent *virtualMachine,
287 288
       esxVI_VirtualMachinePowerState *powerState);

289
int esxVI_GetVirtualMachineQuestionInfo
290
      (esxVI_ObjectContent *virtualMachine,
291 292
       esxVI_VirtualMachineQuestionInfo **questionInfo);

293 294 295 296 297 298 299 300 301 302 303 304 305
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);

306
int esxVI_LookupNumberOfDomainsByPowerState
307 308
      (esxVI_Context *ctx, esxVI_VirtualMachinePowerState powerState,
       esxVI_Boolean inverse);
309

310
int esxVI_GetVirtualMachineIdentity(esxVI_ObjectContent *virtualMachine,
311 312
                                    int *id, char **name, unsigned char *uuid);

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
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);

331
int esxVI_LookupResourcePoolByHostSystem
332
      (esxVI_Context *ctx, esxVI_ObjectContent *hostSystem,
333
       esxVI_ManagedObjectReference **resourcePool);
M
Matthias Bolte 已提交
334

335
int esxVI_LookupHostSystemByIp(esxVI_Context *ctx, const char *ipAddress,
M
Matthias Bolte 已提交
336
                               esxVI_String *propertyNameList,
337 338
                               esxVI_ObjectContent **hostSystem);

339
int esxVI_LookupVirtualMachineByUuid(esxVI_Context *ctx,
340 341
                                     const unsigned char *uuid,
                                     esxVI_String *propertyNameList,
342
                                     esxVI_ObjectContent **virtualMachine,
M
Matthias Bolte 已提交
343
                                     esxVI_Occurrence occurrence);
344

345 346 347 348 349
int esxVI_LookupVirtualMachineByName(esxVI_Context *ctx, const char *name,
                                     esxVI_String *propertyNameList,
                                     esxVI_ObjectContent **virtualMachine,
                                     esxVI_Occurrence occurrence);

350
int esxVI_LookupVirtualMachineByUuidAndPrepareForTask
351
      (esxVI_Context *ctx, const unsigned char *uuid,
352 353 354
       esxVI_String *propertyNameList, esxVI_ObjectContent **virtualMachine,
       esxVI_Boolean autoAnswer);

355
int esxVI_LookupDatastoreByName(esxVI_Context *ctx, const char *name,
M
Matthias Bolte 已提交
356 357
                                esxVI_String *propertyNameList,
                                esxVI_ObjectContent **datastore,
M
Matthias Bolte 已提交
358
                                esxVI_Occurrence occurrence);
M
Matthias Bolte 已提交
359

360
int esxVI_LookupTaskInfoByTask(esxVI_Context *ctx,
361 362 363 364
                               esxVI_ManagedObjectReference *task,
                               esxVI_TaskInfo **taskInfo);

int esxVI_LookupPendingTaskInfoListByVirtualMachine
365
      (esxVI_Context *ctx, esxVI_ObjectContent *virtualMachine,
366 367
       esxVI_TaskInfo **pendingTaskInfoList);

368
int esxVI_LookupAndHandleVirtualMachineQuestion(esxVI_Context *ctx,
369
                                                const unsigned char *uuid,
370 371 372
                                                esxVI_Occurrence occurrence,
                                                esxVI_Boolean autoAnswer,
                                                esxVI_Boolean *blocked);
373

374 375 376 377 378 379 380 381 382
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);

383
int esxVI_HandleVirtualMachineQuestion
384
      (esxVI_Context *ctx,
385 386
       esxVI_ManagedObjectReference *virtualMachine,
       esxVI_VirtualMachineQuestionInfo *questionInfo,
387
       esxVI_Boolean autoAnswer, esxVI_Boolean *blocked);
388

389
int esxVI_WaitForTaskCompletion(esxVI_Context *ctx,
390
                                esxVI_ManagedObjectReference *task,
391
                                const unsigned char *virtualMachineUuid,
392
                                esxVI_Occurrence virtualMachineOccurrence,
393
                                esxVI_Boolean autoAnswer,
394 395
                                esxVI_TaskInfoState *finalState);

396 397 398
int esxVI_ParseHostCpuIdInfo(esxVI_ParsedHostCpuIdInfo *parsedHostCpuIdInfo,
                             esxVI_HostCpuIdInfo *hostCpuIdInfo);

399
#endif /* __ESX_VI_H__ */