esx_vi_types.c 52.6 KB
Newer Older
1 2 3 4

/*
 * esx_vi_types.c: client for the VMware VI API 2.5 to manage ESX hosts
 *
5
 * Copyright (C) 2010 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 25
 *
 * 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
 *
 */

#include <config.h>

26
#include <stdint.h>
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include <libxml/parser.h>
#include <libxml/xpathInternals.h>

#include "buf.h"
#include "datatypes.h"
#include "memory.h"
#include "logging.h"
#include "util.h"
#include "esx_vi.h"
#include "esx_vi_types.h"

#define VIR_FROM_THIS VIR_FROM_ESX



42
#define ESX_VI__TEMPLATE__ALLOC(__type)                                       \
43
    int                                                                       \
44
    esxVI_##__type##_Alloc(esxVI_##__type **ptrptr)                           \
45
    {                                                                         \
46 47 48 49 50 51 52
        if (esxVI_Alloc((void **)ptrptr, sizeof (esxVI_##__type)) < 0) {      \
            return -1;                                                        \
        }                                                                     \
                                                                              \
        (*ptrptr)->_type = esxVI_Type_##__type;                               \
                                                                              \
        return 0;                                                             \
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    }



#define ESX_VI__TEMPLATE__FREE(_type, _body)                                  \
    void                                                                      \
    esxVI_##_type##_Free(esxVI_##_type **ptrptr)                              \
    {                                                                         \
        esxVI_##_type *item = NULL;                                           \
                                                                              \
        if (ptrptr == NULL || *ptrptr == NULL) {                              \
            return;                                                           \
        }                                                                     \
                                                                              \
        item = *ptrptr;                                                       \
                                                                              \
        _body                                                                 \
                                                                              \
        VIR_FREE(*ptrptr);                                                    \
    }



76
#define ESX_VI__TEMPLATE__VALIDATE(__type, _require)                          \
77
    int                                                                       \
78
    esxVI_##__type##_Validate(esxVI_##__type *item)                           \
79
    {                                                                         \
80 81 82 83 84 85 86 87
        const char *type_name = esxVI_Type_ToString(esxVI_Type_##__type);     \
                                                                              \
        if (item->_type <= esxVI_Type_Undefined ||                            \
            item->_type >= esxVI_Type_Other) {                                \
            ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,                              \
                         "%s object has invalid dynamic type", type_name);    \
            return -1;                                                        \
        }                                                                     \
88 89 90 91 92 93 94 95
                                                                              \
        _require                                                              \
                                                                              \
        return 0;                                                             \
    }



96
#define ESX_VI__TEMPLATE__DEEP_COPY(_type, _deep_copy)                        \
97
    int                                                                       \
98
    esxVI_##_type##_DeepCopy(esxVI_##_type **dest, esxVI_##_type *src)        \
99
    {                                                                         \
100
        if (dest == NULL || *dest != NULL) {                                  \
101 102
            ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",                        \
                         _("Invalid argument"));                              \
103 104 105 106 107 108 109 110 111 112 113 114 115
            return -1;                                                        \
        }                                                                     \
                                                                              \
        if (src == NULL) {                                                    \
            return 0;                                                         \
        }                                                                     \
                                                                              \
        if (esxVI_##_type##_Alloc(dest) < 0) {                                \
            goto failure;                                                     \
        }                                                                     \
                                                                              \
        _deep_copy                                                            \
                                                                              \
116
        return 0;                                                             \
117 118 119 120 121
                                                                              \
      failure:                                                                \
        esxVI_##_type##_Free(dest);                                           \
                                                                              \
        return -1;                                                            \
122 123 124 125
    }



126 127
#define ESX_VI__TEMPLATE__LIST__APPEND(_type)                                 \
    int                                                                       \
128
    esxVI_##_type##_AppendToList(esxVI_##_type **list,  esxVI_##_type *item)  \
129
    {                                                                         \
130
        return esxVI_List_Append((esxVI_List **)list, (esxVI_List *)item);    \
131 132 133 134 135 136
    }



#define ESX_VI__TEMPLATE__LIST__DEEP_COPY(_type)                              \
    int                                                                       \
137
    esxVI_##_type##_DeepCopyList(esxVI_##_type **destList,                    \
138 139 140
                                 esxVI_##_type *srcList)                      \
    {                                                                         \
        return esxVI_List_DeepCopy                                            \
141
                 ((esxVI_List **)destList, (esxVI_List *)srcList,             \
142 143 144 145 146 147
                  (esxVI_List_DeepCopyFunc)esxVI_##_type##_DeepCopy,          \
                  (esxVI_List_FreeFunc)esxVI_##_type##_Free);                 \
    }



148 149
#define ESX_VI__TEMPLATE__LIST__CAST_FROM_ANY_TYPE(_type)                     \
    int                                                                       \
150
    esxVI_##_type##_CastListFromAnyType(esxVI_AnyType *anyType,               \
151 152 153
                                        esxVI_##_type **list)                 \
    {                                                                         \
        return esxVI_List_CastFromAnyType                                     \
154
                 (anyType, (esxVI_List **)list,                               \
155 156 157 158 159 160 161
                  (esxVI_List_CastFromAnyTypeFunc)                            \
                    esxVI_##_type##_CastFromAnyType,                          \
                  (esxVI_List_FreeFunc)esxVI_##_type##_Free);                 \
    }



162 163
#define ESX_VI__TEMPLATE__LIST__SERIALIZE(_type)                              \
    int                                                                       \
164
    esxVI_##_type##_SerializeList(esxVI_##_type *list, const char *element,   \
165
                                  virBufferPtr output)                        \
166
    {                                                                         \
167
        return esxVI_List_Serialize((esxVI_List *)list, element, output,      \
168 169 170 171 172 173 174 175
                                    (esxVI_List_SerializeFunc)                \
                                      esxVI_##_type##_Serialize);             \
    }



#define ESX_VI__TEMPLATE__LIST__DESERIALIZE(_type)                            \
    int                                                                       \
176
    esxVI_##_type##_DeserializeList(xmlNodePtr node, esxVI_##_type **list)    \
177 178
    {                                                                         \
        return esxVI_List_Deserialize                                         \
179
                 (node, (esxVI_List **)list,                                  \
180 181 182 183 184 185 186 187
                  (esxVI_List_DeserializeFunc)esxVI_##_type##_Deserialize,    \
                  (esxVI_List_FreeFunc)esxVI_##_type##_Free);                 \
    }



#define ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE(_type)                           \
    int                                                                       \
188
    esxVI_##_type##_CastFromAnyType(esxVI_AnyType *anyType,                   \
189 190 191
                                    esxVI_##_type **ptrptr)                   \
    {                                                                         \
        if (anyType == NULL || ptrptr == NULL || *ptrptr != NULL) {           \
192 193
            ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",                        \
                         _("Invalid argument"));                              \
194 195 196
            return -1;                                                        \
        }                                                                     \
                                                                              \
197
        if (anyType->type != esxVI_Type_##_type) {                            \
198
            ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,                              \
199
                         "Expecting type '%s' but found '%s'",                \
200 201
                         esxVI_Type_ToString(esxVI_Type_##_type),             \
                         anyType->other);                                     \
202 203 204
            return -1;                                                        \
        }                                                                     \
                                                                              \
205
        return esxVI_##_type##_Deserialize(anyType->node, ptrptr);            \
206 207 208 209
    }



210
#define ESX_VI__TEMPLATE__SERIALIZE_EXTRA(_type, _extra, _serialize)          \
211
    int                                                                       \
212
    esxVI_##_type##_Serialize(esxVI_##_type *item,                            \
213
                              const char *element, virBufferPtr output)       \
214 215
    {                                                                         \
        if (element == NULL || output == NULL ) {                             \
216 217
            ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",                        \
                         _("Invalid argument"));                              \
218 219 220 221
            return -1;                                                        \
        }                                                                     \
                                                                              \
        if (item == NULL) {                                                   \
222 223 224
            return 0;                                                         \
        }                                                                     \
                                                                              \
225 226
        _extra                                                                \
                                                                              \
227 228
        if (esxVI_##_type##_Validate(item) < 0) {                             \
            return -1;                                                        \
229 230
        }                                                                     \
                                                                              \
231 232
        ESV_VI__XML_TAG__OPEN(output, element,                                \
                              esxVI_Type_ToString(esxVI_Type_##_type));       \
233 234 235 236 237 238 239 240 241 242 243
                                                                              \
        _serialize                                                            \
                                                                              \
        ESV_VI__XML_TAG__CLOSE(output, element);                              \
                                                                              \
        return 0;                                                             \
    }



#define ESX_VI__TEMPLATE__SERIALIZE(_type, _serialize)                        \
244
    ESX_VI__TEMPLATE__SERIALIZE_EXTRA(_type, /* nothing */, _serialize)
245 246 247



248
#define ESX_VI__TEMPLATE__DESERIALIZE(_type, _deserialize)                    \
249
    int                                                                       \
250
    esxVI_##_type##_Deserialize(xmlNodePtr node, esxVI_##_type **ptrptr)      \
251 252 253 254
    {                                                                         \
        xmlNodePtr childNode = NULL;                                          \
                                                                              \
        if (ptrptr == NULL || *ptrptr != NULL) {                              \
255 256
            ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",                        \
                         _("Invalid argument"));                              \
257 258 259
            return -1;                                                        \
        }                                                                     \
                                                                              \
260
        if (esxVI_##_type##_Alloc(ptrptr) < 0) {                              \
261 262 263
            return -1;                                                        \
        }                                                                     \
                                                                              \
264
        for (childNode = node->children; childNode != NULL;                   \
265 266
             childNode = childNode->next) {                                   \
            if (childNode->type != XML_ELEMENT_NODE) {                        \
267
                ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,                          \
268 269 270 271 272 273 274 275 276
                             "Wrong XML element type %d", childNode->type);   \
                goto failure;                                                 \
            }                                                                 \
                                                                              \
            _deserialize                                                      \
                                                                              \
            VIR_WARN("Unexpected '%s' property", childNode->name);            \
        }                                                                     \
                                                                              \
277 278 279
        if (esxVI_##_type##_Validate(*ptrptr) < 0) {                          \
            goto failure;                                                     \
        }                                                                     \
280 281 282 283 284 285 286 287 288 289 290 291 292
                                                                              \
        return 0;                                                             \
                                                                              \
      failure:                                                                \
        esxVI_##_type##_Free(ptrptr);                                         \
                                                                              \
        return -1;                                                            \
    }



#define ESX_VI__TEMPLATE__DESERIALIZE_NUMBER(_type, _xsdType, _min, _max)     \
    int                                                                       \
293
    esxVI_##_type##_Deserialize(xmlNodePtr node, esxVI_##_type **number)      \
294 295 296 297 298 299
    {                                                                         \
        int result = 0;                                                       \
        char *string;                                                         \
        long long value;                                                      \
                                                                              \
        if (number == NULL || *number != NULL) {                              \
300 301
            ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",                        \
                         _("Invalid argument"));                              \
302 303 304
            return -1;                                                        \
        }                                                                     \
                                                                              \
305
        if (esxVI_##_type##_Alloc(number) < 0) {                              \
306 307 308
            return -1;                                                        \
        }                                                                     \
                                                                              \
309
        string = (char *)xmlNodeListGetString(node->doc, node->children, 1);  \
310 311
                                                                              \
        if (string == NULL) {                                                 \
312
            ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,                              \
313 314 315 316 317 318
                         "XML node doesn't contain text, expecting an "       \
                         _xsdType" value");                                   \
            goto failure;                                                     \
        }                                                                     \
                                                                              \
        if (virStrToLong_ll(string, NULL, 10, &value) < 0) {                  \
319
            ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,                              \
320 321 322 323 324
                         "Unknown value '%s' for "_xsdType, string);          \
            goto failure;                                                     \
        }                                                                     \
                                                                              \
        if (value < (_min) || value > (_max)) {                               \
325
            ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,                              \
326
                         "Value '%s' is not representable as "_xsdType,       \
M
Matthias Bolte 已提交
327
                         (const char *)string);                               \
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
            goto failure;                                                     \
        }                                                                     \
                                                                              \
        (*number)->value = value;                                             \
                                                                              \
      cleanup:                                                                \
        VIR_FREE(string);                                                     \
                                                                              \
        return result;                                                        \
                                                                              \
      failure:                                                                \
        esxVI_##_type##_Free(number);                                         \
                                                                              \
        result = -1;                                                          \
                                                                              \
        goto cleanup;                                                         \
    }



348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
/*
 * Macros for property handling to be used as part of other macros
 */

#define ESX_VI__TEMPLATE__PROPERTY__DEEP_COPY(_type, _name)                   \
    if (esxVI_##_type##_DeepCopy(&(*dest)->_name, src->_name) < 0) {          \
        goto failure;                                                         \
    }



#define ESX_VI__TEMPLATE__PROPERTY__DEEP_COPY_LIST(_type, _name)              \
    if (esxVI_##_type##_DeepCopyList(&(*dest)->_name, src->_name) < 0) {      \
        goto failure;                                                         \
    }



#define ESX_VI__TEMPLATE__PROPERTY__DEEP_COPY_VALUE(_type, _name)             \
    if (esxVI_##_type##_DeepCopyValue(&(*dest)->_name, src->_name) < 0) {     \
        goto failure;                                                         \
    }



373 374
#define ESX_VI__TEMPLATE__PROPERTY__SERIALIZE(_type, _name)                   \
    if (esxVI_##_type##_Serialize(item->_name, #_name, output) < 0) {         \
375 376 377 378 379
        return -1;                                                            \
    }



380 381
#define ESX_VI__TEMPLATE__PROPERTY__SERIALIZE_VALUE(_type, _name)             \
    if (esxVI_##_type##_SerializeValue(item->_name, #_name, output) < 0) {    \
382 383 384 385 386
        return -1;                                                            \
    }



387 388
#define ESX_VI__TEMPLATE__PROPERTY__SERIALIZE_LIST(_type, _name)              \
    if (esxVI_##_type##_SerializeList(item->_name, #_name, output) < 0) {     \
389 390 391 392 393 394 395
        return -1;                                                            \
    }



#define ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE(_type, _name)                 \
    if (xmlStrEqual(childNode->name, BAD_CAST #_name)) {                      \
396
        if (esxVI_##_type##_Deserialize(childNode, &(*ptrptr)->_name) < 0) {  \
397 398 399 400 401 402 403 404
            goto failure;                                                     \
        }                                                                     \
                                                                              \
        continue;                                                             \
    }



405
#define ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE_IGNORE(_name)                 \
406 407 408 409 410 411
    if (xmlStrEqual(childNode->name, BAD_CAST #_name)) {                      \
        continue;                                                             \
    }



412
#define ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE_VALUE(_type, _name)           \
413
    if (xmlStrEqual(childNode->name, BAD_CAST #_name)) {                      \
414 415
        if (esxVI_##_type##_DeserializeValue(childNode,                       \
                                             &(*ptrptr)->_name) < 0) {        \
416 417 418 419 420 421 422 423 424 425 426 427
            goto failure;                                                     \
        }                                                                     \
                                                                              \
        continue;                                                             \
    }



#define ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE_LIST(_type, _name)            \
    if (xmlStrEqual(childNode->name, BAD_CAST #_name)) {                      \
        esxVI_##_type *_name##Item = NULL;                                    \
                                                                              \
428
        if (esxVI_##_type##_Deserialize(childNode, &_name##Item) < 0) {       \
429 430 431
            goto failure;                                                     \
        }                                                                     \
                                                                              \
432
        if (esxVI_##_type##_AppendToList(&(*ptrptr)->_name,                   \
433 434 435 436 437 438 439 440 441 442 443 444 445
                                         _name##Item) < 0) {                  \
            esxVI_##_type##_Free(&_name##Item);                               \
            goto failure;                                                     \
        }                                                                     \
                                                                              \
        continue;                                                             \
    }



/*
 * A required property must be != 0 (NULL for pointers, "undefined" == 0 for
 * enumeration values).
446 447
 *
 * To be used as part of ESX_VI__TEMPLATE__VALIDATE.
448
 */
449 450
#define ESX_VI__TEMPLATE__PROPERTY__REQUIRE(_name)                            \
    if (item->_name == 0) {                                                   \
451
        ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,                                  \
452 453 454
                     "%s object is missing the required '%s' property",       \
                     type_name, #_name);                                      \
        return -1;                                                            \
455 456 457 458
    }



459 460 461 462
/*
 * Macros to implement enumerations
 */

463 464
#define ESX_VI__TEMPLATE__ENUMERATION__CAST_FROM_ANY_TYPE(_type)              \
    int                                                                       \
465
    esxVI_##_type##_CastFromAnyType(esxVI_AnyType *anyType,                   \
466 467 468
                                    esxVI_##_type *value)                     \
    {                                                                         \
        return esxVI_Enumeration_CastFromAnyType                              \
469
                 (&_esxVI_##_type##_Enumeration, anyType, (int *)value);      \
470 471 472 473 474 475
    }



#define ESX_VI__TEMPLATE__ENUMERATION__SERIALIZE(_type)                       \
    int                                                                       \
476
    esxVI_##_type##_Serialize(esxVI_##_type value, const char *element,       \
477
                              virBufferPtr output)                            \
478
    {                                                                         \
479
        return esxVI_Enumeration_Serialize(&_esxVI_##_type##_Enumeration,     \
480
                                           value, element, output);           \
481 482 483 484 485 486
    }



#define ESX_VI__TEMPLATE__ENUMERATION__DESERIALIZE(_type)                     \
    int                                                                       \
487
    esxVI_##_type##_Deserialize(xmlNodePtr node, esxVI_##_type *value)        \
488
    {                                                                         \
489
        return esxVI_Enumeration_Deserialize(&_esxVI_##_type##_Enumeration,   \
490 491 492 493 494
                                             node, (int *)value);             \
    }



495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
/*
 * Macros to implement dynamic dispatched functions
 */

#define ESX_VI__TEMPLATE__DISPATCH(__type, _dispatch, _error_return)          \
    switch (item->_type) {                                                    \
      _dispatch                                                               \
                                                                              \
      case esxVI_Type_##__type:                                               \
        break;                                                                \
                                                                              \
      default:                                                                \
        ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,                                  \
                     "Call to %s for unexpected type '%s'", __FUNCTION__,     \
                     esxVI_Type_ToString(item->_type));                       \
        return _error_return;                                                 \
    }



#define ESX_VI__TEMPLATE__DISPATCH__FREE(_type)                               \
    case esxVI_Type_##_type:                                                  \
      esxVI_##_type##_Free((esxVI_##_type **)ptrptr);                         \
      return;



#define ESX_VI__TEMPLATE__DISPATCH__DEEP_COPY(_type)                          \
    case esxVI_Type_##_type:                                                  \
      return esxVI_##_type##_DeepCopy((esxVI_##_type **)dst,                  \
                                      (esxVI_##_type *)src);



#define ESX_VI__TEMPLATE__DISPATCH__SERIALIZE(_type)                          \
    case esxVI_Type_##_type:                                                  \
      return esxVI_##_type##_Serialize((esxVI_##_type *)item, element,        \
                                       output);



#define ESX_VI__TEMPLATE__DYNAMIC_FREE(__type, _dispatch, _body)              \
    ESX_VI__TEMPLATE__FREE(__type,                                            \
      ESX_VI__TEMPLATE__DISPATCH(__type, _dispatch, /* nothing */)            \
      _body)



#define ESX_VI__TEMPLATE__DYNAMIC_CAST(__type, _accept)                       \
    esxVI_##__type *                                                          \
    esxVI_##__type##_DynamicCast(void *item)                                  \
    {                                                                         \
        if (item == NULL) {                                                   \
548 549
            ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",                        \
                         _("Invalid argument"));                              \
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
            return NULL;                                                      \
        }                                                                     \
                                                                              \
        _accept                                                               \
                                                                              \
        return NULL;                                                          \
    }



#define ESX_VI__TEMPLATE__DYNAMIC_CAST__ACCEPT(__type)                        \
    if (((esxVI_Object *)item)->_type == esxVI_Type_##__type) {               \
        return item;                                                          \
    }



#define ESX_VI__TEMPLATE__DYNAMIC_SERIALIZE(__type, _dispatch, _serialize)    \
    ESX_VI__TEMPLATE__SERIALIZE_EXTRA(__type,                                 \
      ESX_VI__TEMPLATE__DISPATCH(__type, _dispatch, -1),                      \
      _serialize)



574 575 576 577 578
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * XSI: Type
 */

const char *
579
esxVI_Type_ToString(esxVI_Type type)
580 581
{
    switch (type) {
582
      default:
583
      case esxVI_Type_Undefined:
584
        return "<undefined>";
585 586 587 588

      case esxVI_Type_Boolean:
        return "xsd:boolean";

589 590 591
      case esxVI_Type_AnyType:
        return "xsd:anyType";

592 593 594 595 596 597 598 599 600 601 602 603
      case esxVI_Type_String:
        return "xsd:string";

      case esxVI_Type_Short:
        return "xsd:short";

      case esxVI_Type_Int:
        return "xsd:int";

      case esxVI_Type_Long:
        return "xsd:long";

604 605
      case esxVI_Type_DateTime:
        return "xsd:dateTime";
606

607 608 609 610 611 612 613 614 615 616
      case esxVI_Type_Fault:
        return "Fault";

      case esxVI_Type_ManagedObjectReference:
        return "ManagedObjectReference";

#include "esx_vi_types.generated.typetostring"

      case esxVI_Type_Other:
        return "<other>";
617 618 619
    }
}

620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
esxVI_Type
esxVI_Type_FromString(const char *type)
{
    if (type == NULL || STREQ(type, "<undefined>")) {
        return esxVI_Type_Undefined;
    } else if (STREQ(type, "xsd:boolean")) {
        return esxVI_Type_Boolean;
    } else if (STREQ(type, "xsd:anyType")) {
        return esxVI_Type_AnyType;
    } else if (STREQ(type, "xsd:string")) {
        return esxVI_Type_String;
    } else if (STREQ(type, "xsd:short")) {
        return esxVI_Type_Short;
    } else if (STREQ(type, "xsd:int")) {
        return esxVI_Type_Int;
    } else if (STREQ(type, "xsd:long")) {
        return esxVI_Type_Long;
    } else if (STREQ(type, "xsd:dateTime")) {
        return esxVI_Type_DateTime;
    } else if (STREQ(type, "Fault")) {
        return esxVI_Type_Fault;
    } else if (STREQ(type, "ManagedObjectReference")) {
        return esxVI_Type_ManagedObjectReference;
    }

#include "esx_vi_types.generated.typefromstring"

    else {
        return esxVI_Type_Other;
    }
}
651 652 653 654 655 656 657


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * XSD: Boolean
 */

static const esxVI_Enumeration _esxVI_Boolean_Enumeration = {
658
    esxVI_Type_Boolean, {
659 660 661 662 663 664 665
        { "true", esxVI_Boolean_True },
        { "false", esxVI_Boolean_False },
        { NULL, -1 },
    },
};

/* esxVI_Boolean_Serialize */
666
ESX_VI__TEMPLATE__ENUMERATION__SERIALIZE(Boolean)
667 668

/* esxVI_Boolean_Deserialize */
669
ESX_VI__TEMPLATE__ENUMERATION__DESERIALIZE(Boolean)
670 671 672 673 674 675 676 677



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * XSD: AnyType
 */

/* esxVI_AnyType_Alloc */
678
ESX_VI__TEMPLATE__ALLOC(AnyType)
679 680 681 682

/* esxVI_AnyType_Free */
ESX_VI__TEMPLATE__FREE(AnyType,
{
683
    xmlFreeNode(item->node);
684 685
    VIR_FREE(item->other);
    VIR_FREE(item->value);
686
})
687 688

int
689
esxVI_AnyType_ExpectType(esxVI_AnyType *anyType, esxVI_Type type)
690 691
{
    if (anyType->type != type) {
692
        ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,
693
                     _("Expecting type '%s' but found '%s'"),
694
                     esxVI_Type_ToString(type),
695
                     anyType->type != esxVI_Type_Other
696
                       ? esxVI_Type_ToString(anyType->type)
697 698 699 700 701 702 703 704
                       : anyType->other);
        return -1;
    }

    return 0;
}

int
705
esxVI_AnyType_DeepCopy(esxVI_AnyType **dest, esxVI_AnyType *src)
706 707
{
    if (dest == NULL || *dest != NULL) {
708
        ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
709 710 711 712 713 714 715
        return -1;
    }

    if (src == NULL) {
        return 0;
    }

716
    if (esxVI_AnyType_Alloc(dest) < 0) {
717 718 719
        goto failure;
    }

720 721
    (*dest)->_type = src->_type;
    (*dest)->node = xmlCopyNode(src->node, 1);
722

723
    if ((*dest)->node == NULL) {
724 725
        ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
                     _("Could not copy an XML node"));
726 727 728 729 730
        goto failure;
    }

    (*dest)->type = src->type;

731 732
    if (esxVI_String_DeepCopyValue(&(*dest)->other, src->other) < 0 ||
        esxVI_String_DeepCopyValue(&(*dest)->value, src->value) < 0) {
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
        goto failure;
    }

    switch (src->type) {
      case esxVI_Type_Boolean:
        (*dest)->boolean = src->boolean;
        break;

      case esxVI_Type_String:
        (*dest)->string = (*dest)->value;
        break;

      case esxVI_Type_Short:
        (*dest)->int16 = src->int16;
        break;

      case esxVI_Type_Int:
        (*dest)->int32 = src->int32;
        break;

      case esxVI_Type_Long:
        (*dest)->int64 = src->int64;
        break;

      default:
        break;
    }

    return 0;

  failure:
    esxVI_AnyType_Free(dest);

    return -1;
}

int
770
esxVI_AnyType_Deserialize(xmlNodePtr node, esxVI_AnyType **anyType)
771
{
772
    long long int number;
773 774

    if (anyType == NULL || *anyType != NULL) {
775
        ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
776 777 778
        return -1;
    }

779
    if (esxVI_AnyType_Alloc(anyType) < 0) {
780 781 782
        return -1;
    }

783
    (*anyType)->node = xmlCopyNode(node, 1);
784

785
    if ((*anyType)->node == NULL) {
786 787
        ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
                     _("Could not copy an XML node"));
788 789 790 791 792 793 794 795 796
        goto failure;
    }

    (*anyType)->other =
      (char *)xmlGetNsProp
                (node, BAD_CAST "type",
                 BAD_CAST "http://www.w3.org/2001/XMLSchema-instance");

    if ((*anyType)->other == NULL) {
797 798
        ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
                     _("AnyType is missing 'type' property"));
799 800 801
        goto failure;
    }

802 803 804 805
    (*anyType)->type = esxVI_Type_FromString((*anyType)->other);

    if ((*anyType)->type == esxVI_Type_Undefined) {
        ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,
806
                     _("Unknown value '%s' for AnyType 'type' property"),
807 808 809 810
                     (*anyType)->other);
        goto failure;
    }

