Skip to content

Commit 75f2762

Browse files
committed
govc: Add import.ova -net flag
Network can already be set via -options, but this simplifies the common case. Fixes #3679 Signed-off-by: Doug MacEachern <[email protected]>
1 parent f372b9f commit 75f2762

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

cli/importx/ovf.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ import (
88
"context"
99
"errors"
1010
"flag"
11+
"fmt"
1112

1213
"github.com/vmware/govmomi/cli"
1314
"github.com/vmware/govmomi/cli/flags"
15+
"github.com/vmware/govmomi/fault"
1416
"github.com/vmware/govmomi/object"
1517
"github.com/vmware/govmomi/ovf/importer"
18+
"github.com/vmware/govmomi/vim25/types"
1619
)
1720

1821
type ovfx struct {
@@ -27,6 +30,7 @@ type ovfx struct {
2730
Importer importer.Importer
2831

2932
lease bool
33+
net string // No need for *flags.NetworkFlag here
3034
}
3135

3236
func init() {
@@ -52,6 +56,7 @@ func (cmd *ovfx) Register(ctx context.Context, f *flag.FlagSet) {
5256
f.BoolVar(&cmd.Importer.VerifyManifest, "m", false, "Verify checksum of uploaded files against manifest (.mf)")
5357
f.BoolVar(&cmd.Importer.Hidden, "hidden", false, "Enable hidden properties")
5458
f.BoolVar(&cmd.lease, "lease", false, "Output NFC Lease only")
59+
f.StringVar(&cmd.net, "net", "", "Network")
5560
}
5661

5762
func (cmd *ovfx) Process(ctx context.Context) error {
@@ -91,10 +96,32 @@ func (cmd *ovfx) Run(ctx context.Context, f *flag.FlagSet) error {
9196

9297
cmd.Importer.Archive = archive
9398

94-
return cmd.Import(ctx, fpath)
99+
if err = cmd.Import(ctx, fpath); err != nil {
100+
if fault.Is(err, &types.OvfNoHostNic{}) {
101+
hint := "specify Network with '-net' or '-options'"
102+
return fmt.Errorf("%s (%s)", err.Error(), hint)
103+
}
104+
return err
105+
}
106+
return nil
95107
}
96108

97109
func (cmd *ovfx) Import(ctx context.Context, fpath string) error {
110+
if cmd.net != "" {
111+
if len(cmd.Options.NetworkMapping) == 0 {
112+
env, err := importer.Spec(fpath, cmd.Importer.Archive, false, false)
113+
if err != nil {
114+
return err
115+
}
116+
117+
cmd.Options.NetworkMapping = env.NetworkMapping
118+
}
119+
120+
for i := range cmd.Options.NetworkMapping {
121+
cmd.Options.NetworkMapping[i].Network = cmd.net
122+
}
123+
}
124+
98125
if cmd.lease {
99126
_, lease, err := cmd.Importer.ImportVApp(ctx, fpath, cmd.Options)
100127
if err != nil {

govc/USAGE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3772,6 +3772,7 @@ Options:
37723772
-lease=false Output NFC Lease only
37733773
-m=false Verify checksum of uploaded files against manifest (.mf)
37743774
-name= Name to use for new entity
3775+
-net= Network
37753776
-options= Options spec file path for VM deployment
37763777
-pool= Resource pool [GOVC_RESOURCE_POOL]
37773778
```
@@ -3789,6 +3790,7 @@ Options:
37893790
-lease=false Output NFC Lease only
37903791
-m=false Verify checksum of uploaded files against manifest (.mf)
37913792
-name= Name to use for new entity
3793+
-net= Network
37923794
-options= Options spec file path for VM deployment
37933795
-pool= Resource pool [GOVC_RESOURCE_POOL]
37943796
```

govc/test/import.bats

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ load test_helper
216216
run govc import.ovf -options - "$ovf" <<<"$options"
217217
assert_success # using raw MO id
218218
grep "invalid NetworkMapping.Name" <<<"$output"
219+
220+
run govc import.ovf -name netflag -net enoent "$ovf"
221+
assert_failure
222+
223+
run govc import.ovf -name netflag -net "VM Network" "$ovf"
224+
assert_success
219225
}
220226

221227
@test "import invalid disk provisioning" {

ovf/importer/importer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/vmware/govmomi/nfc"
1818
"github.com/vmware/govmomi/object"
1919
"github.com/vmware/govmomi/ovf"
20+
"github.com/vmware/govmomi/task"
2021
"github.com/vmware/govmomi/vapi/library"
2122
"github.com/vmware/govmomi/vim25"
2223
"github.com/vmware/govmomi/vim25/progress"
@@ -118,7 +119,7 @@ func (imp *Importer) ImportVApp(ctx context.Context, fpath string, opts Options)
118119
return nil, nil, err
119120
}
120121
if spec.Error != nil {
121-
return nil, nil, errors.New(spec.Error[0].LocalizedMessage)
122+
return nil, nil, &task.Error{LocalizedMethodFault: &spec.Error[0]}
122123
}
123124
if spec.Warning != nil {
124125
for _, w := range spec.Warning {

0 commit comments

Comments
 (0)