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

时间:2017-04-28 09:08:08

标签: rest api vmware vcenter ova

我尝试使用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。 (内容库 - &gt;选择库 - &gt;导入项目 - &gt;从本地选择.ovf文件 - &gt;(提示选择参考文件)从本地选择相关的.vmdk)

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

到目前为止我所做的步骤是:

  1. 创建图书馆
  2.   

    网址: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. 创建图书馆资料
    2.   

      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. 创建新的更新会话
      2.   

        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"
        }
        

        接下来,我尝试将文件上传到content-library项目,这就是Iam面临的问题。

        1. 上传ovf
        2. 图4(a)。请求端点上传.ovf文件

            

          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
            }
          }
          

          回应机构:

          {
            "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,并将文件作为多部分表单数据上传(我使用postman执行此操作 - 我通过Vcenter api资源管理器完成了其余的调用)

          enter image description here

          图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?

1 个答案:

答案 0 :(得分:1)

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

要实现此目的,您的数据上传PUT需要两个设置:

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