esx_vi_methods.c 41.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

/*
 * esx_vi_methods.c: client for the VMware VI API 2.5 to manage ESX hosts
 *
 * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
 *
 * 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>

#include "buf.h"
#include "memory.h"
#include "logging.h"
#include "uuid.h"
#include "virterror_internal.h"
#include "esx_vi_methods.h"
#include "esx_util.h"

#define VIR_FROM_THIS VIR_FROM_ESX

#define ESX_VI_ERROR(conn, code, fmt...)                                      \
    virReportErrorHelper(conn, VIR_FROM_ESX, code, __FILE__,  __FUNCTION__,   \
                         __LINE__, fmt)

#define ESX_VI__SOAP__REQUEST_HEADER                                          \
    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"                            \
    "<soapenv:Envelope "                                                      \
      "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" "          \
      "xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" "          \
      "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "              \
      "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"                       \
    "<soapenv:Body>"

#define ESX_VI__SOAP__REQUEST_FOOTER                                          \
    "</soapenv:Body>"                                                         \
    "</soapenv:Envelope>"

#define ESX_VI__SOAP__RESPONSE_XPATH(_type)                                   \
53
    "/soapenv:Envelope/soapenv:Body/vim:"_type"Response/vim:returnval"
54 55 56 57 58 59 60 61 62 63 64 65



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * VI Methods
 */

int
esxVI_RetrieveServiceContent(virConnectPtr conn, esxVI_Context *ctx,
                             esxVI_ServiceContent **serviceContent)
{
    int result = 0;
66 67 68 69 70 71 72 73 74 75
    const char *request = ESX_VI__SOAP__REQUEST_HEADER
                            "<RetrieveServiceContent xmlns=\"urn:vim25\">"
                              "<_this xmlns=\"urn:vim25\" "
                                     "xsi:type=\"ManagedObjectReference\" "
                                     "type=\"ServiceInstance\">"
                                "ServiceInstance"
                              "</_this>"
                            "</RetrieveServiceContent>"
                          ESX_VI__SOAP__REQUEST_FOOTER;
    esxVI_Response *response = NULL;
76 77 78 79 80 81

    if (serviceContent == NULL || *serviceContent != NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid argument");
        return -1;
    }

82 83 84 85
    if (esxVI_Context_Execute(conn, ctx, request,
                              ESX_VI__SOAP__RESPONSE_XPATH("RetrieveServiceContent"),
                              &response, esxVI_Boolean_False) < 0 ||
        esxVI_ServiceContent_Deserialize(conn, response->node,
86
                                         serviceContent) < 0) {
87 88 89 90
        goto failure;
    }

  cleanup:
91
    esxVI_Response_Free(&response);
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

    return result;

  failure:
    result = -1;

    goto cleanup;
}



int
esxVI_Login(virConnectPtr conn, esxVI_Context *ctx,
            const char *userName, const char *password,
            esxVI_UserSession **userSession)
{
    int result = 0;
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
110 111
    char *request = NULL;
    esxVI_Response *response = NULL;
112 113 114 115 116 117 118 119 120 121 122 123 124 125

    if (ctx->service == NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid call");
        return -1;
    }

    if (userSession == NULL || *userSession != NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid argument");
        return -1;
    }

    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_HEADER);
    virBufferAddLit(&buffer, "<Login xmlns=\"urn:vim25\">");

126
    if (esxVI_ManagedObjectReference_Serialize(conn,
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
                                               ctx->service->sessionManager,
                                               "_this", &buffer,
                                               esxVI_Boolean_True) < 0 ||
        esxVI_String_SerializeValue(conn, userName, "userName", &buffer,
                                    esxVI_Boolean_True) < 0 ||
        esxVI_String_SerializeValue(conn, password, "password", &buffer,
                                    esxVI_Boolean_True) < 0) {
        goto failure;
    }

    virBufferAddLit(&buffer, "</Login>");
    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_FOOTER);

    if (virBufferError(&buffer)) {
        virReportOOMError(conn);
        goto failure;
    }

145
    request = virBufferContentAndReset(&buffer);
146

147 148 149 150
    if (esxVI_Context_Execute(conn, ctx, request,
                              ESX_VI__SOAP__RESPONSE_XPATH("Login"),
                              &response, esxVI_Boolean_False) < 0 ||
        esxVI_UserSession_Deserialize(conn, response->node, userSession) < 0) {
151 152 153 154
        goto failure;
    }

  cleanup:
155 156
    VIR_FREE(request);
    esxVI_Response_Free(&response);
157 158 159 160

    return result;

  failure:
161
    virBufferFreeAndReset(&buffer);
162 163 164 165 166 167 168 169 170 171 172 173 174

    result = -1;

    goto cleanup;
}



int
esxVI_Logout(virConnectPtr conn, esxVI_Context *ctx)
{
    int result = 0;
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
175 176
    char *request = NULL;
    esxVI_Response *response = NULL;
177 178 179 180 181 182 183 184 185

    if (ctx->service == NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid call");
        return -1;
    }

    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_HEADER);
    virBufferAddLit(&buffer, "<Logout xmlns=\"urn:vim25\">");

