github.com/vmware/govmomi@v0.51.0/simulator/object.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
     6  
     7  import (
     8  	"bytes"
     9  
    10  	"github.com/vmware/govmomi/internal"
    11  	"github.com/vmware/govmomi/vim25/methods"
    12  	"github.com/vmware/govmomi/vim25/soap"
    13  	"github.com/vmware/govmomi/vim25/types"
    14  	"github.com/vmware/govmomi/vim25/xml"
    15  )
    16  
    17  func SetCustomValue(ctx *Context, req *types.SetCustomValue) soap.HasFault {
    18  	body := &methods.SetCustomValueBody{}
    19  
    20  	cfm := ctx.Map.CustomFieldsManager()
    21  
    22  	_, field := cfm.findByNameType(req.Key, req.This.Type)
    23  	if field == nil {
    24  		body.Fault_ = Fault("", &types.InvalidArgument{InvalidProperty: "key"})
    25  		return body
    26  	}
    27  
    28  	res := cfm.SetField(ctx, &types.SetField{
    29  		This:   cfm.Reference(),
    30  		Entity: req.This,
    31  		Key:    field.Key,
    32  		Value:  req.Value,
    33  	})
    34  
    35  	if res.Fault() != nil {
    36  		body.Fault_ = res.Fault()
    37  		return body
    38  	}
    39  
    40  	body.Res = &types.SetCustomValueResponse{}
    41  	return body
    42  }
    43  
    44  // newUUID returns a stable UUID string based on input s
    45  func newUUID(s string) string {
    46  	return internal.OID(s).String()
    47  }
    48  
    49  // deepCopy uses xml encode/decode to copy src to dst
    50  func deepCopy(src, dst any) {
    51  	b, err := xml.Marshal(src)
    52  	if err != nil {
    53  		panic(err)
    54  	}
    55  
    56  	dec := xml.NewDecoder(bytes.NewReader(b))
    57  	dec.TypeFunc = types.TypeFunc()
    58  	err = dec.Decode(dst)
    59  	if err != nil {
    60  		panic(err)
    61  	}
    62  }