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
175package simulator
186
197import (
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+ }
0 commit comments