811
    (*anyType)->value =
812
      (char *)xmlNodeListGetString(node->doc, node->children, 1);
813 814 815 816 817

    if ((*anyType)->value == NULL) {
        (*anyType)->value = strdup("");

        if ((*anyType)->value == NULL) {
818
            virReportOOMError();
819 820 821 822
            goto failure;
        }
    }

823
#define _DESERIALIZE_NUMBER(_type, _xsdType, _name, _min, _max)               \
824 825
        do {                                                                  \
            if (virStrToLong_ll((*anyType)->value, NULL, 10, &number) < 0) {  \
826
                ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,                          \
827 828
                             _("Unknown value '%s' for %s"),                  \
                             (*anyType)->value, _xsdType);                    \
829 830 831 832
                goto failure;                                                 \
            }                                                                 \
                                                                              \
            if (number < (_min) || number > (_max)) {                         \
833
                ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,                          \
834 835
                             _("Value '%s' is out of %s range"),              \
                             (*anyType)->value, _xsdType);                    \
836 837 838 839 840 841
                goto failure;                                                 \
            }                                                                 \
                                                                              \
            (*anyType)->_name = number;                                       \
        } while (0)

842 843
    switch ((*anyType)->type) {
      case esxVI_Type_Boolean:
844 845 846 847 848
        if (STREQ((*anyType)->value, "true")) {
            (*anyType)->boolean = esxVI_Boolean_True;
        } else if (STREQ((*anyType)->value, "false")) {
            (*anyType)->boolean = esxVI_Boolean_False;
        } else {
849
            ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,
850
                         _("Unknown value '%s' for xsd:boolean"),
851 852 853
                         (*anyType)->value);
            goto failure;
        }
