Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: case insensitive govc import.ova PropertyMapping #3246

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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