code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_describe_network.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package api
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  
    22  	"code.vegaprotocol.io/vega/libs/jsonrpc"
    23  
    24  	"github.com/mitchellh/mapstructure"
    25  )
    26  
    27  type AdminDescribeNetworkParams struct {
    28  	Name string `json:"name"`
    29  }
    30  
    31  type AdminDescribeNetwork struct {
    32  	networkStore NetworkStore
    33  }
    34  
    35  func (h *AdminDescribeNetwork) Handle(_ context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
    36  	params, err := validateDescribeNetworkParams(rawParams)
    37  	if err != nil {
    38  		return nil, InvalidParams(err)
    39  	}
    40  
    41  	if exist, err := h.networkStore.NetworkExists(params.Name); err != nil {
    42  		return nil, InternalError(fmt.Errorf("could not verify the network existence: %w", err))
    43  	} else if !exist {
    44  		return nil, InvalidParams(ErrNetworkDoesNotExist)
    45  	}
    46  
    47  	n, err := h.networkStore.GetNetwork(params.Name)
    48  	if err != nil {
    49  		return nil, InternalError(fmt.Errorf("could not retrieve the network configuration: %w", err))
    50  	}
    51  
    52  	resp := AdminNetwork{
    53  		Name:     n.Name,
    54  		Metadata: n.Metadata,
    55  		API: AdminAPIConfig{
    56  			GRPC: AdminGRPCConfig{
    57  				Hosts: n.API.GRPC.Hosts,
    58  			},
    59  			REST: AdminRESTConfig{
    60  				Hosts: n.API.REST.Hosts,
    61  			},
    62  			GraphQL: AdminGraphQLConfig{
    63  				Hosts: n.API.GraphQL.Hosts,
    64  			},
    65  		},
    66  		Apps: AdminAppConfig{
    67  			Explorer:   n.Apps.Explorer,
    68  			Console:    n.Apps.Console,
    69  			Governance: n.Apps.Governance,
    70  		},
    71  	}
    72  
    73  	// make sure nil maps come through as empty slices
    74  	if resp.API.GRPC.Hosts == nil {
    75  		resp.API.GRPC.Hosts = []string{}
    76  	}
    77  	if resp.API.GraphQL.Hosts == nil {
    78  		resp.API.GraphQL.Hosts = []string{}
    79  	}
    80  	if resp.API.REST.Hosts == nil {
    81  		resp.API.REST.Hosts = []string{}
    82  	}
    83  
    84  	return resp, nil
    85  }
    86  
    87  func validateDescribeNetworkParams(rawParams jsonrpc.Params) (AdminDescribeNetworkParams, error) {
    88  	if rawParams == nil {
    89  		return AdminDescribeNetworkParams{}, ErrParamsRequired
    90  	}
    91  
    92  	params := AdminDescribeNetworkParams{}
    93  	if err := mapstructure.Decode(rawParams, &params); err != nil {
    94  		return AdminDescribeNetworkParams{}, ErrParamsDoNotMatch
    95  	}
    96  
    97  	if params.Name == "" {
    98  		return AdminDescribeNetworkParams{}, ErrNetworkNameIsRequired
    99  	}
   100  
   101  	return params, nil
   102  }
   103  
   104  func NewAdminDescribeNetwork(
   105  	networkStore NetworkStore,
   106  ) *AdminDescribeNetwork {
   107  	return &AdminDescribeNetwork{
   108  		networkStore: networkStore,
   109  	}
   110  }