186
    if (esxVI_ManagedObjectReference_Serialize(conn,
187 188 189 190 191 192 193 194 195 196 197 198 199 200
                                               ctx->service->sessionManager,
                                               "_this", &buffer,
                                               esxVI_Boolean_True) < 0) {
        goto failure;
    }

    virBufferAddLit(&buffer, "</Logout>");
    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_FOOTER);

    if (virBufferError(&buffer)) {
        virReportOOMError(conn);
        goto failure;
    }

201
    request = virBufferContentAndReset(&buffer);
202

203 204
    if (esxVI_Context_Execute(conn, ctx, request, NULL, &response,
                              esxVI_Boolean_False) < 0) {
205 206 207 208
        goto failure;
    }

  cleanup:
209 210
    VIR_FREE(request);
    esxVI_Response_Free(&response);
211 212 213 214

    return result;

  failure:
215
    virBufferFreeAndReset(&buffer);
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

    result = -1;

    goto cleanup;
}



int
esxVI_SessionIsActive(virConnectPtr conn, esxVI_Context *ctx,
                      const char *sessionID, const char *userName,
                      esxVI_Boolean *active)
{
    int result = 0;
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
231 232
    char *request = NULL;
    esxVI_Response *response = NULL;
233 234 235 236 237 238 239 240 241 242 243 244 245 246

    if (ctx->service == NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid call");
        return -1;
    }

    if (active == NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid argument");
        return -1;
    }

    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_HEADER);
    virBufferAddLit(&buffer, "<SessionIsActive xmlns=\"urn:vim25\">");

247
    if (esxVI_ManagedObjectReference_Serialize(conn,
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
                                               ctx->service->sessionManager,
                                               "_this", &buffer,
                                               esxVI_Boolean_True) < 0 ||
        esxVI_String_SerializeValue(conn, sessionID, "sessionID", &buffer,
                                    esxVI_Boolean_True) < 0 ||
        esxVI_String_SerializeValue(conn, userName, "userName", &buffer,
                                    esxVI_Boolean_True) < 0) {
        goto failure;
    }

    virBufferAddLit(&buffer, "</SessionIsActive>");
    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_FOOTER);

    if (virBufferError(&buffer)) {
        virReportOOMError(conn);
        goto failure;
    }

266
    request = virBufferContentAndReset(&buffer);
267

268 269 270 271
    if (esxVI_Context_Execute(conn, ctx, request,
                              ESX_VI__SOAP__RESPONSE_XPATH("SessionIsActive"),
                              &response, esxVI_Boolean_False) < 0 ||
        esxVI_Boolean_Deserialize(conn, response->node, active) < 0) {
272 273 274 275
        goto failure;
    }

  cleanup:
276 277
    VIR_FREE(request);
    esxVI_Response_Free(&response);
278 279 280 281

    return result;

  failure:
282
    virBufferFreeAndReset(&buffer);
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

    result = -1;

    goto cleanup;
}



int
esxVI_RetrieveProperties(virConnectPtr conn, esxVI_Context *ctx,
                         esxVI_PropertyFilterSpec *propertyFilterSpecList,
                         esxVI_ObjectContent **objectContentList)
{
    int result = 0;
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
298 299
    char *request = NULL;
    esxVI_Response *response = NULL;
300 301 302 303 304 305 306 307 308 309 310 311 312 313

    if (ctx->service == NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid call");
        return -1;
    }

    if (objectContentList == NULL || *objectContentList != NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid argument");
        return -1;
    }

    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_HEADER);
    virBufferAddLit(&buffer, "<RetrieveProperties xmlns=\"urn:vim25\">");

314
    if (esxVI_ManagedObjectReference_Serialize(conn,
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
                                               ctx->service->propertyCollector,
                                               "_this", &buffer,
                                               esxVI_Boolean_True) < 0 ||
        esxVI_PropertyFilterSpec_SerializeList(conn, propertyFilterSpecList,
                                               "specSet", &buffer,
                                               esxVI_Boolean_True) < 0) {
        goto failure;
    }

    virBufferAddLit(&buffer, "</RetrieveProperties>");
    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_FOOTER);

    if (virBufferError(&buffer)) {
        virReportOOMError(conn);
        goto failure;
    }

332
    request = virBufferContentAndReset(&buffer);
333

334 335 336 337
    if (esxVI_Context_Execute(conn, ctx, request,
                              ESX_VI__SOAP__RESPONSE_XPATH("RetrieveProperties"),
                              &response, esxVI_Boolean_True) < 0 ||
        esxVI_ObjectContent_DeserializeList(conn, response->node,
338
                                            objectContentList) < 0) {
339 340 341 342
        goto failure;
    }

  cleanup:
343 344
    VIR_FREE(request);
    esxVI_Response_Free(&response);
345 346 347 348

    return result;

  failure:
349
    virBufferFreeAndReset(&buffer);
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445

    result = -1;

    goto cleanup;
}



int
esxVI_PowerOnVM_Task(virConnectPtr conn, esxVI_Context *ctx,
                     esxVI_ManagedObjectReference *virtualMachine,
                     esxVI_ManagedObjectReference **task)
{
    return esxVI_StartSimpleVirtualMachineTask(conn, ctx, "PowerOnVM",
                                               virtualMachine, task);
}



