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

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