github.com/vmware/govmomi@v0.51.0/lookup/simulator/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 simulator_test 6 7 import ( 8 "context" 9 "fmt" 10 "log" 11 12 "github.com/vmware/govmomi" 13 "github.com/vmware/govmomi/lookup" 14 lsim "github.com/vmware/govmomi/lookup/simulator" 15 "github.com/vmware/govmomi/lookup/types" 16 "github.com/vmware/govmomi/simulator" 17 ) 18 19 func ExampleServiceRegistration() { 20 model := simulator.VPX() 21 22 // TODO: using simulator.Run() would be simpler, 23 // but access to lookup namespace Registry is not exported in that case. 24 // Using lookup/simulator.New() directly in this example gives us access. 25 defer model.Remove() 26 err := model.Create() 27 if err != nil { 28 log.Fatal(err) 29 } 30 31 s := model.Service.NewServer() 32 defer s.Close() 33 34 sdk := lsim.New() 35 36 model.Service.RegisterSDK(sdk) 37 38 ctx := context.Background() 39 40 vc, err := govmomi.NewClient(ctx, s.URL, true) 41 if err != nil { 42 log.Fatal(err) 43 } 44 45 // Note that ServiceRegistration.Info is generated the first time RetrieveServiceContent() 46 // is called, so we do that here before modifying the Info list. 47 c, err := lookup.NewClient(ctx, vc.Client) 48 if err != nil { 49 log.Fatal(err) 50 } 51 52 // Get a pointer to the in-memory lookup.ServiceRegistration object, which we can modify directly. 53 r := sdk.Get(*c.ServiceContent.ServiceRegistration).(*lsim.ServiceRegistration) 54 55 // Change the NodeId 56 for i := range r.Info { 57 if r.Info[i].ServiceType.Type == "vcenterserver" { 58 r.Info[i].NodeId = "example-id" 59 break 60 } 61 } 62 63 filter := &types.LookupServiceRegistrationFilter{ 64 ServiceType: &types.LookupServiceRegistrationServiceType{ 65 Product: "com.vmware.cis", 66 Type: "vcenterserver", 67 }, 68 } 69 70 info, err := c.List(ctx, filter) 71 if err != nil { 72 log.Fatal(err) 73 } 74 75 fmt.Println(info[0].NodeId) 76 77 // Output: 78 // example-id 79 }