github.com/vmware/govmomi@v0.37.2/simulator/datastore.go (about)

     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  */
    16  
    17  package simulator
    18  
    19  import (
    20  	"net/url"
    21  	"os"
    22  	"path"
    23  	"strings"
    24  	"time"
    25  
    26  	"github.com/vmware/govmomi/object"
    27  	"github.com/vmware/govmomi/vim25/methods"
    28  	"github.com/vmware/govmomi/vim25/mo"
    29  	"github.com/vmware/govmomi/vim25/soap"
    30  	"github.com/vmware/govmomi/vim25/types"
    31  )
    32  
    33  type Datastore struct {
    34  	mo.Datastore
    35  }
    36  
    37  func (ds *Datastore) eventArgument() *types.DatastoreEventArgument {
    38  	return &types.DatastoreEventArgument{
    39  		Datastore:           ds.Self,
    40  		EntityEventArgument: types.EntityEventArgument{Name: ds.Name},
    41  	}
    42  }
    43  
    44  func (ds *Datastore) model(m *Model) error {
    45  	info := ds.Info.GetDatastoreInfo()
    46  	u, _ := url.Parse(info.Url)
    47  	if u.Scheme == "ds" {
    48  		// rewrite saved vmfs path to a local temp dir
    49  		u.Path = path.Clean(u.Path)
    50  		parent := strings.ReplaceAll(path.Dir(u.Path), "/", "_")
    51  		name := strings.ReplaceAll(path.Base(u.Path), ":", "_")
    52  
    53  		dir, err := m.createTempDir(parent, name)
    54  		if err != nil {
    55  			return err
    56  		}
    57  
    58  		info.Url = dir
    59  	}
    60  	return nil
    61  }
    62  
    63  func parseDatastorePath(dsPath string) (*object.DatastorePath, types.BaseMethodFault) {
    64  	var p object.DatastorePath
    65  
    66  	if p.FromString(dsPath) {
    67  		return &p, nil
    68  	}
    69  
    70  	return nil, &types.InvalidDatastorePath{DatastorePath: dsPath}
    71  }
    72  
    73  func (ds *Datastore) RefreshDatastore(*types.RefreshDatastore) soap.HasFault {
    74  	r := &methods.RefreshDatastoreBody{}
    75  
    76  	_, err := os.Stat(ds.Info.GetDatastoreInfo().Url)
    77  	if err != nil {
    78  		r.Fault_ = Fault(err.Error(), &types.HostConfigFault{})
    79  		return r
    80  	}
    81  
    82  	info := ds.Info.GetDatastoreInfo()
    83  
    84  	info.Timestamp = types.NewTime(time.Now())
    85  
    86  	r.Res = &types.RefreshDatastoreResponse{}
    87  	return r
    88  }
    89  
    90  func (ds *Datastore) DestroyTask(ctx *Context, req *types.Destroy_Task) soap.HasFault {
    91  	task := CreateTask(ds, "destroy", func(*Task) (types.AnyType, types.BaseMethodFault) {
    92  		if len(ds.Vm) != 0 {
    93  			return nil, &types.ResourceInUse{
    94  				Type: ds.Self.Type,
    95  				Name: ds.Name,
    96  			}
    97  		}
    98  
    99  		for _, mount := range ds.Host {
   100  			host := ctx.Map.Get(mount.Key).(*HostSystem)
   101  			ctx.Map.RemoveReference(ctx, host, &host.Datastore, ds.Self)
   102  			parent := hostParent(&host.HostSystem)
   103  			ctx.Map.RemoveReference(ctx, parent, &parent.Datastore, ds.Self)
   104  		}
   105  
   106  		p, _ := asFolderMO(ctx.Map.Get(*ds.Parent))
   107  		folderRemoveChild(ctx, p, ds.Self)
   108  
   109  		return nil, nil
   110  	})
   111  
   112  	return &methods.Destroy_TaskBody{
   113  		Res: &types.Destroy_TaskResponse{
   114  			Returnval: task.Run(ctx),
   115  		},
   116  	}
   117  }