int
esxVI_PowerOffVM_Task(virConnectPtr conn, esxVI_Context *ctx,
                      esxVI_ManagedObjectReference *virtualMachine,
                      esxVI_ManagedObjectReference **task)
{
    return esxVI_StartSimpleVirtualMachineTask(conn, ctx, "PowerOffVM",
                                               virtualMachine, task);
}



int
esxVI_SuspendVM_Task(virConnectPtr conn, esxVI_Context *ctx,
                     esxVI_ManagedObjectReference *virtualMachine,
                     esxVI_ManagedObjectReference **task)
{
    return esxVI_StartSimpleVirtualMachineTask(conn, ctx, "SuspendVM",
                                               virtualMachine, task);
}



int
esxVI_MigrateVM_Task(virConnectPtr conn, esxVI_Context *ctx,
                     esxVI_ManagedObjectReference *virtualMachine,
                     esxVI_ManagedObjectReference *resourcePool,
                     esxVI_ManagedObjectReference *hostSystem,
                     esxVI_ManagedObjectReference **task)
{
    int result = 0;
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
    char *request = NULL;

    if (task == NULL || *task != NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid argument");
        return -1;
    }

    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_HEADER);
    virBufferAddLit(&buffer, "<MigrateVM_Task xmlns=\"urn:vim25\">");

    if (esxVI_ManagedObjectReference_Serialize(conn, virtualMachine, "_this",
                                               &buffer,
                                               esxVI_Boolean_True) < 0 ||
        esxVI_ManagedObjectReference_Serialize(conn, resourcePool, "pool",
                                               &buffer,
                                               esxVI_Boolean_True) < 0 ||
        esxVI_ManagedObjectReference_Serialize(conn, hostSystem, "host",
                                               &buffer,
                                               esxVI_Boolean_True) < 0 ||
        esxVI_VirtualMachineMovePriority_Serialize
          (conn, esxVI_VirtualMachineMovePriority_DefaultPriority,
           "priority", &buffer, esxVI_Boolean_True) < 0) {
        goto failure;
    }

    virBufferAddLit(&buffer, "</MigrateVM_Task>");
    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_FOOTER);

    if (virBufferError(&buffer)) {
        virReportOOMError(conn);
        goto failure;
    }

    request = virBufferContentAndReset(&buffer);

    if (esxVI_StartVirtualMachineTask(conn, ctx, "MigrateVM", request,
                                      task) < 0) {
        goto failure;
    }

  cleanup:
    VIR_FREE(request);

    return result;

  failure:
446
    virBufferFreeAndReset(&buffer);
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492

    result = -1;

    goto cleanup;
}



int
esxVI_ReconfigVM_Task(virConnectPtr conn, esxVI_Context *ctx,
                      esxVI_ManagedObjectReference *virtualMachine,
                      esxVI_VirtualMachineConfigSpec *spec,
                      esxVI_ManagedObjectReference **task)
{
    int result = 0;
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
    char *request = NULL;

    if (task == NULL || *task != NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid argument");
        return -1;
    }

    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_HEADER);
    virBufferAddLit(&buffer, "<ReconfigVM_Task xmlns=\"urn:vim25\">");

    if (esxVI_ManagedObjectReference_Serialize(conn, virtualMachine, "_this",
                                               &buffer,
                                               esxVI_Boolean_True) < 0 ||
        esxVI_VirtualMachineConfigSpec_Serialize(conn, spec, "spec", &buffer,
                                                 esxVI_Boolean_True) < 0) {
        goto failure;
    }

    virBufferAddLit(&buffer, "</ReconfigVM_Task>");
    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_FOOTER);

    if (virBufferError(&buffer)) {
        virReportOOMError(conn);
        goto failure;
    }

    request = virBufferContentAndReset(&buffer);

    if (esxVI_StartVirtualMachineTask(conn, ctx, "ReconfigVM", request,
                                      task) < 0) {
M
Matthias Bolte 已提交
493 494 495 496 497 498 499 500 501
        goto failure;
    }

  cleanup:
    VIR_FREE(request);

    return result;

  failure:
502
    virBufferFreeAndReset(&buffer);
M
Matthias Bolte 已提交
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 548 549 550 551 552 553 554 555 556 557 558 559 560

    result = -1;

    goto cleanup;
}



