VCenter: REST API: how to load ova into content library

I am trying to create a virtual machine from ova using REST API. I can do it with this Rest API:

https://vcenter_ip/rest/com/vmware/vcenter/ovf/library-item/id:<library_item_id_ie_ova_item>?~action=deploy

      

To do this, I downloaded ova through the vSphere client. (Content Library -> Select Library -> Import Item -> Select .ovf file from local -> (hints for selecting link files) select dependent .vmdk from local)

But I want to upload via Rest API.

The steps I have taken so far:

  • Create library

URL: https: // vcenter_ip / rest / com / vmware / content / local-library

Method: POST
Headers:
   Key:Content-Type 
   value: application/json
Request Body:
{
    "create_spec": {
        "name": "CL1",
        "description": "CL1 Desc",
        "publish_info": {
            "authentication_method": "NONE",
            "persist_json_enabled": false,
            "published": false
        },
        "storage_backings": [
            {
                "datastore_id": "datastore-144",
                "type": "DATASTORE"
            }
        ],
        "type": "LOCAL"
    }
}

      

  1. Creating a library item

URL: https: // vcenter_ip / rest / com / vmware / content / library / item

Method: POST
Headers:
   Key:Content-Type 
   value: application/json

Request Body:
{
  "create_spec": {
    "library_id": "<library_id",
    "description": "mydesc",
    "name": "Damn Small Linux",
    "type": "ovf"
  }
}

      

  1. Create update session

URL: https: // vcenter_ip / rest / com / vmware / content / library / item / update-session

Method: POST
Headers:
   Key:Content-Type 
   value: application/json

Request Body:
{
  "create_spec":{
  "library_item_id": "<lib_item_id>"
  }
}

Response:
{
  "value": "26f143a2-fb9d-4ed9-bd5f-c7be5825ee37:ec4f8df0-249f-4887-ab84-0933f86e106c"
}

      

Next, I am trying to upload files to a content library item and this is the problem Iam faced.

  1. Download ovf

4 (a). request endpoint to download .ovf file

URL: https: // vcenter_ip / rest / com / vmware / content / library / item / updatesession / file / id: f539fb7e-af8f-4cc8-ad66-cdcbd80f5dc4% 3Aec4f8df0-249f-4887-ab84-0933f86e106c? ~ Action = add

Method: POST
Headers:
   Key:Content-Type 
   value: application/json
   Key:update_session_id    
   value: 26f143a2-fb9d-4ed9-bd5f-c7be5825ee37:ec4f8df0-249f-4887-ab84-0933f86e106c

{
  "file_spec": {
    "source_type": "PUSH",
    "name": "Damn Small Linux.ovf",
    "size": 9
  }
}

      

Response body:

{
  "value": {
    "bytes_transferred": 0,
    "upload_endpoint": {
      "uri": "https://<vcenter_ip>:443/cls/data/1a5129a1-87e8-4650-855d-9041c493c475/Damn%20Small%20Linux.ovf"
    },
    "name": "Damn Small Linux.ovf",
    "source_type": "PUSH",
    "status": "WAITING_FOR_TRANSFER"
  }
}

      

4 (b). Upload to this URI with a post request with file uploaded as plural form data (I did this with mailbox - I made the rest of the calls via Vcenter api explorer)

enter image description here

4 (c). Checking a file in an update session

URL: https://vcenter_ip/rest/com/vmware/content/library/item/updatesession/file/id:26f143a2-fb9d-4ed9-bd5f-c7be5825ee37%3Aec4f8df0-249f-4887-ab84-0933f86e106c?~action=validate
Method: POST
Headers:
   Key:Content-Type 
   value: application/json
   Key:update_session_id    
   value: 26f143a2-fb9d-4ed9-bd5f-c7be5825ee37:ec4f8df0-249f-4887-ab84-0933f86e106c

Response Body: 
{
  "value": {
    "has_errors": true,
    "invalid_files": [
      {
        "error_message": {
          "args": [
            "Damn Small Linux.ovf",
            "1:1:PARSE_ERROR: Parse error: Unexpected character '-' (code 45) in prolog; expected '<'\r\n at [row,col {unknown-source}]: [1,1].\r\n"
          ],
          "default_message": "File Damn Small Linux.ovf was considered invalid. Reason: 1:1:PARSE_ERROR: Parse error: Unexpected character '-' (code 45) in prolog; expected '<'\r\n at [row,col {unknown-source}]: [1,1].\r\n.",
          "id": "com.vmware.vdcs.cls-main.update_session_file_validation"
        },
        "name": "Damn Small Linux.ovf"
      }
    ],
    "missing_files": []
  }
}

      

But this is the same file that I downloaded when uploading via vsphere client successfully.

What is the problem? And how can I download the following .vmdk link after that?

+3


source to share


1 answer


This is an old question, but I'll answer it in case anyone comes across this:

To get this to work, two settings are needed to load the data PUT

:



  • Set headers Content-Type

    asapplication/octet-stream

  • In the body make sure you select binary for your file (ovf)
+1


source







All Articles