code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_update_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  	"code.vegaprotocol.io/vega/wallet/network"
    24  
    25  	"github.com/mitchellh/mapstructure"
    26  )
    27  
    28  type AdminUpdateNetwork struct {
    29  	networkStore NetworkStore
    30  }
    31  
    32  func (h *AdminUpdateNetwork) Handle(_ context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
    33  	updatedNetwork, err := validateUpdateNetworkParams(rawParams)
    34  	if err != nil {
    35  		return nil, InvalidParams(err)
    36  	}
    37  
    38  	if exists, err := h.networkStore.NetworkExists(updatedNetwork.Name); err != nil {
    39  		return nil, InternalError(fmt.Errorf("could not verify the network existence: %w", err))
    40  	} else if !exists {
    41  		return nil, InvalidParams(ErrNetworkDoesNotExist)
    42  	}
    43  
    44  	if err := h.networkStore.SaveNetwork(&updatedNetwork); err != nil {
    45  		return nil, InternalError(fmt.Errorf("could not save the network: %w", err))
    46  	}
    47  	return nil, nil
    48  }
    49  
    50  func validateUpdateNetworkParams(rawParams jsonrpc.Params) (network.Network, error) {
    51  	if rawParams == nil {
    52  		return network.Network{}, ErrParamsRequired
    53  	}
    54  
    55  	params := AdminNetwork{}
    56  	if err := mapstructure.Decode(rawParams, &params); err != nil {
    57  		return network.Network{}, ErrParamsDoNotMatch
    58  	}
    59  
    60  	if params.Name == "" {
    61  		return network.Network{}, ErrNetworkNameIsRequired
    62  	}
    63  
    64  	net := network.Network{
    65  		Name:     params.Name,
    66  		Metadata: params.Metadata,
    67  		API: network.APIConfig{
    68  			GRPC: network.HostConfig{
    69  				Hosts: params.API.GRPC.Hosts,
    70  			},
    71  			REST: network.HostConfig{
    72  				Hosts: params.API.REST.Hosts,
    73  			},
    74  			GraphQL: network.HostConfig{
    75  				Hosts: params.API.GraphQL.Hosts,
    76  			},
    77  		},
    78  		Apps: network.AppsConfig{
    79  			Console:    params.Apps.Console,
    80  			Governance: params.Apps.Governance,
    81  			Explorer:   params.Apps.Explorer,
    82  		},
    83  	}
    84  
    85  	if err := net.EnsureCanConnectGRPCNode(); err != nil {
    86  		return network.Network{}, err
    87  	}
    88  
    89  	return net, nil
    90  }
    91  
    92  func NewAdminUpdateNetwork(
    93  	networkStore NetworkStore,
    94  ) *AdminUpdateNetwork {
    95  	return &AdminUpdateNetwork{
    96  		networkStore: networkStore,
    97  	}
    98  }