github.com/vmware/govmomi@v0.37.2/vapi/library/library_item_downloadsession_file.go (about)

     1  /*
     2  Copyright (c) 2018 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package library
    18  
    19  import (
    20  	"context"
    21  	"net/http"
    22  
    23  	"github.com/vmware/govmomi/vapi/internal"
    24  	"github.com/vmware/govmomi/vapi/rest"
    25  )
    26  
    27  // DownloadFile is the specification for the downloadsession
    28  // operations file:add, file:get, and file:list.
    29  type DownloadFile struct {
    30  	BytesTransferred int64                    `json:"bytes_transferred"`
    31  	Checksum         *Checksum                `json:"checksum_info,omitempty"`
    32  	DownloadEndpoint *TransferEndpoint        `json:"download_endpoint,omitempty"`
    33  	ErrorMessage     *rest.LocalizableMessage `json:"error_message,omitempty"`
    34  	Name             string                   `json:"name"`
    35  	Size             int64                    `json:"size,omitempty"`
    36  	Status           string                   `json:"status"`
    37  }
    38  
    39  // GetLibraryItemDownloadSessionFile retrieves information about a specific file that is a part of an download session.
    40  func (c *Manager) GetLibraryItemDownloadSessionFile(ctx context.Context, sessionID string, name string) (*DownloadFile, error) {
    41  	url := c.Resource(internal.LibraryItemDownloadSessionFile).WithID(sessionID).WithAction("get")
    42  	spec := struct {
    43  		Name string `json:"file_name"`
    44  	}{name}
    45  	var res DownloadFile
    46  	err := c.Do(ctx, url.Request(http.MethodPost, spec), &res)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	if res.Status == "ERROR" {
    51  		return nil, res.ErrorMessage
    52  	}
    53  	return &res, nil
    54  }
    55  
    56  // ListLibraryItemDownloadSessionFile retrieves information about a specific file that is a part of an download session.
    57  func (c *Manager) ListLibraryItemDownloadSessionFile(ctx context.Context, sessionID string) ([]DownloadFile, error) {
    58  	url := c.Resource(internal.LibraryItemDownloadSessionFile).WithParam("download_session_id", sessionID)
    59  	var res []DownloadFile
    60  	return res, c.Do(ctx, url.Request(http.MethodGet), &res)
    61  }
    62  
    63  // PrepareLibraryItemDownloadSessionFile retrieves information about a specific file that is a part of an download session.
    64  func (c *Manager) PrepareLibraryItemDownloadSessionFile(ctx context.Context, sessionID string, name string) (*DownloadFile, error) {
    65  	url := c.Resource(internal.LibraryItemDownloadSessionFile).WithID(sessionID).WithAction("prepare")
    66  	spec := struct {
    67  		Name string `json:"file_name"`
    68  	}{name}
    69  	var res DownloadFile
    70  	return &res, c.Do(ctx, url.Request(http.MethodPost, spec), &res)
    71  }