854 855 856 857

        break;

      case esxVI_Type_String:
858
        (*anyType)->string = (*anyType)->value;
859 860 861
        break;

      case esxVI_Type_Short:
862
        _DESERIALIZE_NUMBER(Short, "xsd:short", int16, INT16_MIN, INT16_MAX);
863 864 865
        break;

      case esxVI_Type_Int:
866
        _DESERIALIZE_NUMBER(Int, "xsd:int", int32, INT32_MIN, INT32_MAX);
867 868 869
        break;

      case esxVI_Type_Long:
870
        _DESERIALIZE_NUMBER(Long, "xsd:long", int64, INT64_MIN, INT64_MAX);
871 872 873 874
        break;

      default:
        break;
875 876
    }

877
#undef _DESERIALIZE_NUMBER
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893

    return 0;

  failure:
    esxVI_AnyType_Free(anyType);

    return -1;
}



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * XSD: String
 */

/* esxVI_String_Alloc */
894
ESX_VI__TEMPLATE__ALLOC(String)
895 896 897 898 899 900 901

/* esxVI_String_Free */
ESX_VI__TEMPLATE__FREE(String,
{
    esxVI_String_Free(&item->_next);

    VIR_FREE(item->value);
902
})
903

904 905 906 907 908 909
/* esxVI_String_Validate */
ESX_VI__TEMPLATE__VALIDATE(String,
{
    ESX_VI__TEMPLATE__PROPERTY__REQUIRE(value)
})

