1/*
2Copyright (c) 2015 VMware, Inc. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package object
18
19import (
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
27type SearchIndex struct {
28	Common
29}
30
31func 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.
40func (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.
59func (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.
82func (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	return NewReference(s.c, *res.Returnval), nil
97}
98
99// FindByIp finds a virtual machine or host by IP address.
100func (s SearchIndex) FindByIp(ctx context.Context, dc *Datacenter, ip string, vmSearch bool) (Reference, error) {
101	req := types.FindByIp{
102		This:     s.Reference(),
103		Ip:       ip,
104		VmSearch: vmSearch,
105	}
106	if dc != nil {
107		ref := dc.Reference()
108		req.Datacenter = &ref
109	}
110
111	res, err := methods.FindByIp(ctx, s.c, &req)
112	if err != nil {
113		return nil, err
114	}
115
116	if res.Returnval == nil {
117		return nil, nil
118	}
119	return NewReference(s.c, *res.Returnval), nil
120}
121
122// FindByUuid finds a virtual machine or host by UUID.
123func (s SearchIndex) FindByUuid(ctx context.Context, dc *Datacenter, uuid string, vmSearch bool, instanceUuid *bool) (Reference, error) {
124	req := types.FindByUuid{
125		This:         s.Reference(),
126		Uuid:         uuid,
127		VmSearch:     vmSearch,
128		InstanceUuid: instanceUuid,
129	}
130	if dc != nil {
131		ref := dc.Reference()
132		req.Datacenter = &ref
133	}
134
135	res, err := methods.FindByUuid(ctx, s.c, &req)
136	if err != nil {
137		return nil, err
138	}
139
140	if res.Returnval == nil {
141		return nil, nil
142	}
143	return NewReference(s.c, *res.Returnval), nil
144}
145
146// FindChild finds a particular child based on a managed entity name.
147func (s SearchIndex) FindChild(ctx context.Context, entity Reference, name string) (Reference, error) {
148	req := types.FindChild{
149		This:   s.Reference(),
150		Entity: entity.Reference(),
151		Name:   name,
152	}
153
154	res, err := methods.FindChild(ctx, s.c, &req)
155	if err != nil {
156		return nil, err
157	}
158
159	if res.Returnval == nil {
160		return nil, nil
161	}
162	return NewReference(s.c, *res.Returnval), nil
163}
164