Skip to content

Commit 29fe42f

Browse files
committed
govc: add volume.rm -keep flag to retain backing disk
vcsim: don't delete backing disk in CnsDeleteVolume when DeleteDisk==false Signed-off-by: Doug MacEachern <[email protected]>
1 parent 55b7637 commit 29fe42f

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed

cli/volume/rm.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717

1818
type rm struct {
1919
*flags.ClientFlag
20+
21+
keep bool
2022
}
2123

2224
func init() {
@@ -26,6 +28,8 @@ func init() {
2628
func (cmd *rm) Register(ctx context.Context, f *flag.FlagSet) {
2729
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
2830
cmd.ClientFlag.Register(ctx, f)
31+
32+
f.BoolVar(&cmd.keep, "keep", false, "Keep backing disk")
2933
}
3034

3135
func (cmd *rm) Usage() string {
@@ -55,7 +59,7 @@ func (cmd *rm) Run(ctx context.Context, f *flag.FlagSet) error {
5559

5660
ids := []types.CnsVolumeId{{Id: f.Arg(0)}}
5761

58-
task, err := c.DeleteVolume(ctx, ids, true)
62+
task, err := c.DeleteVolume(ctx, ids, !cmd.keep)
5963
if err != nil {
6064
return err
6165
}

cns/simulator/simulator.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,22 @@ func (m *CnsVolumeManager) CnsDeleteVolume(ctx *simulator.Context, req *cnstypes
331331
vsom := vctx.Map.VStorageObjectManager()
332332

333333
task := simulator.CreateTask(m, "CnsDeleteVolume", func(*simulator.Task) (vim25types.AnyType, vim25types.BaseMethodFault) {
334-
operationResult := []cnstypes.BaseCnsVolumeOperationResult{}
335-
for _, volumeId := range req.VolumeIds {
336-
for ds, dsVolumes := range m.volumes {
337-
res := &cnstypes.CnsVolumeOperationResult{
338-
VolumeId: volumeId,
339-
}
340-
if _, ok := dsVolumes[volumeId]; ok {
334+
if len(req.VolumeIds) != 1 {
335+
return nil, &vim25types.InvalidArgument{InvalidProperty: "InputSpec"} // Same as real VC, currently
336+
}
337+
338+
found := false
339+
volumeId := req.VolumeIds[0]
340+
res := &cnstypes.CnsVolumeOperationResult{
341+
VolumeId: volumeId,
342+
}
343+
344+
for ds, volumes := range m.volumes {
345+
if _, ok := volumes[volumeId]; ok {
346+
found = true
347+
delete(m.volumes[ds], volumeId)
348+
349+
if req.DeleteDisk {
341350
val := vsom.DeleteVStorageObjectTask(vctx, &vim25types.DeleteVStorageObject_Task{
342351
This: vsom.Self,
343352
Id: vim25types.ID(volumeId),
@@ -349,17 +358,17 @@ func (m *CnsVolumeManager) CnsDeleteVolume(ctx *simulator.Context, req *cnstypes
349358
task.Wait()
350359
if task.Info.Error != nil {
351360
res.Fault = task.Info.Error
352-
} else {
353-
delete(m.volumes[ds], volumeId)
354361
}
355-
} else {
356-
res.Fault = &vim25types.LocalizedMethodFault{Fault: new(vim25types.NotFound)}
357362
}
358-
operationResult = append(operationResult, res)
359363
}
360364
}
365+
366+
if !found {
367+
res.Fault = &vim25types.LocalizedMethodFault{Fault: new(vim25types.NotFound)}
368+
}
369+
361370
return &cnstypes.CnsVolumeOperationBatchResult{
362-
VolumeResults: operationResult,
371+
VolumeResults: []cnstypes.BaseCnsVolumeOperationResult{res},
363372
}, nil
364373
})
365374

govc/USAGE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7691,6 +7691,7 @@ Examples:
76917691
govc volume.rm f75989dc-95b9-4db7-af96-8583f24bc59d
76927692
76937693
Options:
7694+
-keep=false Keep backing disk
76947695
```
76957696

76967697
## volume.snapshot.create

govc/test/volume.bats

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,15 @@ load test_helper
133133
@test "volume.rm" {
134134
vcsim_env
135135

136-
run env GOVC_SHOW_UNRELEASED=true govc volume.create \
137-
-cluster-id my-cluster my-volume
136+
export GOVC_SHOW_UNRELEASED=true
137+
138+
run govc volume.create -cluster-id my-cluster my-volume
138139
assert_success
139140
id="$output"
140141

142+
run govc disk.ls "$id"
143+
assert_success
144+
141145
run govc volume.rm invalid
142146
assert_failure
143147

@@ -146,6 +150,19 @@ load test_helper
146150

147151
run govc volume.rm "$id"
148152
assert_failure
153+
154+
run govc disk.ls "$id"
155+
assert_failure
156+
157+
run govc volume.create -cluster-id my-cluster my-volume
158+
assert_success
159+
id="$output"
160+
161+
run govc volume.rm -keep "$id"
162+
assert_success
163+
164+
run govc disk.ls "$id"
165+
assert_success
149166
}
150167

151168
@test "volume.snapshot" {

0 commit comments

Comments
 (0)