910
/* esxVI_String_AppendToList */
911
ESX_VI__TEMPLATE__LIST__APPEND(String)
912 913

int
914
esxVI_String_AppendValueToList(esxVI_String **stringList, const char *value)
915 916 917
{
    esxVI_String *string = NULL;

918
    if (esxVI_String_Alloc(&string) < 0) {
919 920 921 922 923 924
        goto failure;
    }

    string->value = strdup(value);

    if (string->value == NULL) {
925
        virReportOOMError();
926 927 928
        goto failure;
    }

929
    if (esxVI_String_AppendToList(stringList, string) < 0) {
930 931 932 933 934 935 936 937 938 939 940 941
        goto failure;
    }

    return 0;

  failure:
    esxVI_String_Free(&string);

    return -1;
}

int
942
esxVI_String_AppendValueListToList(esxVI_String **stringList,
943 944 945 946 947 948
                                   const char *valueList)
{
    esxVI_String *stringListToAppend = NULL;
    const char *value = valueList;

    while (value != NULL && *value != '\0') {
949
        if (esxVI_String_AppendValueToList(&stringListToAppend, value) < 0) {
950 951 952 953 954 955
            goto failure;
        }

        value += strlen(value) + 1;
    }

956
    if (esxVI_String_AppendToList(stringList, stringListToAppend) < 0) {
957 958 959 960 961 962 963 964 965 966 967
        goto failure;
    }

    return 0;

  failure:
    esxVI_String_Free(&stringListToAppend);

    return -1;
}

