diff --git a/cli/cmd/changeCommit.go b/cli/cmd/changeCommit.go index 406da9e9c17198b04e6b39b63822c0597c7cb0fe..744a90eeb22053b718c5a0b55489e9c78dfc2454 100644 --- a/cli/cmd/changeCommit.go +++ b/cli/cmd/changeCommit.go @@ -51,7 +51,11 @@ Change UUID must be specified as positional argument.`, pterm.Error.Println(err) 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 { pterm.Error.Println(err) return diff --git a/cli/cmd/changeConfirm.go b/cli/cmd/changeConfirm.go index 41550ede05caa6f6fd3763dfa70e6bb217be973b..a67b8392687bd74990b216fbfb6082f044206ca1 100644 --- a/cli/cmd/changeConfirm.go +++ b/cli/cmd/changeConfirm.go @@ -52,7 +52,10 @@ Change UUID must be specified as positional argument`, 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 { pterm.Error.Println(err) return diff --git a/cli/cmd/changeGet.go b/cli/cmd/changeGet.go index ff8e3c3b44a624aec1a00cb6db67ef75913beb89..debb73d8a1b318cf69e7903d3c82d2a498f0a12e 100644 --- a/cli/cmd/changeGet.go +++ b/cli/cmd/changeGet.go @@ -46,7 +46,10 @@ var getCmd = &cobra.Command{ changes UUID has to be specified via a positional argument.`, 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 { pterm.Error.Println(err) return diff --git a/cli/cmd/changeList.go b/cli/cmd/changeList.go index 2b1a57501ee11f171bc077d59df5b30935bb8313..bbe5834614fa6f81445a94ce14ed7f01026fb9e7 100644 --- a/cli/cmd/changeList.go +++ b/cli/cmd/changeList.go @@ -45,12 +45,19 @@ var changeListCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { 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 { spinner.Fail(err) return } - pending, err := pndAdapter.PendingChanges(createContextWithAuthorization()) + + // create a authorizedContext for further requests + ctx, ctxCancelFn = createContextWithAuthorization() + + pending, err := pndAdapter.PendingChanges(ctx) if err != nil { spinner.Fail(err) return diff --git a/cli/cmd/list.go b/cli/cmd/list.go index 2e476d4e7487361d53504ee1d3b51d7505237cab..6427712a7a591f86896bc0e784ec5478f9bdca5d 100644 --- a/cli/cmd/list.go +++ b/cli/cmd/list.go @@ -47,13 +47,18 @@ var listCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { 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 { pterm.Error.Println(err) return } 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 { pterm.Error.Println(err) return diff --git a/cli/cmd/login.go b/cli/cmd/login.go index babf01ee8f167ae75a396faf306e0debc2c30ca1..60a685bcd32ffe7fcbcda7f51ea32326d6e8f12b 100644 --- a/cli/cmd/login.go +++ b/cli/cmd/login.go @@ -32,8 +32,6 @@ POSSIBILITY OF SUCH DAMAGE. package cmd import ( - "context" - "code.fbi.h-da.de/danet/gosdn/controller/api" "github.com/pterm/pterm" "github.com/spf13/cobra" @@ -58,13 +56,10 @@ var loginCmd = &cobra.Command{ pterm.Info.Println("New controller address: ", viper.GetString("controllerAPIEndpoint")) } - // log out to remove active session in case an user is already logged in - if userToken != "" { - _, _ = api.Logout(createContextWithAuthorization(), viper.GetString("controllerAPIEndpoint"), nbUserName) - } + // create a authorizedContext for further requests + ctx, ctxCancelFn = createContextWithAuthorization() - // TODO: maybe add credentials in context instead of context.TODO() - resp, err := api.Login(context.TODO(), viper.GetString("controllerAPIEndpoint"), nbUserName, nbUserPwd) + resp, err := api.Login(ctx, viper.GetString("controllerAPIEndpoint"), nbUserName, nbUserPwd) if err != nil { spinner.Fail("Login failed: ", err) return diff --git a/cli/cmd/logout.go b/cli/cmd/logout.go index 68bc8a7394c6ed7bf2c9ab91fa72de9b1c39c2f5..f042097b4fcc42742ebbcc432b757270b8fb93c7 100644 --- a/cli/cmd/logout.go +++ b/cli/cmd/logout.go @@ -49,7 +49,10 @@ var logoutCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { 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 { spinner.Fail("Logout failed: ", err) return diff --git a/cli/cmd/networkElementCreate.go b/cli/cmd/networkElementCreate.go index e2ed6cb9d14f741d73ffc1a34cda9a2cd6833dc4..ffffd5e25c35922713a5d9299febd91ca08ad7bf 100644 --- a/cli/cmd/networkElementCreate.go +++ b/cli/cmd/networkElementCreate.go @@ -71,7 +71,10 @@ if they diverge from the default credentials (user:'admin' and pw:'arista').`, 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 { spinner.Fail(err) return diff --git a/cli/cmd/networkElementList.go b/cli/cmd/networkElementList.go index be6ce028a0a9353f445bc687cca7e86676d6561e..1ec9fdb7bcaac6cabc225631d85f41502ca1c06d 100644 --- a/cli/cmd/networkElementList.go +++ b/cli/cmd/networkElementList.go @@ -47,7 +47,10 @@ var networkElementListCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { 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 { spinner.Fail(err) return @@ -69,14 +72,4 @@ var networkElementListCmd = &cobra.Command{ func init() { 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") } diff --git a/cli/cmd/networkElementPathDelete.go b/cli/cmd/networkElementPathDelete.go index b40bdccc1d266436da657cd069d03241ce20b7ef..ee81e442565f368de90a791d33a78328af305191 100644 --- a/cli/cmd/networkElementPathDelete.go +++ b/cli/cmd/networkElementPathDelete.go @@ -62,8 +62,11 @@ The network element UUID and request path must be specified as a positional argu return } + // create a authorizedContext for further requests + ctx, ctxCancelFn = createContextWithAuthorization() + resp, err := pndAdapter.ChangeMNE( - createContextWithAuthorization(), + ctx, mneid, mnepb.ApiOperation_API_OPERATION_DELETE, path, diff --git a/cli/cmd/networkElementPathGet.go b/cli/cmd/networkElementPathGet.go index f7ab234e4003296400b3bd7275fc43cda1018078..56c99dab9a9ede72a7305606accf30b331755b72 100644 --- a/cli/cmd/networkElementPathGet.go +++ b/cli/cmd/networkElementPathGet.go @@ -54,8 +54,11 @@ The network element UUID and request path must be specified as a positional argu return } + // create a authorizedContext for further requests + ctx, ctxCancelFn = createContextWithAuthorization() + res, err := pndAdapter.RequestPath( - createContextWithAuthorization(), + ctx, mneid, args[1], ) diff --git a/cli/cmd/networkElementPathGetIntended.go b/cli/cmd/networkElementPathGetIntended.go index ceb8f50ed9eb4055ae580734213c74d48cf43dc5..b8ceba56cf7e4c785a7ab7bbcf88b8dac0636c21 100644 --- a/cli/cmd/networkElementPathGetIntended.go +++ b/cli/cmd/networkElementPathGetIntended.go @@ -56,8 +56,11 @@ The network element UUID and request path must be specified as a positional argu return } + // create a authorizedContext for further requests + ctx, ctxCancelFn = createContextWithAuthorization() + res, err := pndAdapter.RequestIntendedPath( - createContextWithAuthorization(), + ctx, mneid, args[1], ) diff --git a/cli/cmd/networkElementPathSet.go b/cli/cmd/networkElementPathSet.go index e29cbf332ade8f9cae822ec0f9713d90a3108b78..087b95e6a8a0e6f6927eff03319f5ac82b238718 100644 --- a/cli/cmd/networkElementPathSet.go +++ b/cli/cmd/networkElementPathSet.go @@ -119,8 +119,11 @@ To enable replacing behaviour (destructive!), set the --replace flag."`, return } + // create a authorizedContext for further requests + ctx, ctxCancelFn = createContextWithAuthorization() + resp, err := pndAdapter.ChangeMNE( - createContextWithAuthorization(), + ctx, mneid, operation, path, diff --git a/cli/cmd/networkElementRemove.go b/cli/cmd/networkElementRemove.go index b31fa14459e95b9d353eb7a867c0ec51af0c0c58..1e80546a8085993d0d615f71847aeaf6860c1f6e 100644 --- a/cli/cmd/networkElementRemove.go +++ b/cli/cmd/networkElementRemove.go @@ -54,7 +54,11 @@ The network element UUID must be specified as a positional argument.`, spinner.Fail(err) return } - _, err = pndAdapter.RemoveNetworkElement(createContextWithAuthorization(), mneid) + + // create a authorizedContext for further requests + ctx, ctxCancelFn = createContextWithAuthorization() + + _, err = pndAdapter.RemoveNetworkElement(ctx, mneid) if err != nil { spinner.Fail(err) return diff --git a/cli/cmd/networkElementShow.go b/cli/cmd/networkElementShow.go index 6e150c4da08ab88bce3a172ee08886553cc25bf2..7213814866527d6cc702dbcd649d0aace15683c5 100644 --- a/cli/cmd/networkElementShow.go +++ b/cli/cmd/networkElementShow.go @@ -47,7 +47,10 @@ The network element information returned is the information as currently stored The actual network element is not queried directly.`, 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 { pterm.Error.Println(err) return diff --git a/cli/cmd/networkElementSubscribe.go b/cli/cmd/networkElementSubscribe.go index 5b96828188d306b1b23e08366bce354487e20c3c..554e1e12a58ec93c6e7b474e0e36e31d5b3e9982 100644 --- a/cli/cmd/networkElementSubscribe.go +++ b/cli/cmd/networkElementSubscribe.go @@ -57,8 +57,11 @@ The device UUID and requested paths must be specified as a positional arguments. return } + // create a authorizedContext for further requests + ctx, ctxCancelFn = createContextWithAuthorization() + subClient, err := pndAdapter.SubscribeMNEPath( - createContextWithAuthorization(), + ctx, did, &mnepb.SubscriptionList{ Subscription: []*mnepb.Subscription{ diff --git a/cli/cmd/pluginList.go b/cli/cmd/pluginList.go index 8594f28364ff1973b34892daaed394ba88ff946f..4b6e189353b77ada597d3d0a8f5ce1e2140be084 100644 --- a/cli/cmd/pluginList.go +++ b/cli/cmd/pluginList.go @@ -46,7 +46,11 @@ var pluginListCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { 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 { spinner.Fail(err) return @@ -69,14 +73,4 @@ var pluginListCmd = &cobra.Command{ func init() { 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") } diff --git a/cli/cmd/pndCreate.go b/cli/cmd/pndCreate.go index dc4c6c6c632b72e709452a8ed47063f89a141cd3..4e2ed18088949c9012aafcca2d8477dd41601f07 100644 --- a/cli/cmd/pndCreate.go +++ b/cli/cmd/pndCreate.go @@ -51,7 +51,11 @@ A description must be passed as positional argument.`, Run: func(cmd *cobra.Command, args []string) { 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 { spinner.Fail(err) return diff --git a/cli/cmd/pndGet.go b/cli/cmd/pndGet.go index 7fdabe07fa67e4c2e44077076a9b3fc88e32d96f..5fecaed109d83a06505269e8cd65c082d086e6f2 100644 --- a/cli/cmd/pndGet.go +++ b/cli/cmd/pndGet.go @@ -46,7 +46,11 @@ var pndGetCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { 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 { spinner.Fail(err) return diff --git a/cli/cmd/pndList.go b/cli/cmd/pndList.go index f6b15d422ac28f12f0a5a943caf56c5d6bdb9714..51adf9946f52a1107e105c5b83c6ede3f0612949 100644 --- a/cli/cmd/pndList.go +++ b/cli/cmd/pndList.go @@ -47,7 +47,11 @@ var pndListCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { 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 { spinner.Fail(err) return @@ -69,14 +73,4 @@ var pndListCmd = &cobra.Command{ func init() { 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") } diff --git a/cli/cmd/pndRemove.go b/cli/cmd/pndRemove.go index 3c641a8174032adf143882012caa98c4ad2a2a49..4ed1032816777c35cd589f5ab4a67e6f56b73bea 100644 --- a/cli/cmd/pndRemove.go +++ b/cli/cmd/pndRemove.go @@ -52,7 +52,11 @@ var pndRemoveCmd = &cobra.Command{ spinner.Fail(err) return } - _, err = pndAdapter.RemovePnd(createContextWithAuthorization(), pid) + + // create a authorizedContext for further requests + ctx, ctxCancelFn = createContextWithAuthorization() + + _, err = pndAdapter.RemovePnd(ctx, pid) if err != nil { spinner.Fail(err) return diff --git a/cli/cmd/pndUse.go b/cli/cmd/pndUse.go index b1c0cee7c69399785812db70ccbfcd77859f9f65..03835c08132e5f94ff39a398054e5708eaf2213b 100644 --- a/cli/cmd/pndUse.go +++ b/cli/cmd/pndUse.go @@ -55,7 +55,10 @@ var pndUseCmd = &cobra.Command{ return } - _, err = api.GetPnd(createContextWithAuthorization(), viper.GetString("controllerAPIEndpoint"), newPND) + // create a authorizedContext for further requests + ctx, ctxCancelFn = createContextWithAuthorization() + + _, err = api.GetPnd(ctx, viper.GetString("controllerAPIEndpoint"), newPND) if err != nil { pterm.Error.Println(err) return diff --git a/cli/cmd/prompt.go b/cli/cmd/prompt.go index f297fe392b72c7f0b4bde44bc7e5bbcbd215b787..4fe3141ff4d7b19ceeb88dbe8eb37490c4b24b87 100644 --- a/cli/cmd/prompt.go +++ b/cli/cmd/prompt.go @@ -311,7 +311,9 @@ func completionBasedOnCmd(c *PromptCompleter, cmd *cobra.Command, inputSplit []s // the result is converted into a prompt.Suggest slice. func getNetworkElements() ([]prompt.Suggest, error) { spinner, _ := pterm.DefaultSpinner.Start("Fetching Network Elements from controller.") - resp, err := pndAdapter.GetFlattenedNetworkElements(createContextWithAuthorization()) + // create a authorizedContext for further requests + ctx, ctxCancelFn = createContextWithAuthorization() + resp, err := pndAdapter.GetFlattenedNetworkElements(ctx) if err != nil { spinner.Fail(err) return []prompt.Suggest{}, err @@ -327,7 +329,9 @@ func getNetworkElements() ([]prompt.Suggest, error) { func getAvailablePlugins() ([]prompt.Suggest, error) { spinner, _ := pterm.DefaultSpinner.Start("Fetching available plugins from controller.") - resp, err := pndAdapter.GetAvailablePlugins(createContextWithAuthorization()) + // create a authorizedContext for further requests + ctx, ctxCancelFn = createContextWithAuthorization() + resp, err := pndAdapter.GetAvailablePlugins(ctx) if err != nil { spinner.Fail(err) return []prompt.Suggest{}, err @@ -346,8 +350,9 @@ func getAvailablePlugins() ([]prompt.Suggest, error) { // current pulling status with the help of pterm. func getSchemaTreeForNetworkElementID(id uuid.UUID) (map[string]*yang.Entry, error) { spinner, _ := pterm.DefaultSpinner.Start("Fetching schema tree for Device with ID: ", id) - - networkElement, err := pndAdapter.GetFlattenedNetworkElement(createContextWithAuthorization(), id.String()) + // create a authorizedContext for further requests + ctx, ctxCancelFn = createContextWithAuthorization() + networkElement, err := pndAdapter.GetFlattenedNetworkElement(ctx, id.String()) if err != nil { spinner.Fail(err) return nil, err @@ -355,7 +360,9 @@ func getSchemaTreeForNetworkElementID(id uuid.UUID) (map[string]*yang.Entry, err pluginID := networkElement.GetMne().GetPluginid() pluginUUID := uuid.MustParse(pluginID) - schemaTree, err := pndAdapter.GetPluginSchemaTree(createContextWithAuthorization(), pluginUUID) + // create a authorizedContext for further requests + ctx, ctxCancelFn = createContextWithAuthorization() + schemaTree, err := pndAdapter.GetPluginSchemaTree(ctx, pluginUUID) if err != nil { spinner.Fail(err) return nil, err @@ -369,7 +376,9 @@ func getSchemaTreeForNetworkElementID(id uuid.UUID) (map[string]*yang.Entry, err // result is converted into a prompt.Suggest slice. func getPnds() ([]prompt.Suggest, error) { spinner, _ := pterm.DefaultSpinner.Start("Fetching PNDs from controller.") - resp, err := api.GetIds(createContextWithAuthorization(), viper.GetString("controllerAPIEndpoint")) + // create a authorizedContext for further requests + ctx, ctxCancelFn = createContextWithAuthorization() + resp, err := api.GetIds(ctx, viper.GetString("controllerAPIEndpoint")) if err != nil { spinner.Fail(err) return []prompt.Suggest{}, err @@ -389,8 +398,9 @@ func getPnds() ([]prompt.Suggest, error) { // slice. func getPendingChanges() ([]prompt.Suggest, error) { spinner, _ := pterm.DefaultSpinner.Start("Fetching committed changes.") - - resp, err := pndAdapter.PendingChanges(createContextWithAuthorization()) + // create a authorizedContext for further requests + ctx, ctxCancelFn = createContextWithAuthorization() + resp, err := pndAdapter.PendingChanges(ctx) if err != nil { spinner.Fail(err) return []prompt.Suggest{}, err @@ -410,8 +420,9 @@ func getPendingChanges() ([]prompt.Suggest, error) { // slice. func getCommittedChanges() ([]prompt.Suggest, error) { spinner, _ := pterm.DefaultSpinner.Start("Fetching pending changes.") - - resp, err := pndAdapter.CommittedChanges(createContextWithAuthorization()) + // create a authorizedContext for further requests + ctx, ctxCancelFn = createContextWithAuthorization() + resp, err := pndAdapter.CommittedChanges(ctx) if err != nil { spinner.Fail(err) return []prompt.Suggest{}, err diff --git a/cli/cmd/root.go b/cli/cmd/root.go index 75ea0313953a482f66c9d0b7cb372e6679177a03..bb88e3c96dd43d05e4bb6a479ea8ae495eca0c93 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -31,9 +31,11 @@ POSSIBILITY OF SUCH DAMAGE. package cmd import ( + "context" "errors" "fmt" "os" + "os/signal" "code.fbi.h-da.de/danet/gosdn/cli/adapter" @@ -53,6 +55,9 @@ var nbUserName string var nbUserPwd string var userToken string +var ctx context.Context +var ctxCancelFn context.CancelFunc + var pndAdapter *adapter.PndAdapter // rootCmd represents the base command when called without any subcommands. @@ -79,7 +84,8 @@ func Execute() { } func init() { - cobra.OnInitialize(initConfig) + ctx, ctxCancelFn = context.WithCancel(context.Background()) + cobra.OnInitialize(initConfig, startContextListener) // add CLI global parameters rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (./.gosdnc.toml)") @@ -89,6 +95,19 @@ func init() { rootCmd.Flags().StringVar(&grpcPort, "grpc-port", "55055", "port for gRPC NBI") } +func startContextListener() { + // listen for CTRL-C as interrupt + ch := make(chan os.Signal, 1) + signal.Notify(ch, os.Interrupt) + go func() { + select { + case <-ch: + ctxCancelFn() + case <-ctx.Done(): + } + }() +} + // initConfig reads in config file and ENV variables if set. func initConfig() { configFileName := ".gosdnc.toml" diff --git a/cli/cmd/userCreate.go b/cli/cmd/userCreate.go index 8a07b75ce3ebae205769dbc0fdb36349d751a6ec..fab47e9d24d09fdf060aea693002f5ffe5d652f2 100644 --- a/cli/cmd/userCreate.go +++ b/cli/cmd/userCreate.go @@ -63,7 +63,10 @@ var userCreateCmd = &cobra.Command{ }, } - resp, err := api.CreateUsers(createContextWithAuthorization(), viper.GetString("controllerAPIEndpoint"), users) + // create a authorizedContext for further requests + ctx, ctxCancelFn = createContextWithAuthorization() + + resp, err := api.CreateUsers(ctx, viper.GetString("controllerAPIEndpoint"), users) if err != nil { pterm.Error.Println(err) return diff --git a/cli/cmd/userDelete.go b/cli/cmd/userDelete.go index aa9fdc610d1af052c60da902101bb2db495dbc91..4fd0bc5251453e55b43da112bd38a10c083b5503 100644 --- a/cli/cmd/userDelete.go +++ b/cli/cmd/userDelete.go @@ -51,7 +51,10 @@ var userDeleteCmd = &cobra.Command{ // only one user for now, add more later if needed users := []string{nbUserName} - resp, err := api.DeleteUsers(createContextWithAuthorization(), viper.GetString("controllerAPIEndpoint"), users) + // create a authorizedContext for further requests + ctx, ctxCancelFn = createContextWithAuthorization() + + resp, err := api.DeleteUsers(ctx, viper.GetString("controllerAPIEndpoint"), users) if err != nil { pterm.Error.Println(err) return diff --git a/cli/cmd/userGet.go b/cli/cmd/userGet.go index 357966913129735b7c630689224ac698b96bf017..0aadd3767922ffed9361a0e5ddd4d840c3c5663b 100644 --- a/cli/cmd/userGet.go +++ b/cli/cmd/userGet.go @@ -47,8 +47,11 @@ var userGetCmd = &cobra.Command{ Long: `Requests one user using the provided name to search for it in the stored users.`, Run: func(cmd *cobra.Command, args []string) { + // create a authorizedContext for further requests + ctx, ctxCancelFn = createContextWithAuthorization() + resp, err := api.GetUser( - createContextWithAuthorization(), + ctx, viper.GetString("controllerAPIEndpoint"), nbUserName, uuid.Nil, diff --git a/cli/cmd/userGetAll.go b/cli/cmd/userGetAll.go index 8a5c650f0a1a396cfad9d8f2d7fd272149841f1f..a2611d3400334fe745feb9e426d80120fcc81869 100644 --- a/cli/cmd/userGetAll.go +++ b/cli/cmd/userGetAll.go @@ -46,7 +46,10 @@ var userGetAllCmd = &cobra.Command{ Long: `Requests all the available users.`, Run: func(cmd *cobra.Command, args []string) { - resp, err := api.GetAllUsers(createContextWithAuthorization(), viper.GetString("controllerAPIEndpoint")) + // create a authorizedContext for further requests + ctx, ctxCancelFn = createContextWithAuthorization() + + resp, err := api.GetAllUsers(ctx, viper.GetString("controllerAPIEndpoint")) if err != nil { pterm.Error.Println(err) return diff --git a/cli/cmd/userUpdate.go b/cli/cmd/userUpdate.go index 7849d583595cef4396e23508b2c9d3093b03917f..2130f9867cbfc653f10616cd701c7b084d03faba 100644 --- a/cli/cmd/userUpdate.go +++ b/cli/cmd/userUpdate.go @@ -50,8 +50,11 @@ var userUpdateCmd = &cobra.Command{ role is optional but needed to operate on PNDs.`, Run: func(cmd *cobra.Command, args []string) { + // create a authorizedContext for further requests + ctx, ctxCancelFn = createContextWithAuthorization() + existingUser, err := api.GetUser( - createContextWithAuthorization(), + ctx, viper.GetString("controllerAPIEndpoint"), nbUserName, uuid.Nil, @@ -76,7 +79,10 @@ var userUpdateCmd = &cobra.Command{ }, } - resp, err := api.UpdateUsers(createContextWithAuthorization(), viper.GetString("controllerAPIEndpoint"), users) + // create a authorizedContext for further requests + ctx, ctxCancelFn = createContextWithAuthorization() + + resp, err := api.UpdateUsers(ctx, viper.GetString("controllerAPIEndpoint"), users) if err != nil { pterm.Error.Println(err) return diff --git a/cli/cmd/utils.go b/cli/cmd/utils.go index ca51618da5b45ff5fde1c0691adeab6c9664498e..10c1b95b24546fb414dd8316943001782180f7fb 100644 --- a/cli/cmd/utils.go +++ b/cli/cmd/utils.go @@ -37,6 +37,7 @@ import ( "fmt" "net" "strconv" + "time" gpb "github.com/openconfig/gnmi/proto/gnmi" gnmiv "github.com/openconfig/gnmi/value" @@ -68,10 +69,12 @@ func sliceContains[T comparable](slice []T, toCompare T) bool { return false } -func createContextWithAuthorization() context.Context { +func createContextWithAuthorization() (context.Context, context.CancelFunc) { //TODO: try to get token string first, if "" return err, followed by print in cli about required login md := metadata.Pairs("authorize", userToken) - return metadata.NewOutgoingContext(context.Background(), md) + deadline := time.Now().Add(10 * time.Second) + c, cancel := context.WithDeadline(context.Background(), deadline) + return metadata.NewOutgoingContext(c, md), cancel } // convertStringToGnmiTypedValue allows to convert a string into a