github.com/MetalBlockchain/metalgo@v1.11.9/api/admin/client.go (about)

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