int
esxVI_RegisterVM_Task(virConnectPtr conn, esxVI_Context *ctx,
                      esxVI_ManagedObjectReference *folder,
                      const char *path, const char *name,
                      esxVI_Boolean asTemplate,
                      esxVI_ManagedObjectReference *resourcePool,
                      esxVI_ManagedObjectReference *hostSystem,
                      esxVI_ManagedObjectReference **task)
{
    int result = 0;
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
    char *request = NULL;

    if (task == NULL || *task != NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid argument");
        return -1;
    }

    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_HEADER);
    virBufferAddLit(&buffer, "<RegisterVM_Task xmlns=\"urn:vim25\">");

    if (esxVI_ManagedObjectReference_Serialize(conn, folder, "_this", &buffer,
                                               esxVI_Boolean_True) < 0 ||
        esxVI_String_SerializeValue(conn, path, "path", &buffer,
                                    esxVI_Boolean_True) < 0 ||
        esxVI_String_SerializeValue(conn, name, "name", &buffer,
                                    esxVI_Boolean_False) < 0 ||
        esxVI_Boolean_Serialize(conn, asTemplate, "asTemplate", &buffer,
                                esxVI_Boolean_False) < 0 ||
        esxVI_ManagedObjectReference_Serialize(conn, resourcePool, "pool",
                                               &buffer,
                                               esxVI_Boolean_False) < 0 ||
        esxVI_ManagedObjectReference_Serialize(conn, hostSystem, "host",
                                               &buffer,
                                               esxVI_Boolean_False) < 0) {
        goto failure;
    }

    virBufferAddLit(&buffer, "</RegisterVM_Task>");
    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_FOOTER);

    if (virBufferError(&buffer)) {
        virReportOOMError(conn);
        goto failure;
    }

    request = virBufferContentAndReset(&buffer);

    if (esxVI_StartVirtualMachineTask(conn, ctx, "RegisterVM", request,
                                      task) < 0) {
561 562 563 564 565 566 567 568 569
        goto failure;
    }

  cleanup:
    VIR_FREE(request);

    return result;

  failure:
570
    virBufferFreeAndReset(&buffer);
571 572 573 574 575 576 577 578

    result = -1;

    goto cleanup;
}



579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
int
esxVI_CancelTask(virConnectPtr conn, esxVI_Context *ctx,
                 esxVI_ManagedObjectReference *task)
{
    int result = 0;
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
    char *request = NULL;
    esxVI_Response *response = NULL;

    if (ctx->service == NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid call");
        return -1;
    }

    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_HEADER);
    virBufferAddLit(&buffer, "<CancelTask xmlns=\"urn:vim25\">");

    if (esxVI_ManagedObjectReference_Serialize(conn, task, "_this", &buffer,
                                               esxVI_Boolean_True) < 0) {
        goto failure;
    }

    virBufferAddLit(&buffer, "</CancelTask>");
    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_FOOTER);

    if (virBufferError(&buffer)) {
        virReportOOMError(conn);
        goto failure;
    }

    request = virBufferContentAndReset(&buffer);

    if (esxVI_Context_Execute(conn, ctx, request, NULL, &response,
                              esxVI_Boolean_False) < 0) {
        goto failure;
    }

  cleanup:
    VIR_FREE(request);
    esxVI_Response_Free(&response);

    return result;

  failure:
    if (request == NULL) {
        request = virBufferContentAndReset(&buffer);
    }

    result = -1;

    goto cleanup;
}



634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
int
esxVI_UnregisterVM(virConnectPtr conn, esxVI_Context *ctx,
                   esxVI_ManagedObjectReference *virtualMachine)
{
    int result = 0;
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
    char *request = NULL;
    esxVI_Response *response = NULL;

    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_HEADER);
    virBufferAddLit(&buffer, "<UnregisterVM xmlns=\"urn:vim25\">");

    if (esxVI_ManagedObjectReference_Serialize(conn, virtualMachine, "_this",
                                               &buffer,
                                               esxVI_Boolean_True) < 0) {
        goto failure;
    }

    virBufferAddLit(&buffer, "</UnregisterVM>");
    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_FOOTER);

    if (virBufferError(&buffer)) {
        virReportOOMError(conn);
        goto failure;
    }

    request = virBufferContentAndReset(&buffer);

    if (esxVI_Context_Execute(conn, ctx, request, NULL, &response,
                              esxVI_Boolean_False) < 0) {
        goto failure;
    }

  cleanup:
    VIR_FREE(request);
    esxVI_Response_Free(&response);

    return result;

  failure:
674
    virBufferFreeAndReset(&buffer);
675 676 677 678 679 680 681 682

    result = -1;

    goto cleanup;
}



683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
int
esxVI_AnswerVM(virConnectPtr conn, esxVI_Context *ctx,
               esxVI_ManagedObjectReference *virtualMachine,
               const char *questionId, const char *answerChoice)
{
    int result = 0;
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
    char *request = NULL;
    esxVI_Response *response = NULL;

    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_HEADER);
    virBufferAddLit(&buffer, "<AnswerVM xmlns=\"urn:vim25\">");

    if (esxVI_ManagedObjectReference_Serialize(conn, virtualMachine, "_this",
                                               &buffer,
                                               esxVI_Boolean_True) < 0 ||
        esxVI_String_SerializeValue(conn, questionId, "questionId",
                                    &buffer, esxVI_Boolean_True) < 0 ||
        esxVI_String_SerializeValue(conn, answerChoice, "answerChoice",
                                    &buffer, esxVI_Boolean_True) < 0) {
        goto failure;
    }

    virBufferAddLit(&buffer, "</AnswerVM>");
    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_FOOTER);

    if (virBufferError(&buffer)) {
        virReportOOMError(conn);
        goto failure;
    }

    request = virBufferContentAndReset(&buffer);

    if (esxVI_Context_Execute(conn, ctx, request, NULL, &response,
                              esxVI_Boolean_False) < 0) {
        goto failure;
    }

  cleanup:
    VIR_FREE(request);
    esxVI_Response_Free(&response);

    return result;

  failure:
    if (request == NULL) {
        request = virBufferContentAndReset(&buffer);
    }

    result = -1;

    goto cleanup;
}



