search_index.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. Copyright (c) 2017 VMware, Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package simulator
  14. import (
  15. "strings"
  16. "github.com/vmware/govmomi/object"
  17. "github.com/vmware/govmomi/vim25/methods"
  18. "github.com/vmware/govmomi/vim25/mo"
  19. "github.com/vmware/govmomi/vim25/soap"
  20. "github.com/vmware/govmomi/vim25/types"
  21. )
  22. type SearchIndex struct {
  23. mo.SearchIndex
  24. }
  25. func NewSearchIndex(ref types.ManagedObjectReference) object.Reference {
  26. m := &SearchIndex{}
  27. m.Self = ref
  28. return m
  29. }
  30. func (s *SearchIndex) FindByDatastorePath(r *types.FindByDatastorePath) soap.HasFault {
  31. res := &methods.FindByDatastorePathBody{Res: new(types.FindByDatastorePathResponse)}
  32. for ref, obj := range Map.objects {
  33. vm, ok := obj.(*VirtualMachine)
  34. if !ok {
  35. continue
  36. }
  37. if vm.Config.Files.VmPathName == r.Path {
  38. res.Res.Returnval = &ref
  39. break
  40. }
  41. }
  42. return res
  43. }
  44. func (s *SearchIndex) FindByInventoryPath(req *types.FindByInventoryPath) soap.HasFault {
  45. body := &methods.FindByInventoryPathBody{Res: new(types.FindByInventoryPathResponse)}
  46. split := func(c rune) bool {
  47. return c == '/'
  48. }
  49. path := strings.FieldsFunc(req.InventoryPath, split)
  50. if len(path) < 1 {
  51. return body
  52. }
  53. root := Map.content().RootFolder
  54. o := &root
  55. for _, name := range path {
  56. f := s.FindChild(&types.FindChild{Entity: *o, Name: name})
  57. o = f.(*methods.FindChildBody).Res.Returnval
  58. if o == nil {
  59. break
  60. }
  61. }
  62. body.Res.Returnval = o
  63. return body
  64. }
  65. func (s *SearchIndex) FindChild(req *types.FindChild) soap.HasFault {
  66. body := &methods.FindChildBody{}
  67. obj := Map.Get(req.Entity)
  68. if obj == nil {
  69. body.Fault_ = Fault("", &types.ManagedObjectNotFound{Obj: req.Entity})
  70. return body
  71. }
  72. body.Res = new(types.FindChildResponse)
  73. var children []types.ManagedObjectReference
  74. switch e := obj.(type) {
  75. case *Datacenter:
  76. children = []types.ManagedObjectReference{e.VmFolder, e.HostFolder, e.DatastoreFolder, e.NetworkFolder}
  77. case *Folder:
  78. children = e.ChildEntity
  79. case *mo.ComputeResource:
  80. children = e.Host
  81. children = append(children, *e.ResourcePool)
  82. case *ClusterComputeResource:
  83. children = e.Host
  84. children = append(children, *e.ResourcePool)
  85. case *ResourcePool:
  86. children = e.ResourcePool.ResourcePool
  87. children = append(children, e.Vm...)
  88. case *VirtualApp:
  89. children = e.ResourcePool.ResourcePool
  90. children = append(children, e.Vm...)
  91. }
  92. match := Map.FindByName(req.Name, children)
  93. if match != nil {
  94. ref := match.Reference()
  95. body.Res.Returnval = &ref
  96. }
  97. return body
  98. }
  99. func (s *SearchIndex) FindByUuid(req *types.FindByUuid) soap.HasFault {
  100. body := &methods.FindByUuidBody{Res: new(types.FindByUuidResponse)}
  101. if req.VmSearch {
  102. // Find Virtual Machine using UUID
  103. for ref, obj := range Map.objects {
  104. vm, ok := obj.(*VirtualMachine)
  105. if !ok {
  106. continue
  107. }
  108. if req.InstanceUuid != nil && *req.InstanceUuid {
  109. if vm.Config.InstanceUuid == req.Uuid {
  110. body.Res.Returnval = &ref
  111. break
  112. }
  113. } else {
  114. if vm.Config.Uuid == req.Uuid {
  115. body.Res.Returnval = &ref
  116. break
  117. }
  118. }
  119. }
  120. } else {
  121. // Find Host System using UUID
  122. for ref, obj := range Map.objects {
  123. host, ok := obj.(*HostSystem)
  124. if !ok {
  125. continue
  126. }
  127. if host.Summary.Hardware.Uuid == req.Uuid {
  128. body.Res.Returnval = &ref
  129. break
  130. }
  131. }
  132. }
  133. return body
  134. }
  135. func (s *SearchIndex) FindByDnsName(req *types.FindByDnsName) soap.HasFault {
  136. body := &methods.FindByDnsNameBody{Res: new(types.FindByDnsNameResponse)}
  137. all := types.FindAllByDnsName(*req)
  138. switch r := s.FindAllByDnsName(&all).(type) {
  139. case *methods.FindAllByDnsNameBody:
  140. if len(r.Res.Returnval) > 0 {
  141. body.Res.Returnval = &r.Res.Returnval[0]
  142. }
  143. default:
  144. // no need until FindAllByDnsName below returns a Fault
  145. }
  146. return body
  147. }
  148. func (s *SearchIndex) FindAllByDnsName(req *types.FindAllByDnsName) soap.HasFault {
  149. body := &methods.FindAllByDnsNameBody{Res: new(types.FindAllByDnsNameResponse)}
  150. if req.VmSearch {
  151. // Find Virtual Machine using DNS name
  152. for ref, obj := range Map.objects {
  153. vm, ok := obj.(*VirtualMachine)
  154. if !ok {
  155. continue
  156. }
  157. if vm.Guest.HostName == req.DnsName {
  158. body.Res.Returnval = append(body.Res.Returnval, ref)
  159. }
  160. }
  161. } else {
  162. // Find Host System using DNS name
  163. for ref, obj := range Map.objects {
  164. host, ok := obj.(*HostSystem)
  165. if !ok {
  166. continue
  167. }
  168. for _, net := range host.Config.Network.NetStackInstance {
  169. if net.DnsConfig.GetHostDnsConfig().HostName == req.DnsName {
  170. body.Res.Returnval = append(body.Res.Returnval, ref)
  171. }
  172. }
  173. }
  174. }
  175. return body
  176. }
  177. func (s *SearchIndex) FindByIp(req *types.FindByIp) soap.HasFault {
  178. body := &methods.FindByIpBody{Res: new(types.FindByIpResponse)}
  179. all := types.FindAllByIp(*req)
  180. switch r := s.FindAllByIp(&all).(type) {
  181. case *methods.FindAllByIpBody:
  182. if len(r.Res.Returnval) > 0 {
  183. body.Res.Returnval = &r.Res.Returnval[0]
  184. }
  185. default:
  186. // no need until FindAllByIp below returns a Fault
  187. }
  188. return body
  189. }
  190. func (s *SearchIndex) FindAllByIp(req *types.FindAllByIp) soap.HasFault {
  191. body := &methods.FindAllByIpBody{Res: new(types.FindAllByIpResponse)}
  192. if req.VmSearch {
  193. // Find Virtual Machine using IP
  194. for ref, obj := range Map.objects {
  195. vm, ok := obj.(*VirtualMachine)
  196. if !ok {
  197. continue
  198. }
  199. if vm.Guest.IpAddress == req.Ip {
  200. body.Res.Returnval = append(body.Res.Returnval, ref)
  201. }
  202. }
  203. } else {
  204. // Find Host System using IP
  205. for ref, obj := range Map.objects {
  206. host, ok := obj.(*HostSystem)
  207. if !ok {
  208. continue
  209. }
  210. for _, net := range host.Config.Network.Vnic {
  211. if net.Spec.Ip.IpAddress == req.Ip {
  212. body.Res.Returnval = append(body.Res.Returnval, ref)
  213. }
  214. }
  215. }
  216. }
  217. return body
  218. }