1/*
2Copyright (c) 2017 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	"encoding/json"
22
23	"github.com/vmware/govmomi/vim25"
24	"github.com/vmware/govmomi/vim25/methods"
25	"github.com/vmware/govmomi/vim25/types"
26)
27
28type HostVsanInternalSystem struct {
29	Common
30}
31
32func NewHostVsanInternalSystem(c *vim25.Client, ref types.ManagedObjectReference) *HostVsanInternalSystem {
33	m := HostVsanInternalSystem{
34		Common: NewCommon(c, ref),
35	}
36
37	return &m
38}
39
40// QueryVsanObjectUuidsByFilter returns vSAN DOM object uuids by filter.
41func (m HostVsanInternalSystem) QueryVsanObjectUuidsByFilter(ctx context.Context, uuids []string, limit int32, version int32) ([]string, error) {
42	req := types.QueryVsanObjectUuidsByFilter{
43		This:    m.Reference(),
44		Uuids:   uuids,
45		Limit:   &limit,
46		Version: version,
47	}
48
49	res, err := methods.QueryVsanObjectUuidsByFilter(ctx, m.Client(), &req)
50	if err != nil {
51		return nil, err
52	}
53
54	return res.Returnval, nil
55}
56
57type VsanObjExtAttrs struct {
58	Type  string `json:"Object type"`
59	Class string `json:"Object class"`
60	Size  string `json:"Object size"`
61	Path  string `json:"Object path"`
62	Name  string `json:"User friendly name"`
63}
64
65func (a *VsanObjExtAttrs) DatastorePath(dir string) string {
66	l := len(dir)
67	path := a.Path
68
69	if len(path) >= l {
70		path = a.Path[l:]
71	}
72
73	if path != "" {
74		return path
75	}
76
77	return a.Name // vmnamespace
78}
79
80// GetVsanObjExtAttrs is internal and intended for troubleshooting/debugging situations in the field.
81// WARNING: This API can be slow because we do IOs (reads) to all the objects.
82func (m HostVsanInternalSystem) GetVsanObjExtAttrs(ctx context.Context, uuids []string) (map[string]VsanObjExtAttrs, error) {
83	req := types.GetVsanObjExtAttrs{
84		This:  m.Reference(),
85		Uuids: uuids,
86	}
87
88	res, err := methods.GetVsanObjExtAttrs(ctx, m.Client(), &req)
89	if err != nil {
90		return nil, err
91	}
92
93	var attrs map[string]VsanObjExtAttrs
94
95	err = json.Unmarshal([]byte(res.Returnval), &attrs)
96
97	return attrs, err
98}
99
100// DeleteVsanObjects is internal and intended for troubleshooting/debugging only.
101// WARNING: This API can be slow because we do IOs to all the objects.
102// DOM won't allow access to objects which have lost quorum. Such objects can be deleted with the optional "force" flag.
103// These objects may however re-appear with quorum if the absent components come back (network partition gets resolved, etc.)
104func (m HostVsanInternalSystem) DeleteVsanObjects(ctx context.Context, uuids []string, force *bool) ([]types.HostVsanInternalSystemDeleteVsanObjectsResult, error) {
105	req := types.DeleteVsanObjects{
106		This:  m.Reference(),
107		Uuids: uuids,
108		Force: force,
109	}
110
111	res, err := methods.DeleteVsanObjects(ctx, m.Client(), &req)
112	if err != nil {
113		return nil, err
114	}
115
116	return res.Returnval, nil
117}
118