739 740 741 742 743 744 745 746
int
esxVI_CreateFilter(virConnectPtr conn, esxVI_Context *ctx,
                   esxVI_PropertyFilterSpec *propertyFilterSpec,
                   esxVI_Boolean partialUpdates,
                   esxVI_ManagedObjectReference **propertyFilter)
{
    int result = 0;
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
747 748
    char *request = NULL;
    esxVI_Response *response = NULL;
749 750 751 752 753 754 755 756 757 758 759 760 761 762

    if (ctx->service == NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid call");
        return -1;
    }

    if (propertyFilter == NULL || *propertyFilter != NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid argument");
        return -1;
    }

    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_HEADER);
    virBufferAddLit(&buffer, "<CreateFilter xmlns=\"urn:vim25\">");

763
    if (esxVI_ManagedObjectReference_Serialize(conn,
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
                                               ctx->service->propertyCollector,
                                               "_this", &buffer,
                                               esxVI_Boolean_True) < 0 ||
        esxVI_PropertyFilterSpec_Serialize(conn, propertyFilterSpec, "spec",
                                           &buffer, esxVI_Boolean_True) < 0 ||
        esxVI_Boolean_Serialize(conn, partialUpdates, "partialUpdates",
                                &buffer, esxVI_Boolean_True) < 0) {
        goto failure;
    }

    virBufferAddLit(&buffer, "</CreateFilter>");
    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_FOOTER);

    if (virBufferError(&buffer)) {
        virReportOOMError(conn);
        goto failure;
    }

782
    request = virBufferContentAndReset(&buffer);
783

784 785 786 787 788 789
    if (esxVI_Context_Execute(conn, ctx, request,
                              ESX_VI__SOAP__RESPONSE_XPATH("CreateFilter"),
                              &response, esxVI_Boolean_False) < 0 ||
        esxVI_ManagedObjectReference_Deserialize(conn, response->node,
                                                 propertyFilter,
                                                 "PropertyFilter") < 0) {
790 791 792 793
        goto failure;
    }

  cleanup:
794 795
    VIR_FREE(request);
    esxVI_Response_Free(&response);
796 797 798 799

    return result;

  failure:
800
    virBufferFreeAndReset(&buffer);
801 802 803 804 805 806 807 808 809 810 811 812 813 814

    result = -1;

    goto cleanup;
}



int
esxVI_DestroyPropertyFilter(virConnectPtr conn, esxVI_Context *ctx,
                            esxVI_ManagedObjectReference *propertyFilter)
{
    int result = 0;
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
815 816
    char *request = NULL;
    esxVI_Response *response = NULL;
817 818 819 820 821 822 823 824 825

    if (ctx->service == NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid call");
        return -1;
    }

    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_HEADER);
    virBufferAddLit(&buffer, "<DestroyPropertyFilter xmlns=\"urn:vim25\">");

826
    if (esxVI_ManagedObjectReference_Serialize(conn, propertyFilter, "_this",
827 828 829 830 831 832 833 834 835 836 837 838 839
                                               &buffer,
                                               esxVI_Boolean_True) < 0) {
        goto failure;
    }

    virBufferAddLit(&buffer, "</DestroyPropertyFilter>");
    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_FOOTER);

    if (virBufferError(&buffer)) {
        virReportOOMError(conn);
        goto failure;
    }

840
    request = virBufferContentAndReset(&buffer);
841

842 843
    if (esxVI_Context_Execute(conn, ctx, request, NULL, &response,
                              esxVI_Boolean_False) < 0) {
844 845 846 847
        goto failure;
    }

  cleanup:
848 849
    VIR_FREE(request);
    esxVI_Response_Free(&response);
850 851 852 853

    return result;

  failure:
854
    virBufferFreeAndReset(&buffer);
855 856 857 858 859 860 861 862 863 864 865 866 867 868

    result = -1;

    goto cleanup;
}



int
esxVI_WaitForUpdates(virConnectPtr conn, esxVI_Context *ctx,
                     const char *version, esxVI_UpdateSet **updateSet)
{
    int result = 0;
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
869 870
    char *request = NULL;
    esxVI_Response *response = NULL;
871 872 873 874 875 876 877 878 879 880 881 882 883 884

    if (ctx->service == NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid call");
        return -1;
    }

    if (updateSet == NULL || *updateSet != NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid argument");
        return -1;
    }

    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_HEADER);
    virBufferAddLit(&buffer, "<WaitForUpdates xmlns=\"urn:vim25\">");

885
    if (esxVI_ManagedObjectReference_Serialize(conn,
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
                                               ctx->service->propertyCollector,
                                               "_this", &buffer,
                                               esxVI_Boolean_True) < 0 ||
        esxVI_String_SerializeValue(conn, version, "version", &buffer,
                                    esxVI_Boolean_True) < 0) {
        goto failure;
    }

    virBufferAddLit(&buffer, "</WaitForUpdates>");
    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_FOOTER);

    if (virBufferError(&buffer)) {
        virReportOOMError(conn);
        goto failure;
    }

902
    request = virBufferContentAndReset(&buffer);
903

