proto: refactor GetStatus method

Signed-off-by: Steffen Vogel <post@steffenvogel.de>
This commit is contained in:
Steffen Vogel
2022-09-28 16:18:40 +02:00
parent eef7ac16f9
commit 4c6a739af1
5 changed files with 21 additions and 20 deletions

View File

@@ -40,10 +40,10 @@ func statusValidArgs(cmd *cobra.Command, args []string, toComplete string) ([]st
rpcConnect(cmd, args)
defer rpcDisconnect(cmd, args)
p := &rpcproto.StatusParams{}
p := &rpcproto.GetStatusParams{}
if len(args) > 0 {
p.Intf = args[0]
p.Interface = args[0]
}
sts, err := rpcClient.GetStatus(context.Background(), p)
@@ -68,10 +68,10 @@ func statusValidArgs(cmd *cobra.Command, args []string, toComplete string) ([]st
}
func status(cmd *cobra.Command, args []string) {
p := &rpcproto.StatusParams{}
p := &rpcproto.GetStatusParams{}
if len(args) > 0 {
p.Intf = args[0]
p.Interface = args[0]
if len(args) > 1 {
pk, err := crypto.ParseKey(args[1])
if err != nil {

View File

@@ -27,8 +27,8 @@ func init() {
func wgShowConf(cmd *cobra.Command, args []string) error {
intfName := args[0]
sts, err := rpcClient.GetStatus(context.Background(), &rpcproto.StatusParams{
Intf: intfName,
sts, err := rpcClient.GetStatus(context.Background(), &rpcproto.GetStatusParams{
Interface: intfName,
})
if err != nil {
return fmt.Errorf("failed RPC request: %w", err)

View File

@@ -50,7 +50,7 @@ func wgShowValidArgs(cmd *cobra.Command, args []string, toComplete string) ([]st
rpcConnect(cmd, args)
defer rpcDisconnect(cmd, args)
sts, err := rpcClient.GetStatus(context.Background(), &rpcproto.StatusParams{})
sts, err := rpcClient.GetStatus(context.Background(), &rpcproto.GetStatusParams{})
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
@@ -106,8 +106,8 @@ func wgShow(cmd *cobra.Command, args []string) error {
field = "all"
}
sts, err := rpcClient.GetStatus(context.Background(), &rpcproto.StatusParams{
Intf: intf,
sts, err := rpcClient.GetStatus(context.Background(), &rpcproto.GetStatusParams{
Interface: intf,
})
if err != nil {
return fmt.Errorf("failed RPC request: %w", err)

View File

@@ -108,7 +108,7 @@ func (s *DaemonServer) Sync(ctx context.Context, params *proto.Empty) (*proto.Em
return &proto.Empty{}, nil
}
func (s *DaemonServer) GetStatus(ctx context.Context, p *rpcproto.StatusParams) (*rpcproto.StatusResp, error) {
func (s *DaemonServer) GetStatus(ctx context.Context, p *rpcproto.GetStatusParams) (*rpcproto.GetStatusResp, error) {
var err error
var pk crypto.Key
@@ -140,13 +140,13 @@ func (s *DaemonServer) GetStatus(ctx context.Context, p *rpcproto.StatusParams)
})
// Check if filters matched anything
if p.Intf != "" && len(qis) == 0 {
return nil, status.Errorf(codes.NotFound, "no such interface '%s'", p.Intf)
if p.Interface != "" && len(qis) == 0 {
return nil, status.Errorf(codes.NotFound, "no such interface '%s'", p.Interface)
} else if pk.IsSet() && len(qis[0].Peers) == 0 {
return nil, status.Errorf(codes.NotFound, "no such peer '%s' for interface '%s'", pk, p.Intf)
return nil, status.Errorf(codes.NotFound, "no such peer '%s' for interface '%s'", pk, p.Interface)
}
return &rpcproto.StatusResp{
return &rpcproto.GetStatusResp{
Interfaces: qis,
}, nil
}

View File

@@ -7,13 +7,13 @@ import "core/interface.proto";
import "common.proto";
import "rpc/event.proto";
message StatusResp {
repeated core.Interface interfaces = 1;
message GetStatusParams {
string interface = 1;
bytes peer = 2;
}
message StatusParams {
string intf = 1;
bytes peer = 2;
message GetStatusResp {
repeated core.Interface interfaces = 1;
}
message SetConfigParams {
@@ -35,7 +35,8 @@ service Daemon {
rpc Stop(Empty) returns (Empty) {}
rpc Restart(Empty) returns (Empty) {}
rpc Sync(Empty) returns (Empty) {}
rpc GetStatus(StatusParams) returns (StatusResp) {}
rpc GetStatus(GetStatusParams) returns (GetStatusResp) {}
rpc SetConfig(SetConfigParams) returns (Empty) {}
rpc GetConfig(GetConfigParams) returns (GetConfigResp) {}
rpc ReloadConfig(Empty) returns (Empty) {}