github.com/vmware/govmomi@v0.37.2/object/search_index.go (about)

     1  /*
     2  Copyright (c) 2015 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 object
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/vmware/govmomi/vim25"
    23  	"github.com/vmware/govmomi/vim25/methods"
    24  	"github.com/vmware/govmomi/vim25/types"
    25  )
    26  
    27  type SearchIndex struct {
    28  	Common
    29  }
    30  
    31  func NewSearchIndex(c *vim25.Client) *SearchIndex {
    32  	s := SearchIndex{
    33  		Common: NewCommon(c, *c.ServiceContent.SearchIndex),
    34  	}
    35  
    36  	return &s
    37  }
    38  
    39  // FindByDatastorePath finds a virtual machine by its location on a datastore.
    40  func (s SearchIndex) FindByDatastorePath(ctx context.Context, dc *Datacenter, path string) (Reference, error) {
    41  	req := types.FindByDatastorePath{
    42  		This:       s.Reference(),
    43  		Datacenter: dc.Reference(),
    44  		Path:       path,
    45  	}
    46  
    47  	res, err := methods.FindByDatastorePath(ctx, s.c, &req)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	if res.Returnval == nil {
    53  		return nil, nil
    54  	}
    55  	return NewReference(s.c, *res.Returnval), nil
    56  }
    57  
    58  // FindByDnsName finds a virtual machine or host by DNS name.
    59  func (s SearchIndex) FindByDnsName(ctx context.Context, dc *Datacenter, dnsName string, vmSearch bool) (Reference, error) {
    60  	req := types.FindByDnsName{
    61  		This:     s.Reference(),
    62  		DnsName:  dnsName,
    63  		VmSearch: vmSearch,
    64  	}
    65  	if dc != nil {
    66  		ref := dc.Reference()
    67  		req.Datacenter = &ref
    68  	}
    69  
    70  	res, err := methods.FindByDnsName(ctx, s.c, &req)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  
    75  	if res.Returnval == nil {
    76  		return nil, nil
    77  	}
    78  	return NewReference(s.c, *res.Returnval), nil
    79  }
    80  
    81  // FindByInventoryPath finds a managed entity based on its location in the inventory.
    82  func (s SearchIndex) FindByInventoryPath(ctx context.Context, path string) (Reference, error) {
    83  	req := types.FindByInventoryPath{
    84  		This:          s.Reference(),
    85  		InventoryPath: path,
    86  	}
    87  
    88  	res, err := methods.FindByInventoryPath(ctx, s.c, &req)
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  
    93  	if res.Returnval == nil {
    94  		return nil, nil
    95  	}
    96  
    97  	r := NewReference(s.c, *res.Returnval)
    98  
    99  	type common interface {
   100  		SetInventoryPath(string)
   101  	}
   102  
   103  	if c, ok := r.(common); ok {
   104  		c.SetInventoryPath(path)
   105  	}
   106  
   107  	return r, nil
   108  }
   109  
   110  // FindByIp finds a virtual machine or host by IP address.
   111  func (s SearchIndex) FindByIp(ctx context.Context, dc *Datacenter, ip string, vmSearch bool) (Reference, error) {
   112  	req := types.FindByIp{
   113  		This:     s.Reference(),
   114  		Ip:       ip,
   115  		VmSearch: vmSearch,
   116  	}
   117  	if dc != nil {
   118  		ref := dc.Reference()
   119  		req.Datacenter = &ref
   120  	}
   121  
   122  	res, err := methods.FindByIp(ctx, s.c, &req)
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  
   127  	if res.Returnval == nil {
   128  		return nil, nil
   129  	}
   130  	return NewReference(s.c, *res.Returnval), nil
   131  }
   132  
   133  // FindByUuid finds a virtual machine or host by UUID.
   134  func (s SearchIndex) FindByUuid(ctx context.Context, dc *Datacenter, uuid string, vmSearch bool, instanceUuid *bool) (Reference, error) {
   135  	req := types.FindByUuid{
   136  		This:         s.Reference(),
   137  		Uuid:         uuid,
   138  		VmSearch:     vmSearch,
   139  		InstanceUuid: instanceUuid,
   140  	}
   141  	if dc != nil {
   142  		ref := dc.Reference()
   143  		req.Datacenter = &ref
   144  	}
   145  
   146  	res, err := methods.FindByUuid(ctx, s.c, &req)
   147  	if err != nil {
   148  		return nil, err
   149  	}
   150  
   151  	if res.Returnval == nil {
   152  		return nil, nil
   153  	}
   154  	return NewReference(s.c, *res.Returnval), nil
   155  }
   156  
   157  // FindChild finds a particular child based on a managed entity name.
   158  func (s SearchIndex) FindChild(ctx context.Context, entity Reference, name string) (Reference, error) {
   159  	req := types.FindChild{
   160  		This:   s.Reference(),
   161  		Entity: entity.Reference(),
   162  		Name:   name,
   163  	}
   164  
   165  	res, err := methods.FindChild(ctx, s.c, &req)
   166  	if err != nil {
   167  		return nil, err
   168  	}
   169  
   170  	if res.Returnval == nil {
   171  		return nil, nil
   172  	}
   173  	return NewReference(s.c, *res.Returnval), nil
   174  }
   175  
   176  // FindAllByDnsName finds all virtual machines or hosts by DNS name.
   177  func (s SearchIndex) FindAllByDnsName(ctx context.Context, dc *Datacenter, dnsName string, vmSearch bool) ([]Reference, error) {
   178  	req := types.FindAllByDnsName{
   179  		This:     s.Reference(),
   180  		DnsName:  dnsName,
   181  		VmSearch: vmSearch,
   182  	}
   183  	if dc != nil {
   184  		ref := dc.Reference()
   185  		req.Datacenter = &ref
   186  	}
   187  
   188  	res, err := methods.FindAllByDnsName(ctx, s.c, &req)
   189  	if err != nil {
   190  		return nil, err
   191  	}
   192  
   193  	if len(res.Returnval) == 0 {
   194  		return nil, nil
   195  	}
   196  
   197  	var references []Reference
   198  	for _, returnval := range res.Returnval {
   199  		references = append(references, NewReference(s.c, returnval))
   200  	}
   201  	return references, nil
   202  }
   203  
   204  // FindAllByIp finds all virtual machines or hosts by IP address.
   205  func (s SearchIndex) FindAllByIp(ctx context.Context, dc *Datacenter, ip string, vmSearch bool) ([]Reference, error) {
   206  	req := types.FindAllByIp{
   207  		This:     s.Reference(),
   208  		Ip:       ip,
   209  		VmSearch: vmSearch,
   210  	}
   211  	if dc != nil {
   212  		ref := dc.Reference()
   213  		req.Datacenter = &ref
   214  	}
   215  
   216  	res, err := methods.FindAllByIp(ctx, s.c, &req)
   217  	if err != nil {
   218  		return nil, err
   219  	}
   220  
   221  	if len(res.Returnval) == 0 {
   222  		return nil, nil
   223  	}
   224  
   225  	var references []Reference
   226  	for _, returnval := range res.Returnval {
   227  		references = append(references, NewReference(s.c, returnval))
   228  	}
   229  	return references, nil
   230  }
   231  
   232  // FindAllByUuid finds all virtual machines or hosts by UUID.
   233  func (s SearchIndex) FindAllByUuid(ctx context.Context, dc *Datacenter, uuid string, vmSearch bool, instanceUuid *bool) ([]Reference, error) {
   234  	req := types.FindAllByUuid{
   235  		This:         s.Reference(),
   236  		Uuid:         uuid,
   237  		VmSearch:     vmSearch,
   238  		InstanceUuid: instanceUuid,
   239  	}
   240  	if dc != nil {
   241  		ref := dc.Reference()
   242  		req.Datacenter = &ref
   243  	}
   244  
   245  	res, err := methods.FindAllByUuid(ctx, s.c, &req)
   246  	if err != nil {
   247  		return nil, err
   248  	}
   249  
   250  	if len(res.Returnval) == 0 {
   251  		return nil, nil
   252  	}
   253  
   254  	var references []Reference
   255  	for _, returnval := range res.Returnval {
   256  		references = append(references, NewReference(s.c, returnval))
   257  	}
   258  	return references, nil
   259  }