904 905 906 907
    if (esxVI_Context_Execute(conn, ctx, request,
                              ESX_VI__SOAP__RESPONSE_XPATH("WaitForUpdates"),
                              &response, esxVI_Boolean_False) < 0 ||
        esxVI_UpdateSet_Deserialize(conn, response->node, updateSet) < 0) {
908 909 910 911
        goto failure;
    }

  cleanup:
912 913
    VIR_FREE(request);
    esxVI_Response_Free(&response);
914 915 916 917

    return result;

  failure:
918
    virBufferFreeAndReset(&buffer);
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957

    result = -1;

    goto cleanup;
}



int
esxVI_RebootGuest(virConnectPtr conn, esxVI_Context *ctx,
                  esxVI_ManagedObjectReference *virtualMachine)
{
    return esxVI_SimpleVirtualMachineMethod(conn, ctx, "RebootGuest",
                                            virtualMachine);
}



int
esxVI_ShutdownGuest(virConnectPtr conn, esxVI_Context *ctx,
                    esxVI_ManagedObjectReference *virtualMachine)
{
    return esxVI_SimpleVirtualMachineMethod(conn, ctx, "ShutdownGuest",
                                            virtualMachine);
}



int
esxVI_ValidateMigration(virConnectPtr conn, esxVI_Context *ctx,
                        esxVI_ManagedObjectReference *virtualMachineList,
                        esxVI_VirtualMachinePowerState powerState,
                        esxVI_String *testTypeList,
                        esxVI_ManagedObjectReference *resourcePool,
                        esxVI_ManagedObjectReference *hostSystem,
                        esxVI_Event **eventList)
{
    int result = 0;
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
958 959
    char *request = NULL;
    esxVI_Response *response = NULL;
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978

    if (ctx->service == NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid call");
        return -1;
    }

    if (eventList == NULL || *eventList != NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid argument");
        return -1;
    }

    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_HEADER);
    virBufferAddLit(&buffer, "<ValidateMigration xmlns=\"urn:vim25\">"
                             "<_this xmlns=\"urn:vim25\" "
                                    "xsi:type=\"ManagedObjectReference\" "
                                    "type=\"ServiceInstance\">"
                               "ServiceInstance"
                             "</_this>");

979
    if (esxVI_ManagedObjectReference_SerializeList(conn, virtualMachineList,
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
                                                   "vm", &buffer,
                                                   esxVI_Boolean_True) < 0 ||
        esxVI_VirtualMachinePowerState_Serialize(conn, powerState, "state",
                                                 &buffer,
                                                 esxVI_Boolean_False) < 0 ||
        esxVI_String_SerializeList(conn, testTypeList, "testType", &buffer,
                                   esxVI_Boolean_False) < 0 ||
        esxVI_ManagedObjectReference_Serialize(conn, resourcePool, "pool",
                                               &buffer,
                                               esxVI_Boolean_True) < 0 ||
        esxVI_ManagedObjectReference_Serialize(conn, hostSystem, "host",
                                               &buffer,
                                               esxVI_Boolean_True) < 0) {
        goto failure;
    }

    virBufferAddLit(&buffer, "</ValidateMigration>");
    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_FOOTER);

    if (virBufferError(&buffer)) {
        virReportOOMError(conn);
        goto failure;
    }

1004
    request = virBufferContentAndReset(&buffer);
1005

1006 1007 1008 1009
    if (esxVI_Context_Execute(conn, ctx, request,
                              ESX_VI__SOAP__RESPONSE_XPATH("ValidateMigration"),
                              &response, esxVI_Boolean_True) < 0 ||
        esxVI_Event_DeserializeList(conn, response->node, eventList) < 0) {
1010 1011 1012 1013
        goto failure;
    }

  cleanup:
1014 1015
    VIR_FREE(request);
    esxVI_Response_Free(&response);
1016 1017 1018 1019

    return result;

  failure:
1020
    virBufferFreeAndReset(&buffer);
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036

    result = -1;

    goto cleanup;
}



int
esxVI_FindByIp(virConnectPtr conn, esxVI_Context *ctx,
                 esxVI_ManagedObjectReference *datacenter,
                 const char *ip, esxVI_Boolean vmSearch,
                 esxVI_ManagedObjectReference **managedObjectReference)
{
    int result = 0;
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
1037 1038
    char *request = NULL;
    esxVI_Response *response = NULL;
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052

    if (ctx->service == NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid call");
        return -1;
    }

    if (managedObjectReference == NULL || *managedObjectReference != NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid argument");
        return -1;
    }

    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_HEADER);
    virBufferAddLit(&buffer, "<FindByIp xmlns=\"urn:vim25\">");

1053
    if (esxVI_ManagedObjectReference_Serialize(conn, ctx->service->searchIndex,
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
                                               "_this", &buffer,
                                               esxVI_Boolean_True) < 0 ||
        esxVI_ManagedObjectReference_Serialize(conn, datacenter,
                                               "datacenter", &buffer,
                                               esxVI_Boolean_False) < 0 ||
        esxVI_String_SerializeValue(conn, ip, "ip", &buffer,
                                    esxVI_Boolean_True) < 0 ||
        esxVI_Boolean_Serialize(conn, vmSearch, "vmSearch", &buffer,
                                esxVI_Boolean_True) < 0) {
        goto failure;
    }

    virBufferAddLit(&buffer, "</FindByIp>");
    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_FOOTER);

    if (virBufferError(&buffer)) {
        virReportOOMError(conn);
        goto failure;
    }

