search_index.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. Copyright (c) 2015 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 object
  14. import (
  15. "context"
  16. "github.com/vmware/govmomi/vim25"
  17. "github.com/vmware/govmomi/vim25/methods"
  18. "github.com/vmware/govmomi/vim25/types"
  19. )
  20. type SearchIndex struct {
  21. Common
  22. }
  23. func NewSearchIndex(c *vim25.Client) *SearchIndex {
  24. s := SearchIndex{
  25. Common: NewCommon(c, *c.ServiceContent.SearchIndex),
  26. }
  27. return &s
  28. }
  29. // FindByDatastorePath finds a virtual machine by its location on a datastore.
  30. func (s SearchIndex) FindByDatastorePath(ctx context.Context, dc *Datacenter, path string) (Reference, error) {
  31. req := types.FindByDatastorePath{
  32. This: s.Reference(),
  33. Datacenter: dc.Reference(),
  34. Path: path,
  35. }
  36. res, err := methods.FindByDatastorePath(ctx, s.c, &req)
  37. if err != nil {
  38. return nil, err
  39. }
  40. if res.Returnval == nil {
  41. return nil, nil
  42. }
  43. return NewReference(s.c, *res.Returnval), nil
  44. }
  45. // FindByDnsName finds a virtual machine or host by DNS name.
  46. func (s SearchIndex) FindByDnsName(ctx context.Context, dc *Datacenter, dnsName string, vmSearch bool) (Reference, error) {
  47. req := types.FindByDnsName{
  48. This: s.Reference(),
  49. DnsName: dnsName,
  50. VmSearch: vmSearch,
  51. }
  52. if dc != nil {
  53. ref := dc.Reference()
  54. req.Datacenter = &ref
  55. }
  56. res, err := methods.FindByDnsName(ctx, s.c, &req)
  57. if err != nil {
  58. return nil, err
  59. }
  60. if res.Returnval == nil {
  61. return nil, nil
  62. }
  63. return NewReference(s.c, *res.Returnval), nil
  64. }
  65. // FindByInventoryPath finds a managed entity based on its location in the inventory.
  66. func (s SearchIndex) FindByInventoryPath(ctx context.Context, path string) (Reference, error) {
  67. req := types.FindByInventoryPath{
  68. This: s.Reference(),
  69. InventoryPath: path,
  70. }
  71. res, err := methods.FindByInventoryPath(ctx, s.c, &req)
  72. if err != nil {
  73. return nil, err
  74. }
  75. if res.Returnval == nil {
  76. return nil, nil
  77. }
  78. return NewReference(s.c, *res.Returnval), nil
  79. }
  80. // FindByIp finds a virtual machine or host by IP address.
  81. func (s SearchIndex) FindByIp(ctx context.Context, dc *Datacenter, ip string, vmSearch bool) (Reference, error) {
  82. req := types.FindByIp{
  83. This: s.Reference(),
  84. Ip: ip,
  85. VmSearch: vmSearch,
  86. }
  87. if dc != nil {
  88. ref := dc.Reference()
  89. req.Datacenter = &ref
  90. }
  91. res, err := methods.FindByIp(ctx, s.c, &req)
  92. if err != nil {
  93. return nil, err
  94. }
  95. if res.Returnval == nil {
  96. return nil, nil
  97. }
  98. return NewReference(s.c, *res.Returnval), nil
  99. }
  100. // FindByUuid finds a virtual machine or host by UUID.
  101. func (s SearchIndex) FindByUuid(ctx context.Context, dc *Datacenter, uuid string, vmSearch bool, instanceUuid *bool) (Reference, error) {
  102. req := types.FindByUuid{
  103. This: s.Reference(),
  104. Uuid: uuid,
  105. VmSearch: vmSearch,
  106. InstanceUuid: instanceUuid,
  107. }
  108. if dc != nil {
  109. ref := dc.Reference()
  110. req.Datacenter = &ref
  111. }
  112. res, err := methods.FindByUuid(ctx, s.c, &req)
  113. if err != nil {
  114. return nil, err
  115. }
  116. if res.Returnval == nil {
  117. return nil, nil
  118. }
  119. return NewReference(s.c, *res.Returnval), nil
  120. }
  121. // FindChild finds a particular child based on a managed entity name.
  122. func (s SearchIndex) FindChild(ctx context.Context, entity Reference, name string) (Reference, error) {
  123. req := types.FindChild{
  124. This: s.Reference(),
  125. Entity: entity.Reference(),
  126. Name: name,
  127. }
  128. res, err := methods.FindChild(ctx, s.c, &req)
  129. if err != nil {
  130. return nil, err
  131. }
  132. if res.Returnval == nil {
  133. return nil, nil
  134. }
  135. return NewReference(s.c, *res.Returnval), nil
  136. }