VirtualBox

Changeset 91171 in vbox


Ignore:
Timestamp:
Sep 9, 2021 1:17:57 AM (3 years ago)
Author:
vboxsync
Message:

OCI: Return VBOX_E_OBJECT_NOT_FOUND when we can't find the machine.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VBoxManage/VBoxManageCloudMachine.cpp

    r91163 r91171  
    4343static HRESULT getMachineById(ComPtr<ICloudMachine> &pMachineOut,
    4444                              const ComPtr<ICloudClient> &pClient,
    45                               const char *pcszStrId);
     45                              const char *pcszId);
     46static HRESULT getMachineByName(ComPtr<ICloudMachine> &pMachineOut,
     47                                const ComPtr<ICloudClient> &pClient,
     48                                const char *pcszName);
     49static HRESULT getMachine(ComPtr<ICloudMachine> &pMachineOut,
     50                          const ComPtr<ICloudClient> &pClient,
     51                          const char *pcszWhatever);
    4652
    4753
     
    278284getMachineById(ComPtr<ICloudMachine> &pMachineOut,
    279285               const ComPtr<ICloudClient> &pClient,
    280                const char *pcszStrId)
     286               const char *pcszId)
    281287{
    282288    HRESULT hrc;
     
    284290    ComPtr<ICloudMachine> pMachine;
    285291    CHECK_ERROR2_RET(hrc, pClient,
    286         GetCloudMachine(com::Bstr(pcszStrId).raw(),
     292        GetCloudMachine(com::Bstr(pcszId).raw(),
    287293                        pMachine.asOutParam()), hrc);
    288294
     
    298304    return S_OK;
    299305}
     306
     307
     308static HRESULT
     309getMachineByName(ComPtr<ICloudMachine> &pMachineOut,
     310                 const ComPtr<ICloudClient> &pClient,
     311                 const char *pcszName)
     312{
     313    HRESULT hrc;
     314
     315    com::SafeIfaceArray<ICloudMachine> aMachines;
     316    hrc = getMachineList(aMachines, pClient);
     317    if (FAILED(hrc))
     318        return hrc;
     319
     320    const size_t cMachines = aMachines.size();
     321    if (cMachines == 0)
     322        return VBOX_E_OBJECT_NOT_FOUND;
     323
     324    ComPtr<ICloudMachine> pMachineFound;
     325    for (size_t i = 0; i < cMachines; ++i)
     326    {
     327        const ComPtr<ICloudMachine> pMachine = aMachines[i];
     328
     329        com::Bstr bstrName;
     330        CHECK_ERROR2_RET(hrc, pMachine,
     331            COMGETTER(Name)(bstrName.asOutParam()),
     332                hrc);
     333
     334        if (!bstrName.equals(pcszName))
     335            continue;
     336
     337        if (pMachineFound.isNull())
     338        {
     339            pMachineFound = pMachine;
     340        }
     341        else
     342        {
     343            com::Bstr bstrId1, bstrId2;
     344            CHECK_ERROR2_RET(hrc, pMachineFound,
     345                COMGETTER(Id)(bstrId1.asOutParam()),
     346                    hrc);
     347            CHECK_ERROR2_RET(hrc, pMachine,
     348                COMGETTER(Id)(bstrId2.asOutParam()),
     349                    hrc);
     350
     351            RTMsgError("ambiguous name: %ls and %ls", bstrId1, bstrId2);
     352            return VBOX_E_OBJECT_NOT_FOUND;
     353        }
     354    }
     355
     356    if (pMachineFound.isNull())
     357        return VBOX_E_OBJECT_NOT_FOUND;
     358
     359    pMachineOut = pMachineFound;
     360    return S_OK;
     361}
     362
     363
     364/*
     365 * Try to find the machine refered by pcszWhatever.  If the look up by
     366 * id fails we might want to fallback to look up by name, b/c someone
     367 * might want to use a uuid as a display name of a machine.  But cloud
     368 * lookups are not fast, so that would be incurring performance
     369 * penalty for typos or for machines that are gone.  Should provide
     370 * explicit --id/--name options instead.
     371 */
     372static HRESULT
     373getMachine(ComPtr<ICloudMachine> &pMachineOut,
     374           const ComPtr<ICloudClient> &pClient,
     375           const char *pcszWhatever)
     376{
     377    ComPtr<ICloudMachine> pMachine;
     378
     379    HRESULT hrc;
     380
     381    RTUUID Uuid;
     382    int rc = RTUuidFromStr(&Uuid, pcszWhatever);
     383    if (RT_SUCCESS(rc))
     384        hrc = getMachineById(pMachine, pClient, pcszWhatever);
     385    else
     386        hrc = getMachineByName(pMachine, pClient, pcszWhatever);
     387
     388    if (FAILED(hrc))
     389        return hrc;
     390
     391    pMachineOut = pMachine;
     392    return S_OK;
     393}
     394
    300395
    301396
     
    603698    {
    604699        ComPtr<ICloudMachine> pMachine;
    605         hrc = getMachineById(pMachine, pClient, a->argv[i]);
    606         if (FAILED(hrc))
     700        hrc = getMachine(pMachine, pClient, a->argv[i]);
     701        if (hrc == VBOX_E_OBJECT_NOT_FOUND)
     702            return RTMsgErrorExit(RTEXITCODE_FAILURE,
     703                       "%s: not found", a->argv[i]);
     704        else if (FAILED(hrc))
    607705            return RTEXITCODE_FAILURE;
    608706
     
    871969
    872970    ComPtr<ICloudMachine> pMachine;
    873     hrc = getMachineById(pMachine, pClient, a->argv[iFirst]);
     971    hrc = getMachine(pMachine, pClient, a->argv[iFirst]);
    874972    if (FAILED(hrc))
    875973        return RTEXITCODE_FAILURE;
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette