Skip to content

Commit f02e7e9

Browse files
committedJan 6, 2025·
vcsim: add DatastoreNamespaceManager Create/Delete directory methods
Signed-off-by: Doug MacEachern <dougm@broadcom.com>
1 parent 4eab163 commit f02e7e9

File tree

3 files changed

+188
-34
lines changed

3 files changed

+188
-34
lines changed
 

‎object/namespace_manager_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 object_test
6+
7+
import (
8+
"context"
9+
"testing"
10+
11+
"github.com/vmware/govmomi/fault"
12+
"github.com/vmware/govmomi/find"
13+
"github.com/vmware/govmomi/object"
14+
"github.com/vmware/govmomi/simulator"
15+
"github.com/vmware/govmomi/vim25"
16+
"github.com/vmware/govmomi/vim25/types"
17+
)
18+
19+
func TestDatastoreNamespaceManager(t *testing.T) {
20+
simulator.Test(func(ctx context.Context, c *vim25.Client) {
21+
m := object.NewDatastoreNamespaceManager(c)
22+
23+
finder := find.NewFinder(c)
24+
dc, err := finder.DefaultDatacenter(ctx)
25+
if err != nil {
26+
t.Fatal(err)
27+
}
28+
29+
ds, err := finder.DefaultDatastore(ctx)
30+
if err != nil {
31+
t.Fatal(err)
32+
}
33+
34+
store := simulator.Map.Get(ds.Reference()).(*simulator.Datastore)
35+
36+
name := "foo"
37+
38+
dir, err := m.CreateDirectory(ctx, ds, name, "")
39+
if !fault.Is(err, &types.CannotCreateFile{}) {
40+
t.Errorf("err=%v", err)
41+
}
42+
43+
store.Summary.Type = string(types.HostFileSystemVolumeFileSystemTypeVsan)
44+
store.Capability.TopLevelDirectoryCreateSupported = types.NewBool(true)
45+
46+
dir, err = m.CreateDirectory(ctx, ds, name, "")
47+
if err != nil {
48+
t.Errorf("err=%v", err)
49+
}
50+
51+
err = m.DeleteDirectory(ctx, dc, name)
52+
if !fault.Is(err, &types.InvalidDatastorePath{}) {
53+
t.Errorf("err=%v", err)
54+
}
55+
56+
err = m.DeleteDirectory(ctx, dc, dir)
57+
if err != nil {
58+
t.Errorf("delete %s, err=%v", dir, err)
59+
}
60+
})
61+
}

‎simulator/datastore_namespace_manager.go

+123-16
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
1-
/*
2-
Copyright (c) 2024-2024 VMware, Inc. All Rights Reserved.
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
*/
1+
// © Broadcom. All Rights Reserved.
2+
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: Apache-2.0
164

175
package simulator
186