968 969
/* esxVI_String_DeepCopy */
ESX_VI__TEMPLATE__DEEP_COPY(String,
970
{
971 972
    ESX_VI__TEMPLATE__PROPERTY__DEEP_COPY_VALUE(String, value)
})
973 974

/* esxVI_String_DeepCopyList */
975
ESX_VI__TEMPLATE__LIST__DEEP_COPY(String)
976 977

int
978
esxVI_String_DeepCopyValue(char **dest, const char *src)
979 980
{
    if (dest == NULL || *dest != NULL) {
981
        ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
982 983 984 985 986 987 988 989 990 991
        return -1;
    }

    if (src == NULL) {
        return 0;
    }

    *dest = strdup(src);

    if (*dest == NULL) {
992
        virReportOOMError();
993 994 995 996 997 998 999
        return -1;
    }

    return 0;
}

int
1000
esxVI_String_Serialize(esxVI_String *string, const char *element,
1001
                       virBufferPtr output)
1002
{
1003
    return esxVI_String_SerializeValue(string != NULL ? string->value : NULL,
1004
                                       element, output);
1005 1006 1007
}

/* esxVI_String_SerializeList */
1008
ESX_VI__TEMPLATE__LIST__SERIALIZE(String)
1009 1010

int
1011
esxVI_String_SerializeValue(const char *value, const char *element,
1012
                            virBufferPtr output)
