Skip to content

Commit

Permalink
vcsim: add VirtualMachine AttachDisk and DetachDisk methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dougm committed Sep 4, 2024
1 parent f881d9b commit f06d083
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
8 changes: 8 additions & 0 deletions govc/test/disk.bats
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ load test_helper

govc disk.ls -json "$id" | jq .

vm=DC0_H0_VM0

run govc disk.attach -vm $vm "$id"
assert_success

run govc disk.detach -vm $vm "$id"
assert_success

run govc disk.rm "$id"
assert_success

Expand Down
57 changes: 57 additions & 0 deletions simulator/virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2605,6 +2605,63 @@ func (vm *VirtualMachine) RemoveAllSnapshotsTask(ctx *Context, req *types.Remove
}
}

func (vm *VirtualMachine) fcd(ctx *Context, ds types.ManagedObjectReference, id types.ID) *VStorageObject {
m := ctx.Map.Get(*ctx.Map.content().VStorageObjectManager).(*VcenterVStorageObjectManager)
if ds.Value != "" {
return m.objects[ds][id]
}
for _, set := range m.objects {
for key, val := range set {
if key == id {
return val
}
}
}
return nil
}

func (vm *VirtualMachine) AttachDiskTask(ctx *Context, req *types.AttachDisk_Task) soap.HasFault {
task := CreateTask(vm, "attachDisk", func(t *Task) (types.AnyType, types.BaseMethodFault) {
fcd := vm.fcd(ctx, req.Datastore, req.DiskId)
if fcd == nil {
return nil, new(types.InvalidArgument)
}

fcd.Config.ConsumerId = []types.ID{{Id: vm.Config.Uuid}}

// TODO: add device

return nil, nil
})

return &methods.AttachDisk_TaskBody{
Res: &types.AttachDisk_TaskResponse{
Returnval: task.Run(ctx),
},
}
}

func (vm *VirtualMachine) DetachDiskTask(ctx *Context, req *types.DetachDisk_Task) soap.HasFault {
task := CreateTask(vm, "detachDisk", func(t *Task) (types.AnyType, types.BaseMethodFault) {
fcd := vm.fcd(ctx, types.ManagedObjectReference{}, req.DiskId)
if fcd == nil {
return nil, new(types.InvalidArgument)
}

fcd.Config.ConsumerId = nil

// TODO: remove device

return nil, nil
})

return &methods.DetachDisk_TaskBody{
Res: &types.DetachDisk_TaskResponse{
Returnval: task.Run(ctx),
},
}
}

func (vm *VirtualMachine) ShutdownGuest(ctx *Context, c *types.ShutdownGuest) soap.HasFault {
r := &methods.ShutdownGuestBody{}

Expand Down
8 changes: 6 additions & 2 deletions simulator/vstorage_object_manager.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Copyright (c) 2018 VMware, Inc. All Rights Reserved.
Copyright (c) 2018-2024 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
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,
Expand Down Expand Up @@ -287,6 +287,10 @@ func (m *VcenterVStorageObjectManager) DeleteVStorageObjectTask(ctx *Context, re
return nil, &types.InvalidArgument{}
}

if len(obj.Config.ConsumerId) != 0 {
return nil, &types.InvalidState{}
}

backing := obj.Config.Backing.(*types.BaseConfigInfoDiskFileBackingInfo)
ds := ctx.Map.Get(req.Datastore).(*Datastore)
dc := ctx.Map.getEntityDatacenter(ds)
Expand Down

0 comments on commit f06d083

Please sign in to comment.