197
import (
8+
"path"
209
"strings"
2110

2211
"github.com/vmware/govmomi/internal"
12+
"github.com/vmware/govmomi/object"
2313
"github.com/vmware/govmomi/vim25/methods"
2414
"github.com/vmware/govmomi/vim25/mo"
2515
"github.com/vmware/govmomi/vim25/soap"
@@ -38,7 +28,11 @@ func (m *DatastoreNamespaceManager) ConvertNamespacePathToUuidPath(ctx *Context,
3828
return body
3929
}
4030

41-
dc := ctx.Map.Get(*req.Datacenter).(*Datacenter)
31+
dc, ok := ctx.Map.Get(*req.Datacenter).(*Datacenter)
32+
if !ok {
33+
body.Fault_ = Fault("", &types.ManagedObjectNotFound{Obj: *req.Datacenter})
34+
return body
35+
}
4236

4337
var ds *Datastore
4438
for _, ref := range dc.Datastore {
@@ -65,3 +59,116 @@ func (m *DatastoreNamespaceManager) ConvertNamespacePathToUuidPath(ctx *Context,
6559

6660
return body
6761
}
62+
63+
func (m *DatastoreNamespaceManager) CreateDirectory(ctx *Context, req *types.CreateDirectory) soap.HasFault {
64+
body := new(methods.CreateDirectoryBody)
65+
66+
ds, ok := ctx.Map.Get(req.Datastore).(*Datastore)
67+
if !ok {
68+
body.Fault_ = Fault("", &types.ManagedObjectNotFound{Obj: req.Datastore})
69+
return body
70+
}
71+
72+
if !internal.IsDatastoreVSAN(ds.Datastore) {
73+
body.Fault_ = Fault("", &types.CannotCreateFile{
74+
FileFault: types.FileFault{
75+
File: "Datastore not supported for directory creation by DatastoreNamespaceManager",
76+
},
77+
})
78+
return body
79+
}
80+
81+
if !isValidFileName(req.DisplayName) {
82+
body.Fault_ = Fault("", &types.InvalidDatastorePath{DatastorePath: req.DisplayName})
83+
return body
84+
}
85+
86+
p := object.DatastorePath{
87+
Datastore: ds.Name,
88+
Path: req.DisplayName,
89+
}
90+
91+
dc := ctx.Map.getEntityDatacenter(ds)
92+
93+
fm := ctx.Map.FileManager()
94+
95+
fault := fm.MakeDirectory(&types.MakeDirectory{
96+
This: fm.Self,
97+
Name: p.String(),
98+
Datacenter: &dc.Self,
99+
})
100+
101+
if fault.Fault() != nil {
102+
body.Fault_ = fault.Fault()
103+
} else {
104+
dir := ds.Info.GetDatastoreInfo().Url
105+
106+
body.Res = &types.CreateDirectoryResponse{
107+
Returnval: path.Join(dir, req.DisplayName),
108+
}
109+
}
110+
111+
return body
112+
}
113+
114+
func (m *DatastoreNamespaceManager) DeleteDirectory(ctx *Context, req *types.DeleteDirectory) soap.HasFault {
115+
body := new(methods.DeleteDirectoryBody)
116+
117+
if req.Datacenter == nil {
118+
body.Fault_ = Fault("", &types.InvalidArgument{InvalidProperty: "datacenterRef"})
119+
return body
120+
}
121+
122+
dc, ok := ctx.Map.Get(*req.Datacenter).(*Datacenter)
123+
if !ok {
124+
body.Fault_ = Fault("", &types.ManagedObjectNotFound{Obj: *req.Datacenter})
125+
return body
126+
}
127+
128+
var ds *Datastore
129+
for _, ref := range dc.Datastore {
130+
ds = ctx.Map.Get(ref).(*Datastore)
131+
if strings.HasPrefix(req.DatastorePath, ds.Summary.Url) {
132+
break
133+
}
134+
ds = nil
135+
}
136+
137+
if ds == nil || strings.Contains(req.DatastorePath, "..") {
138+
body.Fault_ = Fault("", &types.InvalidDatastorePath{DatastorePath: req.DatastorePath})
139+
return body
140+
}
141+
142+
if !internal.IsDatastoreVSAN(ds.Datastore) {
143+
body.Fault_ = Fault("", &types.FileFault{
144+
File: "Datastore not supported for directory deletion by DatastoreNamespaceManager",
145+
})
146+
return body
147+
}
148+
149+
name := &object.DatastorePath{
150+
Datastore: ds.Name,
151+
Path: path.Base(req.DatastorePath),
152+
}
153+
154+
fm := ctx.Map.FileManager()
155+
156+
fault := fm.deleteDatastoreFile(&types.DeleteDatastoreFile_Task{
157+
Name: name.String(),
158+
Datacenter: req.Datacenter,
159+
})
160+
161+
if fault != nil {
162+
body.Fault_ = Fault("", fault)
163+
} else {
164+
body.Res = new(types.DeleteDirectoryResponse)
165+
}
166+
167+
return body
168+
}
169+
170+
func isValidFileName(s string) bool {
171+
return !strings.Contains(s, "/") &&
172+
!strings.Contains(s, "\\") &&
173+
!strings.Contains(s, "..")
174+
}

‎simulator/esx/datacenter.go

+4-18
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
1-
/*
2-
Copyright (c) 2017-2023 VMware, Inc. All Rights Reserved.
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
*/
1+
// © Broadcom. All Rights Reserved.
2+
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: Apache-2.0
164

175
package esx
186

@@ -50,9 +38,7 @@ var Datacenter = mo.Datacenter{
5038
HostFolder: types.ManagedObjectReference{Type: "Folder", Value: "ha-folder-host"},
5139
DatastoreFolder: types.ManagedObjectReference{Type: "Folder", Value: "ha-folder-datastore"},
5240
NetworkFolder: types.ManagedObjectReference{Type: "Folder", Value: "ha-folder-network"},
53-
Datastore: []types.ManagedObjectReference{
54-
{Type: "Datastore", Value: "57089c25-85e3ccd4-17b6-000c29d0beb3"},
55-
},
41+
Datastore: nil,
5642
Network: []types.ManagedObjectReference{
5743
{Type: "Network", Value: "HaNetwork-VM Network"},
5844
},

0 commit comments

Comments
 (0)
Please sign in to comment.