1013 1014
{
    if (element == NULL || output == NULL) {
1015
        ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1016 1017 1018 1019
        return -1;
    }

    if (value == NULL) {
1020
        return 0;
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
    }

    ESV_VI__XML_TAG__OPEN(output, element, "xsd:string");

    virBufferAdd(output, value, -1);

    ESV_VI__XML_TAG__CLOSE(output, element);

    return 0;
}

1032 1033
/* esxVI_String_Deserialize */
ESX_VI__TEMPLATE__DESERIALIZE(String,
1034
{
1035 1036
    ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE_VALUE(String, value)
})
1037 1038

/* esxVI_String_DeserializeList */
1039
ESX_VI__TEMPLATE__LIST__DESERIALIZE(String)
1040 1041

int
1042
esxVI_String_DeserializeValue(xmlNodePtr node, char **value)
1043 1044
{
    if (value == NULL || *value != NULL) {
1045
        ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1046 1047 1048
        return -1;
    }

1049
    *value = (char *)xmlNodeListGetString(node->doc, node->children, 1);
1050 1051 1052 1053 1054

    if (*value == NULL) {
        *value = strdup("");

        if (*value == NULL) {
1055
            virReportOOMError();
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
            return -1;
        }
    }

    return 0;
}



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * XSD: Int
 */

/* esxVI_Int_Alloc */
1070
ESX_VI__TEMPLATE__ALLOC(Int)
1071 1072 1073 1074 1075

/* esxVI_Int_Free */
ESX_VI__TEMPLATE__FREE(Int,
{
    esxVI_Int_Free(&item->_next);
1076
})
1077

1078
/* esxVI_Int_Validate */
1079 1080 1081
ESX_VI__TEMPLATE__VALIDATE(Int,
{
})
1082

1083
/* esxVI_Int_AppendToList */
1084
ESX_VI__TEMPLATE__LIST__APPEND(Int)
1085

1086 1087
/* esxVI_Int_DeepCopy */
ESX_VI__TEMPLATE__DEEP_COPY(Int,
1088 1089
{
    (*dest)->value = src->value;
1090
})
1091 1092

/* esxVI_Int_Serialize */
1093
ESX_VI__TEMPLATE__SERIALIZE(Int,
1094
{
1095
    virBufferVSprintf(output, "%d", (int)item->value);
1096
})
1097 1098

/* esxVI_Int_SerializeList */
1099
ESX_VI__TEMPLATE__LIST__SERIALIZE(Int)
1100 1101