1074
    request = virBufferContentAndReset(&buffer);
1075

1076 1077 1078
    if (esxVI_Context_Execute(conn, ctx, request,
                              ESX_VI__SOAP__RESPONSE_XPATH("FindByIp"),
                              &response, esxVI_Boolean_False) < 0 ||
1079
        esxVI_ManagedObjectReference_Deserialize
1080
          (conn, response->node, managedObjectReference,
1081 1082 1083 1084 1085 1086
           vmSearch == esxVI_Boolean_True ? "VirtualMachine"
                                          : "HostSystem") < 0) {
        goto failure;
    }

  cleanup:
1087 1088
    VIR_FREE(request);
    esxVI_Response_Free(&response);
1089 1090 1091 1092

    return result;

  failure:
1093
    virBufferFreeAndReset(&buffer);
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110

    result = -1;

    goto cleanup;
}



int
esxVI_FindByUuid(virConnectPtr conn, esxVI_Context *ctx,
                 esxVI_ManagedObjectReference *datacenter,
                 const unsigned char *uuid, esxVI_Boolean vmSearch,
                 esxVI_ManagedObjectReference **managedObjectReference)
{
    int result = 0;
    char uuid_string[VIR_UUID_STRING_BUFLEN] = "";
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
1111 1112
    char *request = NULL;
    esxVI_Response *response = NULL;
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128

    if (ctx->service == NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid call");
        return -1;
    }

    if (managedObjectReference == NULL || *managedObjectReference != NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid argument");
        return -1;
    }

    virUUIDFormat(uuid, uuid_string);

    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_HEADER);
    virBufferAddLit(&buffer, "<FindByUuid xmlns=\"urn:vim25\">");

1129
    if (esxVI_ManagedObjectReference_Serialize(conn, ctx->service->searchIndex,
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
                                               "_this", &buffer,
                                               esxVI_Boolean_True) < 0 ||
        esxVI_ManagedObjectReference_Serialize(conn, datacenter,
                                               "datacenter", &buffer,
                                               esxVI_Boolean_False) < 0 ||
        esxVI_String_SerializeValue(conn, uuid_string, "uuid", &buffer,
                                    esxVI_Boolean_True) < 0 ||
        esxVI_Boolean_Serialize(conn, vmSearch, "vmSearch", &buffer,
                                esxVI_Boolean_True) < 0) {
        goto failure;
    }

    virBufferAddLit(&buffer, "</FindByUuid>");
    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_FOOTER);

    if (virBufferError(&buffer)) {
        virReportOOMError(conn);
        goto failure;
    }

1150
    request = virBufferContentAndReset(&buffer);
1151

M
Matthias Bolte 已提交
1152
    /* FIXME: Use esxVI_Occurrence instead of expectList */
1153 1154
    if (esxVI_Context_Execute(conn, ctx, request,
                              ESX_VI__SOAP__RESPONSE_XPATH("FindByUuid"),
1155 1156 1157 1158 1159 1160 1161 1162 1163
                              &response, esxVI_Boolean_True) < 0) {
        goto failure;
    }

    if (response->node == NULL) {
        goto cleanup;
    }

    if (esxVI_ManagedObjectReference_Deserialize
1164
          (conn, response->node, managedObjectReference,
1165 1166 1167 1168 1169 1170
           vmSearch == esxVI_Boolean_True ? "VirtualMachine"
                                          : "HostSystem") < 0) {
        goto failure;
    }

  cleanup:
1171 1172
    VIR_FREE(request);
    esxVI_Response_Free(&response);
1173 1174 1175 1176

    return result;

  failure:
1177
    virBufferFreeAndReset(&buffer);
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194

    result = -1;

    goto cleanup;
}



int
esxVI_QueryAvailablePerfMetric(virConnectPtr conn, esxVI_Context *ctx,
                               esxVI_ManagedObjectReference *entity,
                               esxVI_DateTime *beginTime,
                               esxVI_DateTime *endTime, esxVI_Int *intervalId,
                               esxVI_PerfMetricId **perfMetricIdList)
{
    int result = 0;
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
1195 1196
    char *request = NULL;
    esxVI_Response *response = NULL;
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210

    if (ctx->service == NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid call");
        return -1;
    }

    if (perfMetricIdList == NULL || *perfMetricIdList != NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid argument");
        return -1;
    }

    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_HEADER);
    virBufferAddLit(&buffer, "<QueryAvailablePerfMetric xmlns=\"urn:vim25\">");

1211
    if (esxVI_ManagedObjectReference_Serialize(conn, ctx->service->perfManager,
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
                                               "_this", &buffer,
                                               esxVI_Boolean_True) < 0 ||
        esxVI_ManagedObjectReference_Serialize(conn, entity,
                                               "entity", &buffer,
                                               esxVI_Boolean_True) < 0 ||
        esxVI_DateTime_Serialize(conn, beginTime, "beginTime", &buffer,
                                 esxVI_Boolean_False) < 0 ||
        esxVI_DateTime_Serialize(conn, endTime, "endTime", &buffer,
                                 esxVI_Boolean_False) < 0 ||
        esxVI_Int_Serialize(conn, intervalId, "intervalId", &buffer,
                            esxVI_Boolean_False) < 0) {
        goto failure;
    }

    virBufferAddLit(&buffer, "</QueryAvailablePerfMetric>");
    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_FOOTER);

    if (virBufferError(&buffer)) {
        virReportOOMError(conn);
        goto failure;
    }

