github.com/vmware/govmomi@v0.37.1/simulator/search_index.go (about)

     1  /*
     2  Copyright (c) 2017 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  	"strings"
    21  
    22  	"github.com/vmware/govmomi/vim25/methods"
    23  	"github.com/vmware/govmomi/vim25/mo"
    24  	"github.com/vmware/govmomi/vim25/soap"
    25  	"github.com/vmware/govmomi/vim25/types"
    26  )
    27  
    28  type SearchIndex struct {
    29  	mo.SearchIndex
    30  }
    31  
    32  func (s *SearchIndex) FindByDatastorePath(r *types.FindByDatastorePath) soap.HasFault {
    33  	res := &methods.FindByDatastorePathBody{Res: new(types.FindByDatastorePathResponse)}
    34  
    35  	Map.m.Lock()
    36  	defer Map.m.Unlock()
    37  
    38  	for ref, obj := range Map.objects {
    39  		vm, ok := asVirtualMachineMO(obj)
    40  		if !ok {
    41  			continue
    42  		}
    43  
    44  		if vm.Config.Files.VmPathName == r.Path {
    45  			res.Res.Returnval = &ref
    46  			break
    47  		}
    48  	}
    49  
    50  	return res
    51  }
    52  
    53  func (s *SearchIndex) FindByInventoryPath(req *types.FindByInventoryPath) soap.HasFault {
    54  	body := &methods.FindByInventoryPathBody{Res: new(types.FindByInventoryPathResponse)}
    55  
    56  	root := Map.content().RootFolder
    57  	o := &root
    58  
    59  	if req.InventoryPath == "/" {
    60  		body.Res.Returnval = o
    61  		return body
    62  	}
    63  
    64  	split := func(c rune) bool {
    65  		return c == '/'
    66  	}
    67  	path := strings.FieldsFunc(req.InventoryPath, split)
    68  	if len(path) < 1 {
    69  		return body
    70  	}
    71  
    72  	for _, name := range path {
    73  		f := s.FindChild(&types.FindChild{Entity: *o, Name: name})
    74  
    75  		o = f.(*methods.FindChildBody).Res.Returnval
    76  		if o == nil {
    77  			break
    78  		}
    79  	}
    80  
    81  	body.Res.Returnval = o
    82  
    83  	return body
    84  }
    85  
    86  func (s *SearchIndex) FindChild(req *types.FindChild) soap.HasFault {
    87  	body := &methods.FindChildBody{}
    88  
    89  	obj := Map.Get(req.Entity)
    90  
    91  	if obj == nil {
    92  		body.Fault_ = Fault("", &types.ManagedObjectNotFound{Obj: req.Entity})
    93  		return body
    94  	}
    95  
    96  	body.Res = new(types.FindChildResponse)
    97  
    98  	var children []types.ManagedObjectReference
    99  
   100  	switch e := obj.(type) {
   101  	case *Datacenter:
   102  		children = []types.ManagedObjectReference{e.VmFolder, e.HostFolder, e.DatastoreFolder, e.NetworkFolder}
   103  	case *Folder:
   104  		children = e.ChildEntity
   105  	case *mo.ComputeResource:
   106  		children = e.Host
   107  		children = append(children, *e.ResourcePool)
   108  	case *ClusterComputeResource:
   109  		children = e.Host
   110  		children = append(children, *e.ResourcePool)
   111  	case *ResourcePool:
   112  		children = e.ResourcePool.ResourcePool
   113  		children = append(children, e.Vm...)
   114  	case *VirtualApp:
   115  		children = e.ResourcePool.ResourcePool
   116  		children = append(children, e.Vm...)
   117  	}
   118  
   119  	match := Map.FindByName(req.Name, children)
   120  
   121  	if match != nil {
   122  		ref := match.Reference()
   123  		body.Res.Returnval = &ref
   124  	}
   125  
   126  	return body
   127  }
   128  
   129  func (s *SearchIndex) FindByUuid(req *types.FindByUuid) soap.HasFault {
   130  	body := &methods.FindByUuidBody{Res: new(types.FindByUuidResponse)}
   131  
   132  	Map.m.Lock()
   133  	defer Map.m.Unlock()
   134  
   135  	if req.VmSearch {
   136  		// Find Virtual Machine using UUID
   137  		for ref, obj := range Map.objects {
   138  			vm, ok := asVirtualMachineMO(obj)
   139  			if !ok {
   140  				continue
   141  			}
   142  			if req.InstanceUuid != nil && *req.InstanceUuid {
   143  				if vm.Config.InstanceUuid == req.Uuid {
   144  					body.Res.Returnval = &ref
   145  					break
   146  				}
   147  			} else {
   148  				if vm.Config.Uuid == req.Uuid {
   149  					body.Res.Returnval = &ref
   150  					break
   151  				}
   152  			}
   153  		}
   154  	} else {
   155  		// Find Host System using UUID
   156  		for ref, obj := range Map.objects {
   157  			host, ok := asHostSystemMO(obj)
   158  			if !ok {
   159  				continue
   160  			}
   161  			if host.Summary.Hardware.Uuid == req.Uuid {
   162  				body.Res.Returnval = &ref
   163  				break
   164  			}
   165  		}
   166  	}
   167  
   168  	return body
   169  }
   170  
   171  func (s *SearchIndex) FindByDnsName(req *types.FindByDnsName) soap.HasFault {
   172  	body := &methods.FindByDnsNameBody{Res: new(types.FindByDnsNameResponse)}
   173  
   174  	all := types.FindAllByDnsName(*req)
   175  
   176  	switch r := s.FindAllByDnsName(&all).(type) {
   177  	case *methods.FindAllByDnsNameBody:
   178  		if len(r.Res.Returnval) > 0 {
   179  			body.Res.Returnval = &r.Res.Returnval[0]
   180  		}
   181  	default:
   182  		// no need until FindAllByDnsName below returns a Fault
   183  	}
   184  
   185  	return body
   186  }
   187  
   188  func (s *SearchIndex) FindAllByDnsName(req *types.FindAllByDnsName) soap.HasFault {
   189  	body := &methods.FindAllByDnsNameBody{Res: new(types.FindAllByDnsNameResponse)}
   190  
   191  	Map.m.Lock()
   192  	defer Map.m.Unlock()
   193  
   194  	if req.VmSearch {
   195  		// Find Virtual Machine using DNS name
   196  		for ref, obj := range Map.objects {
   197  			vm, ok := asVirtualMachineMO(obj)
   198  			if !ok {
   199  				continue
   200  			}
   201  			if vm.Guest.HostName == req.DnsName {
   202  				body.Res.Returnval = append(body.Res.Returnval, ref)
   203  			}
   204  		}
   205  	} else {
   206  		// Find Host System using DNS name
   207  		for ref, obj := range Map.objects {
   208  			host, ok := asHostSystemMO(obj)
   209  			if !ok {
   210  				continue
   211  			}
   212  			for _, net := range host.Config.Network.NetStackInstance {
   213  				if net.DnsConfig.GetHostDnsConfig().HostName == req.DnsName {
   214  					body.Res.Returnval = append(body.Res.Returnval, ref)
   215  				}
   216  			}
   217  		}
   218  	}
   219  
   220  	return body
   221  }
   222  
   223  func (s *SearchIndex) FindByIp(req *types.FindByIp) soap.HasFault {
   224  	body := &methods.FindByIpBody{Res: new(types.FindByIpResponse)}
   225  
   226  	all := types.FindAllByIp(*req)
   227  
   228  	switch r := s.FindAllByIp(&all).(type) {
   229  	case *methods.FindAllByIpBody:
   230  		if len(r.Res.Returnval) > 0 {
   231  			body.Res.Returnval = &r.Res.Returnval[0]
   232  		}
   233  	default:
   234  		// no need until FindAllByIp below returns a Fault
   235  	}
   236  
   237  	return body
   238  }
   239  
   240  func (s *SearchIndex) FindAllByIp(req *types.FindAllByIp) soap.HasFault {
   241  	body := &methods.FindAllByIpBody{Res: new(types.FindAllByIpResponse)}
   242  
   243  	Map.m.Lock()
   244  	defer Map.m.Unlock()
   245  
   246  	if req.VmSearch {
   247  		// Find Virtual Machine using IP
   248  		for ref, obj := range Map.objects {
   249  			vm, ok := asVirtualMachineMO(obj)
   250  			if !ok {
   251  				continue
   252  			}
   253  			if vm.Guest.IpAddress == req.Ip {
   254  				body.Res.Returnval = append(body.Res.Returnval, ref)
   255  			}
   256  		}
   257  	} else {
   258  		// Find Host System using IP
   259  		for ref, obj := range Map.objects {
   260  			host, ok := asHostSystemMO(obj)
   261  			if !ok {
   262  				continue
   263  			}
   264  			for _, net := range host.Config.Network.Vnic {
   265  				if net.Spec.Ip.IpAddress == req.Ip {
   266  					body.Res.Returnval = append(body.Res.Returnval, ref)
   267  				}
   268  			}
   269  		}
   270  	}
   271  
   272  	return body
   273  }