github.com/vmware/govmomi@v0.51.0/vapi/vm/dataset/dataset.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package dataset
     6  
     7  import (
     8  	"context"
     9  	"net/http"
    10  	"net/url"
    11  	"path"
    12  	"strconv"
    13  
    14  	"github.com/vmware/govmomi/vapi/rest"
    15  	"github.com/vmware/govmomi/vapi/vm/internal"
    16  )
    17  
    18  // Manager extends rest.Client, adding data set related methods.
    19  //
    20  // Data sets functionality was introduced in vSphere 8.0,
    21  // and requires the VM to have virtual hardware version 20 or newer.
    22  //
    23  // See the VMware Guest SDK Programming Guide for details on using data sets
    24  // from within the guest OS of a VM.
    25  //
    26  // See https://developer.vmware.com/apis/vsphere-automation/latest/vcenter/vm/data_sets/
    27  type Manager struct {
    28  	*rest.Client
    29  }
    30  
    31  // NewManager creates a new Manager instance with the given client.
    32  func NewManager(client *rest.Client) *Manager {
    33  	return &Manager{
    34  		Client: client,
    35  	}
    36  }
    37  
    38  // Access permission to the entries of a data set.
    39  type Access string
    40  
    41  const (
    42  	AccessNone      = Access("NONE")
    43  	AccessReadOnly  = Access("READ_ONLY")
    44  	AccessReadWrite = Access("READ_WRITE")
    45  )
    46  
    47  // Describes a data set to be created.
    48  type CreateSpec struct {
    49  	// Name should take the form "com.company.project" to avoid conflict with other uses.
    50  	// Must not be empty.
    51  	Name string `json:"name"`
    52  
    53  	// Description of the data set.
    54  	Description string `json:"description"`
    55  
    56  	// Host controls access to the data set entries by the ESXi host and the vCenter.
    57  	// For example, if the host access is set to NONE, the entries of this data set
    58  	// will not be accessible through the vCenter API.
    59  	// Must not be empty.
    60  	Host Access `json:"host"`
    61  
    62  	// Guest controls access to the data set entries by the guest OS of the VM (i.e. in-guest APIs).
    63  	// For example, if the guest access is set to READ_ONLY, it will be forbidden
    64  	// to create, delete, and update entries in this data set via the VMware Guest SDK.
    65  	// Must not be empty.
    66  	Guest Access `json:"guest"`
    67  
    68  	// OmitFromSnapshotAndClone controls whether the data set is included in snapshots and clones of the VM.
    69  	// When a VM is reverted to a snapshot, any data set with OmitFromSnapshotAndClone=true will be destroyed.
    70  	// Default is false.
    71  	OmitFromSnapshotAndClone *bool `json:"omit_from_snapshot_and_clone,omitempty"`
    72  }
    73  
    74  // Describes modifications to a data set.
    75  type UpdateSpec struct {
    76  	Description              *string `json:"description,omitempty"`
    77  	Host                     *Access `json:"host,omitempty"`
    78  	Guest                    *Access `json:"guest,omitempty"`
    79  	OmitFromSnapshotAndClone *bool   `json:"omit_from_snapshot_and_clone,omitempty"`
    80  }
    81  
    82  // Data set information.
    83  type Info struct {
    84  	Name                     string `json:"name"`
    85  	Description              string `json:"description"`
    86  	Host                     Access `json:"host"`
    87  	Guest                    Access `json:"guest"`
    88  	Used                     int    `json:"used"`
    89  	OmitFromSnapshotAndClone bool   `json:"omit_from_snapshot_and_clone"`
    90  }
    91  
    92  // Brief data set information.
    93  type Summary struct {
    94  	DataSet     string `json:"data_set"`
    95  	Name        string `json:"name"`
    96  	Description string `json:"description"`
    97  }
    98  
    99  const dataSetsPathField = "data-sets"
   100  
   101  func dataSetPath(vm string, dataSet string) string {
   102  	return path.Join(internal.VCenterVMPath, url.PathEscape(vm), dataSetsPathField, url.PathEscape(dataSet))
   103  }
   104  
   105  func dataSetsPath(vm string) string {
   106  	return path.Join(internal.VCenterVMPath, url.PathEscape(vm), dataSetsPathField)
   107  }
   108  
   109  const entriesPathField = "entries"
   110  
   111  func entryPath(vm string, dataSet string, key string) string {
   112  	return path.Join(internal.VCenterVMPath, url.PathEscape(vm), dataSetsPathField, url.PathEscape(dataSet), entriesPathField, url.PathEscape(key))
   113  }
   114  
   115  func entriesPath(vm string, dataSet string) string {
   116  	return path.Join(internal.VCenterVMPath, url.PathEscape(vm), dataSetsPathField, url.PathEscape(dataSet), entriesPathField)
   117  }
   118  
   119  // CreateDataSet creates a data set associated with the given virtual machine.
   120  func (c *Manager) CreateDataSet(ctx context.Context, vm string, spec *CreateSpec) (string, error) {
   121  	url := c.Resource(dataSetsPath(vm))
   122  	var res string
   123  	err := c.Do(ctx, url.Request(http.MethodPost, spec), &res)
   124  	return res, err
   125  }
   126  
   127  // DeleteDataSet deletes an existing data set from the given virtual machine.
   128  // The operation will fail if the data set is not empty.
   129  // Set the force flag to delete a non-empty data set.
   130  func (c *Manager) DeleteDataSet(ctx context.Context, vm string, dataSet string, force bool) error {
   131  	url := c.Resource(dataSetPath(vm, dataSet))
   132  	if force {
   133  		url.WithParam("force", strconv.FormatBool(force))
   134  	}
   135  	return c.Do(ctx, url.Request(http.MethodDelete), nil)
   136  }
   137  
   138  // GetDataSet retrieves information about the given data set.
   139  func (c *Manager) GetDataSet(ctx context.Context, vm string, dataSet string) (*Info, error) {
   140  	url := c.Resource(dataSetPath(vm, dataSet))
   141  	var res Info
   142  	err := c.Do(ctx, url.Request(http.MethodGet), &res)
   143  	return &res, err
   144  }
   145  
   146  // UpdateDataSet modifies the given data set.
   147  func (c *Manager) UpdateDataSet(ctx context.Context, vm string, dataSet string, spec *UpdateSpec) error {
   148  	url := c.Resource(dataSetPath(vm, dataSet))
   149  	return c.Do(ctx, url.Request(http.MethodPatch, spec), nil)
   150  }
   151  
   152  // ListDataSets returns a list of brief descriptions of the data sets on with the given virtual machine.
   153  func (c *Manager) ListDataSets(ctx context.Context, vm string) ([]Summary, error) {
   154  	url := c.Resource(dataSetsPath(vm))
   155  	var res []Summary
   156  	err := c.Do(ctx, url.Request(http.MethodGet), &res)
   157  	return res, err
   158  }
   159  
   160  // SetEntry creates or updates an entry in the given data set.
   161  // If an entry with the given key already exists, it will be overwritten.
   162  // The key can be at most 4096 bytes. The value can be at most 1MB.
   163  func (c *Manager) SetEntry(ctx context.Context, vm string, dataSet string, key string, value string) error {
   164  	url := c.Resource(entryPath(vm, dataSet, key))
   165  	return c.Do(ctx, url.Request(http.MethodPut, value), nil)
   166  }
   167  
   168  // GetEntry returns the value of the data set entry with the given key.
   169  func (c *Manager) GetEntry(ctx context.Context, vm string, dataSet string, key string) (string, error) {
   170  	url := c.Resource(entryPath(vm, dataSet, key))
   171  	var res string
   172  	err := c.Do(ctx, url.Request(http.MethodGet), &res)
   173  	return res, err
   174  }
   175  
   176  // DeleteEntry removes an existing entry from the given data set.
   177  func (c *Manager) DeleteEntry(ctx context.Context, vm string, dataSet string, key string) error {
   178  	url := c.Resource(entryPath(vm, dataSet, key))
   179  	return c.Do(ctx, url.Request(http.MethodDelete), nil)
   180  }
   181  
   182  // ListEntries returns a list of all entry keys in the given data set.
   183  func (c *Manager) ListEntries(ctx context.Context, vm string, dataSet string) ([]string, error) {
   184  	url := c.Resource(entriesPath(vm, dataSet))
   185  	var res []string
   186  	err := c.Do(ctx, url.Request(http.MethodGet), &res)
   187  	return res, err
   188  }