1234
    request = virBufferContentAndReset(&buffer);
1235

1236 1237 1238 1239
    if (esxVI_Context_Execute(conn, ctx, request,
                              ESX_VI__SOAP__RESPONSE_XPATH("QueryAvailablePerfMetric"),
                              &response, esxVI_Boolean_True) < 0 ||
        esxVI_PerfMetricId_DeserializeList(conn, response->node,
1240
                                           perfMetricIdList) < 0) {
1241 1242 1243 1244
        goto failure;
    }

  cleanup:
1245 1246
    VIR_FREE(request);
    esxVI_Response_Free(&response);
1247 1248 1249 1250

    return result;

  failure:
1251
    virBufferFreeAndReset(&buffer);
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266

    result = -1;

    goto cleanup;
}



int
esxVI_QueryPerfCounter(virConnectPtr conn, esxVI_Context *ctx,
                       esxVI_Int *counterIdList,
                       esxVI_PerfCounterInfo **perfCounterInfoList)
{
    int result = 0;
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
1267 1268
    char *request = NULL;
    esxVI_Response *response = NULL;
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282

    if (ctx->service == NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid call");
        return -1;
    }

    if (perfCounterInfoList == NULL || *perfCounterInfoList != NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid argument");
        return -1;
    }

    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_HEADER);
    virBufferAddLit(&buffer, "<QueryPerfCounter xmlns=\"urn:vim25\">");

1283
    if (esxVI_ManagedObjectReference_Serialize(conn, ctx->service->perfManager,
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
                                               "_this", &buffer,
                                               esxVI_Boolean_True) < 0 ||
        esxVI_Int_SerializeList(conn, counterIdList, "counterId", &buffer,
                                esxVI_Boolean_True) < 0) {
        goto failure;
    }

    virBufferAddLit(&buffer, "</QueryPerfCounter>");
    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_FOOTER);

    if (virBufferError(&buffer)) {
        virReportOOMError(conn);
        goto failure;
    }

1299
    request = virBufferContentAndReset(&buffer);
1300

1301 1302 1303 1304
    if (esxVI_Context_Execute(conn, ctx, request,
                              ESX_VI__SOAP__RESPONSE_XPATH("QueryPerfCounter"),
                              &response, esxVI_Boolean_True) < 0 ||
        esxVI_PerfCounterInfo_DeserializeList(conn, response->node,
1305
                                              perfCounterInfoList) < 0) {
1306 1307 1308 1309
        goto failure;
    }

  cleanup:
1310 1311
    VIR_FREE(request);
    esxVI_Response_Free(&response);
1312 1313 1314 1315

    return result;

  failure:
1316
    virBufferFreeAndReset(&buffer);
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331

    result = -1;

    goto cleanup;
}



int
esxVI_QueryPerf(virConnectPtr conn, esxVI_Context *ctx,
                esxVI_PerfQuerySpec *querySpecList,
                esxVI_PerfEntityMetric **perfEntityMetricList)
{
    int result = 0;
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
1332 1333
    char *request = NULL;
    esxVI_Response *response = NULL;
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347

    if (ctx->service == NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid call");
        return -1;
    }

    if (perfEntityMetricList == NULL || *perfEntityMetricList != NULL) {
        ESX_VI_ERROR(conn, VIR_ERR_INTERNAL_ERROR, "Invalid argument");
        return -1;
    }

    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_HEADER);
    virBufferAddLit(&buffer, "<QueryPerf xmlns=\"urn:vim25\">");

1348
    if (esxVI_ManagedObjectReference_Serialize(conn, ctx->service->perfManager,
1349 1350 1351
                                               "_this", &buffer,
                                               esxVI_Boolean_True) < 0 ||
        esxVI_PerfQuerySpec_SerializeList(conn, querySpecList, "querySpec",
1352
                                          &buffer, esxVI_Boolean_True) < 0) {
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
        goto failure;
    }

    virBufferAddLit(&buffer, "</QueryPerf>");
    virBufferAddLit(&buffer, ESX_VI__SOAP__REQUEST_FOOTER);

    if (virBufferError(&buffer)) {
        virReportOOMError(conn);
        goto failure;
    }

1364
    request = virBufferContentAndReset(&buffer);
1365

1366 1367 1368 1369
    if (esxVI_Context_Execute(conn, ctx, request,
                              ESX_VI__SOAP__RESPONSE_XPATH("QueryPerf"),
                              &response, esxVI_Boolean_True) < 0 ||
        esxVI_PerfEntityMetric_DeserializeList(conn, response->node,
1370
                                               perfEntityMetricList) < 0) {
1371 1372 1373 1374
        goto failure;
    }

  cleanup:
1375 1376
    VIR_FREE(request);
    esxVI_Response_Free(&response);
1377 1378 1379 1380

    return result;

  failure:
1381
    virBufferFreeAndReset(&buffer);
1382 1383 1384 1385 1386

    result = -1;

    goto cleanup;
}