Skip to content

Commit

Permalink
Merge pull request #3246 from dougm/issue-3245
Browse files Browse the repository at this point in the history
fix: case insensitive govc import.ova PropertyMapping
  • Loading branch information
dougm authored Sep 27, 2023
2 parents 23cd318 + bed9513 commit 8f49f6c
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 6 deletions.
35 changes: 33 additions & 2 deletions govc/importx/options.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2015 VMware, Inc. All Rights Reserved.
Copyright (c) 2015-2023 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,8 +29,39 @@ import (
"github.com/vmware/govmomi/vim25/types"
)

type KeyValue struct {
Key string
Value string
}

// case insensitive for Key + Value
func (kv *KeyValue) UnmarshalJSON(b []byte) error {
e := struct {
types.KeyValue
Key *string
Value *string
}{
types.KeyValue{}, &kv.Key, &kv.Value,
}

err := json.Unmarshal(b, &e)
if err != nil {
return err
}

if kv.Key == "" {
kv.Key = e.KeyValue.Key // "key"
}

if kv.Value == "" {
kv.Value = e.KeyValue.Value // "value"
}

return nil
}

type Property struct {
types.KeyValue
KeyValue
Spec *ovf.Property `json:",omitempty"`
}

Expand Down
70 changes: 70 additions & 0 deletions govc/importx/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Copyright (c) 2023-2023 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package importx_test

import (
"bytes"
"encoding/json"
"testing"

"github.com/vmware/govmomi/govc/importx"
)

func TestDecodeOptions(t *testing.T) {
spec := []byte(`{
"DiskProvisioning": "flat",
"IPAllocationPolicy": "dhcpPolicy",
"IPProtocol": "IPv4",
"PropertyMapping": [
{
"Key": "ntp_server",
"Value": "time.vmware.com"
},
{
"key": "enable_ssh",
"value": "True"
}
],
"NetworkMapping": [
{
"Name": "VM Network",
"Network": ""
}
],
"MarkAsTemplate": false,
"PowerOn": false,
"InjectOvfEnv": false,
"WaitForIP": false,
"Name": null
}
`)
var opts importx.Options
err := json.NewDecoder(bytes.NewReader(spec)).Decode(&opts)
if err != nil {
t.Fatal(err)
}

// KeyValue are case insensitive
for i, p := range opts.PropertyMapping {
if p.Key == "" {
t.Errorf("empty PropertyMapping[%d].Key", i)
}
if p.Value == "" {
t.Errorf("empty PropertyMapping[%d].Value", i)
}
}
}
7 changes: 5 additions & 2 deletions govc/importx/ovf.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2014-2015 VMware, Inc. All Rights Reserved.
Copyright (c) 2014-2023 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -161,7 +161,10 @@ func (cmd *ovfx) Prepare(f *flag.FlagSet) (string, error) {

func (cmd *ovfx) Map(op []Property) (p []types.KeyValue) {
for _, v := range op {
p = append(p, v.KeyValue)
p = append(p, types.KeyValue{
Key: v.Key,
Value: v.Value,
})
}

return
Expand Down
4 changes: 2 additions & 2 deletions govc/importx/spec.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2015-2016 VMware, Inc. All Rights Reserved.
Copyright (c) 2015-2023 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -171,7 +171,7 @@ func (cmd *spec) Map(e *ovf.Envelope) (res []Property) {
k = fmt.Sprintf("%s.%s", k, *p.Instance)
}

np := Property{KeyValue: types.KeyValue{Key: k, Value: d}}
np := Property{KeyValue: KeyValue{Key: k, Value: d}}
if cmd.Verbose() {
np.Spec = &p.Property[i]
}
Expand Down

0 comments on commit 8f49f6c

Please sign in to comment.