4

我正在尝试使用 REST API 从 ova 创建一个 VM。我可以使用这个 Rest API 来做到这一点:

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

为此,我通过 vSphere 客户端上传了 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"
    }
}
  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"
  }
}
  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"
}

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

  1. 上传ovf

4(一)。请求端点上传 .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
  }
}

回复正文:

{
  "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)。使用 put 请求上传到该 URI,并将文件上传为多部分表单数据(我使用邮递员完成了此操作 - 我通过 Vcenter api explorer 完成了其余的调用)

在此处输入图像描述

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": []
  }
}

但这与我通过 vsphere 客户端成功上传时上传的文件相同。

这里有什么问题?接下来如何上传参考 .vmdk?

4

1 回答 1

1

这是一个老问题,但如果有人遇到这个问题,我会提出答案:

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

  1. 将标题设置Content-Typeapplication/octet-stream
  2. 在正文下,确保为 (ovf) 文件选择二进制文件
于 2017-12-18T20:39:13.697 回答