/* esxVI_Int_Deserialize */
1102
ESX_VI__TEMPLATE__DESERIALIZE_NUMBER(Int, "xsd:int", INT32_MIN, INT32_MAX)
1103 1104 1105 1106 1107 1108 1109 1110



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * XSD: Long
 */

/* esxVI_Long_Alloc */
1111
ESX_VI__TEMPLATE__ALLOC(Long)
1112 1113 1114 1115 1116

/* esxVI_Long_Free */
ESX_VI__TEMPLATE__FREE(Long,
{
    esxVI_Long_Free(&item->_next);
1117
})
1118

1119
/* esxVI_Long_Validate */
1120 1121 1122
ESX_VI__TEMPLATE__VALIDATE(Long,
{
})
1123

1124
/* esxVI_Long_AppendToList */
1125
ESX_VI__TEMPLATE__LIST__APPEND(Long)
1126 1127

/* esxVI_Long_Serialize */
1128
ESX_VI__TEMPLATE__SERIALIZE(Long,
1129
{
1130
    virBufferVSprintf(output, "%lld", (long long int)item->value);
1131
})
1132 1133

/* esxVI_Long_SerializeList */
1134
ESX_VI__TEMPLATE__LIST__SERIALIZE(Long)
1135 1136

/* esxVI_Long_Deserialize */
1137
ESX_VI__TEMPLATE__DESERIALIZE_NUMBER(Long, "xsd:long", INT64_MIN, INT64_MAX)
1138 1139 1140 1141 1142 1143 1144 1145



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * XSD: DateTime
 */

/* esxVI_DateTime_Alloc */
1146
ESX_VI__TEMPLATE__ALLOC(DateTime)
1147 1148 1149 1150 1151

/* esxVI_DateTime_Free */
ESX_VI__TEMPLATE__FREE(DateTime,
{
    VIR_FREE(item->value);
1152
})
1153

1154 1155 1156 1157
/* esxVI_DateTime_Validate */
ESX_VI__TEMPLATE__VALIDATE(DateTime,
{
    ESX_VI__TEMPLATE__PROPERTY__REQUIRE(value);
1158
})
1159

1160 1161 1162 1163 1164 1165
/* esxVI_DateTime_DeepCopy */
ESX_VI__TEMPLATE__DEEP_COPY(DateTime,
{
    ESX_VI__TEMPLATE__PROPERTY__DEEP_COPY_VALUE(String, value)
})

1166
/* esxVI_DateTime_Serialize */
1167
ESX_VI__TEMPLATE__SERIALIZE(DateTime,
1168 1169
{
    virBufferAdd(output, item->value, -1);
1170
})
1171 1172

int
1173
esxVI_DateTime_Deserialize(xmlNodePtr node, esxVI_DateTime **dateTime)
1174 1175
{
    if (dateTime == NULL || *dateTime != NULL) {
1176
        ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1177 1178 1179
        return -1;
    }

1180
    if (esxVI_DateTime_Alloc(dateTime) < 0) {
1181 1182 1183 1184
        return -1;
    }

    (*dateTime)->value =
1185
      (char *)xmlNodeListGetString(node->doc, node->children, 1);
1186 1187

    if ((*dateTime)->value == NULL) {
1188 1189 1190
        ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
                     _("XML node doesn't contain text, expecting an "
                       "xsd:dateTime value"));
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
        goto failure;
    }

    return 0;

  failure:
    esxVI_DateTime_Free(dateTime);

    return -1;
}

1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
int
esxVI_DateTime_ConvertToCalendarTime(esxVI_DateTime *dateTime,
                                     time_t *secondsSinceEpoch)
{
    char value[64] = "";
    char *tmp;
    struct tm tm;
    int milliseconds;
    char sign;
    int tz_hours;
    int tz_minutes;
    int tz_offset = 0;

    if (dateTime == NULL || secondsSinceEpoch == NULL) {
        ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
        return -1;
    }

    if (virStrcpyStatic(value, dateTime->value) == NULL) {
        ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,
                     _("xsd:dateTime value '%s' too long for destination"),
                     dateTime->value);
        return -1;
    }

    /*
     * expected format: [-]CCYY-MM-DDTHH:MM:SS[.ssssss][((+|-)HH:MM|Z)]
     * typical example: 2010-04-05T12:13:55.316789+02:00
     *
     * see http://www.w3.org/TR/xmlschema-2/#dateTime
     *
     * map negative years to 0, since the base for time_t is the year 1970.
     */
    if (*value == '-') {
        *secondsSinceEpoch = 0;
        return 0;
    }

    tmp = strptime(value, "%Y-%m-%dT%H:%M:%S", &tm);

    if (tmp == NULL) {
        ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,
                     _("xsd:dateTime value '%s' has unexpected format"),
                     dateTime->value);
        return -1;
    }

    if (*tmp != '\0') {
        /* skip .ssssss part if present */
        if (*tmp == '.' &&
            virStrToLong_i(tmp + 1, &tmp, 10, &milliseconds) < 0) {
            ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,
                         _("xsd:dateTime value '%s' has unexpected format"),
                         dateTime->value);
            return -1;
        }

        /* parse timezone offset if present. if missing assume UTC */
        if (*tmp == '+' || *tmp == '-') {
            sign = *tmp;

            if (virStrToLong_i(tmp + 1, &tmp, 10, &tz_hours) < 0 ||
                *tmp != ':' ||
                virStrToLong_i(tmp + 1, NULL, 10, &tz_minutes) < 0) {
                ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,
                             _("xsd:dateTime value '%s' has unexpected format"),
                             dateTime->value);
                return -1;
            }

            tz_offset = tz_hours * 60 * 60 + tz_minutes * 60;

            if (sign == '-') {
                tz_offset = -tz_offset;
            }
        } else if (STREQ(tmp, "Z")) {
            /* Z refers to UTC. tz_offset is already initialized to zero */
        } else {
            ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,
                         _("xsd:dateTime value '%s' has unexpected format"),
                         dateTime->value);
            return -1;
        }
    }

    /*
     * xsd:dateTime represents local time relative to the optional timezone
     * given as offset. pretend the local time is in UTC and use timegm in
     * order to avoid interference with the timezone to this computer.
     * apply timezone correction afterwards, because it's simpler than
     * handling all the possible over- and underflows when trying to apply
     * it to the tm struct.
     */
    *secondsSinceEpoch = timegm(&tm) - tz_offset;

    return 0;
}

