【问题标题】:VCenter: REST API: How to upload ova to content LibraryVCenter:REST API:如何将 ova 上传到内容库
【发布时间】:2017-09-26 08:14:00
【问题描述】:

我正在尝试使用 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(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
  }
}

响应正文:

{
  "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?

【问题讨论】:

    标签: rest api vmware vcenter ova


    【解决方案1】:

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

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

    1. 将标题Content-Type 设置为application/octet-stream
    2. 在正文下,确保为您的 (ovf) 文件选择 binary

    【讨论】:

    • 你能解释一下第二点是什么意思,jiwni可以做到吗?
    • @LearningPhase 我不太确定你在这里问什么。这是使用 Postman 和要使用的配置的 RE。如果以编程方式,设置正确的 Content-Type 很重要。
    猜你喜欢
    • 2013-12-29
    • 1970-01-01
    • 2022-10-26
    • 1970-01-01
    • 2018-08-18
    • 2012-12-04
    • 2014-03-27
    • 2021-09-04
    • 1970-01-01
    相关资源
    最近更新 更多