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