1300 1301


1302
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1303
 * VI Type: Fault
1304 1305
 */

1306 1307
/* esxVI_Fault_Alloc */
ESX_VI__TEMPLATE__ALLOC(Fault);
1308

1309 1310 1311 1312 1313
/* esxVI_Fault_Free */
ESX_VI__TEMPLATE__FREE(Fault,
{
    VIR_FREE(item->faultcode);
    VIR_FREE(item->faultstring);
1314
})
1315

1316 1317 1318 1319 1320
/* esxVI_Fault_Validate */
ESX_VI__TEMPLATE__VALIDATE(Fault,
{
    ESX_VI__TEMPLATE__PROPERTY__REQUIRE(faultcode);
    ESX_VI__TEMPLATE__PROPERTY__REQUIRE(faultstring);
1321
})
1322

1323 1324 1325 1326 1327 1328
/* esxVI_Fault_Deserialize */
ESX_VI__TEMPLATE__DESERIALIZE(Fault,
{
    ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE_VALUE(String, faultcode);
    ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE_VALUE(String, faultstring);
    ESX_VI__TEMPLATE__PROPERTY__DESERIALIZE_IGNORE(detail); /* FIXME */
1329
})
1330 1331 1332 1333



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1334
 * VI Type: ManagedObjectReference
1335 1336
 */

1337
/* esxVI_ManagedObjectReference_Alloc */
1338
ESX_VI__TEMPLATE__ALLOC(ManagedObjectReference)
1339

1340 1341 1342 1343
/* esxVI_ManagedObjectReference_Free */
ESX_VI__TEMPLATE__FREE(ManagedObjectReference,
{
    esxVI_ManagedObjectReference_Free(&item->_next);
1344

1345 1346
    VIR_FREE(item->type);
    VIR_FREE(item->value);
1347
})
1348

1349 1350 1351 1352 1353 1354
/* esxVI_ManagedObjectReference_DeepCopy */
ESX_VI__TEMPLATE__DEEP_COPY(ManagedObjectReference,
{
    ESX_VI__TEMPLATE__PROPERTY__DEEP_COPY_VALUE(String, type)
    ESX_VI__TEMPLATE__PROPERTY__DEEP_COPY_VALUE(String, value)
})
1355

1356
/* esxVI_ManagedObjectReference_AppendToList */
1357
ESX_VI__TEMPLATE__LIST__APPEND(ManagedObjectReference)
1358

1359 1360
/* esxVI_ManagedObjectReference_CastFromAnyType */
ESX_VI__TEMPLATE__CAST_FROM_ANY_TYPE(ManagedObjectReference)
1361

1362 1363
/* esxVI_ManagedObjectReference_CastListFromAnyType */
ESX_VI__TEMPLATE__LIST__CAST_FROM_ANY_TYPE(ManagedObjectReference)
1364

1365 1366
int
esxVI_ManagedObjectReference_Serialize
1367
  (esxVI_ManagedObjectReference *managedObjectReference,
1368
   const char *element, virBufferPtr output)
1369 1370
{
    if (element == NULL || output == NULL) {
1371
        ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1372 1373 1374 1375
        return -1;
    }

    if (managedObjectReference == NULL) {
1376
        return 0;
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
    }

    virBufferAddLit(output, "<");
    virBufferAdd(output, element, -1);
    virBufferVSprintf(output,
                      " xmlns=\"urn:vim25\" "
                      "xsi:type=\"ManagedObjectReference\" type=\"%s\">",
                      managedObjectReference->type);

    virBufferAdd(output, managedObjectReference->value, -1);

    ESV_VI__XML_TAG__CLOSE(output, element);

    return 0;
}

/* esxVI_ManagedObjectReference_SerializeList */
1394
ESX_VI__TEMPLATE__LIST__SERIALIZE(ManagedObjectReference)
1395 1396 1397

int
esxVI_ManagedObjectReference_Deserialize
1398
  (xmlNodePtr node, esxVI_ManagedObjectReference **managedObjectReference)
1399 1400
{
    if (managedObjectReference == NULL || *managedObjectReference != NULL) {
1401
        ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1402 1403 1404
        return -1;
    }

1405
    if (esxVI_ManagedObjectReference_Alloc(managedObjectReference) < 0) {
1406 1407 1408 1409 1410 1411 1412
        return -1;
    }

    (*managedObjectReference)->type =
      (char *)xmlGetNoNsProp(node, BAD_CAST "type");

    if ((*managedObjectReference)->type == NULL) {
1413 1414
        ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
                     _("ManagedObjectReference is missing 'type' property"));
1415 1416 1417
        goto failure;
    }

1418
    if (esxVI_String_DeserializeValue(node,
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
                                      &(*managedObjectReference)->value) < 0) {
        goto failure;
    }

    return 0;

  failure:
    esxVI_ManagedObjectReference_Free(managedObjectReference);

    return -1;
}

1431 1432


1433
#include "esx_vi_types.generated.c"
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * VI Enum: VirtualMachinePowerState (Additions)
 */

int
esxVI_VirtualMachinePowerState_ConvertToLibvirt
  (esxVI_VirtualMachinePowerState powerState)
{
    switch (powerState) {
      case esxVI_VirtualMachinePowerState_PoweredOff:
        return VIR_DOMAIN_SHUTOFF;

      case esxVI_VirtualMachinePowerState_PoweredOn:
        return VIR_DOMAIN_RUNNING;

      case esxVI_VirtualMachinePowerState_Suspended:
        return VIR_DOMAIN_PAUSED;

      default:
        return VIR_DOMAIN_NOSTATE;
    }
}