Skip to content
Snippets Groups Projects
Commit 2706931b authored by Malte Bauch's avatar Malte Bauch
Browse files

Change context to context with deadline and use cancel within cli calls

parent b0066340
No related branches found
No related tags found
1 merge request!548Draft: Resolve "Deadline and Cancellation for gRPC calls"
Pipeline #160949 passed
Showing
with 93 additions and 59 deletions
...@@ -51,7 +51,11 @@ Change UUID must be specified as positional argument.`, ...@@ -51,7 +51,11 @@ Change UUID must be specified as positional argument.`,
pterm.Error.Println(err) pterm.Error.Println(err)
return return
} }
resp, err := pndAdapter.Commit(createContextWithAuthorization(), cuid)
// create a authorizedContext for further requests
ctx, ctxCancelFn = createContextWithAuthorization()
resp, err := pndAdapter.Commit(ctx, cuid)
if err != nil { if err != nil {
pterm.Error.Println(err) pterm.Error.Println(err)
return return
......
...@@ -52,7 +52,10 @@ Change UUID must be specified as positional argument`, ...@@ -52,7 +52,10 @@ Change UUID must be specified as positional argument`,
return return
} }
resp, err := pndAdapter.Confirm(createContextWithAuthorization(), cuid) // create a authorizedContext for further requests
ctx, ctxCancelFn = createContextWithAuthorization()
resp, err := pndAdapter.Confirm(ctx, cuid)
if err != nil { if err != nil {
pterm.Error.Println(err) pterm.Error.Println(err)
return return
......
...@@ -46,7 +46,10 @@ var getCmd = &cobra.Command{ ...@@ -46,7 +46,10 @@ var getCmd = &cobra.Command{
changes UUID has to be specified via a positional argument.`, changes UUID has to be specified via a positional argument.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
changes, err := pndAdapter.GetChange(createContextWithAuthorization(), args[0]) // create a authorizedContext for further requests
ctx, ctxCancelFn = createContextWithAuthorization()
changes, err := pndAdapter.GetChange(ctx, args[0])
if err != nil { if err != nil {
pterm.Error.Println(err) pterm.Error.Println(err)
return return
......
...@@ -45,12 +45,19 @@ var changeListCmd = &cobra.Command{ ...@@ -45,12 +45,19 @@ var changeListCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
spinner, _ := pterm.DefaultSpinner.Start("Process change list request") spinner, _ := pterm.DefaultSpinner.Start("Process change list request")
committed, err := pndAdapter.CommittedChanges(createContextWithAuthorization()) // create a authorizedContext for further requests
ctx, ctxCancelFn = createContextWithAuthorization()
committed, err := pndAdapter.CommittedChanges(ctx)
if err != nil { if err != nil {
spinner.Fail(err) spinner.Fail(err)
return return
} }
pending, err := pndAdapter.PendingChanges(createContextWithAuthorization())
// create a authorizedContext for further requests
ctx, ctxCancelFn = createContextWithAuthorization()
pending, err := pndAdapter.PendingChanges(ctx)
if err != nil { if err != nil {
spinner.Fail(err) spinner.Fail(err)
return return
......
...@@ -47,13 +47,18 @@ var listCmd = &cobra.Command{ ...@@ -47,13 +47,18 @@ var listCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
addr := viper.GetString("controllerApiEndpoint") addr := viper.GetString("controllerApiEndpoint")
resp, err := api.GetIds(createContextWithAuthorization(), addr) // create a authorizedContext for further requests
ctx, ctxCancelFn = createContextWithAuthorization()
resp, err := api.GetIds(ctx, addr)
if err != nil { if err != nil {
pterm.Error.Println(err) pterm.Error.Println(err)
return return
} }
for i, pnd := range resp { for i, pnd := range resp {
mneResp, err := api.GetFlattenedNetworkElements(createContextWithAuthorization(), addr, pnd.GetId()) // create a authorizedContext for further requests
ctx, ctxCancelFn = createContextWithAuthorization()
mneResp, err := api.GetFlattenedNetworkElements(ctx, addr, pnd.GetId())
if err != nil { if err != nil {
pterm.Error.Println(err) pterm.Error.Println(err)
return return
......
...@@ -32,8 +32,6 @@ POSSIBILITY OF SUCH DAMAGE. ...@@ -32,8 +32,6 @@ POSSIBILITY OF SUCH DAMAGE.
package cmd package cmd
import ( import (
"context"
"code.fbi.h-da.de/danet/gosdn/controller/api" "code.fbi.h-da.de/danet/gosdn/controller/api"
"github.com/pterm/pterm" "github.com/pterm/pterm"
"github.com/spf13/cobra" "github.com/spf13/cobra"
...@@ -58,13 +56,10 @@ var loginCmd = &cobra.Command{ ...@@ -58,13 +56,10 @@ var loginCmd = &cobra.Command{
pterm.Info.Println("New controller address: ", viper.GetString("controllerAPIEndpoint")) pterm.Info.Println("New controller address: ", viper.GetString("controllerAPIEndpoint"))
} }
// log out to remove active session in case an user is already logged in // create a authorizedContext for further requests
if userToken != "" { ctx, ctxCancelFn = createContextWithAuthorization()
_, _ = api.Logout(createContextWithAuthorization(), viper.GetString("controllerAPIEndpoint"), nbUserName)
}
// TODO: maybe add credentials in context instead of context.TODO() resp, err := api.Login(ctx, viper.GetString("controllerAPIEndpoint"), nbUserName, nbUserPwd)
resp, err := api.Login(context.TODO(), viper.GetString("controllerAPIEndpoint"), nbUserName, nbUserPwd)
if err != nil { if err != nil {
spinner.Fail("Login failed: ", err) spinner.Fail("Login failed: ", err)
return return
......
...@@ -49,7 +49,10 @@ var logoutCmd = &cobra.Command{ ...@@ -49,7 +49,10 @@ var logoutCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
spinner, _ := pterm.DefaultSpinner.Start("Logout attempt for user: ", nbUserName) spinner, _ := pterm.DefaultSpinner.Start("Logout attempt for user: ", nbUserName)
resp, err := api.Logout(createContextWithAuthorization(), viper.GetString("controllerAPIEndpoint"), nbUserName) // create a authorizedContext for further requests
ctx, ctxCancelFn = createContextWithAuthorization()
resp, err := api.Logout(ctx, viper.GetString("controllerAPIEndpoint"), nbUserName)
if err != nil { if err != nil {
spinner.Fail("Logout failed: ", err) spinner.Fail("Logout failed: ", err)
return return
......
...@@ -71,7 +71,10 @@ if they diverge from the default credentials (user:'admin' and pw:'arista').`, ...@@ -71,7 +71,10 @@ if they diverge from the default credentials (user:'admin' and pw:'arista').`,
return return
} }
resp, err := pndAdapter.AddNetworkElement(createContextWithAuthorization(), mneName, opt, pluginUUID) // create a authorizedContext for further requests
ctx, ctxCancelFn = createContextWithAuthorization()
resp, err := pndAdapter.AddNetworkElement(ctx, mneName, opt, pluginUUID)
if err != nil { if err != nil {
spinner.Fail(err) spinner.Fail(err)
return return
......
...@@ -47,7 +47,10 @@ var networkElementListCmd = &cobra.Command{ ...@@ -47,7 +47,10 @@ var networkElementListCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
spinner, _ := pterm.DefaultSpinner.Start("Fetching data from controller") spinner, _ := pterm.DefaultSpinner.Start("Fetching data from controller")
resp, err := pndAdapter.GetFlattenedNetworkElements(createContextWithAuthorization()) // create a authorizedContext for further requests
ctx, ctxCancelFn = createContextWithAuthorization()
resp, err := pndAdapter.GetFlattenedNetworkElements(ctx)
if err != nil { if err != nil {
spinner.Fail(err) spinner.Fail(err)
return return
...@@ -69,14 +72,4 @@ var networkElementListCmd = &cobra.Command{ ...@@ -69,14 +72,4 @@ var networkElementListCmd = &cobra.Command{
func init() { func init() {
networkElementCmd.AddCommand(networkElementListCmd) networkElementCmd.AddCommand(networkElementListCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// listDeviceCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// listDeviceCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
} }
...@@ -62,8 +62,11 @@ The network element UUID and request path must be specified as a positional argu ...@@ -62,8 +62,11 @@ The network element UUID and request path must be specified as a positional argu
return return
} }
// create a authorizedContext for further requests
ctx, ctxCancelFn = createContextWithAuthorization()
resp, err := pndAdapter.ChangeMNE( resp, err := pndAdapter.ChangeMNE(
createContextWithAuthorization(), ctx,
mneid, mneid,
mnepb.ApiOperation_API_OPERATION_DELETE, mnepb.ApiOperation_API_OPERATION_DELETE,
path, path,
......
...@@ -54,8 +54,11 @@ The network element UUID and request path must be specified as a positional argu ...@@ -54,8 +54,11 @@ The network element UUID and request path must be specified as a positional argu
return return
} }
// create a authorizedContext for further requests
ctx, ctxCancelFn = createContextWithAuthorization()
res, err := pndAdapter.RequestPath( res, err := pndAdapter.RequestPath(
createContextWithAuthorization(), ctx,
mneid, mneid,
args[1], args[1],
) )
......
...@@ -56,8 +56,11 @@ The network element UUID and request path must be specified as a positional argu ...@@ -56,8 +56,11 @@ The network element UUID and request path must be specified as a positional argu
return return
} }
// create a authorizedContext for further requests
ctx, ctxCancelFn = createContextWithAuthorization()
res, err := pndAdapter.RequestIntendedPath( res, err := pndAdapter.RequestIntendedPath(
createContextWithAuthorization(), ctx,
mneid, mneid,
args[1], args[1],
) )
......
...@@ -119,8 +119,11 @@ To enable replacing behaviour (destructive!), set the --replace flag."`, ...@@ -119,8 +119,11 @@ To enable replacing behaviour (destructive!), set the --replace flag."`,
return return
} }
// create a authorizedContext for further requests
ctx, ctxCancelFn = createContextWithAuthorization()
resp, err := pndAdapter.ChangeMNE( resp, err := pndAdapter.ChangeMNE(
createContextWithAuthorization(), ctx,
mneid, mneid,
operation, operation,
path, path,
......
...@@ -54,7 +54,11 @@ The network element UUID must be specified as a positional argument.`, ...@@ -54,7 +54,11 @@ The network element UUID must be specified as a positional argument.`,
spinner.Fail(err) spinner.Fail(err)
return return
} }
_, err = pndAdapter.RemoveNetworkElement(createContextWithAuthorization(), mneid)
// create a authorizedContext for further requests
ctx, ctxCancelFn = createContextWithAuthorization()
_, err = pndAdapter.RemoveNetworkElement(ctx, mneid)
if err != nil { if err != nil {
spinner.Fail(err) spinner.Fail(err)
return return
......
...@@ -47,7 +47,10 @@ The network element information returned is the information as currently stored ...@@ -47,7 +47,10 @@ The network element information returned is the information as currently stored
The actual network element is not queried directly.`, The actual network element is not queried directly.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
resp, err := pndAdapter.GetNetworkElement(createContextWithAuthorization(), args[0]) // create a authorizedContext for further requests
ctx, ctxCancelFn = createContextWithAuthorization()
resp, err := pndAdapter.GetNetworkElement(ctx, args[0])
if err != nil { if err != nil {
pterm.Error.Println(err) pterm.Error.Println(err)
return return
......
...@@ -57,8 +57,11 @@ The device UUID and requested paths must be specified as a positional arguments. ...@@ -57,8 +57,11 @@ The device UUID and requested paths must be specified as a positional arguments.
return return
} }
// create a authorizedContext for further requests
ctx, ctxCancelFn = createContextWithAuthorization()
subClient, err := pndAdapter.SubscribeMNEPath( subClient, err := pndAdapter.SubscribeMNEPath(
createContextWithAuthorization(), ctx,
did, did,
&mnepb.SubscriptionList{ &mnepb.SubscriptionList{
Subscription: []*mnepb.Subscription{ Subscription: []*mnepb.Subscription{
......
...@@ -46,7 +46,11 @@ var pluginListCmd = &cobra.Command{ ...@@ -46,7 +46,11 @@ var pluginListCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
spinner, _ := pterm.DefaultSpinner.Start("Fetching list of available plugins from the controller.") spinner, _ := pterm.DefaultSpinner.Start("Fetching list of available plugins from the controller.")
resp, err := pndAdapter.GetAvailablePlugins(createContextWithAuthorization())
// create a authorizedContext for further requests
ctx, ctxCancelFn = createContextWithAuthorization()
resp, err := pndAdapter.GetAvailablePlugins(ctx)
if err != nil { if err != nil {
spinner.Fail(err) spinner.Fail(err)
return return
...@@ -69,14 +73,4 @@ var pluginListCmd = &cobra.Command{ ...@@ -69,14 +73,4 @@ var pluginListCmd = &cobra.Command{
func init() { func init() {
pluginCmd.AddCommand(pluginListCmd) pluginCmd.AddCommand(pluginListCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// listPNDCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// listPNDCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
} }
...@@ -51,7 +51,11 @@ A description must be passed as positional argument.`, ...@@ -51,7 +51,11 @@ A description must be passed as positional argument.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
spinner, _ := pterm.DefaultSpinner.Start("Creating new PND") spinner, _ := pterm.DefaultSpinner.Start("Creating new PND")
resp, err := api.AddPnd(createContextWithAuthorization(), viper.GetString("controllerApiEndpoint"), pndName, pndDescription)
// create a authorizedContext for further requests
ctx, ctxCancelFn = createContextWithAuthorization()
resp, err := api.AddPnd(ctx, viper.GetString("controllerApiEndpoint"), pndName, pndDescription)
if err != nil { if err != nil {
spinner.Fail(err) spinner.Fail(err)
return return
......
...@@ -46,7 +46,11 @@ var pndGetCmd = &cobra.Command{ ...@@ -46,7 +46,11 @@ var pndGetCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
spinner, _ := pterm.DefaultSpinner.Start("Fetching requested PNDs from controller.") spinner, _ := pterm.DefaultSpinner.Start("Fetching requested PNDs from controller.")
resp, err := api.GetPnd(createContextWithAuthorization(), viper.GetString("controllerApiEndpoint"), args[0])
// create a authorizedContext for further requests
ctx, ctxCancelFn = createContextWithAuthorization()
resp, err := api.GetPnd(ctx, viper.GetString("controllerApiEndpoint"), args[0])
if err != nil { if err != nil {
spinner.Fail(err) spinner.Fail(err)
return return
......
...@@ -47,7 +47,11 @@ var pndListCmd = &cobra.Command{ ...@@ -47,7 +47,11 @@ var pndListCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
spinner, _ := pterm.DefaultSpinner.Start("Fetching PND list from controller") spinner, _ := pterm.DefaultSpinner.Start("Fetching PND list from controller")
resp, err := api.GetPnds(createContextWithAuthorization(), pndAdapter.Endpoint())
// create a authorizedContext for further requests
ctx, ctxCancelFn = createContextWithAuthorization()
resp, err := api.GetPnds(ctx, pndAdapter.Endpoint())
if err != nil { if err != nil {
spinner.Fail(err) spinner.Fail(err)
return return
...@@ -69,14 +73,4 @@ var pndListCmd = &cobra.Command{ ...@@ -69,14 +73,4 @@ var pndListCmd = &cobra.Command{
func init() { func init() {
pndCmd.AddCommand(pndListCmd) pndCmd.AddCommand(pndListCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// listPNDCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// listPNDCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment