1/*
2Copyright (c) 2015 VMware, Inc. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package object
18
19import (
20	"context"
21
22	"github.com/vmware/govmomi/vim25"
23	"github.com/vmware/govmomi/vim25/methods"
24	"github.com/vmware/govmomi/vim25/types"
25)
26
27type DatastoreNamespaceManager struct {
28	Common
29}
30
31func NewDatastoreNamespaceManager(c *vim25.Client) *DatastoreNamespaceManager {
32	n := DatastoreNamespaceManager{
33		Common: NewCommon(c, *c.ServiceContent.DatastoreNamespaceManager),
34	}
35
36	return &n
37}
38
39// CreateDirectory creates a top-level directory on the given vsan datastore, using
40// the given user display name hint and opaque storage policy.
41func (nm DatastoreNamespaceManager) CreateDirectory(ctx context.Context, ds *Datastore, displayName string, policy string) (string, error) {
42
43	req := &types.CreateDirectory{
44		This:        nm.Reference(),
45		Datastore:   ds.Reference(),
46		DisplayName: displayName,
47		Policy:      policy,
48	}
49
50	resp, err := methods.CreateDirectory(ctx, nm.c, req)
51	if err != nil {
52		return "", err
53	}
54
55	return resp.Returnval, nil
56}
57
58// DeleteDirectory deletes the given top-level directory from a vsan datastore.
59func (nm DatastoreNamespaceManager) DeleteDirectory(ctx context.Context, dc *Datacenter, datastorePath string) error {
60
61	req := &types.DeleteDirectory{
62		This:          nm.Reference(),
63		DatastorePath: datastorePath,
64	}
65
66	if dc != nil {
67		ref := dc.Reference()
68		req.Datacenter = &ref
69	}
70
71	if _, err := methods.DeleteDirectory(ctx, nm.c, req); err != nil {
72		return err
73	}
74
75	return nil
76}
77