code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_rename_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 AdminRenameNetworkParams struct { 28 Network string `json:"network"` 29 NewName string `json:"newName"` 30 } 31 32 type AdminRenameNetwork struct { 33 networkStore NetworkStore 34 } 35 36 // Handle renames a network. 37 func (h *AdminRenameNetwork) Handle(ctx context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) { 38 params, err := validateRenameNetworkParams(rawParams) 39 if err != nil { 40 return nil, InvalidParams(err) 41 } 42 43 if exist, err := h.networkStore.NetworkExists(params.Network); err != nil { 44 return nil, InternalError(fmt.Errorf("could not verify the network existence: %w", err)) 45 } else if !exist { 46 return nil, InvalidParams(ErrNetworkDoesNotExist) 47 } 48 49 if exist, err := h.networkStore.NetworkExists(params.NewName); err != nil { 50 return nil, InternalError(fmt.Errorf("could not verify the network existence: %w", err)) 51 } else if exist { 52 return nil, InvalidParams(ErrNetworkAlreadyExists) 53 } 54 55 if err := h.networkStore.RenameNetwork(params.Network, params.NewName); err != nil { 56 return nil, InternalError(fmt.Errorf("could not rename the network: %w", err)) 57 } 58 59 return nil, nil 60 } 61 62 func validateRenameNetworkParams(rawParams jsonrpc.Params) (AdminRenameNetworkParams, error) { 63 if rawParams == nil { 64 return AdminRenameNetworkParams{}, ErrParamsRequired 65 } 66 67 params := AdminRenameNetworkParams{} 68 if err := mapstructure.Decode(rawParams, ¶ms); err != nil { 69 return AdminRenameNetworkParams{}, ErrParamsDoNotMatch 70 } 71 72 if params.Network == "" { 73 return AdminRenameNetworkParams{}, ErrNetworkIsRequired 74 } 75 76 if params.NewName == "" { 77 return AdminRenameNetworkParams{}, ErrNewNameIsRequired 78 } 79 80 return params, nil 81 } 82 83 func NewAdminRenameNetwork( 84 networkStore NetworkStore, 85 ) *AdminRenameNetwork { 86 return &AdminRenameNetwork{ 87 networkStore: networkStore, 88 } 89 }