VCenter:REST API:如何将ova上传到内容库

Dee*_*epa 5 api rest vmware ova vcenter

我试图使用REST API从ova创建虚拟机。我可以使用以下Rest API来做到这一点:

https://vcenter_ip/rest/com/vmware/vcenter/ovf/library-item/id:<library_item_id_ie_ova_item>?~action=deploy
Run Code Online (Sandbox Code Playgroud)

为此,我通过VSphere Client上传了ova。(内容库->选择库->导入项->从本地选择.ovf文件->(提示选择参考文件)从本地选择相关的.vmdk)

但是我想通过Rest API执行上传。

到目前为止,我已执行的步骤是:

  1. 创建一个图书馆

网址: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"
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. 创建一个图书馆物品

网址: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"
  }
}
Run Code Online (Sandbox Code Playgroud)
  1. 创建新的更新会话

网址: 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"
}
Run Code Online (Sandbox Code Playgroud)

接下来,我尝试将文件上传到内容库项目,这是Iam面临的问题。

  1. 上传ovf

4(a)。请求端点上载.ovf文件

网址: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
  }
}
Run Code Online (Sandbox Code Playgroud)

响应主体:

{
  "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"
  }
}
Run Code Online (Sandbox Code Playgroud)

4(b)。通过put请求将其上传到该URI,并将文件作为多部分表单数据上传(我是用邮递员完成的,其余的调用我是通过Vcenter api资源管理器完成的)

在此处输入图片说明

4(c)。在更新会话中验证文件

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": []
  }
}
Run Code Online (Sandbox Code Playgroud)

但这是我成功通过vsphere Client上传时上传的文件。

这是什么问题?以及接下来我该如何上传参考.vmdk?

NuS*_*ler 2

这是一个老问题,但我会给出答案,以防有人遇到这个问题:

要使其正常工作,数据上传中需要进行两项设置PUT

  1. 将标题设置Content-Typeapplication/octet-stream
  2. 在正文下,确保为 (ovf) 文件选择二进制文件