github.com/vmware/govmomi@v0.43.0/vapi/library/example_test.go (about) 1 /* 2 Copyright (c) 2020 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_test 18 19 import ( 20 "context" 21 "fmt" 22 23 "github.com/vmware/govmomi/find" 24 "github.com/vmware/govmomi/simulator" 25 "github.com/vmware/govmomi/vapi/library" 26 "github.com/vmware/govmomi/vapi/rest" 27 "github.com/vmware/govmomi/vim25" 28 29 _ "github.com/vmware/govmomi/vapi/simulator" 30 ) 31 32 func ExampleManager_CreateLibrary() { 33 simulator.Run(func(ctx context.Context, vc *vim25.Client) error { 34 c := rest.NewClient(vc) 35 36 err := c.Login(ctx, simulator.DefaultLogin) 37 if err != nil { 38 return err 39 } 40 41 ds, err := find.NewFinder(vc).DefaultDatastore(ctx) 42 if err != nil { 43 return err 44 } 45 46 m := library.NewManager(c) 47 48 id, err := m.CreateLibrary(ctx, library.Library{ 49 Name: "example", 50 Type: "LOCAL", 51 Storage: []library.StorageBacking{{ 52 DatastoreID: ds.Reference().Value, 53 Type: "DATASTORE", 54 }}, 55 }) 56 if err != nil { 57 return err 58 } 59 60 l, err := m.GetLibraryByID(ctx, id) 61 if err != nil { 62 return err 63 } 64 65 fmt.Println("created library", l.Name) 66 return nil 67 }) 68 // Output: created library example 69 }