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 DiagnosticManager struct {
28	Common
29}
30
31func NewDiagnosticManager(c *vim25.Client) *DiagnosticManager {
32	m := DiagnosticManager{
33		Common: NewCommon(c, *c.ServiceContent.DiagnosticManager),
34	}
35
36	return &m
37}
38
39func (m DiagnosticManager) Log(ctx context.Context, host *HostSystem, key string) *DiagnosticLog {
40	return &DiagnosticLog{
41		m:    m,
42		Key:  key,
43		Host: host,
44	}
45}
46
47func (m DiagnosticManager) BrowseLog(ctx context.Context, host *HostSystem, key string, start, lines int32) (*types.DiagnosticManagerLogHeader, error) {
48	req := types.BrowseDiagnosticLog{
49		This:  m.Reference(),
50		Key:   key,
51		Start: start,
52		Lines: lines,
53	}
54
55	if host != nil {
56		ref := host.Reference()
57		req.Host = &ref
58	}
59
60	res, err := methods.BrowseDiagnosticLog(ctx, m.Client(), &req)
61	if err != nil {
62		return nil, err
63	}
64
65	return &res.Returnval, nil
66}
67
68func (m DiagnosticManager) GenerateLogBundles(ctx context.Context, includeDefault bool, host []*HostSystem) (*Task, error) {
69	req := types.GenerateLogBundles_Task{
70		This:           m.Reference(),
71		IncludeDefault: includeDefault,
72	}
73
74	if host != nil {
75		for _, h := range host {
76			req.Host = append(req.Host, h.Reference())
77		}
78	}
79
80	res, err := methods.GenerateLogBundles_Task(ctx, m.c, &req)
81	if err != nil {
82		return nil, err
83	}
84
85	return NewTask(m.c, res.Returnval), nil
86}
87
88func (m DiagnosticManager) QueryDescriptions(ctx context.Context, host *HostSystem) ([]types.DiagnosticManagerLogDescriptor, error) {
89	req := types.QueryDescriptions{
90		This: m.Reference(),
91	}
92
93	if host != nil {
94		ref := host.Reference()
95		req.Host = &ref
96	}
97
98	res, err := methods.QueryDescriptions(ctx, m.Client(), &req)
99	if err != nil {
100		return nil, err
101	}
102
103	return res.Returnval, nil
104}
105