From 1405cd2e8ee559ec75205b9eed1686bf31c29ba0 Mon Sep 17 00:00:00 2001 From: Dawei Huang Date: Wed, 8 Jan 2025 18:30:10 +0000 Subject: [PATCH 1/5] format and naming clean up for gnoi_client.go. --- gnoi_client/gnoi_client.go | 205 +++++++++++++++++++------------------ 1 file changed, 105 insertions(+), 100 deletions(-) diff --git a/gnoi_client/gnoi_client.go b/gnoi_client/gnoi_client.go index 7d480e2b..8a9f7f8e 100644 --- a/gnoi_client/gnoi_client.go +++ b/gnoi_client/gnoi_client.go @@ -1,51 +1,53 @@ package main import ( - "google.golang.org/grpc" - gnoi_system_pb "github.com/openconfig/gnoi/system" + "context" + "encoding/json" + "flag" + "fmt" + "github.com/google/gnxi/utils/credentials" gnoi_file_pb "github.com/openconfig/gnoi/file" + gnoi_system_pb "github.com/openconfig/gnoi/system" spb "github.com/sonic-net/sonic-gnmi/proto/gnoi" spb_jwt "github.com/sonic-net/sonic-gnmi/proto/gnoi/jwt" - "context" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" "os" "os/signal" - "fmt" - "flag" - "google.golang.org/grpc/metadata" - "github.com/google/gnxi/utils/credentials" - "encoding/json" ) var ( - module = flag.String("module", "System", "gNOI Module") - rpc = flag.String("rpc", "Time", "rpc call in specified module to call") - target = flag.String("target", "localhost:8080", "Address:port of gNOI Server") - args = flag.String("jsonin", "", "RPC Arguments in json format") - jwtToken = flag.String("jwt_token", "", "JWT Token if required") + module = flag.String("module", "System", "gNOI Module") + rpc = flag.String("rpc", "Time", "rpc call in specified module to call") + target = flag.String("target", "localhost:8080", "Address:port of gNOI Server") + args = flag.String("jsonin", "", "RPC Arguments in json format") + jwtToken = flag.String("jwt_token", "", "JWT Token if required") targetName = flag.String("target_name", "hostname.com", "The target name use to verify the hostname returned by TLS handshake") ) + func setUserCreds(ctx context.Context) context.Context { if len(*jwtToken) > 0 { ctx = metadata.AppendToOutgoingContext(ctx, "access_token", *jwtToken) } return ctx } + func main() { flag.Parse() opts := credentials.ClientCredentials(*targetName) - ctx, cancel := context.WithCancel(context.Background()) - go func() { - c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt) - <-c - cancel() - }() + ctx, cancel := context.WithCancel(context.Background()) + go func() { + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + <-c + cancel() + }() conn, err := grpc.Dial(*target, opts...) if err != nil { panic(err.Error()) } - + switch *module { case "System": sc := gnoi_system_pb.NewSystemClient(conn) @@ -59,7 +61,7 @@ func main() { case "RebootStatus": systemRebootStatus(sc, ctx) case "KillProcess": - killProcess(sc, ctx) + systemKillProcess(sc, ctx) default: panic("Invalid RPC Name") } @@ -78,25 +80,25 @@ func main() { sonicShowTechSupport(sc, ctx) case "copyConfig": sc := spb.NewSonicServiceClient(conn) - copyConfig(sc, ctx) + sonicCopyConfig(sc, ctx) case "authenticate": sc := spb_jwt.NewSonicJwtServiceClient(conn) - authenticate(sc, ctx) + sonicAuthenticate(sc, ctx) case "imageInstall": sc := spb.NewSonicServiceClient(conn) - imageInstall(sc, ctx) + sonicImageInstall(sc, ctx) case "imageDefault": sc := spb.NewSonicServiceClient(conn) - imageDefault(sc, ctx) + sonicImageDefault(sc, ctx) case "imageRemove": sc := spb.NewSonicServiceClient(conn) - imageRemove(sc, ctx) + sonicImageRemove(sc, ctx) case "refresh": sc := spb_jwt.NewSonicJwtServiceClient(conn) - refresh(sc, ctx) + sonicRefresh(sc, ctx) case "clearNeighbors": sc := spb.NewSonicServiceClient(conn) - clearNeighbors(sc, ctx) + sonicClearNeighbors(sc, ctx) default: panic("Invalid RPC Name") } @@ -106,10 +108,11 @@ func main() { } +// RPC for System Services func systemTime(sc gnoi_system_pb.SystemClient, ctx context.Context) { fmt.Println("System Time") ctx = setUserCreds(ctx) - resp,err := sc.Time(ctx, new(gnoi_system_pb.TimeRequest)) + resp, err := sc.Time(ctx, new(gnoi_system_pb.TimeRequest)) if err != nil { panic(err.Error()) } @@ -120,45 +123,26 @@ func systemTime(sc gnoi_system_pb.SystemClient, ctx context.Context) { fmt.Println(string(respstr)) } -func killProcess(sc gnoi_system_pb.SystemClient, ctx context.Context) { +func systemKillProcess(sc gnoi_system_pb.SystemClient, ctx context.Context) { fmt.Println("Kill Process with optional restart") ctx = setUserCreds(ctx) - req := &gnoi_system_pb.KillProcessRequest {} + req := &gnoi_system_pb.KillProcessRequest{} err := json.Unmarshal([]byte(*args), req) if err != nil { panic(err.Error()) } - _,err = sc.KillProcess(ctx, req) + _, err = sc.KillProcess(ctx, req) if err != nil { panic(err.Error()) } } -func fileStat(fc gnoi_file_pb.FileClient, ctx context.Context) { - fmt.Println("File Stat") - ctx = setUserCreds(ctx) - req := &gnoi_file_pb.StatRequest {} - err := json.Unmarshal([]byte(*args), req) - if err != nil { - panic(err.Error()) - } - resp,err := fc.Stat(ctx, req) - if err != nil { - panic(err.Error()) - } - respstr, err := json.Marshal(resp) - if err != nil { - panic(err.Error()) - } - fmt.Println(string(respstr)) -} - func systemReboot(sc gnoi_system_pb.SystemClient, ctx context.Context) { fmt.Println("System Reboot") ctx = setUserCreds(ctx) - req := &gnoi_system_pb.RebootRequest {} + req := &gnoi_system_pb.RebootRequest{} json.Unmarshal([]byte(*args), req) - _,err := sc.Reboot(ctx, req) + _, err := sc.Reboot(ctx, req) if err != nil { panic(err.Error()) } @@ -167,9 +151,9 @@ func systemReboot(sc gnoi_system_pb.SystemClient, ctx context.Context) { func systemCancelReboot(sc gnoi_system_pb.SystemClient, ctx context.Context) { fmt.Println("System CancelReboot") ctx = setUserCreds(ctx) - req := &gnoi_system_pb.CancelRebootRequest {} + req := &gnoi_system_pb.CancelRebootRequest{} json.Unmarshal([]byte(*args), req) - resp,err := sc.CancelReboot(ctx, req) + resp, err := sc.CancelReboot(ctx, req) if err != nil { panic(err.Error()) } @@ -183,8 +167,8 @@ func systemCancelReboot(sc gnoi_system_pb.SystemClient, ctx context.Context) { func systemRebootStatus(sc gnoi_system_pb.SystemClient, ctx context.Context) { fmt.Println("System RebootStatus") ctx = setUserCreds(ctx) - req := &gnoi_system_pb.RebootStatusRequest {} - resp,err := sc.RebootStatus(ctx, req) + req := &gnoi_system_pb.RebootStatusRequest{} + resp, err := sc.RebootStatus(ctx, req) if err != nil { panic(err.Error()) } @@ -195,18 +179,37 @@ func systemRebootStatus(sc gnoi_system_pb.SystemClient, ctx context.Context) { fmt.Println(string(respstr)) } +// RPC for File Services +func fileStat(fc gnoi_file_pb.FileClient, ctx context.Context) { + fmt.Println("File Stat") + ctx = setUserCreds(ctx) + req := &gnoi_file_pb.StatRequest{} + err := json.Unmarshal([]byte(*args), req) + if err != nil { + panic(err.Error()) + } + resp, err := fc.Stat(ctx, req) + if err != nil { + panic(err.Error()) + } + respstr, err := json.Marshal(resp) + if err != nil { + panic(err.Error()) + } + fmt.Println(string(respstr)) +} + +// RPC for Sonic Services func sonicShowTechSupport(sc spb.SonicServiceClient, ctx context.Context) { fmt.Println("Sonic ShowTechsupport") ctx = setUserCreds(ctx) - req := &spb.TechsupportRequest { - Input: &spb.TechsupportRequest_Input{ - - }, + req := &spb.TechsupportRequest{ + Input: &spb.TechsupportRequest_Input{}, } json.Unmarshal([]byte(*args), req) - - resp,err := sc.ShowTechsupport(ctx, req) + + resp, err := sc.ShowTechsupport(ctx, req) if err != nil { panic(err.Error()) } @@ -217,7 +220,7 @@ func sonicShowTechSupport(sc spb.SonicServiceClient, ctx context.Context) { fmt.Println(string(respstr)) } -func copyConfig(sc spb.SonicServiceClient, ctx context.Context) { +func sonicCopyConfig(sc spb.SonicServiceClient, ctx context.Context) { fmt.Println("Sonic CopyConfig") ctx = setUserCreds(ctx) req := &spb.CopyConfigRequest{ @@ -225,7 +228,7 @@ func copyConfig(sc spb.SonicServiceClient, ctx context.Context) { } json.Unmarshal([]byte(*args), req) - resp,err := sc.CopyConfig(ctx, req) + resp, err := sc.CopyConfig(ctx, req) if err != nil { panic(err.Error()) @@ -236,7 +239,8 @@ func copyConfig(sc spb.SonicServiceClient, ctx context.Context) { } fmt.Println(string(respstr)) } -func imageInstall(sc spb.SonicServiceClient, ctx context.Context) { + +func sonicImageInstall(sc spb.SonicServiceClient, ctx context.Context) { fmt.Println("Sonic ImageInstall") ctx = setUserCreds(ctx) req := &spb.ImageInstallRequest{ @@ -244,7 +248,7 @@ func imageInstall(sc spb.SonicServiceClient, ctx context.Context) { } json.Unmarshal([]byte(*args), req) - resp,err := sc.ImageInstall(ctx, req) + resp, err := sc.ImageInstall(ctx, req) if err != nil { panic(err.Error()) @@ -255,7 +259,8 @@ func imageInstall(sc spb.SonicServiceClient, ctx context.Context) { } fmt.Println(string(respstr)) } -func imageRemove(sc spb.SonicServiceClient, ctx context.Context) { + +func sonicImageRemove(sc spb.SonicServiceClient, ctx context.Context) { fmt.Println("Sonic ImageRemove") ctx = setUserCreds(ctx) req := &spb.ImageRemoveRequest{ @@ -263,7 +268,7 @@ func imageRemove(sc spb.SonicServiceClient, ctx context.Context) { } json.Unmarshal([]byte(*args), req) - resp,err := sc.ImageRemove(ctx, req) + resp, err := sc.ImageRemove(ctx, req) if err != nil { panic(err.Error()) @@ -275,7 +280,7 @@ func imageRemove(sc spb.SonicServiceClient, ctx context.Context) { fmt.Println(string(respstr)) } -func imageDefault(sc spb.SonicServiceClient, ctx context.Context) { +func sonicImageDefault(sc spb.SonicServiceClient, ctx context.Context) { fmt.Println("Sonic ImageDefault") ctx = setUserCreds(ctx) req := &spb.ImageDefaultRequest{ @@ -283,7 +288,7 @@ func imageDefault(sc spb.SonicServiceClient, ctx context.Context) { } json.Unmarshal([]byte(*args), req) - resp,err := sc.ImageDefault(ctx, req) + resp, err := sc.ImageDefault(ctx, req) if err != nil { panic(err.Error()) @@ -295,14 +300,14 @@ func imageDefault(sc spb.SonicServiceClient, ctx context.Context) { fmt.Println(string(respstr)) } -func authenticate(sc spb_jwt.SonicJwtServiceClient, ctx context.Context) { +func sonicAuthenticate(sc spb_jwt.SonicJwtServiceClient, ctx context.Context) { fmt.Println("Sonic Authenticate") ctx = setUserCreds(ctx) - req := &spb_jwt.AuthenticateRequest {} - + req := &spb_jwt.AuthenticateRequest{} + json.Unmarshal([]byte(*args), req) - - resp,err := sc.Authenticate(ctx, req) + + resp, err := sc.Authenticate(ctx, req) if err != nil { panic(err.Error()) } @@ -313,14 +318,14 @@ func authenticate(sc spb_jwt.SonicJwtServiceClient, ctx context.Context) { fmt.Println(string(respstr)) } -func refresh(sc spb_jwt.SonicJwtServiceClient, ctx context.Context) { +func sonicRefresh(sc spb_jwt.SonicJwtServiceClient, ctx context.Context) { fmt.Println("Sonic Refresh") ctx = setUserCreds(ctx) - req := &spb_jwt.RefreshRequest {} - + req := &spb_jwt.RefreshRequest{} + json.Unmarshal([]byte(*args), req) - resp,err := sc.Refresh(ctx, req) + resp, err := sc.Refresh(ctx, req) if err != nil { panic(err.Error()) } @@ -331,22 +336,22 @@ func refresh(sc spb_jwt.SonicJwtServiceClient, ctx context.Context) { fmt.Println(string(respstr)) } -func clearNeighbors(sc spb.SonicServiceClient, ctx context.Context) { - fmt.Println("Sonic ClearNeighbors") - ctx = setUserCreds(ctx) - req := &spb.ClearNeighborsRequest{ - Input: &spb.ClearNeighborsRequest_Input{}, - } - json.Unmarshal([]byte(*args), req) - - resp,err := sc.ClearNeighbors(ctx, req) - - if err != nil { - panic(err.Error()) - } - respstr, err := json.Marshal(resp) - if err != nil { - panic(err.Error()) - } - fmt.Println(string(respstr)) -} \ No newline at end of file +func sonicClearNeighbors(sc spb.SonicServiceClient, ctx context.Context) { + fmt.Println("Sonic ClearNeighbors") + ctx = setUserCreds(ctx) + req := &spb.ClearNeighborsRequest{ + Input: &spb.ClearNeighborsRequest_Input{}, + } + json.Unmarshal([]byte(*args), req) + + resp, err := sc.ClearNeighbors(ctx, req) + + if err != nil { + panic(err.Error()) + } + respstr, err := json.Marshal(resp) + if err != nil { + panic(err.Error()) + } + fmt.Println(string(respstr)) +} From 35df113288c81e7e2a6de7ad493d0923e281c1d4 Mon Sep 17 00:00:00 2001 From: Dawei Huang Date: Thu, 9 Jan 2025 19:32:16 +0000 Subject: [PATCH 2/5] seperate flags into a package config. --- gnoi_client/config/flag.go | 18 +++++++++++++ gnoi_client/gnoi_client.go | 53 ++++++++++++++++---------------------- 2 files changed, 40 insertions(+), 31 deletions(-) create mode 100644 gnoi_client/config/flag.go diff --git a/gnoi_client/config/flag.go b/gnoi_client/config/flag.go new file mode 100644 index 00000000..c9f32a42 --- /dev/null +++ b/gnoi_client/config/flag.go @@ -0,0 +1,18 @@ +package config + +import ( + "flag" +) + +var ( + Module = flag.String("module", "System", "gNOI Module") + Rpc = flag.String("rpc", "Time", "rpc call in specified module to call") + Target = flag.String("target", "localhost:8080", "Address:port of gNOI Server") + Args = flag.String("jsonin", "", "RPC Arguments in json format") + JwtToken = flag.String("jwt_token", "", "JWT Token if required") + TargetName = flag.String("target_name", "hostname.com", "The target name use to verify the hostname returned by TLS handshake") +) + +func ParseFlag() { + flag.Parse() +} \ No newline at end of file diff --git a/gnoi_client/gnoi_client.go b/gnoi_client/gnoi_client.go index 8a9f7f8e..fda6f16c 100644 --- a/gnoi_client/gnoi_client.go +++ b/gnoi_client/gnoi_client.go @@ -3,38 +3,29 @@ package main import ( "context" "encoding/json" - "flag" "fmt" "github.com/google/gnxi/utils/credentials" gnoi_file_pb "github.com/openconfig/gnoi/file" gnoi_system_pb "github.com/openconfig/gnoi/system" spb "github.com/sonic-net/sonic-gnmi/proto/gnoi" spb_jwt "github.com/sonic-net/sonic-gnmi/proto/gnoi/jwt" + "github.com/sonic-net/sonic-gnmi/gnoi_client/config" "google.golang.org/grpc" "google.golang.org/grpc/metadata" "os" "os/signal" ) -var ( - module = flag.String("module", "System", "gNOI Module") - rpc = flag.String("rpc", "Time", "rpc call in specified module to call") - target = flag.String("target", "localhost:8080", "Address:port of gNOI Server") - args = flag.String("jsonin", "", "RPC Arguments in json format") - jwtToken = flag.String("jwt_token", "", "JWT Token if required") - targetName = flag.String("target_name", "hostname.com", "The target name use to verify the hostname returned by TLS handshake") -) - func setUserCreds(ctx context.Context) context.Context { - if len(*jwtToken) > 0 { - ctx = metadata.AppendToOutgoingContext(ctx, "access_token", *jwtToken) + if len(*config.JwtToken) > 0 { + ctx = metadata.AppendToOutgoingContext(ctx, "access_token", *config.JwtToken) } return ctx } func main() { - flag.Parse() - opts := credentials.ClientCredentials(*targetName) + config.ParseFlag() + opts := credentials.ClientCredentials(*config.TargetName) ctx, cancel := context.WithCancel(context.Background()) go func() { @@ -43,15 +34,15 @@ func main() { <-c cancel() }() - conn, err := grpc.Dial(*target, opts...) + conn, err := grpc.Dial(*config.Target, opts...) if err != nil { panic(err.Error()) } - switch *module { + switch *config.Module { case "System": sc := gnoi_system_pb.NewSystemClient(conn) - switch *rpc { + switch *config.Rpc { case "Time": systemTime(sc, ctx) case "Reboot": @@ -67,14 +58,14 @@ func main() { } case "File": fc := gnoi_file_pb.NewFileClient(conn) - switch *rpc { + switch *config.Rpc { case "Stat": fileStat(fc, ctx) default: panic("Invalid RPC Name") } case "Sonic": - switch *rpc { + switch *config.Rpc { case "showtechsupport": sc := spb.NewSonicServiceClient(conn) sonicShowTechSupport(sc, ctx) @@ -127,7 +118,7 @@ func systemKillProcess(sc gnoi_system_pb.SystemClient, ctx context.Context) { fmt.Println("Kill Process with optional restart") ctx = setUserCreds(ctx) req := &gnoi_system_pb.KillProcessRequest{} - err := json.Unmarshal([]byte(*args), req) + err := json.Unmarshal([]byte(*config.Args), req) if err != nil { panic(err.Error()) } @@ -141,7 +132,7 @@ func systemReboot(sc gnoi_system_pb.SystemClient, ctx context.Context) { fmt.Println("System Reboot") ctx = setUserCreds(ctx) req := &gnoi_system_pb.RebootRequest{} - json.Unmarshal([]byte(*args), req) + json.Unmarshal([]byte(*config.Args), req) _, err := sc.Reboot(ctx, req) if err != nil { panic(err.Error()) @@ -152,7 +143,7 @@ func systemCancelReboot(sc gnoi_system_pb.SystemClient, ctx context.Context) { fmt.Println("System CancelReboot") ctx = setUserCreds(ctx) req := &gnoi_system_pb.CancelRebootRequest{} - json.Unmarshal([]byte(*args), req) + json.Unmarshal([]byte(*config.Args), req) resp, err := sc.CancelReboot(ctx, req) if err != nil { panic(err.Error()) @@ -184,7 +175,7 @@ func fileStat(fc gnoi_file_pb.FileClient, ctx context.Context) { fmt.Println("File Stat") ctx = setUserCreds(ctx) req := &gnoi_file_pb.StatRequest{} - err := json.Unmarshal([]byte(*args), req) + err := json.Unmarshal([]byte(*config.Args), req) if err != nil { panic(err.Error()) } @@ -207,7 +198,7 @@ func sonicShowTechSupport(sc spb.SonicServiceClient, ctx context.Context) { Input: &spb.TechsupportRequest_Input{}, } - json.Unmarshal([]byte(*args), req) + json.Unmarshal([]byte(*config.Args), req) resp, err := sc.ShowTechsupport(ctx, req) if err != nil { @@ -226,7 +217,7 @@ func sonicCopyConfig(sc spb.SonicServiceClient, ctx context.Context) { req := &spb.CopyConfigRequest{ Input: &spb.CopyConfigRequest_Input{}, } - json.Unmarshal([]byte(*args), req) + json.Unmarshal([]byte(*config.Args), req) resp, err := sc.CopyConfig(ctx, req) @@ -246,7 +237,7 @@ func sonicImageInstall(sc spb.SonicServiceClient, ctx context.Context) { req := &spb.ImageInstallRequest{ Input: &spb.ImageInstallRequest_Input{}, } - json.Unmarshal([]byte(*args), req) + json.Unmarshal([]byte(*config.Args), req) resp, err := sc.ImageInstall(ctx, req) @@ -266,7 +257,7 @@ func sonicImageRemove(sc spb.SonicServiceClient, ctx context.Context) { req := &spb.ImageRemoveRequest{ Input: &spb.ImageRemoveRequest_Input{}, } - json.Unmarshal([]byte(*args), req) + json.Unmarshal([]byte(*config.Args), req) resp, err := sc.ImageRemove(ctx, req) @@ -286,7 +277,7 @@ func sonicImageDefault(sc spb.SonicServiceClient, ctx context.Context) { req := &spb.ImageDefaultRequest{ Input: &spb.ImageDefaultRequest_Input{}, } - json.Unmarshal([]byte(*args), req) + json.Unmarshal([]byte(*config.Args), req) resp, err := sc.ImageDefault(ctx, req) @@ -305,7 +296,7 @@ func sonicAuthenticate(sc spb_jwt.SonicJwtServiceClient, ctx context.Context) { ctx = setUserCreds(ctx) req := &spb_jwt.AuthenticateRequest{} - json.Unmarshal([]byte(*args), req) + json.Unmarshal([]byte(*config.Args), req) resp, err := sc.Authenticate(ctx, req) if err != nil { @@ -323,7 +314,7 @@ func sonicRefresh(sc spb_jwt.SonicJwtServiceClient, ctx context.Context) { ctx = setUserCreds(ctx) req := &spb_jwt.RefreshRequest{} - json.Unmarshal([]byte(*args), req) + json.Unmarshal([]byte(*config.Args), req) resp, err := sc.Refresh(ctx, req) if err != nil { @@ -342,7 +333,7 @@ func sonicClearNeighbors(sc spb.SonicServiceClient, ctx context.Context) { req := &spb.ClearNeighborsRequest{ Input: &spb.ClearNeighborsRequest_Input{}, } - json.Unmarshal([]byte(*args), req) + json.Unmarshal([]byte(*config.Args), req) resp, err := sc.ClearNeighbors(ctx, req) From 1f77986026d1a7d1ba57289cf2a8e0aafd311dd2 Mon Sep 17 00:00:00 2001 From: Dawei Huang Date: Thu, 9 Jan 2025 19:52:36 +0000 Subject: [PATCH 3/5] seperate util and system module. --- gnoi_client/gnoi_client.go | 40 ++++++++++++++++---------------------- gnoi_client/system/time.go | 25 ++++++++++++++++++++++++ gnoi_client/utils/creds.go | 14 +++++++++++++ 3 files changed, 56 insertions(+), 23 deletions(-) create mode 100644 gnoi_client/system/time.go create mode 100644 gnoi_client/utils/creds.go diff --git a/gnoi_client/gnoi_client.go b/gnoi_client/gnoi_client.go index fda6f16c..0087cbf2 100644 --- a/gnoi_client/gnoi_client.go +++ b/gnoi_client/gnoi_client.go @@ -10,19 +10,13 @@ import ( spb "github.com/sonic-net/sonic-gnmi/proto/gnoi" spb_jwt "github.com/sonic-net/sonic-gnmi/proto/gnoi/jwt" "github.com/sonic-net/sonic-gnmi/gnoi_client/config" + "github.com/sonic-net/sonic-gnmi/gnoi_client/utils" + "github.com/sonic-net/sonic-gnmi/gnoi_client/system" "google.golang.org/grpc" - "google.golang.org/grpc/metadata" "os" "os/signal" ) -func setUserCreds(ctx context.Context) context.Context { - if len(*config.JwtToken) > 0 { - ctx = metadata.AppendToOutgoingContext(ctx, "access_token", *config.JwtToken) - } - return ctx -} - func main() { config.ParseFlag() opts := credentials.ClientCredentials(*config.TargetName) @@ -44,7 +38,7 @@ func main() { sc := gnoi_system_pb.NewSystemClient(conn) switch *config.Rpc { case "Time": - systemTime(sc, ctx) + system.Time(conn, ctx) case "Reboot": systemReboot(sc, ctx) case "CancelReboot": @@ -102,7 +96,7 @@ func main() { // RPC for System Services func systemTime(sc gnoi_system_pb.SystemClient, ctx context.Context) { fmt.Println("System Time") - ctx = setUserCreds(ctx) + ctx = utils.SetUserCreds(ctx) resp, err := sc.Time(ctx, new(gnoi_system_pb.TimeRequest)) if err != nil { panic(err.Error()) @@ -116,7 +110,7 @@ func systemTime(sc gnoi_system_pb.SystemClient, ctx context.Context) { func systemKillProcess(sc gnoi_system_pb.SystemClient, ctx context.Context) { fmt.Println("Kill Process with optional restart") - ctx = setUserCreds(ctx) + ctx = utils.SetUserCreds(ctx) req := &gnoi_system_pb.KillProcessRequest{} err := json.Unmarshal([]byte(*config.Args), req) if err != nil { @@ -130,7 +124,7 @@ func systemKillProcess(sc gnoi_system_pb.SystemClient, ctx context.Context) { func systemReboot(sc gnoi_system_pb.SystemClient, ctx context.Context) { fmt.Println("System Reboot") - ctx = setUserCreds(ctx) + ctx = utils.SetUserCreds(ctx) req := &gnoi_system_pb.RebootRequest{} json.Unmarshal([]byte(*config.Args), req) _, err := sc.Reboot(ctx, req) @@ -141,7 +135,7 @@ func systemReboot(sc gnoi_system_pb.SystemClient, ctx context.Context) { func systemCancelReboot(sc gnoi_system_pb.SystemClient, ctx context.Context) { fmt.Println("System CancelReboot") - ctx = setUserCreds(ctx) + ctx = utils.SetUserCreds(ctx) req := &gnoi_system_pb.CancelRebootRequest{} json.Unmarshal([]byte(*config.Args), req) resp, err := sc.CancelReboot(ctx, req) @@ -157,7 +151,7 @@ func systemCancelReboot(sc gnoi_system_pb.SystemClient, ctx context.Context) { func systemRebootStatus(sc gnoi_system_pb.SystemClient, ctx context.Context) { fmt.Println("System RebootStatus") - ctx = setUserCreds(ctx) + ctx = utils.SetUserCreds(ctx) req := &gnoi_system_pb.RebootStatusRequest{} resp, err := sc.RebootStatus(ctx, req) if err != nil { @@ -173,7 +167,7 @@ func systemRebootStatus(sc gnoi_system_pb.SystemClient, ctx context.Context) { // RPC for File Services func fileStat(fc gnoi_file_pb.FileClient, ctx context.Context) { fmt.Println("File Stat") - ctx = setUserCreds(ctx) + ctx = utils.SetUserCreds(ctx) req := &gnoi_file_pb.StatRequest{} err := json.Unmarshal([]byte(*config.Args), req) if err != nil { @@ -193,7 +187,7 @@ func fileStat(fc gnoi_file_pb.FileClient, ctx context.Context) { // RPC for Sonic Services func sonicShowTechSupport(sc spb.SonicServiceClient, ctx context.Context) { fmt.Println("Sonic ShowTechsupport") - ctx = setUserCreds(ctx) + ctx = utils.SetUserCreds(ctx) req := &spb.TechsupportRequest{ Input: &spb.TechsupportRequest_Input{}, } @@ -213,7 +207,7 @@ func sonicShowTechSupport(sc spb.SonicServiceClient, ctx context.Context) { func sonicCopyConfig(sc spb.SonicServiceClient, ctx context.Context) { fmt.Println("Sonic CopyConfig") - ctx = setUserCreds(ctx) + ctx = utils.SetUserCreds(ctx) req := &spb.CopyConfigRequest{ Input: &spb.CopyConfigRequest_Input{}, } @@ -233,7 +227,7 @@ func sonicCopyConfig(sc spb.SonicServiceClient, ctx context.Context) { func sonicImageInstall(sc spb.SonicServiceClient, ctx context.Context) { fmt.Println("Sonic ImageInstall") - ctx = setUserCreds(ctx) + ctx = utils.SetUserCreds(ctx) req := &spb.ImageInstallRequest{ Input: &spb.ImageInstallRequest_Input{}, } @@ -253,7 +247,7 @@ func sonicImageInstall(sc spb.SonicServiceClient, ctx context.Context) { func sonicImageRemove(sc spb.SonicServiceClient, ctx context.Context) { fmt.Println("Sonic ImageRemove") - ctx = setUserCreds(ctx) + ctx = utils.SetUserCreds(ctx) req := &spb.ImageRemoveRequest{ Input: &spb.ImageRemoveRequest_Input{}, } @@ -273,7 +267,7 @@ func sonicImageRemove(sc spb.SonicServiceClient, ctx context.Context) { func sonicImageDefault(sc spb.SonicServiceClient, ctx context.Context) { fmt.Println("Sonic ImageDefault") - ctx = setUserCreds(ctx) + ctx = utils.SetUserCreds(ctx) req := &spb.ImageDefaultRequest{ Input: &spb.ImageDefaultRequest_Input{}, } @@ -293,7 +287,7 @@ func sonicImageDefault(sc spb.SonicServiceClient, ctx context.Context) { func sonicAuthenticate(sc spb_jwt.SonicJwtServiceClient, ctx context.Context) { fmt.Println("Sonic Authenticate") - ctx = setUserCreds(ctx) + ctx = utils.SetUserCreds(ctx) req := &spb_jwt.AuthenticateRequest{} json.Unmarshal([]byte(*config.Args), req) @@ -311,7 +305,7 @@ func sonicAuthenticate(sc spb_jwt.SonicJwtServiceClient, ctx context.Context) { func sonicRefresh(sc spb_jwt.SonicJwtServiceClient, ctx context.Context) { fmt.Println("Sonic Refresh") - ctx = setUserCreds(ctx) + ctx = utils.SetUserCreds(ctx) req := &spb_jwt.RefreshRequest{} json.Unmarshal([]byte(*config.Args), req) @@ -329,7 +323,7 @@ func sonicRefresh(sc spb_jwt.SonicJwtServiceClient, ctx context.Context) { func sonicClearNeighbors(sc spb.SonicServiceClient, ctx context.Context) { fmt.Println("Sonic ClearNeighbors") - ctx = setUserCreds(ctx) + ctx = utils.SetUserCreds(ctx) req := &spb.ClearNeighborsRequest{ Input: &spb.ClearNeighborsRequest_Input{}, } diff --git a/gnoi_client/system/time.go b/gnoi_client/system/time.go new file mode 100644 index 00000000..58074eaa --- /dev/null +++ b/gnoi_client/system/time.go @@ -0,0 +1,25 @@ +package system + +import ( + "context" + "fmt" + "encoding/json" + pb "github.com/openconfig/gnoi/system" + "github.com/sonic-net/sonic-gnmi/gnoi_client/utils" + "google.golang.org/grpc" +) + +func Time(conn *grpc.ClientConn, ctx context.Context) { + fmt.Println("System Time") + ctx = utils.SetUserCreds(ctx) + sc := pb.NewSystemClient(conn) + resp, err := sc.Time(ctx, new(pb.TimeRequest)) + if err != nil { + panic(err.Error()) + } + respstr, err := json.Marshal(resp) + if err != nil { + panic(err.Error()) + } + fmt.Println(string(respstr)) +} \ No newline at end of file diff --git a/gnoi_client/utils/creds.go b/gnoi_client/utils/creds.go new file mode 100644 index 00000000..a9b8dd17 --- /dev/null +++ b/gnoi_client/utils/creds.go @@ -0,0 +1,14 @@ +package utils + +import ( + "context" + "google.golang.org/grpc/metadata" + "github.com/sonic-net/sonic-gnmi/gnoi_client/config" +) + +func SetUserCreds(ctx context.Context) context.Context { + if len(*config.JwtToken) > 0 { + ctx = metadata.AppendToOutgoingContext(ctx, "access_token", *config.JwtToken) + } + return ctx +} From 686766415393bf89542782c75bb920a82c873604 Mon Sep 17 00:00:00 2001 From: Dawei Huang Date: Thu, 9 Jan 2025 20:23:45 +0000 Subject: [PATCH 4/5] finish packaging system module. --- gnoi_client/gnoi_client.go | 81 ++---------------------------- gnoi_client/system/kill_process.go | 26 ++++++++++ gnoi_client/system/reboot.go | 56 +++++++++++++++++++++ 3 files changed, 86 insertions(+), 77 deletions(-) create mode 100644 gnoi_client/system/kill_process.go create mode 100644 gnoi_client/system/reboot.go diff --git a/gnoi_client/gnoi_client.go b/gnoi_client/gnoi_client.go index 0087cbf2..6b566504 100644 --- a/gnoi_client/gnoi_client.go +++ b/gnoi_client/gnoi_client.go @@ -6,7 +6,6 @@ import ( "fmt" "github.com/google/gnxi/utils/credentials" gnoi_file_pb "github.com/openconfig/gnoi/file" - gnoi_system_pb "github.com/openconfig/gnoi/system" spb "github.com/sonic-net/sonic-gnmi/proto/gnoi" spb_jwt "github.com/sonic-net/sonic-gnmi/proto/gnoi/jwt" "github.com/sonic-net/sonic-gnmi/gnoi_client/config" @@ -35,18 +34,17 @@ func main() { switch *config.Module { case "System": - sc := gnoi_system_pb.NewSystemClient(conn) switch *config.Rpc { case "Time": system.Time(conn, ctx) case "Reboot": - systemReboot(sc, ctx) + system.Reboot(conn, ctx) case "CancelReboot": - systemCancelReboot(sc, ctx) + system.CancelReboot(conn, ctx) case "RebootStatus": - systemRebootStatus(sc, ctx) + system.RebootStatus(conn, ctx) case "KillProcess": - systemKillProcess(sc, ctx) + system.KillProcess(conn, ctx) default: panic("Invalid RPC Name") } @@ -93,77 +91,6 @@ func main() { } -// RPC for System Services -func systemTime(sc gnoi_system_pb.SystemClient, ctx context.Context) { - fmt.Println("System Time") - ctx = utils.SetUserCreds(ctx) - resp, err := sc.Time(ctx, new(gnoi_system_pb.TimeRequest)) - if err != nil { - panic(err.Error()) - } - respstr, err := json.Marshal(resp) - if err != nil { - panic(err.Error()) - } - fmt.Println(string(respstr)) -} - -func systemKillProcess(sc gnoi_system_pb.SystemClient, ctx context.Context) { - fmt.Println("Kill Process with optional restart") - ctx = utils.SetUserCreds(ctx) - req := &gnoi_system_pb.KillProcessRequest{} - err := json.Unmarshal([]byte(*config.Args), req) - if err != nil { - panic(err.Error()) - } - _, err = sc.KillProcess(ctx, req) - if err != nil { - panic(err.Error()) - } -} - -func systemReboot(sc gnoi_system_pb.SystemClient, ctx context.Context) { - fmt.Println("System Reboot") - ctx = utils.SetUserCreds(ctx) - req := &gnoi_system_pb.RebootRequest{} - json.Unmarshal([]byte(*config.Args), req) - _, err := sc.Reboot(ctx, req) - if err != nil { - panic(err.Error()) - } -} - -func systemCancelReboot(sc gnoi_system_pb.SystemClient, ctx context.Context) { - fmt.Println("System CancelReboot") - ctx = utils.SetUserCreds(ctx) - req := &gnoi_system_pb.CancelRebootRequest{} - json.Unmarshal([]byte(*config.Args), req) - resp, err := sc.CancelReboot(ctx, req) - if err != nil { - panic(err.Error()) - } - respstr, err := json.Marshal(resp) - if err != nil { - panic(err.Error()) - } - fmt.Println(string(respstr)) -} - -func systemRebootStatus(sc gnoi_system_pb.SystemClient, ctx context.Context) { - fmt.Println("System RebootStatus") - ctx = utils.SetUserCreds(ctx) - req := &gnoi_system_pb.RebootStatusRequest{} - resp, err := sc.RebootStatus(ctx, req) - if err != nil { - panic(err.Error()) - } - respstr, err := json.Marshal(resp) - if err != nil { - panic(err.Error()) - } - fmt.Println(string(respstr)) -} - // RPC for File Services func fileStat(fc gnoi_file_pb.FileClient, ctx context.Context) { fmt.Println("File Stat") diff --git a/gnoi_client/system/kill_process.go b/gnoi_client/system/kill_process.go new file mode 100644 index 00000000..db614838 --- /dev/null +++ b/gnoi_client/system/kill_process.go @@ -0,0 +1,26 @@ +package system + +import ( + "context" + "fmt" + "encoding/json" + pb "github.com/openconfig/gnoi/system" + "github.com/sonic-net/sonic-gnmi/gnoi_client/utils" + "github.com/sonic-net/sonic-gnmi/gnoi_client/config" + "google.golang.org/grpc" +) + +func KillProcess(conn *grpc.ClientConn, ctx context.Context) { + fmt.Println("Kill Process with optional restart") + ctx = utils.SetUserCreds(ctx) + sc := pb.NewSystemClient(conn) + req := &pb.KillProcessRequest{} + err := json.Unmarshal([]byte(*config.Args), req) + if err != nil { + panic(err.Error()) + } + _, err = sc.KillProcess(ctx, req) + if err != nil { + panic(err.Error()) + } +} \ No newline at end of file diff --git a/gnoi_client/system/reboot.go b/gnoi_client/system/reboot.go new file mode 100644 index 00000000..9feff5bc --- /dev/null +++ b/gnoi_client/system/reboot.go @@ -0,0 +1,56 @@ +package system + +import ( + "context" + "fmt" + "encoding/json" + pb "github.com/openconfig/gnoi/system" + "github.com/sonic-net/sonic-gnmi/gnoi_client/utils" + "github.com/sonic-net/sonic-gnmi/gnoi_client/config" + "google.golang.org/grpc" +) + +func Reboot(conn *grpc.ClientConn, ctx context.Context) { + fmt.Println("System Reboot") + ctx = utils.SetUserCreds(ctx) + sc := pb.NewSystemClient(conn) + req := &pb.RebootRequest{} + json.Unmarshal([]byte(*config.Args), req) + _, err := sc.Reboot(ctx, req) + if err != nil { + panic(err.Error()) + } +} + +func CancelReboot(conn *grpc.ClientConn, ctx context.Context) { + fmt.Println("System CancelReboot") + ctx = utils.SetUserCreds(ctx) + sc := pb.NewSystemClient(conn) + req := &pb.CancelRebootRequest{} + json.Unmarshal([]byte(*config.Args), req) + resp, err := sc.CancelReboot(ctx, req) + if err != nil { + panic(err.Error()) + } + respstr, err := json.Marshal(resp) + if err != nil { + panic(err.Error()) + } + fmt.Println(string(respstr)) +} + +func RebootStatus(conn *grpc.ClientConn, ctx context.Context) { + fmt.Println("System RebootStatus") + ctx = utils.SetUserCreds(ctx) + sc := pb.NewSystemClient(conn) + req := &pb.RebootStatusRequest{} + resp, err := sc.RebootStatus(ctx, req) + if err != nil { + panic(err.Error()) + } + respstr, err := json.Marshal(resp) + if err != nil { + panic(err.Error()) + } + fmt.Println(string(respstr)) +} \ No newline at end of file From 6059b189957a6db199aa935ce01fc9b0cfe0f4b0 Mon Sep 17 00:00:00 2001 From: Dawei Huang Date: Thu, 9 Jan 2025 21:04:42 +0000 Subject: [PATCH 5/5] finish package sonic module. --- gnoi_client/file/file.go | 31 ++++++ gnoi_client/gnoi_client.go | 212 ++----------------------------------- gnoi_client/sonic/jwt.go | 46 ++++++++ gnoi_client/sonic/sonic.go | 132 +++++++++++++++++++++++ 4 files changed, 220 insertions(+), 201 deletions(-) create mode 100644 gnoi_client/file/file.go create mode 100644 gnoi_client/sonic/jwt.go create mode 100644 gnoi_client/sonic/sonic.go diff --git a/gnoi_client/file/file.go b/gnoi_client/file/file.go new file mode 100644 index 00000000..e7acb046 --- /dev/null +++ b/gnoi_client/file/file.go @@ -0,0 +1,31 @@ +package file + +import ( + "context" + "fmt" + "encoding/json" + pb "github.com/openconfig/gnoi/file" + "github.com/sonic-net/sonic-gnmi/gnoi_client/utils" + "github.com/sonic-net/sonic-gnmi/gnoi_client/config" + "google.golang.org/grpc" +) + +func Stat(conn *grpc.ClientConn, ctx context.Context) { + fmt.Println("File Stat") + ctx = utils.SetUserCreds(ctx) + fc := pb.NewFileClient(conn) + req := &pb.StatRequest{} + err := json.Unmarshal([]byte(*config.Args), req) + if err != nil { + panic(err.Error()) + } + resp, err := fc.Stat(ctx, req) + if err != nil { + panic(err.Error()) + } + respstr, err := json.Marshal(resp) + if err != nil { + panic(err.Error()) + } + fmt.Println(string(respstr)) +} \ No newline at end of file diff --git a/gnoi_client/gnoi_client.go b/gnoi_client/gnoi_client.go index 6b566504..c4f476cc 100644 --- a/gnoi_client/gnoi_client.go +++ b/gnoi_client/gnoi_client.go @@ -2,15 +2,11 @@ package main import ( "context" - "encoding/json" - "fmt" "github.com/google/gnxi/utils/credentials" - gnoi_file_pb "github.com/openconfig/gnoi/file" - spb "github.com/sonic-net/sonic-gnmi/proto/gnoi" - spb_jwt "github.com/sonic-net/sonic-gnmi/proto/gnoi/jwt" "github.com/sonic-net/sonic-gnmi/gnoi_client/config" - "github.com/sonic-net/sonic-gnmi/gnoi_client/utils" "github.com/sonic-net/sonic-gnmi/gnoi_client/system" + "github.com/sonic-net/sonic-gnmi/gnoi_client/file" + "github.com/sonic-net/sonic-gnmi/gnoi_client/sonic" "google.golang.org/grpc" "os" "os/signal" @@ -49,39 +45,30 @@ func main() { panic("Invalid RPC Name") } case "File": - fc := gnoi_file_pb.NewFileClient(conn) switch *config.Rpc { case "Stat": - fileStat(fc, ctx) + file.Stat(conn, ctx) default: panic("Invalid RPC Name") } case "Sonic": switch *config.Rpc { case "showtechsupport": - sc := spb.NewSonicServiceClient(conn) - sonicShowTechSupport(sc, ctx) + sonic.ShowTechSupport(conn, ctx) case "copyConfig": - sc := spb.NewSonicServiceClient(conn) - sonicCopyConfig(sc, ctx) + sonic.CopyConfig(conn, ctx) case "authenticate": - sc := spb_jwt.NewSonicJwtServiceClient(conn) - sonicAuthenticate(sc, ctx) + sonic.Authenticate(conn, ctx) case "imageInstall": - sc := spb.NewSonicServiceClient(conn) - sonicImageInstall(sc, ctx) + sonic.ImageInstall(conn, ctx) case "imageDefault": - sc := spb.NewSonicServiceClient(conn) - sonicImageDefault(sc, ctx) + sonic.ImageDefault(conn, ctx) case "imageRemove": - sc := spb.NewSonicServiceClient(conn) - sonicImageRemove(sc, ctx) + sonic.ImageRemove(conn, ctx) case "refresh": - sc := spb_jwt.NewSonicJwtServiceClient(conn) - sonicRefresh(sc, ctx) + sonic.Refresh(conn, ctx) case "clearNeighbors": - sc := spb.NewSonicServiceClient(conn) - sonicClearNeighbors(sc, ctx) + sonic.ClearNeighbors(conn, ctx) default: panic("Invalid RPC Name") } @@ -90,180 +77,3 @@ func main() { } } - -// RPC for File Services -func fileStat(fc gnoi_file_pb.FileClient, ctx context.Context) { - fmt.Println("File Stat") - ctx = utils.SetUserCreds(ctx) - req := &gnoi_file_pb.StatRequest{} - err := json.Unmarshal([]byte(*config.Args), req) - if err != nil { - panic(err.Error()) - } - resp, err := fc.Stat(ctx, req) - if err != nil { - panic(err.Error()) - } - respstr, err := json.Marshal(resp) - if err != nil { - panic(err.Error()) - } - fmt.Println(string(respstr)) -} - -// RPC for Sonic Services -func sonicShowTechSupport(sc spb.SonicServiceClient, ctx context.Context) { - fmt.Println("Sonic ShowTechsupport") - ctx = utils.SetUserCreds(ctx) - req := &spb.TechsupportRequest{ - Input: &spb.TechsupportRequest_Input{}, - } - - json.Unmarshal([]byte(*config.Args), req) - - resp, err := sc.ShowTechsupport(ctx, req) - if err != nil { - panic(err.Error()) - } - respstr, err := json.Marshal(resp) - if err != nil { - panic(err.Error()) - } - fmt.Println(string(respstr)) -} - -func sonicCopyConfig(sc spb.SonicServiceClient, ctx context.Context) { - fmt.Println("Sonic CopyConfig") - ctx = utils.SetUserCreds(ctx) - req := &spb.CopyConfigRequest{ - Input: &spb.CopyConfigRequest_Input{}, - } - json.Unmarshal([]byte(*config.Args), req) - - resp, err := sc.CopyConfig(ctx, req) - - if err != nil { - panic(err.Error()) - } - respstr, err := json.Marshal(resp) - if err != nil { - panic(err.Error()) - } - fmt.Println(string(respstr)) -} - -func sonicImageInstall(sc spb.SonicServiceClient, ctx context.Context) { - fmt.Println("Sonic ImageInstall") - ctx = utils.SetUserCreds(ctx) - req := &spb.ImageInstallRequest{ - Input: &spb.ImageInstallRequest_Input{}, - } - json.Unmarshal([]byte(*config.Args), req) - - resp, err := sc.ImageInstall(ctx, req) - - if err != nil { - panic(err.Error()) - } - respstr, err := json.Marshal(resp) - if err != nil { - panic(err.Error()) - } - fmt.Println(string(respstr)) -} - -func sonicImageRemove(sc spb.SonicServiceClient, ctx context.Context) { - fmt.Println("Sonic ImageRemove") - ctx = utils.SetUserCreds(ctx) - req := &spb.ImageRemoveRequest{ - Input: &spb.ImageRemoveRequest_Input{}, - } - json.Unmarshal([]byte(*config.Args), req) - - resp, err := sc.ImageRemove(ctx, req) - - if err != nil { - panic(err.Error()) - } - respstr, err := json.Marshal(resp) - if err != nil { - panic(err.Error()) - } - fmt.Println(string(respstr)) -} - -func sonicImageDefault(sc spb.SonicServiceClient, ctx context.Context) { - fmt.Println("Sonic ImageDefault") - ctx = utils.SetUserCreds(ctx) - req := &spb.ImageDefaultRequest{ - Input: &spb.ImageDefaultRequest_Input{}, - } - json.Unmarshal([]byte(*config.Args), req) - - resp, err := sc.ImageDefault(ctx, req) - - if err != nil { - panic(err.Error()) - } - respstr, err := json.Marshal(resp) - if err != nil { - panic(err.Error()) - } - fmt.Println(string(respstr)) -} - -func sonicAuthenticate(sc spb_jwt.SonicJwtServiceClient, ctx context.Context) { - fmt.Println("Sonic Authenticate") - ctx = utils.SetUserCreds(ctx) - req := &spb_jwt.AuthenticateRequest{} - - json.Unmarshal([]byte(*config.Args), req) - - resp, err := sc.Authenticate(ctx, req) - if err != nil { - panic(err.Error()) - } - respstr, err := json.Marshal(resp) - if err != nil { - panic(err.Error()) - } - fmt.Println(string(respstr)) -} - -func sonicRefresh(sc spb_jwt.SonicJwtServiceClient, ctx context.Context) { - fmt.Println("Sonic Refresh") - ctx = utils.SetUserCreds(ctx) - req := &spb_jwt.RefreshRequest{} - - json.Unmarshal([]byte(*config.Args), req) - - resp, err := sc.Refresh(ctx, req) - if err != nil { - panic(err.Error()) - } - respstr, err := json.Marshal(resp) - if err != nil { - panic(err.Error()) - } - fmt.Println(string(respstr)) -} - -func sonicClearNeighbors(sc spb.SonicServiceClient, ctx context.Context) { - fmt.Println("Sonic ClearNeighbors") - ctx = utils.SetUserCreds(ctx) - req := &spb.ClearNeighborsRequest{ - Input: &spb.ClearNeighborsRequest_Input{}, - } - json.Unmarshal([]byte(*config.Args), req) - - resp, err := sc.ClearNeighbors(ctx, req) - - if err != nil { - panic(err.Error()) - } - respstr, err := json.Marshal(resp) - if err != nil { - panic(err.Error()) - } - fmt.Println(string(respstr)) -} diff --git a/gnoi_client/sonic/jwt.go b/gnoi_client/sonic/jwt.go new file mode 100644 index 00000000..d48d6e0b --- /dev/null +++ b/gnoi_client/sonic/jwt.go @@ -0,0 +1,46 @@ +package sonic + +import ( + "context" + "fmt" + "encoding/json" + pb "github.com/sonic-net/sonic-gnmi/proto/gnoi/jwt" + "github.com/sonic-net/sonic-gnmi/gnoi_client/utils" + "github.com/sonic-net/sonic-gnmi/gnoi_client/config" + "google.golang.org/grpc" +) + +func Authenticate(conn *grpc.ClientConn, ctx context.Context) { + fmt.Println("Sonic Authenticate") + ctx = utils.SetUserCreds(ctx) + sc := pb.NewSonicJwtServiceClient(conn) + req := &pb.AuthenticateRequest{} + json.Unmarshal([]byte(*config.Args), req) + + resp, err := sc.Authenticate(ctx, req) + if err != nil { + panic(err.Error()) + } + respstr, err := json.Marshal(resp) + if err != nil { + panic(err.Error()) + } + fmt.Println(string(respstr)) +} + +func Refresh(conn *grpc.ClientConn, ctx context.Context) { + fmt.Println("Sonic Refresh") + ctx = utils.SetUserCreds(ctx) + sc := pb.NewSonicJwtServiceClient(conn) + req := &pb.RefreshRequest{} + json.Unmarshal([]byte(*config.Args), req) + resp, err := sc.Refresh(ctx, req) + if err != nil { + panic(err.Error()) + } + respstr, err := json.Marshal(resp) + if err != nil { + panic(err.Error()) + } + fmt.Println(string(respstr)) +} diff --git a/gnoi_client/sonic/sonic.go b/gnoi_client/sonic/sonic.go new file mode 100644 index 00000000..8a133f3d --- /dev/null +++ b/gnoi_client/sonic/sonic.go @@ -0,0 +1,132 @@ +package sonic + +import ( + "context" + "fmt" + "encoding/json" + pb "github.com/sonic-net/sonic-gnmi/proto/gnoi" + "github.com/sonic-net/sonic-gnmi/gnoi_client/utils" + "github.com/sonic-net/sonic-gnmi/gnoi_client/config" + "google.golang.org/grpc" +) + +func ShowTechSupport(conn *grpc.ClientConn, ctx context.Context) { + fmt.Println("Sonic ShowTechsupport") + ctx = utils.SetUserCreds(ctx) + sc := pb.NewSonicServiceClient(conn) + req := &pb.TechsupportRequest{ + Input: &pb.TechsupportRequest_Input{}, + } + + json.Unmarshal([]byte(*config.Args), req) + + resp, err := sc.ShowTechsupport(ctx, req) + if err != nil { + panic(err.Error()) + } + respstr, err := json.Marshal(resp) + if err != nil { + panic(err.Error()) + } + fmt.Println(string(respstr)) +} + +func CopyConfig(conn *grpc.ClientConn, ctx context.Context) { + fmt.Println("Sonic CopyConfig") + ctx = utils.SetUserCreds(ctx) + sc := pb.NewSonicServiceClient(conn) + req := &pb.CopyConfigRequest{ + Input: &pb.CopyConfigRequest_Input{}, + } + json.Unmarshal([]byte(*config.Args), req) + resp, err := sc.CopyConfig(ctx, req) + + if err != nil { + panic(err.Error()) + } + respstr, err := json.Marshal(resp) + if err != nil { + panic(err.Error()) + } + fmt.Println(string(respstr)) +} + +func ImageInstall(conn *grpc.ClientConn, ctx context.Context) { + fmt.Println("Sonic ImageInstall") + ctx = utils.SetUserCreds(ctx) + sc := pb.NewSonicServiceClient(conn) + req := &pb.ImageInstallRequest{ + Input: &pb.ImageInstallRequest_Input{}, + } + json.Unmarshal([]byte(*config.Args), req) + + resp, err := sc.ImageInstall(ctx, req) + + if err != nil { + panic(err.Error()) + } + respstr, err := json.Marshal(resp) + if err != nil { + panic(err.Error()) + } + fmt.Println(string(respstr)) +} + +func ImageRemove(conn *grpc.ClientConn, ctx context.Context) { + fmt.Println("Sonic ImageRemove") + ctx = utils.SetUserCreds(ctx) + sc := pb.NewSonicServiceClient(conn) + req := &pb.ImageRemoveRequest{ + Input: &pb.ImageRemoveRequest_Input{}, + } + json.Unmarshal([]byte(*config.Args), req) + + resp, err := sc.ImageRemove(ctx, req) + + if err != nil { + panic(err.Error()) + } + respstr, err := json.Marshal(resp) + if err != nil { + panic(err.Error()) + } + fmt.Println(string(respstr)) +} + +func ImageDefault(conn *grpc.ClientConn, ctx context.Context) { + fmt.Println("Sonic ImageDefault") + ctx = utils.SetUserCreds(ctx) + sc := pb.NewSonicServiceClient(conn) + req := &pb.ImageDefaultRequest{ + Input: &pb.ImageDefaultRequest_Input{}, + } + json.Unmarshal([]byte(*config.Args), req) + resp, err := sc.ImageDefault(ctx, req) + if err != nil { + panic(err.Error()) + } + respstr, err := json.Marshal(resp) + if err != nil { + panic(err.Error()) + } + fmt.Println(string(respstr)) +} + +func ClearNeighbors(conn *grpc.ClientConn, ctx context.Context) { + fmt.Println("Sonic ClearNeighbors") + ctx = utils.SetUserCreds(ctx) + sc := pb.NewSonicServiceClient(conn) + req := &pb.ClearNeighborsRequest{ + Input: &pb.ClearNeighborsRequest_Input{}, + } + json.Unmarshal([]byte(*config.Args), req) + resp, err := sc.ClearNeighbors(ctx, req) + if err != nil { + panic(err.Error()) + } + respstr, err := json.Marshal(resp) + if err != nil { + panic(err.Error()) + } + fmt.Println(string(respstr)) +}