github.com/vmware/govmomi@v0.51.0/vapi/library/example_test.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_test
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  	"net/url"
    11  	"os"
    12  	"path/filepath"
    13  
    14  	"github.com/vmware/govmomi/find"
    15  	"github.com/vmware/govmomi/simulator"
    16  	"github.com/vmware/govmomi/vapi/library"
    17  	"github.com/vmware/govmomi/vapi/rest"
    18  	"github.com/vmware/govmomi/vim25"
    19  	"github.com/vmware/govmomi/vim25/soap"
    20  	"github.com/vmware/govmomi/vim25/types"
    21  
    22  	_ "github.com/vmware/govmomi/vapi/simulator"
    23  )
    24  
    25  func ExampleManager_CreateLibrary() {
    26  	simulator.Run(func(ctx context.Context, vc *vim25.Client) error {
    27  		c := rest.NewClient(vc)
    28  
    29  		err := c.Login(ctx, simulator.DefaultLogin)
    30  		if err != nil {
    31  			return err
    32  		}
    33  
    34  		ds, err := find.NewFinder(vc).DefaultDatastore(ctx)
    35  		if err != nil {
    36  			return err
    37  		}
    38  
    39  		m := library.NewManager(c)
    40  
    41  		id, err := m.CreateLibrary(ctx, library.Library{
    42  			Name: "example",
    43  			Type: "LOCAL",
    44  			Storage: []library.StorageBacking{{
    45  				DatastoreID: ds.Reference().Value,
    46  				Type:        "DATASTORE",
    47  			}},
    48  		})
    49  		if err != nil {
    50  			return err
    51  		}
    52  
    53  		l, err := m.GetLibraryByID(ctx, id)
    54  		if err != nil {
    55  			return err
    56  		}
    57  
    58  		fmt.Println("created library", l.Name)
    59  		return nil
    60  	})
    61  	// Output: created library example
    62  }
    63  
    64  func ExampleManager_CreateLibrary_subscribed() {
    65  	simulator.Run(func(ctx context.Context, vc *vim25.Client) error {
    66  		c := rest.NewClient(vc)
    67  
    68  		err := c.Login(ctx, simulator.DefaultLogin)
    69  		if err != nil {
    70  			return err
    71  		}
    72  
    73  		ds, err := find.NewFinder(vc).DefaultDatastore(ctx)
    74  		if err != nil {
    75  			return err
    76  		}
    77  
    78  		m := library.NewManager(c)
    79  
    80  		pubLibID, err := m.CreateLibrary(ctx, library.Library{
    81  			Name: "my-pub-lib",
    82  			Type: "LOCAL",
    83  			Storage: []library.StorageBacking{
    84  				{
    85  					DatastoreID: ds.Reference().Value,
    86  					Type:        "DATASTORE",
    87  				},
    88  			},
    89  			Publication: &library.Publication{
    90  				Published: types.New(true),
    91  			},
    92  		})
    93  		if err != nil {
    94  			return err
    95  		}
    96  
    97  		pubLib, err := m.GetLibraryByID(ctx, pubLibID)
    98  		if err != nil {
    99  			return err
   100  		}
   101  
   102  		fmt.Println("created library", pubLib.Name)
   103  
   104  		// Upload an OVA.
   105  		pubItemID, err := m.CreateLibraryItem(ctx, library.Item{
   106  			Name:      "my-image",
   107  			Type:      "OVF",
   108  			LibraryID: pubLib.ID,
   109  		})
   110  		if err != nil {
   111  			return err
   112  		}
   113  
   114  		pubItem, err := m.GetLibraryItem(ctx, pubItemID)
   115  		if err != nil {
   116  			return err
   117  		}
   118  
   119  		fmt.Println("  created library item", pubItem.Name)
   120  
   121  		uploadSessionID, err := m.CreateLibraryItemUpdateSession(
   122  			ctx,
   123  			library.Session{
   124  				LibraryItemID: pubItemID,
   125  			})
   126  
   127  		uploadFn := func(path string) error {
   128  			f, err := os.Open(filepath.Clean(path))
   129  			if err != nil {
   130  				return err
   131  			}
   132  			defer f.Close()
   133  
   134  			fi, err := f.Stat()
   135  			if err != nil {
   136  				return err
   137  			}
   138  
   139  			info := library.UpdateFile{
   140  				Name:       filepath.Base(path),
   141  				SourceType: "PUSH",
   142  				Size:       fi.Size(),
   143  			}
   144  
   145  			update, err := m.AddLibraryItemFile(ctx, uploadSessionID, info)
   146  			if err != nil {
   147  				return err
   148  			}
   149  
   150  			u, err := url.Parse(update.UploadEndpoint.URI)
   151  			if err != nil {
   152  				return err
   153  			}
   154  
   155  			p := soap.DefaultUpload
   156  			p.ContentLength = info.Size
   157  
   158  			return m.Client.Upload(ctx, f, u, &p)
   159  		}
   160  
   161  		if err := uploadFn("./testdata/ttylinux-pc_i486-16.1.ova"); err != nil {
   162  			return err
   163  		}
   164  
   165  		if err := m.CompleteLibraryItemUpdateSession(
   166  			ctx, uploadSessionID); err != nil {
   167  
   168  			return err
   169  		}
   170  
   171  		pubItemStor, err := m.ListLibraryItemStorage(ctx, pubItemID)
   172  		if err != nil {
   173  			return err
   174  		}
   175  
   176  		for i := range pubItemStor {
   177  			is := pubItemStor[i]
   178  			fmt.Printf(
   179  				"    uploaded library item file %s, cached=%v, size=%d\n",
   180  				is.Name, is.Cached, is.Size)
   181  		}
   182  
   183  		// Create a subscribed library that points to the one above.
   184  		subLibID, err := m.CreateLibrary(ctx, library.Library{
   185  			Name: "my-sub-lib",
   186  			Type: "SUBSCRIBED",
   187  			Storage: []library.StorageBacking{
   188  				{
   189  					DatastoreID: ds.Reference().Value,
   190  					Type:        "DATASTORE",
   191  				},
   192  			},
   193  			Subscription: &library.Subscription{
   194  				SubscriptionURL: pubLib.Publication.PublishURL,
   195  				OnDemand:        types.New(false),
   196  			},
   197  		})
   198  		if err != nil {
   199  			return err
   200  		}
   201  
   202  		subLib, err := m.GetLibraryByID(ctx, subLibID)
   203  		if err != nil {
   204  			return err
   205  		}
   206  
   207  		fmt.Println("created library", subLib.Name)
   208  
   209  		subItemIDs, err := m.ListLibraryItems(ctx, subLibID)
   210  		if err != nil {
   211  			return err
   212  		}
   213  
   214  		for i := range subItemIDs {
   215  			subItemID := subItemIDs[i]
   216  
   217  			subItem, err := m.GetLibraryItem(ctx, subItemID)
   218  			if err != nil {
   219  				return err
   220  			}
   221  
   222  			fmt.Println("  got subscribed library item", subItem.Name)
   223  
   224  			subItemStor, err := m.ListLibraryItemStorage(ctx, subItemID)
   225  			if err != nil {
   226  				return err
   227  			}
   228  			for i := range subItemStor {
   229  				is := subItemStor[i]
   230  				fmt.Printf(
   231  					"    library item file %s, cached=%v, size=%d\n",
   232  					is.Name, is.Cached, is.Size)
   233  			}
   234  		}
   235  
   236  		return nil
   237  	})
   238  
   239  	// Output:
   240  	// created library my-pub-lib
   241  	//   created library item my-image
   242  	//     uploaded library item file ttylinux-pc_i486-16.1.ovf, cached=true, size=5005
   243  	//     uploaded library item file ttylinux-pc_i486-16.1.mf, cached=true, size=159
   244  	//     uploaded library item file ttylinux-pc_i486-16.1-disk1.vmdk, cached=true, size=1047552
   245  	// created library my-sub-lib
   246  	//   got subscribed library item my-image
   247  	//     library item file ttylinux-pc_i486-16.1.ovf, cached=true, size=5005
   248  	//     library item file ttylinux-pc_i486-16.1.mf, cached=true, size=159
   249  	//     library item file ttylinux-pc_i486-16.1-disk1.vmdk, cached=true, size=1047552
   250  }
   251  
   252  func ExampleManager_CreateLibrary_subscribed_ondemand() {
   253  	simulator.Run(func(ctx context.Context, vc *vim25.Client) error {
   254  		c := rest.NewClient(vc)
   255  
   256  		err := c.Login(ctx, simulator.DefaultLogin)
   257  		if err != nil {
   258  			return err
   259  		}
   260  
   261  		ds, err := find.NewFinder(vc).DefaultDatastore(ctx)
   262  		if err != nil {
   263  			return err
   264  		}
   265  
   266  		m := library.NewManager(c)
   267  
   268  		pubLibID, err := m.CreateLibrary(ctx, library.Library{
   269  			Name: "my-pub-lib",
   270  			Type: "LOCAL",
   271  			Storage: []library.StorageBacking{
   272  				{
   273  					DatastoreID: ds.Reference().Value,
   274  					Type:        "DATASTORE",
   275  				},
   276  			},
   277  			Publication: &library.Publication{
   278  				Published: types.New(true),
   279  			},
   280  		})
   281  		if err != nil {
   282  			return err
   283  		}
   284  
   285  		pubLib, err := m.GetLibraryByID(ctx, pubLibID)
   286  		if err != nil {
   287  			return err
   288  		}
   289  
   290  		fmt.Println("created library", pubLib.Name)
   291  
   292  		// Upload an OVA.
   293  		pubItemID, err := m.CreateLibraryItem(ctx, library.Item{
   294  			Name:      "my-image",
   295  			Type:      "OVF",
   296  			LibraryID: pubLib.ID,
   297  		})
   298  		if err != nil {
   299  			return err
   300  		}
   301  
   302  		pubItem, err := m.GetLibraryItem(ctx, pubItemID)
   303  		if err != nil {
   304  			return err
   305  		}
   306  
   307  		fmt.Println("  created library item", pubItem.Name)
   308  
   309  		uploadSessionID, err := m.CreateLibraryItemUpdateSession(
   310  			ctx,
   311  			library.Session{
   312  				LibraryItemID: pubItemID,
   313  			})
   314  
   315  		uploadFn := func(path string) error {
   316  			f, err := os.Open(filepath.Clean(path))
   317  			if err != nil {
   318  				return err
   319  			}
   320  			defer f.Close()
   321  
   322  			fi, err := f.Stat()
   323  			if err != nil {
   324  				return err
   325  			}
   326  
   327  			info := library.UpdateFile{
   328  				Name:       filepath.Base(path),
   329  				SourceType: "PUSH",
   330  				Size:       fi.Size(),
   331  			}
   332  
   333  			update, err := m.AddLibraryItemFile(ctx, uploadSessionID, info)
   334  			if err != nil {
   335  				return err
   336  			}
   337  
   338  			u, err := url.Parse(update.UploadEndpoint.URI)
   339  			if err != nil {
   340  				return err
   341  			}
   342  
   343  			p := soap.DefaultUpload
   344  			p.ContentLength = info.Size
   345  
   346  			return m.Client.Upload(ctx, f, u, &p)
   347  		}
   348  
   349  		if err := uploadFn("./testdata/ttylinux-pc_i486-16.1.ova"); err != nil {
   350  			return err
   351  		}
   352  
   353  		if err := m.CompleteLibraryItemUpdateSession(
   354  			ctx, uploadSessionID); err != nil {
   355  
   356  			return err
   357  		}
   358  
   359  		pubItemStor, err := m.ListLibraryItemStorage(ctx, pubItemID)
   360  		if err != nil {
   361  			return err
   362  		}
   363  
   364  		for i := range pubItemStor {
   365  			is := pubItemStor[i]
   366  			fmt.Printf(
   367  				"    uploaded library item file %s, cached=%v, size=%d\n",
   368  				is.Name, is.Cached, is.Size)
   369  		}
   370  
   371  		// Create a subscribed library that points to the one above.
   372  		subLibID, err := m.CreateLibrary(ctx, library.Library{
   373  			Name: "my-sub-lib",
   374  			Type: "SUBSCRIBED",
   375  			Storage: []library.StorageBacking{
   376  				{
   377  					DatastoreID: ds.Reference().Value,
   378  					Type:        "DATASTORE",
   379  				},
   380  			},
   381  			Subscription: &library.Subscription{
   382  				SubscriptionURL: pubLib.Publication.PublishURL,
   383  				OnDemand:        types.New(true),
   384  			},
   385  		})
   386  		if err != nil {
   387  			return err
   388  		}
   389  
   390  		subLib, err := m.GetLibraryByID(ctx, subLibID)
   391  		if err != nil {
   392  			return err
   393  		}
   394  
   395  		fmt.Println("created library", subLib.Name)
   396  
   397  		subItemIDs, err := m.ListLibraryItems(ctx, subLibID)
   398  		if err != nil {
   399  			return err
   400  		}
   401  
   402  		for i := range subItemIDs {
   403  			subItemID := subItemIDs[i]
   404  
   405  			subItem, err := m.GetLibraryItem(ctx, subItemID)
   406  			if err != nil {
   407  				return err
   408  			}
   409  
   410  			fmt.Println("  got subscribed library item", subItem.Name)
   411  
   412  			// List the item's storage prior to being synced.
   413  			subItemStor, err := m.ListLibraryItemStorage(ctx, subItemID)
   414  			if err != nil {
   415  				return err
   416  			}
   417  			for i := range subItemStor {
   418  				is := subItemStor[i]
   419  				fmt.Printf(
   420  					"    library item file %s, cached=%v, size=%d\n",
   421  					is.Name, is.Cached, is.Size)
   422  			}
   423  
   424  			// Synchronize the item.
   425  			if err := m.SyncLibraryItem(ctx, subItem, true); err != nil {
   426  				return err
   427  			}
   428  
   429  			fmt.Println("  sync'd (force=true) subscribed library item", subItem.Name)
   430  
   431  			// List the item's storage after being synced.
   432  			subItemStor, err = m.ListLibraryItemStorage(ctx, subItemID)
   433  			if err != nil {
   434  				return err
   435  			}
   436  			for i := range subItemStor {
   437  				is := subItemStor[i]
   438  				fmt.Printf(
   439  					"    library item file %s, cached=%v, size=%d\n",
   440  					is.Name, is.Cached, is.Size)
   441  			}
   442  		}
   443  
   444  		return nil
   445  	})
   446  
   447  	// Output:
   448  	// created library my-pub-lib
   449  	//   created library item my-image
   450  	//     uploaded library item file ttylinux-pc_i486-16.1.ovf, cached=true, size=5005
   451  	//     uploaded library item file ttylinux-pc_i486-16.1.mf, cached=true, size=159
   452  	//     uploaded library item file ttylinux-pc_i486-16.1-disk1.vmdk, cached=true, size=1047552
   453  	// created library my-sub-lib
   454  	//   got subscribed library item my-image
   455  	//     library item file ttylinux-pc_i486-16.1.ovf, cached=true, size=5005
   456  	//     library item file ttylinux-pc_i486-16.1.mf, cached=true, size=159
   457  	//     library item file ttylinux-pc_i486-16.1-disk1.vmdk, cached=false, size=0
   458  	//   sync'd (force=true) subscribed library item my-image
   459  	//     library item file ttylinux-pc_i486-16.1.ovf, cached=true, size=5005
   460  	//     library item file ttylinux-pc_i486-16.1.mf, cached=true, size=159
   461  	//     library item file ttylinux-pc_i486-16.1-disk1.vmdk, cached=true, size=1047552
   462  }