github.com/vmware/govmomi@v0.51.0/vapi/library/library_item.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 library
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  	"net/http"
    11  	"time"
    12  
    13  	"github.com/vmware/govmomi/vapi/internal"
    14  )
    15  
    16  const (
    17  	ItemTypeISO  = "iso"
    18  	ItemTypeOVF  = "ovf"
    19  	ItemTypeVMTX = "vm-template"
    20  )
    21  
    22  // Item provides methods to create, read, update, delete, and enumerate library items.
    23  type Item struct {
    24  	Cached           bool       `json:"cached,omitempty"`
    25  	ContentVersion   string     `json:"content_version,omitempty"`
    26  	CreationTime     *time.Time `json:"creation_time,omitempty"`
    27  	Description      *string    `json:"description,omitempty"`
    28  	ID               string     `json:"id,omitempty"`
    29  	LastModifiedTime *time.Time `json:"last_modified_time,omitempty"`
    30  	LastSyncTime     *time.Time `json:"last_sync_time,omitempty"`
    31  	LibraryID        string     `json:"library_id,omitempty"`
    32  	MetadataVersion  string     `json:"metadata_version,omitempty"`
    33  	Name             string     `json:"name,omitempty"`
    34  	Size             int64      `json:"size,omitempty"`
    35  	SourceID         string     `json:"source_id,omitempty"`
    36  	Type             string     `json:"type,omitempty"`
    37  	Version          string     `json:"version,omitempty"`
    38  
    39  	SecurityCompliance      *bool                        `json:"security_compliance,omitempty"`
    40  	CertificateVerification *ItemCertificateVerification `json:"certificate_verification_info,omitempty"`
    41  }
    42  
    43  // ItemCertificateVerification contains the certificate verification status and item's signing certificate
    44  type ItemCertificateVerification struct {
    45  	Status    string   `json:"status"`
    46  	CertChain []string `json:"cert_chain,omitempty"`
    47  }
    48  
    49  // Patch merges updates from the given src.
    50  func (i *Item) Patch(src *Item) {
    51  	if src.Name != "" {
    52  		i.Name = src.Name
    53  	}
    54  	if src.Description != nil {
    55  		i.Description = src.Description
    56  	}
    57  	if src.Type != "" {
    58  		i.Type = src.Type
    59  	}
    60  	if src.Version != "" {
    61  		i.Version = src.Version
    62  	}
    63  }
    64  
    65  // CreateLibraryItem creates a new library item
    66  func (c *Manager) CreateLibraryItem(ctx context.Context, item Item) (string, error) {
    67  	type createItemSpec struct {
    68  		Name        string `json:"name"`
    69  		Description string `json:"description"`
    70  		LibraryID   string `json:"library_id,omitempty"`
    71  		Type        string `json:"type"`
    72  	}
    73  
    74  	description := ""
    75  	if item.Description != nil {
    76  		description = *item.Description
    77  	}
    78  	spec := struct {
    79  		Item createItemSpec `json:"create_spec"`
    80  	}{
    81  		Item: createItemSpec{
    82  			Name:        item.Name,
    83  			Description: description,
    84  			LibraryID:   item.LibraryID,
    85  			Type:        item.Type,
    86  		},
    87  	}
    88  	url := c.Resource(internal.LibraryItemPath)
    89  	var res string
    90  	return res, c.Do(ctx, url.Request(http.MethodPost, spec), &res)
    91  }
    92  
    93  // CopyLibraryItem copies a library item
    94  func (c *Manager) CopyLibraryItem(ctx context.Context, src *Item, dst Item) (string, error) {
    95  	body := struct {
    96  		Item `json:"destination_create_spec"`
    97  	}{dst}
    98  	url := c.Resource(internal.LibraryItemPath).WithID(src.ID).WithAction("copy")
    99  	var res string
   100  	return res, c.Do(ctx, url.Request(http.MethodPost, body), &res)
   101  }
   102  
   103  // SyncLibraryItem syncs a subscribed library item
   104  func (c *Manager) SyncLibraryItem(ctx context.Context, item *Item, force bool) error {
   105  	body := struct {
   106  		Force bool `json:"force_sync_content"`
   107  	}{force}
   108  	url := c.Resource(internal.SubscribedLibraryItem).WithID(item.ID).WithAction("sync")
   109  	return c.Do(ctx, url.Request(http.MethodPost, body), nil)
   110  }
   111  
   112  // PublishLibraryItem publishes a library item to specified subscriptions.
   113  // If no subscriptions are specified, then publishes the library item to all subscriptions.
   114  func (c *Manager) PublishLibraryItem(ctx context.Context, item *Item, force bool, subscriptions []string) error {
   115  	body := internal.SubscriptionItemDestinationSpec{
   116  		Force: force,
   117  	}
   118  	for i := range subscriptions {
   119  		body.Subscriptions = append(body.Subscriptions, internal.SubscriptionDestination{ID: subscriptions[i]})
   120  	}
   121  	url := c.Resource(internal.LibraryItemPath).WithID(item.ID).WithAction("publish")
   122  	return c.Do(ctx, url.Request(http.MethodPost, body), nil)
   123  }
   124  
   125  // UpdateLibraryItem can update one or both of the item Description and Name fields.
   126  func (c *Manager) UpdateLibraryItem(ctx context.Context, item *Item) error {
   127  	spec := struct {
   128  		Item `json:"update_spec"`
   129  	}{
   130  		Item{
   131  			Name:        item.Name,
   132  			Description: item.Description,
   133  		},
   134  	}
   135  	url := c.Resource(internal.LibraryItemPath).WithID(item.ID)
   136  	return c.Do(ctx, url.Request(http.MethodPatch, spec), nil)
   137  }
   138  
   139  // DeleteLibraryItem deletes an existing library item.
   140  func (c *Manager) DeleteLibraryItem(ctx context.Context, item *Item) error {
   141  	url := c.Resource(internal.LibraryItemPath).WithID(item.ID)
   142  	return c.Do(ctx, url.Request(http.MethodDelete), nil)
   143  }
   144  
   145  // ListLibraryItems returns a list of all items in a content library.
   146  func (c *Manager) ListLibraryItems(ctx context.Context, id string) ([]string, error) {
   147  	url := c.Resource(internal.LibraryItemPath).WithParam("library_id", id)
   148  	var res []string
   149  	return res, c.Do(ctx, url.Request(http.MethodGet), &res)
   150  }
   151  
   152  // GetLibraryItem returns information on a library item for the given ID.
   153  func (c *Manager) GetLibraryItem(ctx context.Context, id string) (*Item, error) {
   154  	url := c.Resource(internal.LibraryItemPath).WithID(id)
   155  	var res Item
   156  	return &res, c.Do(ctx, url.Request(http.MethodGet), &res)
   157  }
   158  
   159  // GetLibraryItems returns a list of all the library items for the specified library.
   160  func (c *Manager) GetLibraryItems(ctx context.Context, libraryID string) ([]Item, error) {
   161  	ids, err := c.ListLibraryItems(ctx, libraryID)
   162  	if err != nil {
   163  		return nil, fmt.Errorf("get library items failed for: %s", err)
   164  	}
   165  	var items []Item
   166  	for _, id := range ids {
   167  		item, err := c.GetLibraryItem(ctx, id)
   168  		if err != nil {
   169  			return nil, fmt.Errorf("get library item for %s failed for %s", id, err)
   170  		}
   171  		items = append(items, *item)
   172  	}
   173  	return items, nil
   174  }
   175  
   176  // FindItem is the search criteria for finding library items.
   177  type FindItem struct {
   178  	Cached    *bool  `json:"cached,omitempty"`
   179  	LibraryID string `json:"library_id,omitempty"`
   180  	Name      string `json:"name,omitempty"`
   181  	SourceID  string `json:"source_id,omitempty"`
   182  	Type      string `json:"type,omitempty"`
   183  }
   184  
   185  // FindLibraryItems returns the IDs of all the library items that match the
   186  // search criteria.
   187  func (c *Manager) FindLibraryItems(
   188  	ctx context.Context, search FindItem) ([]string, error) {
   189  
   190  	url := c.Resource(internal.LibraryItemPath).WithAction("find")
   191  	spec := struct {
   192  		Spec FindItem `json:"spec"`
   193  	}{search}
   194  	var res []string
   195  	return res, c.Do(ctx, url.Request(http.MethodPost, spec), &res)
   196  }
   197  
   198  // EvictSubscribedLibraryItem evicts the cached content of a library item in an on-demand subscribed library.
   199  // This operation allows the cached content of a subscribed library item to be removed to free up storage capacity.
   200  func (c *Manager) EvictSubscribedLibraryItem(ctx context.Context, item *Item) error {
   201  	path := internal.SubscribedLibraryItem
   202  	url := c.Resource(path).WithID(item.ID).WithAction("evict")
   203  	return c.Do(ctx, url.Request(http.MethodPost), nil)
   204  }