-
Notifications
You must be signed in to change notification settings - Fork 649
/
client.go
162 lines (143 loc) · 5.31 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package admin
import (
"context"
"fmt"
"github.com/ava-labs/avalanchego/api"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/rpc"
)
var _ Client = &client{}
// Client interface for the Avalanche Platform Info API Endpoint
type Client interface {
StartCPUProfiler(context.Context, ...rpc.Option) (bool, error)
StopCPUProfiler(context.Context, ...rpc.Option) (bool, error)
MemoryProfile(context.Context, ...rpc.Option) (bool, error)
LockProfile(context.Context, ...rpc.Option) (bool, error)
Alias(ctx context.Context, endpoint string, alias string, options ...rpc.Option) (bool, error)
AliasChain(ctx context.Context, chainID string, alias string, options ...rpc.Option) (bool, error)
GetChainAliases(ctx context.Context, chainID string, options ...rpc.Option) ([]string, error)
Stacktrace(context.Context, ...rpc.Option) (bool, error)
LoadVMs(context.Context, ...rpc.Option) (map[ids.ID][]string, map[ids.ID]string, error)
SetLoggerLevel(ctx context.Context, loggerName, logLevel, displayLevel string, options ...rpc.Option) (bool, error)
GetLoggerLevel(ctx context.Context, loggerName string, options ...rpc.Option) (map[string]LogAndDisplayLevels, error)
GetConfig(ctx context.Context, options ...rpc.Option) (interface{}, error)
}
// Client implementation for the Avalanche Platform Info API Endpoint
type client struct {
requester rpc.EndpointRequester
}
// NewClient returns a new Info API Client
func NewClient(uri string) Client {
return &client{
requester: rpc.NewEndpointRequester(uri, "/ext/admin", "admin"),
}
}
func (c *client) StartCPUProfiler(ctx context.Context, options ...rpc.Option) (bool, error) {
res := &api.SuccessResponse{}
err := c.requester.SendRequest(ctx, "startCPUProfiler", struct{}{}, res, options...)
return res.Success, err
}
func (c *client) StopCPUProfiler(ctx context.Context, options ...rpc.Option) (bool, error) {
res := &api.SuccessResponse{}
err := c.requester.SendRequest(ctx, "stopCPUProfiler", struct{}{}, res, options...)
return res.Success, err
}
func (c *client) MemoryProfile(ctx context.Context, options ...rpc.Option) (bool, error) {
res := &api.SuccessResponse{}
err := c.requester.SendRequest(ctx, "memoryProfile", struct{}{}, res, options...)
return res.Success, err
}
func (c *client) LockProfile(ctx context.Context, options ...rpc.Option) (bool, error) {
res := &api.SuccessResponse{}
err := c.requester.SendRequest(ctx, "lockProfile", struct{}{}, res, options...)
return res.Success, err
}
func (c *client) Alias(ctx context.Context, endpoint, alias string, options ...rpc.Option) (bool, error) {
res := &api.SuccessResponse{}
err := c.requester.SendRequest(ctx, "alias", &AliasArgs{
Endpoint: endpoint,
Alias: alias,
}, res, options...)
return res.Success, err
}
func (c *client) AliasChain(ctx context.Context, chain, alias string, options ...rpc.Option) (bool, error) {
res := &api.SuccessResponse{}
err := c.requester.SendRequest(ctx, "aliasChain", &AliasChainArgs{
Chain: chain,
Alias: alias,
}, res, options...)
return res.Success, err
}
func (c *client) GetChainAliases(ctx context.Context, chain string, options ...rpc.Option) ([]string, error) {
res := &GetChainAliasesReply{}
err := c.requester.SendRequest(ctx, "getChainAliases", &GetChainAliasesArgs{
Chain: chain,
}, res, options...)
return res.Aliases, err
}
func (c *client) Stacktrace(ctx context.Context, options ...rpc.Option) (bool, error) {
res := &api.SuccessResponse{}
err := c.requester.SendRequest(ctx, "stacktrace", struct{}{}, res, options...)
return res.Success, err
}
func (c *client) LoadVMs(ctx context.Context, options ...rpc.Option) (map[ids.ID][]string, map[ids.ID]string, error) {
res := &LoadVMsReply{}
err := c.requester.SendRequest(ctx, "loadVMs", struct{}{}, res, options...)
return res.NewVMs, res.FailedVMs, err
}
func (c *client) SetLoggerLevel(
ctx context.Context,
loggerName,
logLevel,
displayLevel string,
options ...rpc.Option,
) (bool, error) {
var (
res = &api.SuccessResponse{}
logLevelArg logging.Level
displayLevelArg logging.Level
err error
)
if len(logLevel) > 0 {
logLevelArg, err = logging.ToLevel(logLevel)
if err != nil {
return false, fmt.Errorf("couldn't parse %q to log level", logLevel)
}
}
if len(displayLevel) > 0 {
displayLevelArg, err = logging.ToLevel(displayLevel)
if err != nil {
return false, fmt.Errorf("couldn't parse %q to log level", displayLevel)
}
}
err = c.requester.SendRequest(
ctx,
"setLoggerLevel",
&SetLoggerLevelArgs{
LoggerName: loggerName,
LogLevel: &logLevelArg,
DisplayLevel: &displayLevelArg,
},
res,
options...)
return res.Success, err
}
func (c *client) GetLoggerLevel(
ctx context.Context,
loggerName string,
options ...rpc.Option,
) (map[string]LogAndDisplayLevels, error) {
res := &GetLoggerLevelReply{}
err := c.requester.SendRequest(ctx, "getLoggerLevel", &GetLoggerLevelArgs{
LoggerName: loggerName,
}, res, options...)
return res.LoggerLevels, err
}
func (c *client) GetConfig(ctx context.Context, options ...rpc.Option) (interface{}, error) {
var res interface{}
err := c.requester.SendRequest(ctx, "getConfig", struct{}{}, &res, options...)
return res, err
}