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