github.com/vmware/govmomi@v0.37.2/simulator/object.go (about)

     1  /*
     2  Copyright (c) 2017-2018 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
    18  
    19  import (
    20  	"bytes"
    21  
    22  	"github.com/google/uuid"
    23  
    24  	"github.com/vmware/govmomi/vim25/methods"
    25  	"github.com/vmware/govmomi/vim25/soap"
    26  	"github.com/vmware/govmomi/vim25/types"
    27  	"github.com/vmware/govmomi/vim25/xml"
    28  )
    29  
    30  func SetCustomValue(ctx *Context, req *types.SetCustomValue) soap.HasFault {
    31  	body := &methods.SetCustomValueBody{}
    32  
    33  	cfm := Map.CustomFieldsManager()
    34  
    35  	_, field := cfm.findByNameType(req.Key, req.This.Type)
    36  	if field == nil {
    37  		body.Fault_ = Fault("", &types.InvalidArgument{InvalidProperty: "key"})
    38  		return body
    39  	}
    40  
    41  	res := cfm.SetField(ctx, &types.SetField{
    42  		This:   cfm.Reference(),
    43  		Entity: req.This,
    44  		Key:    field.Key,
    45  		Value:  req.Value,
    46  	})
    47  
    48  	if res.Fault() != nil {
    49  		body.Fault_ = res.Fault()
    50  		return body
    51  	}
    52  
    53  	body.Res = &types.SetCustomValueResponse{}
    54  	return body
    55  }
    56  
    57  // newUUID returns a stable UUID string based on input s
    58  func newUUID(s string) string {
    59  	return sha1UUID(s).String()
    60  }
    61  
    62  // sha1UUID returns a stable UUID based on input s
    63  func sha1UUID(s string) uuid.UUID {
    64  	return uuid.NewSHA1(uuid.NameSpaceOID, []byte(s))
    65  }
    66  
    67  // deepCopy uses xml encode/decode to copy src to dst
    68  func deepCopy(src, dst interface{}) {
    69  	b, err := xml.Marshal(src)
    70  	if err != nil {
    71  		panic(err)
    72  	}
    73  
    74  	dec := xml.NewDecoder(bytes.NewReader(b))
    75  	dec.TypeFunc = types.TypeFunc()
    76  	err = dec.Decode(dst)
    77  	if err != nil {
    78  		panic(err)
    79  	}
    80  }