Skip to content

Commit 55b7637

Browse files
committed
govc: add volume.extend command
Signed-off-by: Doug MacEachern <[email protected]>
1 parent c293238 commit 55b7637

File tree

4 files changed

+124
-15
lines changed

4 files changed

+124
-15
lines changed

cli/volume/extend.go

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 volume
6+
7+
import (
8+
"context"
9+
"errors"
10+
"flag"
11+
12+
"github.com/vmware/govmomi/cli"
13+
"github.com/vmware/govmomi/cli/flags"
14+
"github.com/vmware/govmomi/cns"
15+
"github.com/vmware/govmomi/cns/types"
16+
"github.com/vmware/govmomi/units"
17+
)
18+
19+
type extend struct {
20+
*flags.ClientFlag
21+
22+
size units.ByteSize
23+
}
24+
25+
func init() {
26+
cli.Register("volume.extend", &extend{})
27+
}
28+
29+
func (cmd *extend) Register(ctx context.Context, f *flag.FlagSet) {
30+
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
31+
cmd.ClientFlag.Register(ctx, f)
32+
33+
f.Var(&cmd.size, "size", "New size of new volume")
34+
}
35+
36+
func (cmd *extend) Usage() string {
37+
return "ID"
38+
}
39+
40+
func (cmd *extend) Description() string {
41+
return `Extend CNS volume.
42+
43+
Examples:
44+
govc volume.extend -size 10GB f75989dc-95b9-4db7-af96-8583f24bc59d`
45+
}
46+
47+
func (cmd *extend) Run(ctx context.Context, f *flag.FlagSet) error {
48+
if f.NArg() != 1 {
49+
return flag.ErrHelp
50+
}
51+
52+
c, err := cmd.CnsClient()
53+
if err != nil {
54+
return err
55+
}
56+
57+
spec := []types.CnsVolumeExtendSpec{{
58+
VolumeId: types.CnsVolumeId{
59+
Id: f.Arg(0),
60+
},
61+
CapacityInMb: int64(cmd.size) / units.MB,
62+
}}
63+
64+
task, err := c.ExtendVolume(ctx, spec)
65+
if err != nil {
66+
return err
67+
}
68+
69+
info, err := cns.GetTaskInfo(ctx, task)
70+
if err != nil {
71+
return err
72+
}
73+
74+
res, err := cns.GetTaskResult(ctx, info)
75+
if err != nil {
76+
return err
77+
}
78+
79+
fault := res.GetCnsVolumeOperationResult().Fault
80+
if fault != nil {
81+
return errors.New(fault.LocalizedMessage)
82+
}
83+
84+
return nil
85+
}

cns/simulator/simulator.go

+18-15
Original file line numberDiff line numberDiff line change
@@ -473,27 +473,30 @@ func (m *CnsVolumeManager) CnsDetachVolume(ctx *simulator.Context, req *cnstypes
473473
// CnsExtendVolume simulates ExtendVolume call for simulated vc
474474
func (m *CnsVolumeManager) CnsExtendVolume(ctx *simulator.Context, req *cnstypes.CnsExtendVolume) soap.HasFault {
475475
task := simulator.CreateTask(m, "CnsExtendVolume", func(task *simulator.Task) (vim25types.AnyType, vim25types.BaseMethodFault) {
476-
if len(req.ExtendSpecs) == 0 {
477-
return nil, &vim25types.InvalidArgument{InvalidProperty: "CnsExtendVolumeSpec"}
476+
if len(req.ExtendSpecs) != 1 {
477+
return nil, &vim25types.InvalidArgument{InvalidProperty: "InputSpec"} // Same as real VC, currently
478478
}
479-
operationResult := []cnstypes.BaseCnsVolumeOperationResult{}
480479

481-
for _, extendSpecs := range req.ExtendSpecs {
482-
for _, dsVolumes := range m.volumes {
483-
for id, volume := range dsVolumes {
484-
if id.Id == extendSpecs.VolumeId.Id {
485-
volume.BackingObjectDetails.GetCnsBackingObjectDetails().CapacityInMb = extendSpecs.CapacityInMb
486-
operationResult = append(operationResult, &cnstypes.CnsVolumeOperationResult{
487-
VolumeId: volume.VolumeId,
488-
})
489-
break
490-
}
491-
}
480+
found := false
481+
spec := req.ExtendSpecs[0]
482+
res := cnstypes.CnsVolumeOperationResult{
483+
VolumeId: spec.VolumeId,
484+
}
485+
486+
for _, volumes := range m.volumes {
487+
if volume, ok := volumes[spec.VolumeId]; ok {
488+
found = true
489+
volume.BackingObjectDetails.GetCnsBackingObjectDetails().CapacityInMb = spec.CapacityInMb
490+
break
492491
}
493492
}
494493

494+
if !found {
495+
res.Fault = &vim25types.LocalizedMethodFault{Fault: new(vim25types.NotFound)}
496+
}
497+
495498
return &cnstypes.CnsVolumeOperationBatchResult{
496-
VolumeResults: operationResult,
499+
VolumeResults: []cnstypes.BaseCnsVolumeOperationResult{&res},
497500
}, nil
498501
})
499502

govc/USAGE.md

+15
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ but appear via `govc $cmd -h`:
430430
- [vm.unregister](#vmunregister)
431431
- [vm.upgrade](#vmupgrade)
432432
- [vm.vnc](#vmvnc)
433+
- [volume.extend](#volumeextend)
433434
- [volume.ls](#volumels)
434435
- [volume.rm](#volumerm)
435436
- [volume.snapshot.create](#volumesnapshotcreate)
@@ -7633,6 +7634,20 @@ Options:
76337634
-port-range=5900-5999 VNC port auto-select range
76347635
```
76357636

7637+
## volume.extend
7638+
7639+
```
7640+
Usage: govc volume.extend [OPTIONS] ID
7641+
7642+
Extend CNS volume.
7643+
7644+
Examples:
7645+
govc volume.extend -size 10GB f75989dc-95b9-4db7-af96-8583f24bc59d
7646+
7647+
Options:
7648+
-size=0B New size of new volume
7649+
```
7650+
76367651
## volume.ls
76377652

76387653
```

govc/test/volume.bats

+6
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,17 @@ load test_helper
117117
run govc volume.ls "$vol"
118118
assert_success
119119

120+
run govc volume.extend -size 20M "$id"
121+
assert_success
122+
120123
run govc volume.rm "$vol"
121124
assert_success
122125

123126
run govc disk.ls "$id"
124127
assert_failure
128+
129+
run govc volume.extend -size 30M "$id"
130+
assert_failure
125131
}
126132

127133
@test "volume.rm" {

0 commit comments

Comments
 (0)