-
Notifications
You must be signed in to change notification settings - Fork 664
/
client.go
141 lines (122 loc) · 4.89 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
// Copyright (C) 2019-2022, 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)(nil)
// Client interface for the Avalanche Platform Info API Endpoint
type Client interface {
StartCPUProfiler(context.Context, ...rpc.Option) error
StopCPUProfiler(context.Context, ...rpc.Option) error
MemoryProfile(context.Context, ...rpc.Option) error
LockProfile(context.Context, ...rpc.Option) error
Alias(ctx context.Context, endpoint string, alias string, options ...rpc.Option) error
AliasChain(ctx context.Context, chainID string, alias string, options ...rpc.Option) error
GetChainAliases(ctx context.Context, chainID string, options ...rpc.Option) ([]string, error)
Stacktrace(context.Context, ...rpc.Option) 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) 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",
)}
}
func (c *client) StartCPUProfiler(ctx context.Context, options ...rpc.Option) error {
return c.requester.SendRequest(ctx, "admin.startCPUProfiler", struct{}{}, &api.EmptyReply{}, options...)
}
func (c *client) StopCPUProfiler(ctx context.Context, options ...rpc.Option) error {
return c.requester.SendRequest(ctx, "admin.stopCPUProfiler", struct{}{}, &api.EmptyReply{}, options...)
}
func (c *client) MemoryProfile(ctx context.Context, options ...rpc.Option) error {
return c.requester.SendRequest(ctx, "admin.memoryProfile", struct{}{}, &api.EmptyReply{}, options...)
}
func (c *client) LockProfile(ctx context.Context, options ...rpc.Option) error {
return c.requester.SendRequest(ctx, "admin.lockProfile", struct{}{}, &api.EmptyReply{}, options...)
}
func (c *client) Alias(ctx context.Context, endpoint, alias string, options ...rpc.Option) error {
return c.requester.SendRequest(ctx, "admin.alias", &AliasArgs{
Endpoint: endpoint,
Alias: alias,
}, &api.EmptyReply{}, options...)
}
func (c *client) AliasChain(ctx context.Context, chain, alias string, options ...rpc.Option) error {
return c.requester.SendRequest(ctx, "admin.aliasChain", &AliasChainArgs{
Chain: chain,
Alias: alias,
}, &api.EmptyReply{}, options...)
}
func (c *client) GetChainAliases(ctx context.Context, chain string, options ...rpc.Option) ([]string, error) {
res := &GetChainAliasesReply{}
err := c.requester.SendRequest(ctx, "admin.getChainAliases", &GetChainAliasesArgs{
Chain: chain,
}, res, options...)
return res.Aliases, err
}
func (c *client) Stacktrace(ctx context.Context, options ...rpc.Option) error {
return c.requester.SendRequest(ctx, "admin.stacktrace", struct{}{}, &api.EmptyReply{}, options...)
}
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, "admin.loadVMs", struct{}{}, res, options...)
return res.NewVMs, res.FailedVMs, err
}
func (c *client) SetLoggerLevel(
ctx context.Context,
loggerName,
logLevel,
displayLevel string,
options ...rpc.Option,
) error {
var (
logLevelArg logging.Level
displayLevelArg logging.Level
err error
)
if len(logLevel) > 0 {
logLevelArg, err = logging.ToLevel(logLevel)
if err != nil {
return fmt.Errorf("couldn't parse %q to log level", logLevel)
}
}
if len(displayLevel) > 0 {
displayLevelArg, err = logging.ToLevel(displayLevel)
if err != nil {
return fmt.Errorf("couldn't parse %q to log level", displayLevel)
}
}
return c.requester.SendRequest(ctx, "admin.setLoggerLevel", &SetLoggerLevelArgs{
LoggerName: loggerName,
LogLevel: &logLevelArg,
DisplayLevel: &displayLevelArg,
}, &api.EmptyReply{}, options...)
}
func (c *client) GetLoggerLevel(
ctx context.Context,
loggerName string,
options ...rpc.Option,
) (map[string]LogAndDisplayLevels, error) {
res := &GetLoggerLevelReply{}
err := c.requester.SendRequest(ctx, "admin.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, "admin.getConfig", struct{}{}, &res, options...)
return res, err
}