diff --git a/GNUmakefile b/GNUmakefile index 93aa3b17..cc2983d2 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -1,5 +1,5 @@ BINARY=taikun -GOLANGCI_LINTERS_VERSION := v2.11.4 +GOLANGCI_LINTERS_VERSION := v2.12.2 default: install @@ -12,7 +12,7 @@ goreleaser-install: ## Installs goreleaser binary with go install go install github.com/goreleaser/goreleaser/v2@latest go-linters-install: ## Installs Golang's linters locally for verification - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin ${GOLANGCI_LINTERS_VERSION} + curl -sSfL https://golangci-lint.run/install.sh | sh -s -- -b $(shell go env GOPATH)/bin ${GOLANGCI_LINTERS_VERSION} .PHONY: build build: go-vendor ## Builds taikun-cli binary diff --git a/cmd/accessprofile/add/add.go b/cmd/accessprofile/add/add.go index b571a488..da4388e5 100644 --- a/cmd/accessprofile/add/add.go +++ b/cmd/accessprofile/add/add.go @@ -1,8 +1,6 @@ package add import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -56,7 +54,7 @@ func NewCmdAdd() *cobra.Command { Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { opts.Name = args[0] - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -71,7 +69,10 @@ func NewCmdAdd() *cobra.Command { return cmd } -func addRun(opts *AddOptions) error { +func addRun(cmd *cobra.Command, opts *AddOptions) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -112,7 +113,7 @@ func addRun(opts *AddOptions) error { } // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.AccessProfilesAPI.AccessprofilesCreate(context.TODO()).CreateAccessProfileCommand(body).Execute() + data, response, err := myApiClient.Client.AccessProfilesAPI.AccessprofilesCreate(ctx).CreateAccessProfileCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accessprofile/allowedhost/add/add.go b/cmd/accessprofile/allowedhost/add/add.go index 22d793c3..77913d3f 100644 --- a/cmd/accessprofile/allowedhost/add/add.go +++ b/cmd/accessprofile/allowedhost/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "fmt" "github.com/itera-io/taikun-cli/utils/out" tk "github.com/itera-io/taikungoclient" @@ -60,7 +59,7 @@ func NewCmdAdd() *cobra.Command { return fmt.Errorf("mask bits must be in the range of [0, 32]") } - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -78,7 +77,10 @@ func NewCmdAdd() *cobra.Command { return cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -91,7 +93,7 @@ func addRun(opts *AddOptions) (err error) { } // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.AllowedHostAPI.AllowedhostCreate(context.TODO()).CreateAllowedHostCommand(body).Execute() + data, response, err := myApiClient.Client.AllowedHostAPI.AllowedhostCreate(ctx).CreateAllowedHostCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accessprofile/allowedhost/list/list.go b/cmd/accessprofile/allowedhost/list/list.go index a4ab6c21..020178fb 100644 --- a/cmd/accessprofile/allowedhost/list/list.go +++ b/cmd/accessprofile/allowedhost/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -50,7 +49,7 @@ func NewCmdList() *cobra.Command { return cmderr.ErrIDArgumentNotANumber } opts.AccessProfileID = accessProfileID - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -61,9 +60,12 @@ func NewCmdList() *cobra.Command { return cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - myRequest := myApiClient.Client.AllowedHostAPI.AllowedhostList(context.TODO(), opts.AccessProfileID) + myRequest := myApiClient.Client.AllowedHostAPI.AllowedhostList(ctx, opts.AccessProfileID) var allowedHosts []interface{} for { diff --git a/cmd/accessprofile/allowedhost/remove/remove.go b/cmd/accessprofile/allowedhost/remove/remove.go index 09517c6e..bab7599e 100644 --- a/cmd/accessprofile/allowedhost/remove/remove.go +++ b/cmd/accessprofile/allowedhost/remove/remove.go @@ -2,25 +2,28 @@ package remove import ( "context" + "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" - "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" "github.com/spf13/cobra" ) func NewCmdDelete() *cobra.Command { - var id int32 cmd := &cobra.Command{ Use: "delete ...", Short: "Delete an allowed host", - Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) (err error) { - id, err = types.Atoi32(args[0]) + Args: cobra.MinimumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + ids, err := cmdutils.ArgsToNumericalIDs(args) if err != nil { - return + return cmderr.ErrIDArgumentNotANumber } - return deleteRun(id) + return cmdutils.DeleteMultiple(ids, func(id int32) error { + return deleteRun(ctx, id) + }) }, Aliases: cmdutils.DeleteAliases, } @@ -28,12 +31,12 @@ func NewCmdDelete() *cobra.Command { return cmd } -func deleteRun(allowedHostID int32) (err error) { +func deleteRun(ctx context.Context, allowedHostID int32) (err error) { // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - response, err := myApiClient.Client.AllowedHostAPI.AllowedhostDelete(context.TODO(), allowedHostID).Execute() + response, err := myApiClient.Client.AllowedHostAPI.AllowedhostDelete(ctx, allowedHostID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accessprofile/list/list.go b/cmd/accessprofile/list/list.go index 9014648d..56376ec2 100644 --- a/cmd/accessprofile/list/list.go +++ b/cmd/accessprofile/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/api" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/config" @@ -57,7 +56,7 @@ func NewCmdList() *cobra.Command { Use: "list", Short: "List access profiles", RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Args: cobra.NoArgs, Aliases: cmdutils.ListAliases, @@ -72,7 +71,10 @@ func NewCmdList() *cobra.Command { return cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -83,7 +85,7 @@ func listRun(opts *ListOptions) (err error) { myApiClient := tk.NewClient() // Prepare the arguments for the query - myRequest := myApiClient.Client.AccessProfilesAPI.AccessprofilesList(context.TODO()) + myRequest := myApiClient.Client.AccessProfilesAPI.AccessprofilesList(ctx) if opts.OrganizationID != 0 { myRequest = myRequest.OrganizationId(opts.OrganizationID) } diff --git a/cmd/accessprofile/lock/lock.go b/cmd/accessprofile/lock/lock.go index 1368f6b0..2358fe81 100644 --- a/cmd/accessprofile/lock/lock.go +++ b/cmd/accessprofile/lock/lock.go @@ -1,13 +1,13 @@ package lock import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdLock() *cobra.Command { @@ -20,20 +20,23 @@ func NewCmdLock() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return lockRun(id) + return lockRun(cmd, id) }, } return cmd } -func lockRun(accessProfileID int32) (err error) { +func lockRun(cmd *cobra.Command, accessProfileID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.AccessProfilesLockManagementCommand{ Id: &accessProfileID, Mode: *taikuncore.NewNullableString(&types.LockedMode), } - response, err := myApiClient.Client.AccessProfilesAPI.AccessprofilesLockManager(context.TODO()).AccessProfilesLockManagementCommand(body).Execute() + response, err := myApiClient.Client.AccessProfilesAPI.AccessprofilesLockManager(ctx).AccessProfilesLockManagementCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accessprofile/remove/remove.go b/cmd/accessprofile/remove/remove.go index 74ff8b45..f2bf3b58 100644 --- a/cmd/accessprofile/remove/remove.go +++ b/cmd/accessprofile/remove/remove.go @@ -2,6 +2,7 @@ package remove import ( "context" + "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -15,11 +16,15 @@ func NewCmdDelete() *cobra.Command { Short: "Delete one or more access profiles", Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() ids, err := cmdutils.ArgsToNumericalIDs(args) if err != nil { return cmderr.ErrIDArgumentNotANumber } - return cmdutils.DeleteMultiple(ids, deleteRun) + return cmdutils.DeleteMultiple(ids, func(id int32) error { + return deleteRun(ctx, id) + }) }, Aliases: cmdutils.DeleteAliases, } @@ -27,13 +32,12 @@ func NewCmdDelete() *cobra.Command { return cmd } -func deleteRun(accessProfileID int32) (err error) { +func deleteRun(ctx context.Context, accessProfileID int32) (err error) { myApiClient := tk.NewClient() - response, err := myApiClient.Client.AccessProfilesAPI.AccessprofilesDelete(context.TODO(), accessProfileID).Execute() + response, err := myApiClient.Client.AccessProfilesAPI.AccessprofilesDelete(ctx, accessProfileID).Execute() if err != nil { return tk.CreateError(response, err) } out.PrintDeleteSuccess("Access profile", accessProfileID) return - } diff --git a/cmd/accessprofile/sshuser/add/add.go b/cmd/accessprofile/sshuser/add/add.go index 9c1a0736..c4240c34 100644 --- a/cmd/accessprofile/sshuser/add/add.go +++ b/cmd/accessprofile/sshuser/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "fmt" "github.com/itera-io/taikun-cli/utils/out" tk "github.com/itera-io/taikungoclient" @@ -47,7 +46,7 @@ func NewCmdAdd() *cobra.Command { return } - isValid, err := sshPublicKeyIsValid(opts.PublicKey) + isValid, err := sshPublicKeyIsValid(cmd, opts.PublicKey) if err != nil { return } @@ -55,7 +54,7 @@ func NewCmdAdd() *cobra.Command { return fmt.Errorf("SSH public key must be valid") } - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -71,7 +70,10 @@ func NewCmdAdd() *cobra.Command { return cmd } -func sshPublicKeyIsValid(sshPublicKey string) (bool, error) { +func sshPublicKeyIsValid(cmd *cobra.Command, sshPublicKey string) (bool, error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -81,7 +83,7 @@ func sshPublicKeyIsValid(sshPublicKey string) (bool, error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.CheckerAPI.CheckerSsh(context.TODO()).SshKeyCommand(body).Execute() + response, err := myApiClient.Client.CheckerAPI.CheckerSsh(ctx).SshKeyCommand(body).Execute() if err != nil { return false, tk.CreateError(response, err) } @@ -89,7 +91,10 @@ func sshPublicKeyIsValid(sshPublicKey string) (bool, error) { } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -101,7 +106,7 @@ func addRun(opts *AddOptions) (err error) { } // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.SshUsersAPI.SshusersCreate(context.TODO()).CreateSshUserCommand(body).Execute() + data, response, err := myApiClient.Client.SshUsersAPI.SshusersCreate(ctx).CreateSshUserCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accessprofile/sshuser/list/list.go b/cmd/accessprofile/sshuser/list/list.go index 291fe369..ecc7f266 100644 --- a/cmd/accessprofile/sshuser/list/list.go +++ b/cmd/accessprofile/sshuser/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -47,7 +46,7 @@ func NewCmdList() *cobra.Command { return cmderr.ErrIDArgumentNotANumber } opts.AccessProfileID = accessProfileID - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -58,12 +57,15 @@ func NewCmdList() *cobra.Command { return cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.SshUsersAPI.SshusersList(context.TODO(), opts.AccessProfileID).Execute() + data, response, err := myApiClient.Client.SshUsersAPI.SshusersList(ctx, opts.AccessProfileID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accessprofile/sshuser/remove/remove.go b/cmd/accessprofile/sshuser/remove/remove.go index e4a224d2..415e1dc2 100644 --- a/cmd/accessprofile/sshuser/remove/remove.go +++ b/cmd/accessprofile/sshuser/remove/remove.go @@ -16,11 +16,15 @@ func NewCmdDelete() *cobra.Command { Short: "Delete one or more SSH users", Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() ids, err := cmdutils.ArgsToNumericalIDs(args) if err != nil { return cmderr.ErrIDArgumentNotANumber } - return cmdutils.DeleteMultiple(ids, deleteRun) + return cmdutils.DeleteMultiple(ids, func(id int32) error { + return deleteRun(ctx, id) + }) }, Aliases: cmdutils.DeleteAliases, } @@ -28,7 +32,7 @@ func NewCmdDelete() *cobra.Command { return cmd } -func deleteRun(sshUserID int32) (err error) { +func deleteRun(ctx context.Context, sshUserID int32) (err error) { // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -38,7 +42,7 @@ func deleteRun(sshUserID int32) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.SshUsersAPI.SshusersDelete(context.TODO()).DeleteSshUserCommand(body).Execute() + response, err := myApiClient.Client.SshUsersAPI.SshusersDelete(ctx).DeleteSshUserCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accessprofile/trustedregistry/add/add.go b/cmd/accessprofile/trustedregistry/add/add.go index 3f8e4639..797fa4e1 100644 --- a/cmd/accessprofile/trustedregistry/add/add.go +++ b/cmd/accessprofile/trustedregistry/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -43,7 +42,7 @@ func NewCmdAdd() *cobra.Command { if err != nil { return } - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -56,7 +55,10 @@ func NewCmdAdd() *cobra.Command { return cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -67,7 +69,7 @@ func addRun(opts *AddOptions) (err error) { } // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.TrustedRegistriesAPI.TrustedregistriesCreate(context.TODO()).CreateTrustedRegistriesCommand(body).Execute() + data, response, err := myApiClient.Client.TrustedRegistriesAPI.TrustedregistriesCreate(ctx).CreateTrustedRegistriesCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accessprofile/trustedregistry/list/list.go b/cmd/accessprofile/trustedregistry/list/list.go index 523e5e19..81be6e4d 100644 --- a/cmd/accessprofile/trustedregistry/list/list.go +++ b/cmd/accessprofile/trustedregistry/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -44,7 +43,7 @@ func NewCmdList() *cobra.Command { return cmderr.ErrIDArgumentNotANumber } opts.AccessProfileID = accessProfileID - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -55,12 +54,15 @@ func NewCmdList() *cobra.Command { return cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.TrustedRegistriesAPI.TrustedregistriesList(context.TODO(), opts.AccessProfileID).Execute() + data, response, err := myApiClient.Client.TrustedRegistriesAPI.TrustedregistriesList(ctx, opts.AccessProfileID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accessprofile/trustedregistry/remove/remove.go b/cmd/accessprofile/trustedregistry/remove/remove.go index 66900192..09184bd6 100644 --- a/cmd/accessprofile/trustedregistry/remove/remove.go +++ b/cmd/accessprofile/trustedregistry/remove/remove.go @@ -15,11 +15,15 @@ func NewCmdDelete() *cobra.Command { Short: "Delete one or more trusted registries", Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() ids, err := cmdutils.ArgsToNumericalIDs(args) if err != nil { return cmderr.ErrIDArgumentNotANumber } - return cmdutils.DeleteMultiple(ids, deleteRun) + return cmdutils.DeleteMultiple(ids, func(id int32) error { + return deleteRun(ctx, id) + }) }, Aliases: cmdutils.DeleteAliases, } @@ -27,12 +31,12 @@ func NewCmdDelete() *cobra.Command { return cmd } -func deleteRun(trustedRegistryID int32) (err error) { +func deleteRun(ctx context.Context, trustedRegistryID int32) (err error) { // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - response, err := myApiClient.Client.TrustedRegistriesAPI.TrustedregistriesDelete(context.TODO(), trustedRegistryID).Execute() + response, err := myApiClient.Client.TrustedRegistriesAPI.TrustedregistriesDelete(ctx, trustedRegistryID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accessprofile/unlock/unlock.go b/cmd/accessprofile/unlock/unlock.go index ba980518..29df0079 100644 --- a/cmd/accessprofile/unlock/unlock.go +++ b/cmd/accessprofile/unlock/unlock.go @@ -1,13 +1,13 @@ package unlock import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdUnlock() *cobra.Command { @@ -20,20 +20,23 @@ func NewCmdUnlock() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return unlockRun(id) + return unlockRun(cmd, id) }, } return cmd } -func unlockRun(accessProfileID int32) (err error) { +func unlockRun(cmd *cobra.Command, accessProfileID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.AccessProfilesLockManagementCommand{ Id: &accessProfileID, Mode: *taikuncore.NewNullableString(&types.UnlockedMode), } - response, err := myApiClient.Client.AccessProfilesAPI.AccessprofilesLockManager(context.TODO()).AccessProfilesLockManagementCommand(body).Execute() + response, err := myApiClient.Client.AccessProfilesAPI.AccessprofilesLockManager(ctx).AccessProfilesLockManagementCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accounts/add-admin/add-admin.go b/cmd/accounts/add-admin/add-admin.go index 831c6999..9ea8ad37 100644 --- a/cmd/accounts/add-admin/add-admin.go +++ b/cmd/accounts/add-admin/add-admin.go @@ -1,14 +1,13 @@ package addadmin import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type AddAdminOptions struct { @@ -28,7 +27,7 @@ func NewCmdAddAdmin() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return addAdminRun(&opts) + return addAdminRun(cmd, &opts) }, } @@ -38,7 +37,10 @@ func NewCmdAddAdmin() *cobra.Command { return &cmd } -func addAdminRun(opts *AddAdminOptions) (err error) { +func addAdminRun(cmd *cobra.Command, opts *AddAdminOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.AddAccountAdminCommand{ @@ -46,7 +48,7 @@ func addAdminRun(opts *AddAdminOptions) (err error) { UserId: *taikuncore.NewNullableString(&opts.UserID), } - response, err := myApiClient.Client.AccountsAPI.AccountsAddAccountAdmin(context.TODO()).AddAccountAdminCommand(body).Execute() + response, err := myApiClient.Client.AccountsAPI.AccountsAddAccountAdmin(ctx).AddAccountAdminCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accounts/check/check.go b/cmd/accounts/check/check.go index 4022c8d7..a48a99d1 100644 --- a/cmd/accounts/check/check.go +++ b/cmd/accounts/check/check.go @@ -1,12 +1,11 @@ package check import ( - "context" - "github.com/itera-io/taikun-cli/utils/out" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdCheck() *cobra.Command { @@ -15,21 +14,24 @@ func NewCmdCheck() *cobra.Command { Short: "Check if an account name is available", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - return checkAccount(args[0]) + return checkAccount(cmd, args[0]) }, } return &cmd } -func checkAccount(accountName string) (err error) { +func checkAccount(cmd *cobra.Command, accountName string) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.CheckDuplicateAccountCommand{ Name: *taikuncore.NewNullableString(&accountName), } - response, err := myApiClient.Client.AccountsAPI.AccountsCheckDuplicateEntity(context.TODO()).CheckDuplicateAccountCommand(body).Execute() + response, err := myApiClient.Client.AccountsAPI.AccountsCheckDuplicateEntity(ctx).CheckDuplicateAccountCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accounts/create/create.go b/cmd/accounts/create/create.go index 01456161..f4df9a42 100644 --- a/cmd/accounts/create/create.go +++ b/cmd/accounts/create/create.go @@ -1,13 +1,13 @@ package create import ( - "context" "fmt" "github.com/itera-io/taikun-cli/utils/out" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type CreateOptions struct { @@ -23,7 +23,7 @@ func NewCmdCreateAccount() *cobra.Command { Short: "Create a new account", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - return createAccount(args[0], &opts) + return createAccount(cmd, args[0], &opts) }, } @@ -34,7 +34,10 @@ func NewCmdCreateAccount() *cobra.Command { return &cmd } -func createAccount(accountName string, opts *CreateOptions) (err error) { +func createAccount(cmd *cobra.Command, accountName string, opts *CreateOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // input parameters sanity check if opts.Email == "" { return fmt.Errorf("email must be specified") @@ -47,7 +50,7 @@ func createAccount(accountName string, opts *CreateOptions) (err error) { CreateOrganization: &opts.CreateOrg, } - _, response, err := myApiClient.Client.AccountsAPI.AccountsCreate(context.TODO()).CreateAccountCommand(body).Execute() + _, response, err := myApiClient.Client.AccountsAPI.AccountsCreate(ctx).CreateAccountCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accounts/delete/delete.go b/cmd/accounts/delete/delete.go index 9b280ade..b2b9ca92 100644 --- a/cmd/accounts/delete/delete.go +++ b/cmd/accounts/delete/delete.go @@ -1,12 +1,11 @@ package delete import ( - "context" - "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdDeleteAccount() *cobra.Command { @@ -19,17 +18,20 @@ func NewCmdDeleteAccount() *cobra.Command { if err != nil { return err } - return deleteAccount(accountID) + return deleteAccount(cmd, accountID) }, } return &cmd } -func deleteAccount(accountID int32) (err error) { +func deleteAccount(cmd *cobra.Command, accountID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - response, err := myApiClient.Client.AccountsAPI.AccountsDelete(context.TODO(), accountID).Execute() + response, err := myApiClient.Client.AccountsAPI.AccountsDelete(ctx, accountID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accounts/details/details.go b/cmd/accounts/details/details.go index 35d7e5f9..9e806713 100644 --- a/cmd/accounts/details/details.go +++ b/cmd/accounts/details/details.go @@ -1,8 +1,6 @@ package details import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -43,7 +41,7 @@ func NewCmdDetails() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return detailsRun(&opts) + return detailsRun(cmd, &opts) }, } @@ -52,9 +50,12 @@ func NewCmdDetails() *cobra.Command { return &cmd } -func detailsRun(opts *DetailsOptions) (err error) { +func detailsRun(cmd *cobra.Command, opts *DetailsOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - data, response, err := myApiClient.Client.AccountsAPI.AccountsDetails(context.TODO(), opts.AccountID).Execute() + data, response, err := myApiClient.Client.AccountsAPI.AccountsDetails(ctx, opts.AccountID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accounts/disable-2fa/disable_2fa.go b/cmd/accounts/disable-2fa/disable_2fa.go index aae3e43b..163c5522 100644 --- a/cmd/accounts/disable-2fa/disable_2fa.go +++ b/cmd/accounts/disable-2fa/disable_2fa.go @@ -1,12 +1,11 @@ package disable2fa import ( - "context" - "github.com/itera-io/taikun-cli/utils/out" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type Disable2faOptions struct { @@ -21,7 +20,7 @@ func NewCmdDisable2fa() *cobra.Command { Short: "Disable 2FA management", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return disable2fa(&opts) + return disable2fa(cmd, &opts) }, } @@ -30,7 +29,10 @@ func NewCmdDisable2fa() *cobra.Command { return &cmd } -func disable2fa(opts *Disable2faOptions) (err error) { +func disable2fa(cmd *cobra.Command, opts *Disable2faOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.NewDisableTwoFaManagementCommand() @@ -38,7 +40,7 @@ func disable2fa(opts *Disable2faOptions) (err error) { body.SetVerificationCode(opts.VerificationCode) } - response, err := myApiClient.Client.AccountsAPI.AccountsDisable2faManagement(context.TODO()).DisableTwoFaManagementCommand(*body).Execute() + response, err := myApiClient.Client.AccountsAPI.AccountsDisable2faManagement(ctx).DisableTwoFaManagementCommand(*body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accounts/enable-2fa/enable_2fa.go b/cmd/accounts/enable-2fa/enable_2fa.go index 9e965ce9..f35e3019 100644 --- a/cmd/accounts/enable-2fa/enable_2fa.go +++ b/cmd/accounts/enable-2fa/enable_2fa.go @@ -1,11 +1,10 @@ package enable2fa import ( - "context" - "github.com/itera-io/taikun-cli/utils/out" tk "github.com/itera-io/taikungoclient" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdEnable2fa() *cobra.Command { @@ -14,17 +13,20 @@ func NewCmdEnable2fa() *cobra.Command { Short: "Enable 2FA management", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return enable2fa() + return enable2fa(cmd) }, } return &cmd } -func enable2fa() (err error) { +func enable2fa(cmd *cobra.Command) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - response, err := myApiClient.Client.AccountsAPI.AccountsEnable2faManagement(context.TODO()).Body(map[string]interface{}{}).Execute() + response, err := myApiClient.Client.AccountsAPI.AccountsEnable2faManagement(ctx).Body(map[string]interface{}{}).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accounts/groups/info/info.go b/cmd/accounts/groups/info/info.go index 9b38d72e..e2987ec6 100644 --- a/cmd/accounts/groups/info/info.go +++ b/cmd/accounts/groups/info/info.go @@ -1,8 +1,6 @@ package info import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -45,7 +43,7 @@ func NewCmdInfo() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return infoRun(&opts) + return infoRun(cmd, &opts) }, } @@ -54,9 +52,12 @@ func NewCmdInfo() *cobra.Command { return &cmd } -func infoRun(opts *InfoOptions) (err error) { +func infoRun(cmd *cobra.Command, opts *InfoOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - data, response, err := myApiClient.Client.AccountsAPI.AccountsAccountGroupDetails(context.TODO(), opts.AccountID, opts.GroupID).Execute() + data, response, err := myApiClient.Client.AccountsAPI.AccountsAccountGroupDetails(ctx, opts.AccountID, opts.GroupID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accounts/groups/list/list.go b/cmd/accounts/groups/list/list.go index e3f33dd4..45679739 100644 --- a/cmd/accounts/groups/list/list.go +++ b/cmd/accounts/groups/list/list.go @@ -1,8 +1,6 @@ package list import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -38,7 +36,7 @@ func NewCmdList() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return listRun(&opts) + return listRun(cmd, &opts) }, } @@ -48,11 +46,14 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() var groups = make([]taikuncore.CommonDropdownDto, 0) - req := myApiClient.Client.AccountsAPI.AccountsAccountGroupsDropdown(context.TODO(), opts.AccountID).Limit(opts.Limit) + req := myApiClient.Client.AccountsAPI.AccountsAccountGroupsDropdown(ctx, opts.AccountID).Limit(opts.Limit) for { data, response, err := req.Execute() if err != nil { diff --git a/cmd/accounts/list/list.go b/cmd/accounts/list/list.go index f26cea70..39317b98 100644 --- a/cmd/accounts/list/list.go +++ b/cmd/accounts/list/list.go @@ -1,8 +1,6 @@ package list import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -36,7 +34,7 @@ func NewCmdListAccounts() *cobra.Command { Short: "List available accounts", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return listAccounts(&opts) + return listAccounts(cmd, &opts) }, } @@ -46,11 +44,14 @@ func NewCmdListAccounts() *cobra.Command { return &cmd } -func listAccounts(opts *ListOptions) (err error) { +func listAccounts(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() var accounts = make([]taikuncore.AccountList, 0) - req := myApiClient.Client.AccountsAPI.AccountsListAccounts(context.TODO()) + req := myApiClient.Client.AccountsAPI.AccountsListAccounts(ctx) if opts.Search != "" { req = req.Search(opts.Search) } diff --git a/cmd/accounts/organizations/available/available.go b/cmd/accounts/organizations/available/available.go index 93bf75f4..05933f2c 100644 --- a/cmd/accounts/organizations/available/available.go +++ b/cmd/accounts/organizations/available/available.go @@ -1,8 +1,6 @@ package available import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -10,6 +8,7 @@ import ( "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) var listFields = fields.New( @@ -29,16 +28,19 @@ func NewCmdListAvailable() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return listAvailableRun(accountID) + return listAvailableRun(cmd, accountID) }, } return &cmd } -func listAvailableRun(accountID int32) (err error) { +func listAvailableRun(cmd *cobra.Command, accountID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - data, response, err := myApiClient.Client.AccountsAPI.AccountsAccountOrganizationsAvailable(context.TODO(), accountID).Execute() + data, response, err := myApiClient.Client.AccountsAPI.AccountsAccountOrganizationsAvailable(ctx, accountID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accounts/organizations/info/info.go b/cmd/accounts/organizations/info/info.go index 3e09f65b..c21d1896 100644 --- a/cmd/accounts/organizations/info/info.go +++ b/cmd/accounts/organizations/info/info.go @@ -1,8 +1,6 @@ package info import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -46,7 +44,7 @@ func NewCmdInfo() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return infoRun(&opts) + return infoRun(cmd, &opts) }, } @@ -55,9 +53,12 @@ func NewCmdInfo() *cobra.Command { return &cmd } -func infoRun(opts *InfoOptions) (err error) { +func infoRun(cmd *cobra.Command, opts *InfoOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - data, response, err := myApiClient.Client.AccountsAPI.AccountsAccountOrganizationDetails(context.TODO(), opts.AccountID, opts.OrganizationID).Execute() + data, response, err := myApiClient.Client.AccountsAPI.AccountsAccountOrganizationDetails(ctx, opts.AccountID, opts.OrganizationID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accounts/organizations/list/list.go b/cmd/accounts/organizations/list/list.go index cacce5ab..16484fe5 100644 --- a/cmd/accounts/organizations/list/list.go +++ b/cmd/accounts/organizations/list/list.go @@ -1,8 +1,6 @@ package list import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -40,7 +38,7 @@ func NewCmdList() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return listRun(&opts) + return listRun(cmd, &opts) }, } @@ -50,11 +48,14 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() var organizations = make([]taikuncore.OrganizationsWithGroupInfoResultDto, 0) - req := myApiClient.Client.AccountsAPI.AccountsAccountOrganizationsWithGroup(context.TODO(), opts.AccountID).Limit(opts.Limit) + req := myApiClient.Client.AccountsAPI.AccountsAccountOrganizationsWithGroup(ctx, opts.AccountID).Limit(opts.Limit) for { data, response, err := req.Execute() if err != nil { diff --git a/cmd/accounts/projects/info/info.go b/cmd/accounts/projects/info/info.go index af8df815..a2289178 100644 --- a/cmd/accounts/projects/info/info.go +++ b/cmd/accounts/projects/info/info.go @@ -1,8 +1,6 @@ package info import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -49,7 +47,7 @@ func NewCmdInfo() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return infoRun(&opts) + return infoRun(cmd, &opts) }, } @@ -58,9 +56,12 @@ func NewCmdInfo() *cobra.Command { return &cmd } -func infoRun(opts *InfoOptions) (err error) { +func infoRun(cmd *cobra.Command, opts *InfoOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - data, response, err := myApiClient.Client.AccountsAPI.AccountsAccountProjectDetails(context.TODO(), opts.AccountID, opts.ProjectID).Execute() + data, response, err := myApiClient.Client.AccountsAPI.AccountsAccountProjectDetails(ctx, opts.AccountID, opts.ProjectID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accounts/projects/list/list.go b/cmd/accounts/projects/list/list.go index 609d4ac4..ee6f47a0 100644 --- a/cmd/accounts/projects/list/list.go +++ b/cmd/accounts/projects/list/list.go @@ -1,8 +1,6 @@ package list import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -38,7 +36,7 @@ func NewCmdList() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return listRun(&opts) + return listRun(cmd, &opts) }, } @@ -48,11 +46,14 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() var projects = make([]taikuncore.CommonDropdownDto, 0) - req := myApiClient.Client.AccountsAPI.AccountsAccountProjectsDropdown(context.TODO(), opts.AccountID).Limit(opts.Limit) + req := myApiClient.Client.AccountsAPI.AccountsAccountProjectsDropdown(ctx, opts.AccountID).Limit(opts.Limit) for { data, response, err := req.Execute() if err != nil { diff --git a/cmd/accounts/transfer-ownership/transfer-ownership.go b/cmd/accounts/transfer-ownership/transfer-ownership.go index f1313567..4be2b890 100644 --- a/cmd/accounts/transfer-ownership/transfer-ownership.go +++ b/cmd/accounts/transfer-ownership/transfer-ownership.go @@ -1,12 +1,11 @@ package transferownership import ( - "context" - "github.com/itera-io/taikun-cli/utils/out" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type TransferOwnershipOptions struct { @@ -22,20 +21,23 @@ func NewCmdTransferOwnership() *cobra.Command { Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { opts.UserID = args[0] - return transferOwnershipRun(&opts) + return transferOwnershipRun(cmd, &opts) }, } return &cmd } -func transferOwnershipRun(opts *TransferOwnershipOptions) (err error) { +func transferOwnershipRun(cmd *cobra.Command, opts *TransferOwnershipOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.TransferOwnershipCommand{} body.SetUserId(opts.UserID) - response, err := myApiClient.Client.AccountsAPI.AccountsTransferOwnership(context.TODO()).TransferOwnershipCommand(body).Execute() + response, err := myApiClient.Client.AccountsAPI.AccountsTransferOwnership(ctx).TransferOwnershipCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accounts/update/update.go b/cmd/accounts/update/update.go index 4fc55063..06f04434 100644 --- a/cmd/accounts/update/update.go +++ b/cmd/accounts/update/update.go @@ -1,14 +1,13 @@ package update import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type UpdateOptions struct { @@ -29,7 +28,7 @@ func NewCmdUpdate() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return updateRun(&opts) + return updateRun(cmd, &opts) }, } @@ -39,7 +38,10 @@ func NewCmdUpdate() *cobra.Command { return &cmd } -func updateRun(opts *UpdateOptions) (err error) { +func updateRun(cmd *cobra.Command, opts *UpdateOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.UpdateAccountCommand{ @@ -53,7 +55,7 @@ func updateRun(opts *UpdateOptions) (err error) { body.SetEmail(opts.Email) } - _, response, err := myApiClient.Client.AccountsAPI.AccountsUpdate(context.TODO()).UpdateAccountCommand(body).Execute() + _, response, err := myApiClient.Client.AccountsAPI.AccountsUpdate(ctx).UpdateAccountCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accounts/users/available/available.go b/cmd/accounts/users/available/available.go index 736586dd..08c8f0a8 100644 --- a/cmd/accounts/users/available/available.go +++ b/cmd/accounts/users/available/available.go @@ -1,8 +1,6 @@ package available import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -10,6 +8,7 @@ import ( "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) var listFields = fields.New( @@ -29,16 +28,19 @@ func NewCmdListAvailable() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return listAvailableRun(accountID) + return listAvailableRun(cmd, accountID) }, } return &cmd } -func listAvailableRun(accountID int32) (err error) { +func listAvailableRun(cmd *cobra.Command, accountID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - data, response, err := myApiClient.Client.AccountsAPI.AccountsAccountUsersAvailable(context.TODO(), accountID).Execute() + data, response, err := myApiClient.Client.AccountsAPI.AccountsAccountUsersAvailable(ctx, accountID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accounts/users/info/info.go b/cmd/accounts/users/info/info.go index 4bfcc0a4..6db8faa5 100644 --- a/cmd/accounts/users/info/info.go +++ b/cmd/accounts/users/info/info.go @@ -1,8 +1,6 @@ package info import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -44,7 +42,7 @@ func NewCmdInfo() *cobra.Command { return cmderr.ErrIDArgumentNotANumber } opts.UserID = args[1] - return infoRun(&opts) + return infoRun(cmd, &opts) }, } @@ -53,9 +51,12 @@ func NewCmdInfo() *cobra.Command { return &cmd } -func infoRun(opts *InfoOptions) (err error) { +func infoRun(cmd *cobra.Command, opts *InfoOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - data, response, err := myApiClient.Client.AccountsAPI.AccountsAccountUserDetails(context.TODO(), opts.AccountID, opts.UserID).Execute() + data, response, err := myApiClient.Client.AccountsAPI.AccountsAccountUserDetails(ctx, opts.AccountID, opts.UserID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/accounts/users/list/list.go b/cmd/accounts/users/list/list.go index b7f74ce0..9633fb68 100644 --- a/cmd/accounts/users/list/list.go +++ b/cmd/accounts/users/list/list.go @@ -1,8 +1,6 @@ package list import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -39,7 +37,7 @@ func NewCmdList() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return listRun(&opts) + return listRun(cmd, &opts) }, } @@ -49,11 +47,14 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() var users = make([]taikuncore.UserBriefDto, 0) - req := myApiClient.Client.AccountsAPI.AccountsAccountUserDropdown(context.TODO(), opts.AccountID).Limit(opts.Limit) + req := myApiClient.Client.AccountsAPI.AccountsAccountUserDropdown(ctx, opts.AccountID).Limit(opts.Limit) for { data, response, err := req.Execute() if err != nil { diff --git a/cmd/alertingprofile/add/add.go b/cmd/alertingprofile/add/add.go index 9b77357a..5e10ecdf 100644 --- a/cmd/alertingprofile/add/add.go +++ b/cmd/alertingprofile/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -58,7 +57,7 @@ func NewCmdAdd() *cobra.Command { if err := cmdutils.CheckFlagValue("reminder", opts.Reminder, types.AlertingReminders); err != nil { return err } - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -75,7 +74,10 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -105,7 +107,7 @@ func addRun(opts *AddOptions) (err error) { } // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.AlertingProfilesAPI.AlertingprofilesCreate(context.TODO()).CreateAlertingProfileCommand(body).Execute() + data, response, err := myApiClient.Client.AlertingProfilesAPI.AlertingprofilesCreate(ctx).CreateAlertingProfileCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/alertingprofile/integration/add/add.go b/cmd/alertingprofile/integration/add/add.go index 2ceba35c..9be6e390 100644 --- a/cmd/alertingprofile/integration/add/add.go +++ b/cmd/alertingprofile/integration/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "errors" "github.com/itera-io/taikun-cli/utils/out" tk "github.com/itera-io/taikungoclient" @@ -51,7 +50,7 @@ func NewCmdAdd() *cobra.Command { if opts.Type != types.AlertingIntegrationTypeTeams && opts.Token == "" { return errors.New("--token must be set with this integration type") } - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -70,7 +69,10 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -83,7 +85,7 @@ func addRun(opts *AddOptions) (err error) { } // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.AlertingIntegrationsAPI.AlertingintegrationsCreate(context.TODO()).CreateAlertingIntegrationCommand(body).Execute() + data, response, err := myApiClient.Client.AlertingIntegrationsAPI.AlertingintegrationsCreate(ctx).CreateAlertingIntegrationCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/alertingprofile/integration/list/list.go b/cmd/alertingprofile/integration/list/list.go index 90b76f2a..bab47e60 100644 --- a/cmd/alertingprofile/integration/list/list.go +++ b/cmd/alertingprofile/integration/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -50,7 +49,7 @@ func NewCmdList() *cobra.Command { return cmderr.ErrIDArgumentNotANumber } opts.AlertingProfileID = alertingProfileID - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -61,12 +60,15 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - alertingIntegrations, response, err := myApiClient.Client.AlertingIntegrationsAPI.AlertingintegrationsList(context.TODO(), opts.AlertingProfileID).Execute() + alertingIntegrations, response, err := myApiClient.Client.AlertingIntegrationsAPI.AlertingintegrationsList(ctx, opts.AlertingProfileID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/alertingprofile/integration/remove/remove.go b/cmd/alertingprofile/integration/remove/remove.go index 60e64288..3e9c247a 100644 --- a/cmd/alertingprofile/integration/remove/remove.go +++ b/cmd/alertingprofile/integration/remove/remove.go @@ -15,11 +15,15 @@ func NewCmdDelete() *cobra.Command { Short: "Delete one or more alerting integrations", Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() ids, err := cmdutils.ArgsToNumericalIDs(args) if err != nil { return cmderr.ErrIDArgumentNotANumber } - return cmdutils.DeleteMultiple(ids, deleteRun) + return cmdutils.DeleteMultiple(ids, func(id int32) error { + return deleteRun(ctx, id) + }) }, Aliases: cmdutils.DeleteAliases, } @@ -27,12 +31,12 @@ func NewCmdDelete() *cobra.Command { return cmd } -func deleteRun(alertingIntegrationID int32) (err error) { +func deleteRun(ctx context.Context, alertingIntegrationID int32) (err error) { // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - response, err := myApiClient.Client.AlertingIntegrationsAPI.AlertingintegrationsDelete(context.TODO(), alertingIntegrationID).Execute() + response, err := myApiClient.Client.AlertingIntegrationsAPI.AlertingintegrationsDelete(ctx, alertingIntegrationID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/alertingprofile/list/list.go b/cmd/alertingprofile/list/list.go index 7e9291d6..020bc5a4 100644 --- a/cmd/alertingprofile/list/list.go +++ b/cmd/alertingprofile/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/api" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/config" @@ -58,7 +57,7 @@ func NewCmdList() *cobra.Command { Short: "List alerting profiles", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -72,7 +71,10 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -80,7 +82,7 @@ func listRun(opts *ListOptions) (err error) { opts.OrganizationID = orgID myApiClient := tk.NewClient() - myRequest := myApiClient.Client.AlertingProfilesAPI.AlertingprofilesList(context.TODO()) + myRequest := myApiClient.Client.AlertingProfilesAPI.AlertingprofilesList(ctx) if opts.OrganizationID != 0 { myRequest = myRequest.OrganizationId(opts.OrganizationID) } diff --git a/cmd/alertingprofile/lock/lock.go b/cmd/alertingprofile/lock/lock.go index fc8c64dd..13b1fabd 100644 --- a/cmd/alertingprofile/lock/lock.go +++ b/cmd/alertingprofile/lock/lock.go @@ -1,13 +1,13 @@ package lock import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdLock() *cobra.Command { @@ -20,14 +20,17 @@ func NewCmdLock() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return lockRun(id) + return lockRun(cmd, id) }, } return cmd } -func lockRun(alertingProfileID int32) (err error) { +func lockRun(cmd *cobra.Command, alertingProfileID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -38,7 +41,7 @@ func lockRun(alertingProfileID int32) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.AlertingProfilesAPI.AlertingprofilesLockManager(context.TODO()).AlertingProfilesLockManagerCommand(body).Execute() + response, err := myApiClient.Client.AlertingProfilesAPI.AlertingprofilesLockManager(ctx).AlertingProfilesLockManagerCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/alertingprofile/remove/remove.go b/cmd/alertingprofile/remove/remove.go index 02b7fc62..73e9ac46 100644 --- a/cmd/alertingprofile/remove/remove.go +++ b/cmd/alertingprofile/remove/remove.go @@ -15,11 +15,15 @@ func NewCmdDelete() *cobra.Command { Short: "Delete one or more alerting profiles", Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() ids, err := cmdutils.ArgsToNumericalIDs(args) if err != nil { return cmderr.ErrIDArgumentNotANumber } - return cmdutils.DeleteMultiple(ids, deleteRun) + return cmdutils.DeleteMultiple(ids, func(id int32) error { + return deleteRun(ctx, id) + }) }, Aliases: cmdutils.DeleteAliases, } @@ -27,12 +31,12 @@ func NewCmdDelete() *cobra.Command { return cmd } -func deleteRun(alertingProfileID int32) (err error) { +func deleteRun(ctx context.Context, alertingProfileID int32) (err error) { // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - response, err := myApiClient.Client.AlertingProfilesAPI.AlertingprofilesDelete(context.TODO(), alertingProfileID).Execute() + response, err := myApiClient.Client.AlertingProfilesAPI.AlertingprofilesDelete(ctx, alertingProfileID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/alertingprofile/unlock/unlock.go b/cmd/alertingprofile/unlock/unlock.go index f1c18905..40c29e9e 100644 --- a/cmd/alertingprofile/unlock/unlock.go +++ b/cmd/alertingprofile/unlock/unlock.go @@ -1,13 +1,13 @@ package unlock import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdUnlock() *cobra.Command { @@ -20,14 +20,17 @@ func NewCmdUnlock() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return unlockRun(id) + return unlockRun(cmd, id) }, } return cmd } -func unlockRun(alertingProfileID int32) (err error) { +func unlockRun(cmd *cobra.Command, alertingProfileID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -38,7 +41,7 @@ func unlockRun(alertingProfileID int32) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.AlertingProfilesAPI.AlertingprofilesLockManager(context.TODO()).AlertingProfilesLockManagerCommand(body).Execute() + response, err := myApiClient.Client.AlertingProfilesAPI.AlertingprofilesLockManager(ctx).AlertingProfilesLockManagerCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/alertingprofile/webhook/add/add.go b/cmd/alertingprofile/webhook/add/add.go index 9ebff54a..7443c648 100644 --- a/cmd/alertingprofile/webhook/add/add.go +++ b/cmd/alertingprofile/webhook/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "fmt" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" @@ -32,7 +31,7 @@ func NewCmdAdd() *cobra.Command { return cmderr.ErrIDArgumentNotANumber } opts.AlertingProfileID = alertingProfileID - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -44,12 +43,15 @@ func NewCmdAdd() *cobra.Command { return cmd } -func getAlertingProfileWebhooks(alertingProfileID int32) ([]taikuncore.AlertingWebhookDto, error) { +func getAlertingProfileWebhooks(cmd *cobra.Command, alertingProfileID int32) ([]taikuncore.AlertingWebhookDto, error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.AlertingProfilesAPI.AlertingprofilesList(context.TODO()).Id(alertingProfileID).Execute() + data, response, err := myApiClient.Client.AlertingProfilesAPI.AlertingprofilesList(ctx).Id(alertingProfileID).Execute() if err != nil { return nil, tk.CreateError(response, err) } @@ -91,10 +93,13 @@ func parseAddOptions(opts *AddOptions) (*taikuncore.AlertingWebhookDto, error) { } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - alertingWebhooks, err := getAlertingProfileWebhooks(opts.AlertingProfileID) + alertingWebhooks, err := getAlertingProfileWebhooks(cmd, opts.AlertingProfileID) if err != nil { return } @@ -106,7 +111,7 @@ func addRun(opts *AddOptions) (err error) { alertingWebhooks = append(alertingWebhooks, *newAlertingWebhook) - response, err := myApiClient.Client.AlertingProfilesAPI.AlertingprofilesAssignWebhooks(context.TODO(), opts.AlertingProfileID).AlertingWebhookDto(alertingWebhooks).Execute() + response, err := myApiClient.Client.AlertingProfilesAPI.AlertingprofilesAssignWebhooks(ctx, opts.AlertingProfileID).AlertingWebhookDto(alertingWebhooks).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/alertingprofile/webhook/clear/clear.go b/cmd/alertingprofile/webhook/clear/clear.go index a118fed4..9f708360 100644 --- a/cmd/alertingprofile/webhook/clear/clear.go +++ b/cmd/alertingprofile/webhook/clear/clear.go @@ -1,13 +1,13 @@ package clear import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdClear() *cobra.Command { @@ -20,18 +20,21 @@ func NewCmdClear() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return clearRun(id) + return clearRun(cmd, id) }, } return cmd } -func clearRun(alertingProfileID int32) (err error) { +func clearRun(cmd *cobra.Command, alertingProfileID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() emptyWebhokList := make([]taikuncore.AlertingWebhookDto, 0) - response, err := myApiClient.Client.AlertingProfilesAPI.AlertingprofilesAssignWebhooks(context.TODO(), alertingProfileID).AlertingWebhookDto(emptyWebhokList).Execute() + response, err := myApiClient.Client.AlertingProfilesAPI.AlertingprofilesAssignWebhooks(ctx, alertingProfileID).AlertingWebhookDto(emptyWebhokList).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/alertingprofile/webhook/list/list.go b/cmd/alertingprofile/webhook/list/list.go index ce6ee9af..22278eab 100644 --- a/cmd/alertingprofile/webhook/list/list.go +++ b/cmd/alertingprofile/webhook/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "fmt" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" @@ -39,7 +38,7 @@ func NewCmdList() *cobra.Command { return cmderr.ErrIDArgumentNotANumber } opts.AlertingProfileID = alertingProfileID - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -51,12 +50,15 @@ func NewCmdList() *cobra.Command { return cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.AlertingProfilesAPI.AlertingprofilesList(context.TODO()).Id(opts.AlertingProfileID).Execute() + data, response, err := myApiClient.Client.AlertingProfilesAPI.AlertingprofilesList(ctx).Id(opts.AlertingProfileID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/application/list/list.go b/cmd/application/list/list.go index bf7bfb0b..e26b8491 100644 --- a/cmd/application/list/list.go +++ b/cmd/application/list/list.go @@ -1,8 +1,6 @@ package list import ( - "context" - "github.com/itera-io/taikun-cli/api" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/config" @@ -40,7 +38,7 @@ func NewCmdList() *cobra.Command { Short: "List available application", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -53,10 +51,13 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - myRequest := myApiClient.Client.PackageAPI.PackageList(context.TODO()).Limit(1000) + myRequest := myApiClient.Client.PackageAPI.PackageList(ctx).Limit(1000) if config.SortBy != "" { myRequest = myRequest.SortBy(config.SortBy).SortDirection(*api.GetSortDirection()) } diff --git a/cmd/backupcredential/add/add.go b/cmd/backupcredential/add/add.go index cc788e19..c615c9f0 100644 --- a/cmd/backupcredential/add/add.go +++ b/cmd/backupcredential/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "fmt" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" @@ -65,7 +64,7 @@ func NewCmdAdd() *cobra.Command { Short: "Add a backup credential", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - isValid, err := backupCredentialIsValid(&opts) + isValid, err := backupCredentialIsValid(cmd, &opts) if err != nil { return err } @@ -73,7 +72,7 @@ func NewCmdAdd() *cobra.Command { return fmt.Errorf("backup credential must be valid") } opts.S3Name = args[0] - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -97,7 +96,10 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func backupCredentialIsValid(opts *AddOptions) (bool, error) { +func backupCredentialIsValid(cmd *cobra.Command, opts *AddOptions) (bool, error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -110,7 +112,7 @@ func backupCredentialIsValid(opts *AddOptions) (bool, error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.CheckerAPI.CheckerS3(context.TODO()).CheckS3Command(body).Execute() + response, err := myApiClient.Client.CheckerAPI.CheckerS3(ctx).CheckS3Command(body).Execute() if err != nil { return false, tk.CreateError(response, err) } @@ -119,7 +121,10 @@ func backupCredentialIsValid(opts *AddOptions) (bool, error) { } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -140,7 +145,7 @@ func addRun(opts *AddOptions) (err error) { } // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.S3CredentialsAPI.S3credentialsCreate(context.TODO()).BackupCredentialsCreateCommand(body).Execute() + data, response, err := myApiClient.Client.S3CredentialsAPI.S3credentialsCreate(ctx).BackupCredentialsCreateCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/backupcredential/list/list.go b/cmd/backupcredential/list/list.go index bd4091f3..b09f5ca0 100644 --- a/cmd/backupcredential/list/list.go +++ b/cmd/backupcredential/list/list.go @@ -1,8 +1,6 @@ package list import ( - "context" - "github.com/itera-io/taikun-cli/api" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/config" @@ -62,7 +60,7 @@ func NewCmdList() *cobra.Command { Short: "List backup credentials", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -75,7 +73,10 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -83,7 +84,7 @@ func listRun(opts *ListOptions) (err error) { opts.OrganizationID = orgID myApiClient := tk.NewClient() - myRequest := myApiClient.Client.S3CredentialsAPI.S3credentialsList(context.TODO()) + myRequest := myApiClient.Client.S3CredentialsAPI.S3credentialsList(ctx) if config.SortBy != "" { myRequest = myRequest.SortBy(config.SortBy).SortDirection(*api.GetSortDirection()) } diff --git a/cmd/backupcredential/lock/lock.go b/cmd/backupcredential/lock/lock.go index f51cb3c8..2cccecd7 100644 --- a/cmd/backupcredential/lock/lock.go +++ b/cmd/backupcredential/lock/lock.go @@ -1,13 +1,13 @@ package lock import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdLock() *cobra.Command { @@ -20,14 +20,17 @@ func NewCmdLock() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return lockRun(backupCredentialID) + return lockRun(cmd, backupCredentialID) }, } return cmd } -func lockRun(backupCredentialID int32) (err error) { +func lockRun(cmd *cobra.Command, backupCredentialID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -38,7 +41,7 @@ func lockRun(backupCredentialID int32) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.S3CredentialsAPI.S3credentialsLockManagement(context.TODO()).BackupLockManagerCommand(body).Execute() + response, err := myApiClient.Client.S3CredentialsAPI.S3credentialsLockManagement(ctx).BackupLockManagerCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/backupcredential/remove/remove.go b/cmd/backupcredential/remove/remove.go index 10c7e353..b0e07b54 100644 --- a/cmd/backupcredential/remove/remove.go +++ b/cmd/backupcredential/remove/remove.go @@ -15,11 +15,15 @@ func NewCmdDelete() *cobra.Command { Short: "Delete one or more backup credentials", Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() ids, err := cmdutils.ArgsToNumericalIDs(args) if err != nil { return cmderr.ErrIDArgumentNotANumber } - return cmdutils.DeleteMultiple(ids, deleteRun) + return cmdutils.DeleteMultiple(ids, func(id int32) error { + return deleteRun(ctx, id) + }) }, Aliases: cmdutils.DeleteAliases, } @@ -27,12 +31,12 @@ func NewCmdDelete() *cobra.Command { return cmd } -func deleteRun(backupCredentialID int32) (err error) { +func deleteRun(ctx context.Context, backupCredentialID int32) (err error) { // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - response, err := myApiClient.Client.S3CredentialsAPI.S3credentialsDelete(context.TODO(), backupCredentialID).Execute() + response, err := myApiClient.Client.S3CredentialsAPI.S3credentialsDelete(ctx, backupCredentialID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/backupcredential/unlock/unlock.go b/cmd/backupcredential/unlock/unlock.go index 314cdd9a..47269d50 100644 --- a/cmd/backupcredential/unlock/unlock.go +++ b/cmd/backupcredential/unlock/unlock.go @@ -1,13 +1,13 @@ package unlock import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdUnlock() *cobra.Command { @@ -20,14 +20,17 @@ func NewCmdUnlock() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return unlockRun(backupCredentialID) + return unlockRun(cmd, backupCredentialID) }, } return cmd } -func unlockRun(backupCredentialID int32) (err error) { +func unlockRun(cmd *cobra.Command, backupCredentialID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -38,7 +41,7 @@ func unlockRun(backupCredentialID int32) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.S3CredentialsAPI.S3credentialsLockManagement(context.TODO()).BackupLockManagerCommand(body).Execute() + response, err := myApiClient.Client.S3CredentialsAPI.S3credentialsLockManagement(ctx).BackupLockManagerCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/billing/credential/add/add.go b/cmd/billing/credential/add/add.go index 22c47b58..ab89fa65 100644 --- a/cmd/billing/credential/add/add.go +++ b/cmd/billing/credential/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -63,7 +62,7 @@ func NewCmdAdd() *cobra.Command { Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { opts.Name = args[0] - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -84,7 +83,10 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -104,7 +106,7 @@ func addRun(opts *AddOptions) (err error) { } // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.OperationCredentialsAPI.OpscredentialsCreate(context.TODO()).OperationCredentialsCreateCommand(body).Execute() + data, response, err := myApiClient.Client.OperationCredentialsAPI.OpscredentialsCreate(ctx).OperationCredentialsCreateCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/billing/credential/list/list.go b/cmd/billing/credential/list/list.go index 2b96b204..ae1b78fd 100644 --- a/cmd/billing/credential/list/list.go +++ b/cmd/billing/credential/list/list.go @@ -1,8 +1,6 @@ package list import ( - "context" - "github.com/itera-io/taikun-cli/api" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/config" @@ -58,7 +56,7 @@ func NewCmdList() *cobra.Command { Use: "list", Short: "List billing credentials", RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Args: cobra.NoArgs, Aliases: cmdutils.ListAliases, @@ -72,7 +70,10 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -83,7 +84,7 @@ func listRun(opts *ListOptions) (err error) { myApiClient := tk.NewClient() // Prepare the arguments for the query - myRequest := myApiClient.Client.OperationCredentialsAPI.OpscredentialsList(context.TODO()) + myRequest := myApiClient.Client.OperationCredentialsAPI.OpscredentialsList(ctx) if config.SortBy != "" { myRequest = myRequest.SortBy(config.SortBy).SortDirection(*api.GetSortDirection()) } diff --git a/cmd/billing/credential/lock/lock.go b/cmd/billing/credential/lock/lock.go index 591315ea..eadfa508 100644 --- a/cmd/billing/credential/lock/lock.go +++ b/cmd/billing/credential/lock/lock.go @@ -1,13 +1,13 @@ package lock import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdLock() *cobra.Command { @@ -20,14 +20,17 @@ func NewCmdLock() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return lockRun(id) + return lockRun(cmd, id) }, } return cmd } -func lockRun(billingCredentialID int32) (err error) { +func lockRun(cmd *cobra.Command, billingCredentialID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -38,7 +41,7 @@ func lockRun(billingCredentialID int32) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.OperationCredentialsAPI.OpscredentialsLockManager(context.TODO()).OperationCredentialLockManagerCommand(body).Execute() + response, err := myApiClient.Client.OperationCredentialsAPI.OpscredentialsLockManager(ctx).OperationCredentialLockManagerCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/billing/credential/remove/remove.go b/cmd/billing/credential/remove/remove.go index 233c3274..2ecf4e06 100644 --- a/cmd/billing/credential/remove/remove.go +++ b/cmd/billing/credential/remove/remove.go @@ -15,11 +15,15 @@ func NewCmdDelete() *cobra.Command { Short: "Delete one or more billing credentials", Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() ids, err := cmdutils.ArgsToNumericalIDs(args) if err != nil { return cmderr.ErrIDArgumentNotANumber } - return cmdutils.DeleteMultiple(ids, deleteRun) + return cmdutils.DeleteMultiple(ids, func(id int32) error { + return deleteRun(ctx, id) + }) }, Aliases: cmdutils.DeleteAliases, } @@ -27,12 +31,12 @@ func NewCmdDelete() *cobra.Command { return cmd } -func deleteRun(billingCredentialID int32) (err error) { +func deleteRun(ctx context.Context, billingCredentialID int32) (err error) { // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - response, err := myApiClient.Client.OperationCredentialsAPI.OpscredentialsDelete(context.TODO(), billingCredentialID).Execute() + response, err := myApiClient.Client.OperationCredentialsAPI.OpscredentialsDelete(ctx, billingCredentialID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/billing/credential/unlock/unlock.go b/cmd/billing/credential/unlock/unlock.go index c61ffb79..243443ef 100644 --- a/cmd/billing/credential/unlock/unlock.go +++ b/cmd/billing/credential/unlock/unlock.go @@ -1,13 +1,13 @@ package unlock import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdUnlock() *cobra.Command { @@ -20,14 +20,17 @@ func NewCmdUnlock() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return unlockRun(id) + return unlockRun(cmd, id) }, } return cmd } -func unlockRun(billingCredentialID int32) (err error) { +func unlockRun(cmd *cobra.Command, billingCredentialID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -38,7 +41,7 @@ func unlockRun(billingCredentialID int32) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.OperationCredentialsAPI.OpscredentialsLockManager(context.TODO()).OperationCredentialLockManagerCommand(body).Execute() + response, err := myApiClient.Client.OperationCredentialsAPI.OpscredentialsLockManager(ctx).OperationCredentialLockManagerCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/billing/rule/add/add.go b/cmd/billing/rule/add/add.go index 41057b88..1c60f90d 100644 --- a/cmd/billing/rule/add/add.go +++ b/cmd/billing/rule/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -60,7 +59,7 @@ func NewCmdAdd() *cobra.Command { if err := cmdutils.CheckFlagValue("type", opts.Type, types.PrometheusTypes); err != nil { return err } - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -89,7 +88,10 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -109,7 +111,7 @@ func addRun(opts *AddOptions) (err error) { } // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.PrometheusRulesAPI.PrometheusrulesCreate(context.TODO()).RuleCreateCommand(body).Execute() + data, response, err := myApiClient.Client.PrometheusRulesAPI.PrometheusrulesCreate(ctx).RuleCreateCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/billing/rule/label/edit/edit.go b/cmd/billing/rule/label/edit/edit.go index 275cbd4d..43b50c34 100644 --- a/cmd/billing/rule/label/edit/edit.go +++ b/cmd/billing/rule/label/edit/edit.go @@ -1,7 +1,6 @@ package edit import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -27,7 +26,7 @@ func NewCmdEdit() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -37,12 +36,15 @@ func NewCmdEdit() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // List other information that needs to be send - data, response, err := myApiClient.Client.PrometheusRulesAPI.PrometheusrulesList(context.TODO()).Id(opts.BillingRuleID).Execute() + data, response, err := myApiClient.Client.PrometheusRulesAPI.PrometheusrulesList(ctx).Id(opts.BillingRuleID).Execute() if err != nil { return tk.CreateError(response, err) } @@ -62,7 +64,7 @@ func addRun(opts *AddOptions) (err error) { } // Execute a query into the API + graceful exit - response, err = myApiClient.Client.PrometheusRulesAPI.PrometheusrulesUpdate(context.TODO(), opts.BillingRuleID).RuleForUpdateDto(body).Execute() + response, err = myApiClient.Client.PrometheusRulesAPI.PrometheusrulesUpdate(ctx, opts.BillingRuleID).RuleForUpdateDto(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/billing/rule/label/list/list.go b/cmd/billing/rule/label/list/list.go index 2b1e648e..9c789211 100644 --- a/cmd/billing/rule/label/list/list.go +++ b/cmd/billing/rule/label/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -43,7 +42,7 @@ func NewCmdList() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -54,12 +53,15 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.PrometheusRulesAPI.PrometheusrulesList(context.TODO()).Id(opts.BillingRuleID).Execute() + data, response, err := myApiClient.Client.PrometheusRulesAPI.PrometheusrulesList(ctx).Id(opts.BillingRuleID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/billing/rule/list/list.go b/cmd/billing/rule/list/list.go index 6c8633b4..5db0bbf8 100644 --- a/cmd/billing/rule/list/list.go +++ b/cmd/billing/rule/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/api" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/config" @@ -60,7 +59,7 @@ func NewCmdList() *cobra.Command { Short: "List billing rules", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -72,12 +71,15 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Prepare the arguments for the query - myRequest := myApiClient.Client.PrometheusRulesAPI.PrometheusrulesList(context.TODO()) + myRequest := myApiClient.Client.PrometheusRulesAPI.PrometheusrulesList(ctx) if config.SortBy != "" { myRequest = myRequest.SortBy(config.SortBy).SortDirection(*api.GetSortDirection()) } diff --git a/cmd/billing/rule/organization/bind/bind.go b/cmd/billing/rule/organization/bind/bind.go index 6a70afb0..d12ff4e7 100644 --- a/cmd/billing/rule/organization/bind/bind.go +++ b/cmd/billing/rule/organization/bind/bind.go @@ -1,7 +1,6 @@ package bind import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" @@ -28,7 +27,7 @@ func NewCmdBind() *cobra.Command { if err != nil { return err } - return bindRun(&opts) + return bindRun(cmd, &opts) }, } @@ -40,7 +39,10 @@ func NewCmdBind() *cobra.Command { return &cmd } -func bindRun(opts *BindOptions) (err error) { +func bindRun(cmd *cobra.Command, opts *BindOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -60,7 +62,7 @@ func bindRun(opts *BindOptions) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.PrometheusRulesAPI.PrometheusrulesAddOrganizations(context.TODO(), billingRuleId).AddOrganizationsToRuleDto(body).Execute() + response, err := myApiClient.Client.PrometheusRulesAPI.PrometheusrulesAddOrganizations(ctx, billingRuleId).AddOrganizationsToRuleDto(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/billing/rule/organization/list/list.go b/cmd/billing/rule/organization/list/list.go index 7e508bc8..1b0693c6 100644 --- a/cmd/billing/rule/organization/list/list.go +++ b/cmd/billing/rule/organization/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -46,7 +45,7 @@ func NewCmdList() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -57,12 +56,14 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.PrometheusRulesAPI.PrometheusrulesList(context.TODO()).Id(opts.BillingRuleID).Execute() + data, response, err := myApiClient.Client.PrometheusRulesAPI.PrometheusrulesList(ctx).Id(opts.BillingRuleID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/billing/rule/organization/unbind/unbind.go b/cmd/billing/rule/organization/unbind/unbind.go index 96e59f65..28827527 100644 --- a/cmd/billing/rule/organization/unbind/unbind.go +++ b/cmd/billing/rule/organization/unbind/unbind.go @@ -1,7 +1,6 @@ package unbind import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" @@ -26,7 +25,7 @@ func NewCmdUnbind() *cobra.Command { if err != nil { return err } - return unbindRun(&opts) + return unbindRun(cmd, &opts) }, } @@ -35,7 +34,10 @@ func NewCmdUnbind() *cobra.Command { return &cmd } -func unbindRun(opts *UnbindOptions) (err error) { +func unbindRun(cmd *cobra.Command, opts *UnbindOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -50,7 +52,7 @@ func unbindRun(opts *UnbindOptions) (err error) { body := []int32{opts.OrganizationID} // Execute a query into the API + graceful exit - response, err := myApiClient.Client.PrometheusRulesAPI.PrometheusrulesDeleteOrganizations(context.TODO(), billingRuleId).RequestBody(body).Execute() + response, err := myApiClient.Client.PrometheusRulesAPI.PrometheusrulesDeleteOrganizations(ctx, billingRuleId).RequestBody(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/billing/rule/remove/remove.go b/cmd/billing/rule/remove/remove.go index cdb65a93..3d80cfd6 100644 --- a/cmd/billing/rule/remove/remove.go +++ b/cmd/billing/rule/remove/remove.go @@ -15,11 +15,15 @@ func NewCmdDelete() *cobra.Command { Short: "Delete one or more billing rules", Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() ids, err := cmdutils.ArgsToNumericalIDs(args) if err != nil { return cmderr.ErrIDArgumentNotANumber } - return cmdutils.DeleteMultiple(ids, deleteRun) + return cmdutils.DeleteMultiple(ids, func(id int32) error { + return deleteRun(ctx, id) + }) }, Aliases: cmdutils.DeleteAliases, } @@ -27,12 +31,12 @@ func NewCmdDelete() *cobra.Command { return &cmd } -func deleteRun(billingRuleID int32) (err error) { +func deleteRun(ctx context.Context, billingRuleID int32) (err error) { // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - response, err := myApiClient.Client.PrometheusRulesAPI.PrometheusrulesDelete(context.TODO(), billingRuleID).Execute() + response, err := myApiClient.Client.PrometheusRulesAPI.PrometheusrulesDelete(ctx, billingRuleID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/catalog/app/bind/bind.go b/cmd/catalog/app/bind/bind.go index 3b181e02..6f0e2a83 100644 --- a/cmd/catalog/app/bind/bind.go +++ b/cmd/catalog/app/bind/bind.go @@ -1,8 +1,6 @@ package bind import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -11,6 +9,7 @@ import ( tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) var addFields = fields.New( @@ -49,14 +48,17 @@ func NewCmdBind() *cobra.Command { opts.appName = args[1] opts.repoName = args[2] - return bindRun(&opts) + return bindRun(cmd, &opts) }, } return &cmd } -func bindRun(opts *BindOptions) (err error) { +func bindRun(cmd *cobra.Command, opts *BindOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.CreateCatalogAppCommand{ @@ -69,7 +71,7 @@ func bindRun(opts *BindOptions) (err error) { body.SetVersion(opts.version) } - data, response, err := myApiClient.Client.CatalogAppAPI.CatalogAppCreate(context.TODO()).CreateCatalogAppCommand(body).Execute() + data, response, err := myApiClient.Client.CatalogAppAPI.CatalogAppCreate(ctx).CreateCatalogAppCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/catalog/app/instance/cancel/cancel.go b/cmd/catalog/app/instance/cancel/cancel.go index bbbd2907..a332bc75 100644 --- a/cmd/catalog/app/instance/cancel/cancel.go +++ b/cmd/catalog/app/instance/cancel/cancel.go @@ -1,14 +1,13 @@ package cancel import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type CancelOptions struct { @@ -27,21 +26,24 @@ func NewCmdCancel() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return cancelAppRun(opts) + return cancelAppRun(cmd, opts) }, } return &cmd } -func cancelAppRun(opts CancelOptions) (err error) { +func cancelAppRun(cmd *cobra.Command, opts CancelOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.CancelProjectAppCommand{ ProjectAppId: &opts.ProjectAppId, } - response, err := myApiClient.Client.ProjectAppsAPI.ProjectappCancel(context.TODO()).CancelProjectAppCommand(body).Execute() + response, err := myApiClient.Client.ProjectAppsAPI.ProjectappCancel(ctx).CancelProjectAppCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/catalog/app/instance/install/install.go b/cmd/catalog/app/instance/install/install.go index 4d16a4b0..f07e0749 100644 --- a/cmd/catalog/app/instance/install/install.go +++ b/cmd/catalog/app/instance/install/install.go @@ -1,7 +1,6 @@ package install import ( - "context" "encoding/base64" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" @@ -11,6 +10,7 @@ import ( "github.com/spf13/cobra" "log" "os" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type InstallOptions struct { @@ -40,7 +40,7 @@ func NewCmdInstall() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return installAppRun(opts) + return installAppRun(cmd, opts) }, } @@ -54,7 +54,10 @@ func NewCmdInstall() *cobra.Command { return &cmd } -func installAppRun(opts InstallOptions) (err error) { +func installAppRun(cmd *cobra.Command, opts InstallOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() var extraValuesB64 string @@ -80,7 +83,7 @@ func installAppRun(opts InstallOptions) (err error) { AutoSync: &opts.Autosync, } - _, response, err := myApiClient.Client.ProjectAppsAPI.ProjectappInstall(context.TODO()).CreateProjectAppCommand(body).Execute() + _, response, err := myApiClient.Client.ProjectAppsAPI.ProjectappInstall(ctx).CreateProjectAppCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/catalog/app/instance/list/list.go b/cmd/catalog/app/instance/list/list.go index 87abff1a..2d5c9988 100644 --- a/cmd/catalog/app/instance/list/list.go +++ b/cmd/catalog/app/instance/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/api" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" @@ -52,7 +51,7 @@ func NewCmdList() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -67,8 +66,8 @@ func NewCmdList() *cobra.Command { } // listRun calls the API, gets the Users and prints them in a table. -func listRun(opts *ListOptions) (err error) { - users, err := ListAppInstances(opts) +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + users, err := ListAppInstances(cmd, opts) if err != nil { return err } @@ -76,10 +75,13 @@ func listRun(opts *ListOptions) (err error) { return out.PrintResults(users, ListFields) } -func ListAppInstances(opts *ListOptions) (appInstanceList []taikuncore.InstanceAppListDto, err error) { +func ListAppInstances(cmd *cobra.Command, opts *ListOptions) (appInstanceList []taikuncore.InstanceAppListDto, err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Prepare the request myApiClient := tk.NewClient() - myRequest := myApiClient.Client.ProjectAppsAPI.ProjectappList(context.TODO()).ProjectId(opts.projectId) + myRequest := myApiClient.Client.ProjectAppsAPI.ProjectappList(ctx).ProjectId(opts.projectId) // Set Organization ID if it is set in command line options if opts.OrganizationID != 0 { myRequest = myRequest.OrganizationId(opts.OrganizationID) diff --git a/cmd/catalog/app/instance/sync/sync.go b/cmd/catalog/app/instance/sync/sync.go index b740d9ff..a09f6a5b 100644 --- a/cmd/catalog/app/instance/sync/sync.go +++ b/cmd/catalog/app/instance/sync/sync.go @@ -1,13 +1,13 @@ package syncpackage import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type SyncOptions struct { @@ -26,19 +26,22 @@ func NewCmdSync() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return syncAppRun(opts) + return syncAppRun(cmd, opts) }, } return &cmd } -func syncAppRun(opts SyncOptions) (err error) { +func syncAppRun(cmd *cobra.Command, opts SyncOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.SyncProjectAppCommand{ProjectAppId: &opts.ProjectAppId} - response, err := myApiClient.Client.ProjectAppsAPI.ProjectappSync(context.TODO()).SyncProjectAppCommand(body).Execute() + response, err := myApiClient.Client.ProjectAppsAPI.ProjectappSync(ctx).SyncProjectAppCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/catalog/app/instance/uninstall/uninstall.go b/cmd/catalog/app/instance/uninstall/uninstall.go index 901e7daa..6aa5deca 100644 --- a/cmd/catalog/app/instance/uninstall/uninstall.go +++ b/cmd/catalog/app/instance/uninstall/uninstall.go @@ -1,13 +1,12 @@ package uninstall import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type UninstallOptions struct { @@ -26,17 +25,20 @@ func NewCmdUninstall() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return uninstallAppRun(opts) + return uninstallAppRun(cmd, opts) }, } return &cmd } -func uninstallAppRun(opts UninstallOptions) (err error) { +func uninstallAppRun(cmd *cobra.Command, opts UninstallOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - _, response, err := myApiClient.Client.ProjectAppsAPI.ProjectappDelete(context.TODO(), opts.ProjectAppId).Execute() + _, response, err := myApiClient.Client.ProjectAppsAPI.ProjectappDelete(ctx, opts.ProjectAppId).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/catalog/app/list/list.go b/cmd/catalog/app/list/list.go index b705f88c..e60a8bae 100644 --- a/cmd/catalog/app/list/list.go +++ b/cmd/catalog/app/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "fmt" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" @@ -34,7 +33,7 @@ func NewCmdList() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return listRun(catid) + return listRun(cmd, catid) }, Aliases: cmdutils.ListAliases, } @@ -44,10 +43,13 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(catid int32) (err error) { +func listRun(cmd *cobra.Command, catid int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - data, response, err := myApiClient.Client.CatalogAPI.CatalogList(context.TODO()).Id(catid).Execute() + data, response, err := myApiClient.Client.CatalogAPI.CatalogList(ctx).Id(catid).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/catalog/app/unbind/unbind.go b/cmd/catalog/app/unbind/unbind.go index 5e42f193..b181260a 100644 --- a/cmd/catalog/app/unbind/unbind.go +++ b/cmd/catalog/app/unbind/unbind.go @@ -1,12 +1,12 @@ package unbind import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type UnbindOptions struct { @@ -26,17 +26,20 @@ func NewCmdUnbind() *cobra.Command { return cmderr.ErrIDArgumentNotANumber } - return bindRun(&opts) + return bindRun(cmd, &opts) }, } return &cmd } -func bindRun(opts *UnbindOptions) (err error) { +func bindRun(cmd *cobra.Command, opts *UnbindOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - response, err := myApiClient.Client.CatalogAppAPI.CatalogAppDelete(context.TODO(), opts.catalogappid).Execute() + response, err := myApiClient.Client.CatalogAppAPI.CatalogAppDelete(ctx, opts.catalogappid).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/catalog/create/create.go b/cmd/catalog/create/create.go index 1ef1914f..33382aea 100644 --- a/cmd/catalog/create/create.go +++ b/cmd/catalog/create/create.go @@ -1,7 +1,6 @@ package create import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" tk "github.com/itera-io/taikungoclient" @@ -22,7 +21,7 @@ func NewCmdCreatecatalog() *cobra.Command { Short: "Create a new catalog", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - return createcatalogRun(args[0], opts) + return createcatalogRun(cmd, args[0], opts) }, } @@ -34,7 +33,10 @@ func NewCmdCreatecatalog() *cobra.Command { return &cmd } -func createcatalogRun(catalogname string, opts CreateOptions) (err error) { +func createcatalogRun(cmd *cobra.Command, catalogname string, opts CreateOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -52,7 +54,7 @@ func createcatalogRun(catalogname string, opts CreateOptions) (err error) { body.SetOrganizationId(opts.OrganizationID) } - response, err := myApiClient.Client.CatalogAPI.CatalogCreate(context.TODO()).CreateCatalogCommand(body).Execute() + response, err := myApiClient.Client.CatalogAPI.CatalogCreate(ctx).CreateCatalogCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/catalog/delete/delete.go b/cmd/catalog/delete/delete.go index 2b76c94b..c19497c6 100644 --- a/cmd/catalog/delete/delete.go +++ b/cmd/catalog/delete/delete.go @@ -1,11 +1,11 @@ package delete import ( - "context" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdDelete() *cobra.Command { @@ -19,17 +19,20 @@ func NewCmdDelete() *cobra.Command { if err != nil { return err } - return deletecatalogRun(catalogid) + return deletecatalogRun(cmd, catalogid) }, } return &cmd } -func deletecatalogRun(catalogid int32) (err error) { +func deletecatalogRun(cmd *cobra.Command, catalogid int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - response, err := myApiClient.Client.CatalogAPI.CatalogDelete(context.TODO(), catalogid).Execute() + response, err := myApiClient.Client.CatalogAPI.CatalogDelete(ctx, catalogid).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/catalog/list/list.go b/cmd/catalog/list/list.go index 16758128..c58befd3 100644 --- a/cmd/catalog/list/list.go +++ b/cmd/catalog/list/list.go @@ -33,15 +33,16 @@ type ListOptions struct { // We want do display organization name, but want to download the organizations only once. var organizationCache map[int32]string +var catalogCtx context.Context -func initOrganizationCache() { +func initOrganizationCache(ctx context.Context) { if organizationCache != nil { return } organizationCache = make(map[int32]string) var opts list.ListOptions - organizations, err := list.ListOrganizations(&opts) + organizations, err := list.ListOrganizations(ctx, &opts) if err != nil { fmt.Println("Error fetching organizations:", err) return @@ -53,7 +54,7 @@ func initOrganizationCache() { } func formatOrganizationName(orgidinput interface{}) string { - initOrganizationCache() + initOrganizationCache(catalogCtx) orgid := int32(orgidinput.(float64)) if name, found := organizationCache[orgid]; found { @@ -72,7 +73,7 @@ func NewCmdList() *cobra.Command { Short: "List available catalogs", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -85,7 +86,11 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + catalogCtx = ctx + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -95,7 +100,7 @@ func listRun(opts *ListOptions) (err error) { myApiClient := tk.NewClient() var catalogs = make([]taikuncore.CatalogListDto, 0) - myRequest := myApiClient.Client.CatalogAPI.CatalogList(context.TODO()) + myRequest := myApiClient.Client.CatalogAPI.CatalogList(ctx) if config.SortBy != "" { myRequest = myRequest.SortBy(config.SortBy).SortDirection(*api.GetSortDirection()) } diff --git a/cmd/catalog/lock/lock.go b/cmd/catalog/lock/lock.go index a6458fd2..fd48e263 100644 --- a/cmd/catalog/lock/lock.go +++ b/cmd/catalog/lock/lock.go @@ -1,14 +1,13 @@ package lock import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdLock() *cobra.Command { @@ -21,14 +20,17 @@ func NewCmdLock() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return lockRun(id) + return lockRun(cmd, id) }, } return cmd } -func lockRun(catalogID int32) (err error) { +func lockRun(cmd *cobra.Command, catalogID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -39,7 +41,7 @@ func lockRun(catalogID int32) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.CatalogAPI.CatalogLock(context.TODO()).CatalogLockManagementCommand(body).Execute() + response, err := myApiClient.Client.CatalogAPI.CatalogLock(ctx).CatalogLockManagementCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/catalog/makedefault/makedefault.go b/cmd/catalog/makedefault/makedefault.go index 65c28134..68395a0c 100644 --- a/cmd/catalog/makedefault/makedefault.go +++ b/cmd/catalog/makedefault/makedefault.go @@ -1,13 +1,13 @@ package makedefault import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdMakedefault() *cobra.Command { @@ -21,21 +21,24 @@ func NewCmdMakedefault() *cobra.Command { return cmderr.ErrIDArgumentNotANumber } - return makedefaultRun(catalogid) + return makedefaultRun(cmd, catalogid) }, } return &cmd } -func makedefaultRun(catalogid int32) (err error) { +func makedefaultRun(cmd *cobra.Command, catalogid int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.CatalogMakeDefaultCommand{ Id: &catalogid, } - response, err := myApiClient.Client.CatalogAPI.CatalogMakeDefault(context.TODO()).CatalogMakeDefaultCommand(body).Execute() + response, err := myApiClient.Client.CatalogAPI.CatalogMakeDefault(ctx).CatalogMakeDefaultCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/catalog/project/bind/bind.go b/cmd/catalog/project/bind/bind.go index bfd37c9f..628e45c3 100644 --- a/cmd/catalog/project/bind/bind.go +++ b/cmd/catalog/project/bind/bind.go @@ -1,12 +1,12 @@ package bind import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdBind() *cobra.Command { @@ -24,17 +24,20 @@ func NewCmdBind() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return bindRun(catalogid, projectid) + return bindRun(cmd, catalogid, projectid) }, } return &cmd } -func bindRun(catalogid int32, projectid int32) (err error) { +func bindRun(cmd *cobra.Command, catalogid int32, projectid int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - response, err := myApiClient.Client.CatalogAPI.CatalogAddProject(context.TODO(), catalogid).RequestBody([]int32{projectid}).Execute() + response, err := myApiClient.Client.CatalogAPI.CatalogAddProject(ctx, catalogid).RequestBody([]int32{projectid}).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/catalog/project/list/list.go b/cmd/catalog/project/list/list.go index e6eaafb1..e1574bb6 100644 --- a/cmd/catalog/project/list/list.go +++ b/cmd/catalog/project/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "fmt" "github.com/itera-io/taikun-cli/cmd/cmderr" @@ -52,7 +51,7 @@ func NewCmdList() *cobra.Command { return cmderr.ErrIDArgumentNotANumber } localCatID = &catid - return listRun(&catid, opts.OrganizationID) + return listRun(cmd, &catid, opts.OrganizationID) }, Aliases: cmdutils.ListAliases, } @@ -63,10 +62,13 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(catid *int32, orgid int32) (err error) { +func listRun(cmd *cobra.Command, catid *int32, orgid int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - listCommand := myApiClient.Client.CatalogAPI.CatalogList(context.TODO()).Id(*catid) + listCommand := myApiClient.Client.CatalogAPI.CatalogList(ctx).Id(*catid) if orgid != -1 { listCommand = listCommand.OrganizationId(orgid) } diff --git a/cmd/catalog/project/unbind/unbind.go b/cmd/catalog/project/unbind/unbind.go index 236d9c04..4a1f43d0 100644 --- a/cmd/catalog/project/unbind/unbind.go +++ b/cmd/catalog/project/unbind/unbind.go @@ -1,13 +1,12 @@ package unbind import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdUnbind() *cobra.Command { @@ -25,17 +24,20 @@ func NewCmdUnbind() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return unbindRun(catalogid, projectid) + return unbindRun(cmd, catalogid, projectid) }, } return &cmd } -func unbindRun(catalogid int32, projectid int32) (err error) { +func unbindRun(cmd *cobra.Command, catalogid int32, projectid int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - response, err := myApiClient.Client.CatalogAPI.CatalogDeleteProject(context.TODO(), catalogid).RequestBody([]int32{projectid}).Execute() + response, err := myApiClient.Client.CatalogAPI.CatalogDeleteProject(ctx, catalogid).RequestBody([]int32{projectid}).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/catalog/unlock/unlock.go b/cmd/catalog/unlock/unlock.go index 79cb4ebe..c4cdacfd 100644 --- a/cmd/catalog/unlock/unlock.go +++ b/cmd/catalog/unlock/unlock.go @@ -1,14 +1,13 @@ package unlock import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdUnlock() *cobra.Command { @@ -21,14 +20,17 @@ func NewCmdUnlock() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return unlockRun(id) + return unlockRun(cmd, id) }, } return cmd } -func unlockRun(catalogID int32) (err error) { +func unlockRun(cmd *cobra.Command, catalogID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -39,7 +41,7 @@ func unlockRun(catalogID int32) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.CatalogAPI.CatalogLock(context.TODO()).CatalogLockManagementCommand(body).Execute() + response, err := myApiClient.Client.CatalogAPI.CatalogLock(ctx).CatalogLockManagementCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/cloudcredential/aws/add/add.go b/cmd/cloudcredential/aws/add/add.go index 9a14e84f..82422851 100644 --- a/cmd/cloudcredential/aws/add/add.go +++ b/cmd/cloudcredential/aws/add/add.go @@ -1,8 +1,6 @@ package add import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cloudcredential/aws/complete" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -60,7 +58,7 @@ func NewCmdAdd() *cobra.Command { Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { opts.Name = args[0] - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -85,7 +83,10 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -106,7 +107,7 @@ func addRun(opts *AddOptions) (err error) { } // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.AWSCloudCredentialAPI.AwsCreate(context.TODO()).CreateAwsCloudCommand(body).Execute() + data, response, err := myApiClient.Client.AWSCloudCredentialAPI.AwsCreate(ctx).CreateAwsCloudCommand(body).Execute() if err != nil { err = tk.CreateError(response, err) return diff --git a/cmd/cloudcredential/aws/check/check.go b/cmd/cloudcredential/aws/check/check.go index 8a0764b3..48d731b6 100644 --- a/cmd/cloudcredential/aws/check/check.go +++ b/cmd/cloudcredential/aws/check/check.go @@ -1,7 +1,6 @@ package check import ( - "context" "fmt" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" @@ -25,7 +24,7 @@ func NewCmdCheck() *cobra.Command { Short: "Check the validity of an AWS cloud credential", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return checkRun(&opts) + return checkRun(cmd, &opts) }, } @@ -38,7 +37,10 @@ func NewCmdCheck() *cobra.Command { return cmd } -func checkRun(opts *CheckOptions) (err error) { +func checkRun(cmd *cobra.Command, opts *CheckOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -49,7 +51,7 @@ func checkRun(opts *CheckOptions) (err error) { } // Execute a query into the API + graceful exit - myRequest := myApiClient.Client.CheckerAPI.CheckerAws(context.TODO()).CheckAwsCommand(body) + myRequest := myApiClient.Client.CheckerAPI.CheckerAws(ctx).CheckAwsCommand(body) response, err := myRequest.Execute() if err == nil { diff --git a/cmd/cloudcredential/aws/complete/complete.go b/cmd/cloudcredential/aws/complete/complete.go index e7d81fe4..9e320fbc 100644 --- a/cmd/cloudcredential/aws/complete/complete.go +++ b/cmd/cloudcredential/aws/complete/complete.go @@ -1,7 +1,6 @@ package complete import ( - "context" "fmt" "github.com/itera-io/taikun-cli/cmd/cmdutils" tk "github.com/itera-io/taikungoclient" @@ -17,6 +16,9 @@ func MakeAwsRegionCompletionFunc(accessKeyID *string, secretAccessKey *string) c return } + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.RegionListCommand{ @@ -24,7 +26,7 @@ func MakeAwsRegionCompletionFunc(accessKeyID *string, secretAccessKey *string) c AwsSecretAccessKey: *taikuncore.NewNullableString(secretAccessKey), } - data, response, err := myApiClient.Client.AWSCloudCredentialAPI.AwsRegionlist(context.TODO()).RegionListCommand(body).Execute() + data, response, err := myApiClient.Client.AWSCloudCredentialAPI.AwsRegionlist(ctx).RegionListCommand(body).Execute() if err != nil { fmt.Println(tk.CreateError(response, err)) return diff --git a/cmd/cloudcredential/aws/list/list.go b/cmd/cloudcredential/aws/list/list.go index 0b215ca9..ca9d2e7a 100644 --- a/cmd/cloudcredential/aws/list/list.go +++ b/cmd/cloudcredential/aws/list/list.go @@ -59,7 +59,7 @@ func NewCmdList() *cobra.Command { Use: "list", Short: "List AWS cloud credentials", RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Args: cobra.NoArgs, Aliases: cmdutils.ListAliases, @@ -73,13 +73,15 @@ func NewCmdList() *cobra.Command { return cmd } -func listRun(opts *ListOptions) error { +func listRun(cmd *cobra.Command, opts *ListOptions) error { orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err } opts.OrganizationID = orgID - amazonCloudCredentials, err := ListCloudCredentialsAws(opts) + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + amazonCloudCredentials, err := ListCloudCredentialsAws(ctx, opts) if err != nil { return err } @@ -87,9 +89,9 @@ func listRun(opts *ListOptions) error { return out.PrintResults(amazonCloudCredentials, listFields) } -func ListCloudCredentialsAws(opts *ListOptions) (credentials []interface{}, err error) { +func ListCloudCredentialsAws(ctx context.Context, opts *ListOptions) (credentials []interface{}, err error) { myApiClient := tk.NewClient() - myRequest := myApiClient.Client.AWSCloudCredentialAPI.AwsList(context.TODO()) + myRequest := myApiClient.Client.AWSCloudCredentialAPI.AwsList(ctx) if opts.OrganizationID != 0 { myRequest = myRequest.OrganizationId(opts.OrganizationID) } diff --git a/cmd/cloudcredential/aws/subnet-list/subnet-list.go b/cmd/cloudcredential/aws/subnet-list/subnet-list.go index 02ebb545..f96cc32e 100644 --- a/cmd/cloudcredential/aws/subnet-list/subnet-list.go +++ b/cmd/cloudcredential/aws/subnet-list/subnet-list.go @@ -1,8 +1,6 @@ package subnet_list import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cloudcredential/aws/complete" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -48,7 +46,7 @@ func NewCmdSubnetList() *cobra.Command { Short: "List subnets for an AWS cloud credential", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return subnetList(&opts) + return subnetList(cmd, &opts) }, } @@ -68,7 +66,10 @@ func NewCmdSubnetList() *cobra.Command { return cmd } -func subnetList(opts *SubnetListOptions) error { +func subnetList(cmd *cobra.Command, opts *SubnetListOptions) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -81,7 +82,7 @@ func subnetList(opts *SubnetListOptions) error { } // Execute a query into the API + graceful exit - myRequest := myApiClient.Client.AWSCloudCredentialAPI.AwsSubnetList(context.TODO()).AwsSubnetListCommand(body) + myRequest := myApiClient.Client.AWSCloudCredentialAPI.AwsSubnetList(ctx).AwsSubnetListCommand(body) subnets, response, err := myRequest.Execute() // Did it fail because the request failed (e.g. cannot connect to Taikun) or because the credentials are not valid? if err != nil { diff --git a/cmd/cloudcredential/azure/add/add.go b/cmd/cloudcredential/azure/add/add.go index 32688db8..689e0085 100644 --- a/cmd/cloudcredential/azure/add/add.go +++ b/cmd/cloudcredential/azure/add/add.go @@ -1,8 +1,6 @@ package add import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -64,7 +62,7 @@ func NewCmdAdd() *cobra.Command { Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { opts.Name = args[0] - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -94,7 +92,10 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -117,7 +118,7 @@ func addRun(opts *AddOptions) (err error) { } // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.AzureCloudCredentialAPI.AzureCreate(context.TODO()).CreateAzureCloudCommand(body).Execute() + data, response, err := myApiClient.Client.AzureCloudCredentialAPI.AzureCreate(ctx).CreateAzureCloudCommand(body).Execute() if err != nil { err = tk.CreateError(response, err) return diff --git a/cmd/cloudcredential/azure/check/check.go b/cmd/cloudcredential/azure/check/check.go index 75bd6009..39b0991b 100644 --- a/cmd/cloudcredential/azure/check/check.go +++ b/cmd/cloudcredential/azure/check/check.go @@ -1,7 +1,6 @@ package check import ( - "context" "fmt" "strings" @@ -27,7 +26,7 @@ func NewCmdCheck() *cobra.Command { Short: "Check the validity of an Azure cloud credential", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return checkRun(&opts) + return checkRun(cmd, &opts) }, } @@ -43,7 +42,10 @@ func NewCmdCheck() *cobra.Command { return cmd } -func checkRun(opts *CheckOptions) (err error) { +func checkRun(cmd *cobra.Command, opts *CheckOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -55,7 +57,7 @@ func checkRun(opts *CheckOptions) (err error) { } // Execute a query into the API + graceful exit - myRequest := myApiClient.Client.CheckerAPI.CheckerAzure(context.TODO()).CheckAzureCommand(body) + myRequest := myApiClient.Client.CheckerAPI.CheckerAzure(ctx).CheckAzureCommand(body) response, err := myRequest.Execute() if err == nil { diff --git a/cmd/cloudcredential/azure/list/list.go b/cmd/cloudcredential/azure/list/list.go index 591b00f9..ceb4ce22 100644 --- a/cmd/cloudcredential/azure/list/list.go +++ b/cmd/cloudcredential/azure/list/list.go @@ -61,7 +61,7 @@ func NewCmdList() *cobra.Command { Use: "list", Short: "List Azure cloud credentials", RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Args: cobra.NoArgs, Aliases: cmdutils.ListAliases, @@ -76,14 +76,15 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) error { +func listRun(cmd *cobra.Command, opts *ListOptions) error { orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err } opts.OrganizationID = orgID - - azureCloudCredentials, err := ListCloudCredentialsAzure(opts) + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + azureCloudCredentials, err := ListCloudCredentialsAzure(ctx, opts) if err != nil { return err } @@ -91,9 +92,9 @@ func listRun(opts *ListOptions) error { return out.PrintResults(azureCloudCredentials, listFields) } -func ListCloudCredentialsAzure(opts *ListOptions) (credentials []interface{}, err error) { +func ListCloudCredentialsAzure(ctx context.Context, opts *ListOptions) (credentials []interface{}, err error) { myApiClient := tk.NewClient() - myRequest := myApiClient.Client.AzureCloudCredentialAPI.AzureList(context.TODO()) + myRequest := myApiClient.Client.AzureCloudCredentialAPI.AzureList(ctx) if opts.OrganizationID != 0 { myRequest = myRequest.OrganizationId(opts.OrganizationID) } diff --git a/cmd/cloudcredential/azure/offers/offers.go b/cmd/cloudcredential/azure/offers/offers.go index 73ed58ea..3d4536c9 100644 --- a/cmd/cloudcredential/azure/offers/offers.go +++ b/cmd/cloudcredential/azure/offers/offers.go @@ -1,7 +1,6 @@ package offers import ( - "context" "github.com/itera-io/taikun-cli/cmd/cloudcredential/azure/publishers" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -28,7 +27,7 @@ func NewCmdOffers() *cobra.Command { if err != nil { return } - return offersRun(&opts) + return offersRun(cmd, &opts) }, } @@ -44,7 +43,7 @@ func NewCmdOffers() *cobra.Command { opts := publishers.PublishersOptions{ CloudCredentialID: cloudCredentialID, } - completions, _ = publishers.ListPublishers(&opts) + completions, _ = publishers.ListPublishers(cmd, &opts) } } @@ -57,8 +56,8 @@ func NewCmdOffers() *cobra.Command { return &cmd } -func offersRun(opts *OffersOptions) (err error) { - offers, err := ListOffers(opts) +func offersRun(cmd *cobra.Command, opts *OffersOptions) (err error) { + offers, err := ListOffers(cmd, opts) if err == nil { out.PrintStringSlice(offers) } @@ -66,9 +65,12 @@ func offersRun(opts *OffersOptions) (err error) { return } -func ListOffers(opts *OffersOptions) (offers []string, err error) { +func ListOffers(cmd *cobra.Command, opts *OffersOptions) (offers []string, err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - myRequest := myApiClient.Client.AzureCloudCredentialAPI.AzureOffers(context.TODO(), opts.CloudCredentialID, opts.Publisher) + myRequest := myApiClient.Client.AzureCloudCredentialAPI.AzureOffers(ctx, opts.CloudCredentialID, opts.Publisher) offers = make([]string, 0) for { data, response, err := myRequest.Execute() diff --git a/cmd/cloudcredential/azure/publishers/publishers.go b/cmd/cloudcredential/azure/publishers/publishers.go index 208c6887..c07f3ed4 100644 --- a/cmd/cloudcredential/azure/publishers/publishers.go +++ b/cmd/cloudcredential/azure/publishers/publishers.go @@ -1,8 +1,6 @@ package publishers import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" @@ -27,7 +25,7 @@ func NewCmdPublishers() *cobra.Command { if err != nil { return } - return publishersRun(&opts) + return publishersRun(cmd, &opts) }, } @@ -36,8 +34,8 @@ func NewCmdPublishers() *cobra.Command { return &cmd } -func publishersRun(opts *PublishersOptions) (err error) { - publishers, err := ListPublishers(opts) +func publishersRun(cmd *cobra.Command, opts *PublishersOptions) (err error) { + publishers, err := ListPublishers(cmd, opts) if err == nil { out.PrintStringSlice(publishers) } @@ -45,9 +43,12 @@ func publishersRun(opts *PublishersOptions) (err error) { return } -func ListPublishers(opts *PublishersOptions) (publishers []string, err error) { +func ListPublishers(cmd *cobra.Command, opts *PublishersOptions) (publishers []string, err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - myRequest := myApiClient.Client.AzureCloudCredentialAPI.AzurePublishers(context.TODO(), opts.CloudCredentialID).Limit(1000) + myRequest := myApiClient.Client.AzureCloudCredentialAPI.AzurePublishers(ctx, opts.CloudCredentialID).Limit(1000) publishers = make([]string, 0) for { data, response, err := myRequest.Execute() diff --git a/cmd/cloudcredential/azure/skus/skus.go b/cmd/cloudcredential/azure/skus/skus.go index 057c1462..f670a0b5 100644 --- a/cmd/cloudcredential/azure/skus/skus.go +++ b/cmd/cloudcredential/azure/skus/skus.go @@ -1,7 +1,6 @@ package skus import ( - "context" "github.com/itera-io/taikun-cli/cmd/cloudcredential/azure/offers" "github.com/itera-io/taikun-cli/cmd/cloudcredential/azure/publishers" "github.com/itera-io/taikun-cli/cmd/cmdutils" @@ -30,7 +29,7 @@ func NewCmdSKUs() *cobra.Command { if err != nil { return } - return skusRun(&opts) + return skusRun(cmd, &opts) }, } @@ -46,7 +45,7 @@ func NewCmdSKUs() *cobra.Command { opts := publishers.PublishersOptions{ CloudCredentialID: cloudCredentialID, } - completions, _ = publishers.ListPublishers(&opts) + completions, _ = publishers.ListPublishers(cmd, &opts) } } @@ -67,7 +66,7 @@ func NewCmdSKUs() *cobra.Command { CloudCredentialID: cloudCredentialID, Publisher: opts.Publisher, } - completions, _ = offers.ListOffers(&opts) + completions, _ = offers.ListOffers(cmd, &opts) } } @@ -80,8 +79,8 @@ func NewCmdSKUs() *cobra.Command { return &cmd } -func skusRun(opts *SKUsOptions) (err error) { - skus, err := ListSKUs(opts) +func skusRun(cmd *cobra.Command, opts *SKUsOptions) (err error) { + skus, err := ListSKUs(cmd, opts) if err == nil { out.PrintStringSlice(skus) } @@ -89,9 +88,12 @@ func skusRun(opts *SKUsOptions) (err error) { return } -func ListSKUs(opts *SKUsOptions) (skus []string, err error) { +func ListSKUs(cmd *cobra.Command, opts *SKUsOptions) (skus []string, err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - myRequest := myApiClient.Client.AzureCloudCredentialAPI.AzureSkus(context.TODO(), opts.CloudCredentialID, opts.Publisher, opts.Offer) + myRequest := myApiClient.Client.AzureCloudCredentialAPI.AzureSkus(ctx, opts.CloudCredentialID, opts.Publisher, opts.Offer) skus = make([]string, 0) for { data, response, err := myRequest.Execute() diff --git a/cmd/cloudcredential/complete/complete.go b/cmd/cloudcredential/complete/complete.go index 28964a8e..50804b7a 100644 --- a/cmd/cloudcredential/complete/complete.go +++ b/cmd/cloudcredential/complete/complete.go @@ -19,7 +19,7 @@ func MakeAzurePublisherCompletionFunc() cmdutils.CompletionCoreFunc { opts := publishers.PublishersOptions{ CloudCredentialID: cloudCredentialID, } - completions, _ = publishers.ListPublishers(&opts) + completions, _ = publishers.ListPublishers(cmd, &opts) } } @@ -38,7 +38,7 @@ func MakeAzureOfferCompletionFunc(publisher *string) cmdutils.CompletionCoreFunc CloudCredentialID: cloudCredentialID, Publisher: *publisher, } - completions, _ = offers.ListOffers(&opts) + completions, _ = offers.ListOffers(cmd, &opts) } } @@ -58,7 +58,7 @@ func MakeAzureSKUCompletionFunc(publisher *string, offer *string) cmdutils.Compl Publisher: *publisher, Offer: *offer, } - completions, _ = skus.ListSKUs(&opts) + completions, _ = skus.ListSKUs(cmd, &opts) } } diff --git a/cmd/cloudcredential/flavors/flavors.go b/cmd/cloudcredential/flavors/flavors.go index e1a221fa..9c9663ab 100644 --- a/cmd/cloudcredential/flavors/flavors.go +++ b/cmd/cloudcredential/flavors/flavors.go @@ -1,7 +1,6 @@ package flavors import ( - "context" "fmt" "github.com/itera-io/taikun-cli/api" @@ -59,13 +58,13 @@ func NewCmdFlavors() *cobra.Command { } opts.CloudCredentialID = cloudCredentialID - cloudType, err := utils.GetCloudType(opts.CloudCredentialID) + cloudType, err := utils.GetCloudType(cmd, opts.CloudCredentialID) if err != nil { return err } opts.CloudType = cloudType - return flavorRun(&opts) + return flavorRun(cmd, &opts) }, } @@ -81,28 +80,31 @@ func NewCmdFlavors() *cobra.Command { return &cmd } -func flavorRun(opts *FlavorsOptions) (err error) { +func flavorRun(cmd *cobra.Command, opts *FlavorsOptions) (err error) { switch opts.CloudType { case taikuncore.CLOUDTYPE_AWS: - return getAwsFlavors(opts) + return getAwsFlavors(cmd, opts) case taikuncore.CLOUDTYPE_AZURE: - return getAzureFlavors(opts) + return getAzureFlavors(cmd, opts) case taikuncore.CLOUDTYPE_PROXMOX: - return getProxmoxFlavors(opts) + return getProxmoxFlavors(cmd, opts) case taikuncore.CLOUDTYPE_GOOGLE: - return getGoogleFlavors(opts) + return getGoogleFlavors(cmd, opts) case taikuncore.CLOUDTYPE_OPENSTACK: - return getOpenstackFlavors(opts) + return getOpenstackFlavors(cmd, opts) case taikuncore.CLOUDTYPE_VSPHERE: - return getVsphereFlavors(opts) + return getVsphereFlavors(cmd, opts) default: return fmt.Errorf("could not determine cloud type") } } -func getAwsFlavors(opts *FlavorsOptions) (err error) { +func getAwsFlavors(cmd *cobra.Command, opts *FlavorsOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - myRequest := myApiClient.Client.FlavorsAPI.FlavorsAwsInstanceTypes(context.TODO(), opts.CloudCredentialID).StartCpu(opts.MinCPU).EndCpu(opts.MaxCPU) + myRequest := myApiClient.Client.FlavorsAPI.FlavorsAwsInstanceTypes(ctx, opts.CloudCredentialID).StartCpu(opts.MinCPU).EndCpu(opts.MaxCPU) myRequest = myRequest.StartRam(types.GiBToB(opts.MinRAM) - 100000).EndRam(types.GiBToB(opts.MaxRAM) + 100000).Limit(1000) if config.SortBy != "" { @@ -137,9 +139,12 @@ func getAwsFlavors(opts *FlavorsOptions) (err error) { return out.PrintResults(flavors, flavorsFields) } -func getProxmoxFlavors(opts *FlavorsOptions) (err error) { +func getProxmoxFlavors(cmd *cobra.Command, opts *FlavorsOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - myRequest := myApiClient.Client.FlavorsAPI.FlavorsProxmoxFlavors(context.TODO(), opts.CloudCredentialID).StartCpu(opts.MinCPU).EndCpu(opts.MaxCPU) + myRequest := myApiClient.Client.FlavorsAPI.FlavorsProxmoxFlavors(ctx, opts.CloudCredentialID).StartCpu(opts.MinCPU).EndCpu(opts.MaxCPU) myRequest = myRequest.StartRam(types.GiBToB(opts.MinRAM) - 100000).EndRam(types.GiBToB(opts.MaxRAM) + 100000) if config.SortBy != "" { @@ -174,9 +179,12 @@ func getProxmoxFlavors(opts *FlavorsOptions) (err error) { return out.PrintResults(flavors, flavorsFields) } -func getOpenstackFlavors(opts *FlavorsOptions) (err error) { +func getOpenstackFlavors(cmd *cobra.Command, opts *FlavorsOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - myRequest := myApiClient.Client.FlavorsAPI.FlavorsOpenstackFlavors(context.TODO(), opts.CloudCredentialID).StartCpu(opts.MinCPU).EndCpu(opts.MaxCPU) + myRequest := myApiClient.Client.FlavorsAPI.FlavorsOpenstackFlavors(ctx, opts.CloudCredentialID).StartCpu(opts.MinCPU).EndCpu(opts.MaxCPU) myRequest = myRequest.StartRam(types.GiBToB(opts.MinRAM) - 100000).EndRam(types.GiBToB(opts.MaxRAM) + 100000) if config.SortBy != "" { @@ -211,9 +219,12 @@ func getOpenstackFlavors(opts *FlavorsOptions) (err error) { return out.PrintResults(flavors, flavorsFields) } -func getAzureFlavors(opts *FlavorsOptions) (err error) { +func getAzureFlavors(cmd *cobra.Command, opts *FlavorsOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - myRequest := myApiClient.Client.FlavorsAPI.FlavorsAzureVmSizes(context.TODO(), opts.CloudCredentialID).StartCpu(opts.MinCPU).EndCpu(opts.MaxCPU) + myRequest := myApiClient.Client.FlavorsAPI.FlavorsAzureVmSizes(ctx, opts.CloudCredentialID).StartCpu(opts.MinCPU).EndCpu(opts.MaxCPU) myRequest = myRequest.StartRam(types.GiBToB(opts.MinRAM) - 100000).EndRam(types.GiBToB(opts.MaxRAM) + 100000) if config.SortBy != "" { myRequest = myRequest.SortBy(config.SortBy).SortDirection(*api.GetSortDirection()) @@ -247,9 +258,12 @@ func getAzureFlavors(opts *FlavorsOptions) (err error) { return out.PrintResults(flavors, flavorsFields) } -func getGoogleFlavors(opts *FlavorsOptions) (err error) { +func getGoogleFlavors(cmd *cobra.Command, opts *FlavorsOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - myRequest := myApiClient.Client.FlavorsAPI.FlavorsGoogleMachineTypes(context.TODO(), opts.CloudCredentialID).StartCpu(opts.MinCPU).EndCpu(opts.MaxCPU) + myRequest := myApiClient.Client.FlavorsAPI.FlavorsGoogleMachineTypes(ctx, opts.CloudCredentialID).StartCpu(opts.MinCPU).EndCpu(opts.MaxCPU) myRequest = myRequest.StartRam(types.GiBToB(opts.MinRAM) - 100000).EndRam(types.GiBToB(opts.MaxRAM) + 100000) if config.SortBy != "" { myRequest = myRequest.SortBy(config.SortBy).SortDirection(*api.GetSortDirection()) @@ -283,9 +297,12 @@ func getGoogleFlavors(opts *FlavorsOptions) (err error) { return out.PrintResults(flavors, flavorsFields) } -func getVsphereFlavors(opts *FlavorsOptions) (err error) { +func getVsphereFlavors(cmd *cobra.Command, opts *FlavorsOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - myRequest := myApiClient.Client.FlavorsAPI.FlavorsVsphereFlavors(context.TODO(), opts.CloudCredentialID).StartCpu(opts.MinCPU).EndCpu(opts.MaxCPU) + myRequest := myApiClient.Client.FlavorsAPI.FlavorsVsphereFlavors(ctx, opts.CloudCredentialID).StartCpu(opts.MinCPU).EndCpu(opts.MaxCPU) myRequest = myRequest.StartRam(types.GiBToB(opts.MinRAM) - 100000).EndRam(types.GiBToB(opts.MaxRAM) + 100000) if config.SortBy != "" { myRequest = myRequest.SortBy(config.SortBy).SortDirection(*api.GetSortDirection()) diff --git a/cmd/cloudcredential/google/add/add.go b/cmd/cloudcredential/google/add/add.go index 0882c4a2..8e97199b 100644 --- a/cmd/cloudcredential/google/add/add.go +++ b/cmd/cloudcredential/google/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "errors" tk "github.com/itera-io/taikungoclient" "os" @@ -71,7 +70,7 @@ func NewCmdAdd() *cobra.Command { } else if !opts.ImportProject { return errors.New("must set --folder-id if not importing a project") } - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -98,7 +97,10 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -112,7 +114,7 @@ func addRun(opts *AddOptions) (err error) { myApiClient := tk.NewClient() - myRequest := myApiClient.Client.GoogleAPI.GooglecloudCreate(context.TODO()) + myRequest := myApiClient.Client.GoogleAPI.GooglecloudCreate(ctx) myRequest = myRequest.Config(configFile) myRequest = myRequest.Name(opts.Name) myRequest = myRequest.OrganizationId(opts.OrganizationID) diff --git a/cmd/cloudcredential/google/check/check.go b/cmd/cloudcredential/google/check/check.go index 3c512e70..db3c25ba 100644 --- a/cmd/cloudcredential/google/check/check.go +++ b/cmd/cloudcredential/google/check/check.go @@ -1,7 +1,6 @@ package check import ( - "context" "fmt" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" @@ -9,6 +8,7 @@ import ( "github.com/spf13/cobra" "os" "strings" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type CheckOptions struct { @@ -24,20 +24,23 @@ func NewCmdCheck() *cobra.Command { Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) (err error) { opts.ConfigFilePath = args[0] - return checkRun(&opts) + return checkRun(cmd, &opts) }, } return &cmd } -func checkRun(opts *CheckOptions) (err error) { +func checkRun(cmd *cobra.Command, opts *CheckOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() configFile, err := os.Open(opts.ConfigFilePath) if err != nil { return err } - _, response, err := myApiClient.Client.CheckerAPI.CheckerGoogle(context.TODO()).Config(configFile).Execute() + _, response, err := myApiClient.Client.CheckerAPI.CheckerGoogle(ctx).Config(configFile).Execute() if err == nil { out.PrintCheckSuccess("Google Cloud Platform credential") } diff --git a/cmd/cloudcredential/google/list/list.go b/cmd/cloudcredential/google/list/list.go index 4605f2c3..fff8ae9e 100644 --- a/cmd/cloudcredential/google/list/list.go +++ b/cmd/cloudcredential/google/list/list.go @@ -71,7 +71,7 @@ func NewCmdList() *cobra.Command { Short: "List Google Cloud Platform credentials", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) (err error) { - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -85,14 +85,15 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err } opts.OrganizationID = orgID - - googleCloudCredentials, err := ListCloudCredentialsGoogle(opts) + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + googleCloudCredentials, err := ListCloudCredentialsGoogle(ctx, opts) if err != nil { return err } @@ -100,9 +101,9 @@ func listRun(opts *ListOptions) (err error) { return out.PrintResults(googleCloudCredentials, listFields) } -func ListCloudCredentialsGoogle(opts *ListOptions) (credentials []interface{}, err error) { +func ListCloudCredentialsGoogle(ctx context.Context, opts *ListOptions) (credentials []interface{}, err error) { myApiClient := tk.NewClient() - myRequest := myApiClient.Client.GoogleAPI.GooglecloudList(context.TODO()) + myRequest := myApiClient.Client.GoogleAPI.GooglecloudList(ctx) if opts.OrganizationID != 0 { myRequest = myRequest.OrganizationId(opts.OrganizationID) } diff --git a/cmd/cloudcredential/images/images.go b/cmd/cloudcredential/images/images.go index 45804750..bcfa14fd 100644 --- a/cmd/cloudcredential/images/images.go +++ b/cmd/cloudcredential/images/images.go @@ -57,7 +57,7 @@ func NewCmdImages() *cobra.Command { return err } } - return imagesRun(&opts) + return imagesRun(cmd, &opts) }, } @@ -81,8 +81,8 @@ func NewCmdImages() *cobra.Command { return &cmd } -func imagesRun(opts *ImagesOptions) (err error) { - images, err := getImages(opts) +func imagesRun(cmd *cobra.Command, opts *ImagesOptions) (err error) { + images, err := getImages(cmd, opts) if err != nil { return err } @@ -90,33 +90,36 @@ func imagesRun(opts *ImagesOptions) (err error) { return out.PrintResults(images, imagesFields) } -func getImages(opts *ImagesOptions) (images interface{}, err error) { - cloudType, err := utils.GetCloudType(opts.CloudCredentialID) +func getImages(cmd *cobra.Command, opts *ImagesOptions) (images interface{}, err error) { + cloudType, err := utils.GetCloudType(cmd, opts.CloudCredentialID) if err != nil { return } + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + switch cloudType { case taikuncore.CLOUDTYPE_AWS: - images, err = getAwsImages(opts) + images, err = getAwsImages(ctx, opts) case taikuncore.CLOUDTYPE_AZURE: - images, err = getAzureImages(opts) + images, err = getAzureImages(cmd, ctx, opts) case taikuncore.CLOUDTYPE_OPENSTACK: - images, err = getOpenstackImages(opts) + images, err = getOpenstackImages(ctx, opts) case taikuncore.CLOUDTYPE_GOOGLE: - images, err = getGoogleImages(opts) + images, err = getGoogleImages(ctx, opts) case taikuncore.CLOUDTYPE_PROXMOX: - images, err = getProxmoxImages(opts) + images, err = getProxmoxImages(ctx, opts) case taikuncore.CLOUDTYPE_VSPHERE: - images, err = getVsphereImages(opts) + images, err = getVsphereImages(ctx, opts) } return } -func getVsphereImages(opts *ImagesOptions) (vsphereImages interface{}, err error) { +func getVsphereImages(ctx context.Context, opts *ImagesOptions) (vsphereImages interface{}, err error) { myApiClient := tk.NewClient() - myRequest := myApiClient.Client.ImagesAPI.ImagesVsphereImages(context.TODO(), opts.CloudCredentialID) + myRequest := myApiClient.Client.ImagesAPI.ImagesVsphereImages(ctx, opts.CloudCredentialID) images := make([]taikuncore.CommonStringBasedDropdownDto, 0) @@ -149,9 +152,9 @@ func getVsphereImages(opts *ImagesOptions) (vsphereImages interface{}, err error return vsphereImages, nil } -func getProxmoxImages(opts *ImagesOptions) (proxmoxImages interface{}, err error) { +func getProxmoxImages(ctx context.Context, opts *ImagesOptions) (proxmoxImages interface{}, err error) { myApiClient := tk.NewClient() - myRequest := myApiClient.Client.ImagesAPI.ImagesProxmoxImages(context.TODO(), opts.CloudCredentialID) + myRequest := myApiClient.Client.ImagesAPI.ImagesProxmoxImages(ctx, opts.CloudCredentialID) images := make([]taikuncore.CommonStringBasedDropdownDto, 0) @@ -185,11 +188,11 @@ func getProxmoxImages(opts *ImagesOptions) (proxmoxImages interface{}, err error return proxmoxImages, nil } -func getAwsImages(opts *ImagesOptions) (awsImages interface{}, err error) { +func getAwsImages(ctx context.Context, opts *ImagesOptions) (awsImages interface{}, err error) { myApiClient := tk.NewClient() // Get owners - data, response, err := myApiClient.Client.AWSCloudCredentialAPI.AwsOwners(context.TODO()).Execute() + data, response, err := myApiClient.Client.AWSCloudCredentialAPI.AwsOwners(ctx).Execute() if err != nil { return nil, tk.CreateError(response, err) } @@ -204,7 +207,7 @@ func getAwsImages(opts *ImagesOptions) (awsImages interface{}, err error) { Owners: owners, } - myRequest := myApiClient.Client.ImagesAPI.ImagesAwsImagesList(context.TODO()) + myRequest := myApiClient.Client.ImagesAPI.ImagesAwsImagesList(ctx) images := make([]taikuncore.CommonStringBasedDropdownDto, 0) @@ -239,9 +242,9 @@ func getAwsImages(opts *ImagesOptions) (awsImages interface{}, err error) { } -func getOpenstackImages(opts *ImagesOptions) (openStackImages interface{}, err error) { +func getOpenstackImages(ctx context.Context, opts *ImagesOptions) (openStackImages interface{}, err error) { myApiClient := tk.NewClient() - myRequest := myApiClient.Client.ImagesAPI.ImagesOpenstackImages(context.TODO(), opts.CloudCredentialID) + myRequest := myApiClient.Client.ImagesAPI.ImagesOpenstackImages(ctx, opts.CloudCredentialID) images := make([]taikuncore.CommonStringBasedDropdownDto, 0) @@ -276,13 +279,13 @@ func getOpenstackImages(opts *ImagesOptions) (openStackImages interface{}, err e } -func getGoogleImages(opts *ImagesOptions) (googleImages interface{}, err error) { +func getGoogleImages(ctx context.Context, opts *ImagesOptions) (googleImages interface{}, err error) { if opts.GoogleImageType == "" { return nil, errors.New(`required flag(s) "google-image-type" not set`) } myApiClient := tk.NewClient() - myRequest := myApiClient.Client.ImagesAPI.ImagesGoogleImages(context.TODO(), opts.CloudCredentialID, opts.GoogleImageType).Latest(opts.GoogleLatest) + myRequest := myApiClient.Client.ImagesAPI.ImagesGoogleImages(ctx, opts.CloudCredentialID, opts.GoogleImageType).Latest(opts.GoogleLatest) images := make([]taikuncore.CommonStringBasedDropdownDto, 0) @@ -317,13 +320,13 @@ func getGoogleImages(opts *ImagesOptions) (googleImages interface{}, err error) } -func getAzureImages(opts *ImagesOptions) (azureImages interface{}, err error) { +func getAzureImages(cmd *cobra.Command, ctx context.Context, opts *ImagesOptions) (azureImages interface{}, err error) { if opts.AzureSKU != "" { if opts.AzureOffer == "" || opts.AzurePublisher == "" { return nil, errors.New("before setting --azure-sku, please set --azure-publisher and --azure-offer") } - return getAzureImagesWithSKU(opts) + return getAzureImagesWithSKU(cmd, ctx, opts) } if opts.AzureOffer != "" { @@ -331,20 +334,20 @@ func getAzureImages(opts *ImagesOptions) (azureImages interface{}, err error) { return nil, errors.New("before settings --azure-offer, please set --azure-publisher") } - return getAzureImagesWithOffer(opts) + return getAzureImagesWithOffer(cmd, ctx, opts) } if opts.AzurePublisher != "" { - return getAzureImagesWithPublisher(opts) + return getAzureImagesWithPublisher(cmd, ctx, opts) } - return getAllAzureImages(opts) + return getAllAzureImages(cmd, ctx, opts) } -func getAllAzureImages(opts *ImagesOptions) (azureImages []taikuncore.CommonStringBasedDropdownDto, err error) { +func getAllAzureImages(cmd *cobra.Command, ctx context.Context, opts *ImagesOptions) (azureImages []taikuncore.CommonStringBasedDropdownDto, err error) { publishersOptions := publishers.PublishersOptions{CloudCredentialID: opts.CloudCredentialID} - myPublishers, err := publishers.ListPublishers(&publishersOptions) + myPublishers, err := publishers.ListPublishers(cmd, &publishersOptions) if err != nil { return nil, err } @@ -354,7 +357,7 @@ func getAllAzureImages(opts *ImagesOptions) (azureImages []taikuncore.CommonStri for _, publisher := range myPublishers { opts.AzurePublisher = publisher - moreImages, err := getAzureImagesWithPublisher(opts) + moreImages, err := getAzureImagesWithPublisher(cmd, ctx, opts) if err != nil { return nil, err } @@ -372,13 +375,13 @@ func getAllAzureImages(opts *ImagesOptions) (azureImages []taikuncore.CommonStri return azureImages, nil } -func getAzureImagesWithPublisher(opts *ImagesOptions) (azureImages []taikuncore.CommonStringBasedDropdownDto, err error) { +func getAzureImagesWithPublisher(cmd *cobra.Command, ctx context.Context, opts *ImagesOptions) (azureImages []taikuncore.CommonStringBasedDropdownDto, err error) { offersOptions := offers.OffersOptions{ CloudCredentialID: opts.CloudCredentialID, Publisher: opts.AzurePublisher, } - myOffers, err := offers.ListOffers(&offersOptions) + myOffers, err := offers.ListOffers(cmd, &offersOptions) if err != nil { return nil, err } @@ -388,7 +391,7 @@ func getAzureImagesWithPublisher(opts *ImagesOptions) (azureImages []taikuncore. for _, offer := range myOffers { opts.AzureOffer = offer - moreImages, err := getAzureImagesWithOffer(opts) + moreImages, err := getAzureImagesWithOffer(cmd, ctx, opts) if err != nil { return nil, err } @@ -406,14 +409,14 @@ func getAzureImagesWithPublisher(opts *ImagesOptions) (azureImages []taikuncore. return azureImages, nil } -func getAzureImagesWithOffer(opts *ImagesOptions) (azureImages []taikuncore.CommonStringBasedDropdownDto, err error) { +func getAzureImagesWithOffer(cmd *cobra.Command, ctx context.Context, opts *ImagesOptions) (azureImages []taikuncore.CommonStringBasedDropdownDto, err error) { skusOptions := skus.SKUsOptions{ CloudCredentialID: opts.CloudCredentialID, Publisher: opts.AzurePublisher, Offer: opts.AzureOffer, } - mySkus, err := skus.ListSKUs(&skusOptions) + mySkus, err := skus.ListSKUs(cmd, &skusOptions) if err != nil { return nil, err } @@ -423,7 +426,7 @@ func getAzureImagesWithOffer(opts *ImagesOptions) (azureImages []taikuncore.Comm for _, sku := range mySkus { opts.AzureSKU = sku - moreImages, err := getAzureImagesWithSKU(opts) + moreImages, err := getAzureImagesWithSKU(cmd, ctx, opts) if err != nil { return nil, err } @@ -441,9 +444,9 @@ func getAzureImagesWithOffer(opts *ImagesOptions) (azureImages []taikuncore.Comm return azureImages, nil } -func getAzureImagesWithSKU(opts *ImagesOptions) (azureImages []taikuncore.CommonStringBasedDropdownDto, err error) { +func getAzureImagesWithSKU(cmd *cobra.Command, ctx context.Context, opts *ImagesOptions) (azureImages []taikuncore.CommonStringBasedDropdownDto, err error) { myApiClient := tk.NewClient() - myRequest := myApiClient.Client.ImagesAPI.ImagesAzureImages(context.TODO(), opts.CloudCredentialID, opts.AzurePublisher, opts.AzureOffer, opts.AzureSKU) + myRequest := myApiClient.Client.ImagesAPI.ImagesAzureImages(ctx, opts.CloudCredentialID, opts.AzurePublisher, opts.AzureOffer, opts.AzureSKU) azureImages = make([]taikuncore.CommonStringBasedDropdownDto, 0) for { diff --git a/cmd/cloudcredential/list/list.go b/cmd/cloudcredential/list/list.go index b4dc07c6..4800007f 100644 --- a/cmd/cloudcredential/list/list.go +++ b/cmd/cloudcredential/list/list.go @@ -51,7 +51,7 @@ func NewCmdList() *cobra.Command { Use: "list", Short: "List all cloud credentials", RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Args: cobra.NoArgs, Aliases: cmdutils.ListAliases, @@ -65,18 +65,21 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err } opts.OrganizationID = orgID + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + amazonOpts := awslist.ListOptions{ OrganizationID: opts.OrganizationID, } - credentialsAmazon, err := awslist.ListCloudCredentialsAws(&amazonOpts) + credentialsAmazon, err := awslist.ListCloudCredentialsAws(ctx, &amazonOpts) if err != nil { return } @@ -85,7 +88,7 @@ func listRun(opts *ListOptions) (err error) { OrganizationID: opts.OrganizationID, } - credentialsAzure, err := azlist.ListCloudCredentialsAzure(&azureOpts) + credentialsAzure, err := azlist.ListCloudCredentialsAzure(ctx, &azureOpts) if err != nil { return } @@ -94,7 +97,7 @@ func listRun(opts *ListOptions) (err error) { OrganizationID: opts.OrganizationID, } - credentialsGoogle, err := gcplist.ListCloudCredentialsGoogle(&googleOpts) + credentialsGoogle, err := gcplist.ListCloudCredentialsGoogle(ctx, &googleOpts) if err != nil { return } @@ -103,7 +106,7 @@ func listRun(opts *ListOptions) (err error) { OrganizationID: opts.OrganizationID, } - credentialsOpenStack, err := oslist.ListCloudCredentialsOpenStack(&openstackOpts) + credentialsOpenStack, err := oslist.ListCloudCredentialsOpenStack(ctx, &openstackOpts) if err != nil { return } @@ -112,7 +115,7 @@ func listRun(opts *ListOptions) (err error) { OrganizationID: opts.OrganizationID, } - credentialsProxmox, err := proxmoxlist.ListCloudCredentialsProxmox(&proxmoxOpts) + credentialsProxmox, err := proxmoxlist.ListCloudCredentialsProxmox(ctx, &proxmoxOpts) if err != nil { return } @@ -120,7 +123,7 @@ func listRun(opts *ListOptions) (err error) { vsphereOpts := vspherelist.ListOptions{ OrganizationID: opts.OrganizationID, } - credentialsVSphere, err := vspherelist.ListCloudCredentialsvSphere(&vsphereOpts) + credentialsVSphere, err := vspherelist.ListCloudCredentialsvSphere(ctx, &vsphereOpts) if err != nil { return } diff --git a/cmd/cloudcredential/lock/lock.go b/cmd/cloudcredential/lock/lock.go index 4f161bc1..0b1d990f 100644 --- a/cmd/cloudcredential/lock/lock.go +++ b/cmd/cloudcredential/lock/lock.go @@ -1,13 +1,13 @@ package lock import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdLock() *cobra.Command { @@ -20,14 +20,17 @@ func NewCmdLock() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return lockRun(id) + return lockRun(cmd, id) }, } return cmd } -func lockRun(cloudCredentialID int32) (err error) { +func lockRun(cmd *cobra.Command, cloudCredentialID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -38,7 +41,7 @@ func lockRun(cloudCredentialID int32) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.CloudCredentialAPI.CloudcredentialsLockManager(context.TODO()).CloudLockManagerCommand(body).Execute() + response, err := myApiClient.Client.CloudCredentialAPI.CloudcredentialsLockManager(ctx).CloudLockManagerCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/cloudcredential/openstack/add/add.go b/cmd/cloudcredential/openstack/add/add.go index b645b44e..5e1814d3 100644 --- a/cmd/cloudcredential/openstack/add/add.go +++ b/cmd/cloudcredential/openstack/add/add.go @@ -1,8 +1,6 @@ package add import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -66,7 +64,7 @@ func NewCmdAdd() *cobra.Command { return err } } - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -112,7 +110,10 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -162,7 +163,7 @@ func addRun(opts *AddOptions) (err error) { } // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.OpenstackCloudCredentialAPI.OpenstackCreate(context.TODO()).CreateOpenstackCloudCommand(body).Execute() + data, response, err := myApiClient.Client.OpenstackCloudCredentialAPI.OpenstackCreate(ctx).CreateOpenstackCloudCommand(body).Execute() if err != nil { err = tk.CreateError(response, err) return diff --git a/cmd/cloudcredential/openstack/check/check.go b/cmd/cloudcredential/openstack/check/check.go index 45b93657..eaa6dbe3 100644 --- a/cmd/cloudcredential/openstack/check/check.go +++ b/cmd/cloudcredential/openstack/check/check.go @@ -1,7 +1,6 @@ package check import ( - "context" "fmt" "github.com/itera-io/taikun-cli/cmd/cmdutils" @@ -28,7 +27,7 @@ func NewCmdCheck() *cobra.Command { Short: "Check the validity of an OpenStack cloud credential", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return checkRun(&opts) + return checkRun(cmd, &opts) }, } @@ -49,7 +48,10 @@ func NewCmdCheck() *cobra.Command { return cmd } -func checkRun(opts *CheckOptions) (err error) { +func checkRun(cmd *cobra.Command, opts *CheckOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -72,7 +74,7 @@ func checkRun(opts *CheckOptions) (err error) { } // Execute a query into the API + graceful exit - myRequest := myApiClient.Client.CheckerAPI.CheckerOpenstack(context.TODO()).CheckOpenstackCommand(body) + myRequest := myApiClient.Client.CheckerAPI.CheckerOpenstack(ctx).CheckOpenstackCommand(body) data, response, err := myRequest.Execute() if err != nil { return tk.CreateError(response, err) // Something else happened diff --git a/cmd/cloudcredential/openstack/list/list.go b/cmd/cloudcredential/openstack/list/list.go index 14827364..67541977 100644 --- a/cmd/cloudcredential/openstack/list/list.go +++ b/cmd/cloudcredential/openstack/list/list.go @@ -76,7 +76,7 @@ func NewCmdList() *cobra.Command { Use: "list", Short: "List OpenStack cloud credentials", RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Args: cobra.NoArgs, Aliases: cmdutils.ListAliases, @@ -91,14 +91,15 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) error { +func listRun(cmd *cobra.Command, opts *ListOptions) error { orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err } opts.OrganizationID = orgID - - openstackCloudCredentials, err := ListCloudCredentialsOpenStack(opts) + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + openstackCloudCredentials, err := ListCloudCredentialsOpenStack(ctx, opts) if err != nil { return err } @@ -106,9 +107,9 @@ func listRun(opts *ListOptions) error { return out.PrintResults(openstackCloudCredentials, listFields) } -func ListCloudCredentialsOpenStack(opts *ListOptions) (credentials []interface{}, err error) { +func ListCloudCredentialsOpenStack(ctx context.Context, opts *ListOptions) (credentials []interface{}, err error) { myApiClient := tk.NewClient() - myRequest := myApiClient.Client.OpenstackCloudCredentialAPI.OpenstackList(context.TODO()) + myRequest := myApiClient.Client.OpenstackCloudCredentialAPI.OpenstackList(ctx) if opts.OrganizationID != 0 { myRequest = myRequest.OrganizationId(opts.OrganizationID) } diff --git a/cmd/cloudcredential/proxmox/add/add.go b/cmd/cloudcredential/proxmox/add/add.go index e3db2c88..224e8921 100644 --- a/cmd/cloudcredential/proxmox/add/add.go +++ b/cmd/cloudcredential/proxmox/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -75,7 +74,7 @@ func NewCmdAdd() *cobra.Command { Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { opts.Name = args[0] - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -145,7 +144,10 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -187,7 +189,7 @@ func addRun(opts *AddOptions) (err error) { } // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.ProxmoxCloudCredentialAPI.ProxmoxCreate(context.TODO()).CreateProxmoxCommand(body).Execute() + data, response, err := myApiClient.Client.ProxmoxCloudCredentialAPI.ProxmoxCreate(ctx).CreateProxmoxCommand(body).Execute() if err != nil { err = tk.CreateError(response, err) return diff --git a/cmd/cloudcredential/proxmox/check/check.go b/cmd/cloudcredential/proxmox/check/check.go index 559b0176..dcd78e25 100644 --- a/cmd/cloudcredential/proxmox/check/check.go +++ b/cmd/cloudcredential/proxmox/check/check.go @@ -1,7 +1,6 @@ package check import ( - "context" "fmt" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" @@ -26,7 +25,7 @@ func NewCmdCheck() *cobra.Command { Short: "Check the validity of an Proxmox cloud credential", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return checkRun(&opts) + return checkRun(cmd, &opts) }, } @@ -42,7 +41,10 @@ func NewCmdCheck() *cobra.Command { return cmd } -func checkRun(opts *CheckOptions) (err error) { +func checkRun(cmd *cobra.Command, opts *CheckOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -54,7 +56,7 @@ func checkRun(opts *CheckOptions) (err error) { } // Execute a query into the API + graceful exit - myRequest := myApiClient.Client.CheckerAPI.CheckerProxmox(context.TODO()).ProxmoxCheckerCommand(body) + myRequest := myApiClient.Client.CheckerAPI.CheckerProxmox(ctx).ProxmoxCheckerCommand(body) response, err := myRequest.Execute() if err == nil { diff --git a/cmd/cloudcredential/proxmox/list/list.go b/cmd/cloudcredential/proxmox/list/list.go index 219e8bec..375680f4 100644 --- a/cmd/cloudcredential/proxmox/list/list.go +++ b/cmd/cloudcredential/proxmox/list/list.go @@ -55,7 +55,7 @@ func NewCmdList() *cobra.Command { Use: "list", Short: "List Proxmox cloud credentials", RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Args: cobra.NoArgs, Aliases: cmdutils.ListAliases, @@ -70,14 +70,15 @@ func NewCmdList() *cobra.Command { return cmd } -func listRun(opts *ListOptions) error { +func listRun(cmd *cobra.Command, opts *ListOptions) error { orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err } opts.OrganizationID = orgID - - proxmoxCloudCredentials, err := ListCloudCredentialsProxmox(opts) + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + proxmoxCloudCredentials, err := ListCloudCredentialsProxmox(ctx, opts) if err != nil { return err } @@ -85,9 +86,9 @@ func listRun(opts *ListOptions) error { return out.PrintResults(proxmoxCloudCredentials, listFields) } -func ListCloudCredentialsProxmox(opts *ListOptions) (credentials []interface{}, err error) { +func ListCloudCredentialsProxmox(ctx context.Context, opts *ListOptions) (credentials []interface{}, err error) { myApiClient := tk.NewClient() - myRequest := myApiClient.Client.ProxmoxCloudCredentialAPI.ProxmoxList(context.TODO()) + myRequest := myApiClient.Client.ProxmoxCloudCredentialAPI.ProxmoxList(ctx) if opts.OrganizationID != 0 { myRequest = myRequest.OrganizationId(opts.OrganizationID) diff --git a/cmd/cloudcredential/remove/remove.go b/cmd/cloudcredential/remove/remove.go index 2bede4dd..c84205c0 100644 --- a/cmd/cloudcredential/remove/remove.go +++ b/cmd/cloudcredential/remove/remove.go @@ -15,11 +15,15 @@ func NewCmdDelete() *cobra.Command { Short: "Delete one or more cloud credentials", Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() ids, err := cmdutils.ArgsToNumericalIDs(args) if err != nil { return cmderr.ErrIDArgumentNotANumber } - return cmdutils.DeleteMultiple(ids, deleteRun) + return cmdutils.DeleteMultiple(ids, func(id int32) error { + return deleteRun(ctx, id) + }) }, Aliases: cmdutils.DeleteAliases, } @@ -27,12 +31,12 @@ func NewCmdDelete() *cobra.Command { return cmd } -func deleteRun(cloudCredentialID int32) (err error) { +func deleteRun(ctx context.Context, cloudCredentialID int32) (err error) { // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - response, err := myApiClient.Client.CloudCredentialAPI.CloudcredentialsDelete(context.TODO(), cloudCredentialID).Execute() + response, err := myApiClient.Client.CloudCredentialAPI.CloudcredentialsDelete(ctx, cloudCredentialID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/cloudcredential/unlock/unlock.go b/cmd/cloudcredential/unlock/unlock.go index 26f82af0..a307f9ce 100644 --- a/cmd/cloudcredential/unlock/unlock.go +++ b/cmd/cloudcredential/unlock/unlock.go @@ -1,13 +1,13 @@ package unlock import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdUnlock() *cobra.Command { @@ -20,14 +20,17 @@ func NewCmdUnlock() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return unlockRun(id) + return unlockRun(cmd, id) }, } return cmd } -func unlockRun(cloudCredentialID int32) (err error) { +func unlockRun(cmd *cobra.Command, cloudCredentialID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -38,7 +41,7 @@ func unlockRun(cloudCredentialID int32) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.CloudCredentialAPI.CloudcredentialsLockManager(context.TODO()).CloudLockManagerCommand(body).Execute() + response, err := myApiClient.Client.CloudCredentialAPI.CloudcredentialsLockManager(ctx).CloudLockManagerCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/cloudcredential/utils/utils.go b/cmd/cloudcredential/utils/utils.go index 0ef23463..8f156b81 100644 --- a/cmd/cloudcredential/utils/utils.go +++ b/cmd/cloudcredential/utils/utils.go @@ -1,16 +1,19 @@ package utils import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" + "github.com/itera-io/taikun-cli/cmd/cmdutils" + "github.com/spf13/cobra" ) -func GetCloudType(cloudCredentialID int32) (cloudType taikuncore.CloudType, err error) { +func GetCloudType(cmd *cobra.Command, cloudCredentialID int32) (cloudType taikuncore.CloudType, err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - data, response, err := myApiClient.Client.CloudCredentialAPI.CloudcredentialsOrgList(context.TODO()).IsAdmin(false).Id(cloudCredentialID).Execute() + data, response, err := myApiClient.Client.CloudCredentialAPI.CloudcredentialsOrgList(ctx).IsAdmin(false).Id(cloudCredentialID).Execute() if len(data) == 0 { return taikuncore.CLOUDTYPE_NONE, cmderr.ResourceNotFoundError("Cloud credential", cloudCredentialID) diff --git a/cmd/cloudcredential/vsphere/add/add.go b/cmd/cloudcredential/vsphere/add/add.go index 347bafb3..41bc6471 100644 --- a/cmd/cloudcredential/vsphere/add/add.go +++ b/cmd/cloudcredential/vsphere/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "fmt" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -82,7 +81,7 @@ func NewCmdAdd() *cobra.Command { if ((opts.DrsEnabled) && (len(opts.Hypervisors) != 0)) || (!opts.DrsEnabled) && (len(opts.Hypervisors) == 0) { return fmt.Errorf("specify only one of [--drs-enabled,--hypervisors]") } - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -159,7 +158,10 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -176,7 +178,7 @@ func addRun(opts *AddOptions) (err error) { Password: *taikuncore.NewNullableString(&opts.Password), DatacenterName: *taikuncore.NewNullableString(&opts.Datacenter), } - dataDC, responseDC, errDC := myApiClient.Client.VsphereCloudCredentialAPI.VsphereDatacenterList(context.TODO()).DatacenterListCommand(datacenterBody).Execute() + dataDC, responseDC, errDC := myApiClient.Client.VsphereCloudCredentialAPI.VsphereDatacenterList(ctx).DatacenterListCommand(datacenterBody).Execute() if errDC != nil { err = tk.CreateError(responseDC, errDC) return @@ -222,7 +224,7 @@ func addRun(opts *AddOptions) (err error) { } // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.VsphereCloudCredentialAPI.VsphereCreate(context.TODO()).CreateVsphereCommand(body).Execute() + data, response, err := myApiClient.Client.VsphereCloudCredentialAPI.VsphereCreate(ctx).CreateVsphereCommand(body).Execute() if err != nil { err = tk.CreateError(response, err) return diff --git a/cmd/cloudcredential/vsphere/check/check.go b/cmd/cloudcredential/vsphere/check/check.go index 7f9ebcc2..c65a1ef1 100644 --- a/cmd/cloudcredential/vsphere/check/check.go +++ b/cmd/cloudcredential/vsphere/check/check.go @@ -1,7 +1,6 @@ package check import ( - "context" "fmt" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" @@ -26,7 +25,7 @@ func NewCmdCheck() *cobra.Command { Short: "Check the validity of an vSphere cloud credential", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return checkRun(&opts) + return checkRun(cmd, &opts) }, } @@ -42,7 +41,10 @@ func NewCmdCheck() *cobra.Command { return cmd } -func checkRun(opts *CheckOptions) (err error) { +func checkRun(cmd *cobra.Command, opts *CheckOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create an authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -54,7 +56,7 @@ func checkRun(opts *CheckOptions) (err error) { } // Execute a query into the API + graceful exit - myRequest := myApiClient.Client.VsphereCloudCredentialAPI.VsphereValidate(context.TODO()).ValidateVsphereCommand(body) + myRequest := myApiClient.Client.VsphereCloudCredentialAPI.VsphereValidate(ctx).ValidateVsphereCommand(body) response, err := myRequest.Execute() if err == nil { diff --git a/cmd/cloudcredential/vsphere/list/list.go b/cmd/cloudcredential/vsphere/list/list.go index ff353bf5..8d0d0cb6 100644 --- a/cmd/cloudcredential/vsphere/list/list.go +++ b/cmd/cloudcredential/vsphere/list/list.go @@ -55,7 +55,7 @@ func NewCmdList() *cobra.Command { Use: "list", Short: "List vSphere cloud credentials", RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Args: cobra.NoArgs, Aliases: cmdutils.ListAliases, @@ -70,14 +70,15 @@ func NewCmdList() *cobra.Command { return cmd } -func listRun(opts *ListOptions) error { +func listRun(cmd *cobra.Command, opts *ListOptions) error { orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err } opts.OrganizationID = orgID - - vSphereCloudCredentials, err := ListCloudCredentialsvSphere(opts) + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + vSphereCloudCredentials, err := ListCloudCredentialsvSphere(ctx, opts) if err != nil { return err } @@ -85,9 +86,9 @@ func listRun(opts *ListOptions) error { return out.PrintResults(vSphereCloudCredentials, listFields) } -func ListCloudCredentialsvSphere(opts *ListOptions) (credentials []interface{}, err error) { +func ListCloudCredentialsvSphere(ctx context.Context, opts *ListOptions) (credentials []interface{}, err error) { myApiClient := tk.NewClient() - myRequest := myApiClient.Client.VsphereCloudCredentialAPI.VsphereList(context.TODO()) + myRequest := myApiClient.Client.VsphereCloudCredentialAPI.VsphereList(ctx) if opts.OrganizationID != 0 { myRequest = myRequest.OrganizationId(opts.OrganizationID) diff --git a/cmd/cmdutils/context.go b/cmd/cmdutils/context.go new file mode 100644 index 00000000..7c3f7e03 --- /dev/null +++ b/cmd/cmdutils/context.go @@ -0,0 +1,12 @@ +package cmdutils + +import ( + "context" + + "github.com/itera-io/taikun-cli/config" + "github.com/spf13/cobra" +) + +func APIContext(cmd *cobra.Command) (context.Context, context.CancelFunc) { + return context.WithTimeout(cmd.Context(), config.APITimeout) +} diff --git a/cmd/cmdutils/flags.go b/cmd/cmdutils/flags.go index 2289c82f..057aa9a5 100644 --- a/cmd/cmdutils/flags.go +++ b/cmd/cmdutils/flags.go @@ -1,7 +1,6 @@ package cmdutils import ( - "context" "fmt" "strings" @@ -59,7 +58,7 @@ func makeSortByPreRunE(fields fields.Fields) runE { func makeSortByCompletionFunc(sortType string, fields fields.Fields) func(cmd *cobra.Command, args []string, toComplete string) []string { return func(cmd *cobra.Command, args []string, toComplete string) []string { - sortingElements, err := getSortingElements(sortType) + sortingElements, err := getSortingElements(cmd, sortType) if err != nil { return []string{} } @@ -79,12 +78,15 @@ func makeSortByCompletionFunc(sortType string, fields fields.Fields) func(cmd *c } } -func getSortingElements(sortType string) (sortingElements []string, err error) { +func getSortingElements(cmd *cobra.Command, sortType string) (sortingElements []string, err error) { + ctx, cancel := APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.CommonAPI.CommonSortingElements(context.TODO(), sortType).Execute() + data, response, err := myApiClient.Client.CommonAPI.CommonSortingElements(ctx, sortType).Execute() if err != nil { err = tk.CreateError(response, err) return diff --git a/cmd/cmdutils/flavors_completion.go b/cmd/cmdutils/flavors_completion.go index 5b07d571..3518605d 100644 --- a/cmd/cmdutils/flavors_completion.go +++ b/cmd/cmdutils/flavors_completion.go @@ -1,7 +1,6 @@ package cmdutils import ( - "context" "fmt" "github.com/itera-io/taikun-cli/utils/types" @@ -10,6 +9,9 @@ import ( ) func FlavorCompletionFunc(cmd *cobra.Command, args []string, toComplete string) []string { + ctx, cancel := APIContext(cmd) + defer cancel() + if len(args) == 0 { return []string{} } @@ -19,7 +21,7 @@ func FlavorCompletionFunc(cmd *cobra.Command, args []string, toComplete string) return []string{} } myApiClient := tk.NewClient() - myRequest := myApiClient.Client.FlavorsAPI.FlavorsSelectedFlavorsForProject(context.TODO()).ProjectId(projectID) + myRequest := myApiClient.Client.FlavorsAPI.FlavorsSelectedFlavorsForProject(ctx).ProjectId(projectID) completions := make([]string, 0) for { diff --git a/cmd/cmdutils/is_autoscaling_enabled.go b/cmd/cmdutils/is_autoscaling_enabled.go index 8aa1c84e..5297ca9c 100644 --- a/cmd/cmdutils/is_autoscaling_enabled.go +++ b/cmd/cmdutils/is_autoscaling_enabled.go @@ -1,13 +1,16 @@ package cmdutils import ( - "context" tk "github.com/itera-io/taikungoclient" + "github.com/spf13/cobra" ) -func IsAutoscalingEnabled(projectID int32) (autoscalingEnabled bool, err error) { +func IsAutoscalingEnabled(cmd *cobra.Command, projectID int32) (autoscalingEnabled bool, err error) { + ctx, cancel := APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - data, response, err := myApiClient.Client.ServersAPI.ServersDetails(context.TODO(), projectID).Execute() + data, response, err := myApiClient.Client.ServersAPI.ServersDetails(ctx, projectID).Execute() if err != nil { err = tk.CreateError(response, err) return @@ -15,60 +18,3 @@ func IsAutoscalingEnabled(projectID int32) (autoscalingEnabled bool, err error) autoscalingEnabled = data.Project.GetIsAutoscalingEnabled() return } - -//import ( -// "context" -// tk "github.com/itera-io/taikungoclient" -//) -// -//func IsAutoscalingEnabled(projectID int32) (autoscalingEnabled bool, err error) { -// myApiClient := tk.NewClient() -// data, response, err := myApiClient.Client.ServersAPI.ServersDetails(context.TODO(), projectID).Execute() -// if err != nil { -// err = tk.CreateError(response, err) -// return -// } -// autoscalingEnabled = data.Project.GetIsAutoscalingEnabled() -// return -//} - -//func IsAutoscalingEnabled(projectID int32) (autoscalingEnabled bool, err error) { -// myApiClient := tk.NewClient() -// data, response, err := myApiClient.Client.ProjectsAPI.ProjectsList(context.TODO()).Id(projectID).Execute() -// if err != nil { -// err = tk.CreateError(response, err) -// return -// } else if data.GetTotalCount() < 1 { -// err = fmt.Errorf("the project was not found") -// return -// } else { -// result := data.GetData()[0].GetIsAutoscalingEnabled() -// if result != false { -// // Autoscaling is enabled -// autoscalingEnabled = true -// } else { -// // Autoscaling is disabled -// autoscalingEnabled = false -// } -// } -// return -// -// /* -// apiClient, err := taikungoclient.NewClient() -// if err != nil { -// return -// } -// -// params := servers.NewServersDetailsParams().WithV(taikungoclient.Version) -// params = params.WithProjectID(projectID) -// -// response, err := apiClient.Client.Servers.ServersDetails(params, apiClient) -// if err == nil { -// res := response.Payload.Project.IsAutoscalingEnabled -// if res { -// err = errors.New("Project autoscaling already enabled.") -// } -// } -// return -// */ -//} diff --git a/cmd/groups/check/check.go b/cmd/groups/check/check.go index 0a0d0a22..ff648c09 100644 --- a/cmd/groups/check/check.go +++ b/cmd/groups/check/check.go @@ -1,13 +1,12 @@ package check import ( - "context" - "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type CheckOptions struct { @@ -26,7 +25,7 @@ func NewCmdCheckDuplicateEntity() *cobra.Command { if err != nil { return err } - return checkDuplicateEntity(accountID, &opts) + return checkDuplicateEntity(cmd, accountID, &opts) }, } @@ -36,7 +35,10 @@ func NewCmdCheckDuplicateEntity() *cobra.Command { return &cmd } -func checkDuplicateEntity(accountID int32, opts *CheckOptions) (err error) { +func checkDuplicateEntity(cmd *cobra.Command, accountID int32, opts *CheckOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.CheckDuplicateGroupCommand{ @@ -44,7 +46,7 @@ func checkDuplicateEntity(accountID int32, opts *CheckOptions) (err error) { Name: *taikuncore.NewNullableString(&opts.Name), } - response, err := myApiClient.Client.GroupsAPI.GroupsCheckDuplicateEntity(context.TODO()).CheckDuplicateGroupCommand(body).Execute() + response, err := myApiClient.Client.GroupsAPI.GroupsCheckDuplicateEntity(ctx).CheckDuplicateGroupCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/groups/create/create.go b/cmd/groups/create/create.go index 56c9af89..f6fde858 100644 --- a/cmd/groups/create/create.go +++ b/cmd/groups/create/create.go @@ -1,13 +1,13 @@ package create import ( - "context" "fmt" "github.com/itera-io/taikun-cli/utils/out" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type CreateOptions struct { @@ -22,7 +22,7 @@ func NewCmdCreateGroup() *cobra.Command { Short: "Create a new group", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - return createGroup(args[0], opts) + return createGroup(cmd, args[0], opts) }, } @@ -32,7 +32,10 @@ func NewCmdCreateGroup() *cobra.Command { return &cmd } -func createGroup(groupName string, opts CreateOptions) (err error) { +func createGroup(cmd *cobra.Command, groupName string, opts CreateOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // input parameters sanity check if opts.AccountID == 0 { return fmt.Errorf("account ID must be specified") @@ -44,7 +47,7 @@ func createGroup(groupName string, opts CreateOptions) (err error) { AccountId: opts.AccountID, } - _, response, err := myApiClient.Client.GroupsAPI.GroupsCreate(context.TODO()).CreateGroupCommand(body).Execute() + _, response, err := myApiClient.Client.GroupsAPI.GroupsCreate(ctx).CreateGroupCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/groups/delete/delete.go b/cmd/groups/delete/delete.go index e4f035a7..7c07b59f 100644 --- a/cmd/groups/delete/delete.go +++ b/cmd/groups/delete/delete.go @@ -1,12 +1,11 @@ package delete import ( - "context" - "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdDeleteGroup() *cobra.Command { @@ -19,17 +18,20 @@ func NewCmdDeleteGroup() *cobra.Command { if err != nil { return err } - return deleteGroup(groupID) + return deleteGroup(cmd, groupID) }, } return &cmd } -func deleteGroup(groupID int32) (err error) { +func deleteGroup(cmd *cobra.Command, groupID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - response, err := myApiClient.Client.GroupsAPI.GroupsDelete(context.TODO(), groupID).Execute() + response, err := myApiClient.Client.GroupsAPI.GroupsDelete(ctx, groupID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/groups/list/list.go b/cmd/groups/list/list.go index 0d729a2f..b94e442b 100644 --- a/cmd/groups/list/list.go +++ b/cmd/groups/list/list.go @@ -1,8 +1,6 @@ package list import ( - "context" - "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" "github.com/itera-io/taikun-cli/utils/out/fields" @@ -10,6 +8,7 @@ import ( tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) var listFields = fields.New( @@ -30,17 +29,20 @@ func NewCmdListGroups() *cobra.Command { if err != nil { return err } - return listGroups(accountID) + return listGroups(cmd, accountID) }, } return &cmd } -func listGroups(accountID int32) (err error) { +func listGroups(cmd *cobra.Command, accountID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() var groups = make([]taikuncore.GroupListItem, 0) - req := myApiClient.Client.GroupsAPI.GroupsList(context.TODO()) + req := myApiClient.Client.GroupsAPI.GroupsList(ctx) req = req.AccountId(accountID) data, response, err := req.Execute() diff --git a/cmd/groups/organizations/add/add.go b/cmd/groups/organizations/add/add.go index ceacc4cb..3a5105e8 100644 --- a/cmd/groups/organizations/add/add.go +++ b/cmd/groups/organizations/add/add.go @@ -1,8 +1,6 @@ package add import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" @@ -29,7 +27,7 @@ func NewCmdAddOrganizations() *cobra.Command { if err != nil { return err } - return addOrganizationToGroup(groupID, &opts) + return addOrganizationToGroup(cmd, groupID, &opts) }, } @@ -42,7 +40,10 @@ func NewCmdAddOrganizations() *cobra.Command { return &cmd } -func addOrganizationToGroup(groupID int32, opts *AddOptions) (err error) { +func addOrganizationToGroup(cmd *cobra.Command, groupID int32, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + role, err := taikuncore.NewAccessLevelRolesFromValue(opts.Role) if err != nil { return err @@ -52,7 +53,7 @@ func addOrganizationToGroup(groupID int32, opts *AddOptions) (err error) { orgDtos := []taikuncore.CreateGroupOrganizationDto{orgDto} myApiClient := tk.NewClient() - response, err := myApiClient.Client.GroupsAPI.GroupsAddOrganizations(context.TODO(), groupID).CreateGroupOrganizationDto(orgDtos).Execute() + response, err := myApiClient.Client.GroupsAPI.GroupsAddOrganizations(ctx, groupID).CreateGroupOrganizationDto(orgDtos).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/groups/organizations/delete/delete.go b/cmd/groups/organizations/delete/delete.go index dc481400..388e2791 100644 --- a/cmd/groups/organizations/delete/delete.go +++ b/cmd/groups/organizations/delete/delete.go @@ -1,7 +1,6 @@ package delete import ( - "context" "fmt" "github.com/itera-io/taikun-cli/utils/out" @@ -9,6 +8,7 @@ import ( tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type DeleteOptions struct { @@ -29,7 +29,7 @@ func NewCmdDeleteOrganizations() *cobra.Command { if err != nil { return err } - return deleteOrganizationsFromGroup(groupID, &opts) + return deleteOrganizationsFromGroup(cmd, groupID, &opts) }, } @@ -37,7 +37,10 @@ func NewCmdDeleteOrganizations() *cobra.Command { return &cmd } -func deleteOrganizationsFromGroup(groupID int32, opts *DeleteOptions) (err error) { +func deleteOrganizationsFromGroup(cmd *cobra.Command, groupID int32, opts *DeleteOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // input parameters sanity check if len(opts.OrganizationIDs) == 0 { return fmt.Errorf("no organization IDs are specified") @@ -45,7 +48,7 @@ func deleteOrganizationsFromGroup(groupID int32, opts *DeleteOptions) (err error myApiClient := tk.NewClient() body := *taikuncore.NewDeleteOrganizationFromGroupCommand(groupID, opts.OrganizationIDs) - response, err := myApiClient.Client.GroupsAPI.GroupsDeleteOrganizations(context.TODO()).DeleteOrganizationFromGroupCommand(body).Execute() + response, err := myApiClient.Client.GroupsAPI.GroupsDeleteOrganizations(ctx).DeleteOrganizationFromGroupCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/groups/organizations/update/update.go b/cmd/groups/organizations/update/update.go index 08516b57..35228ead 100644 --- a/cmd/groups/organizations/update/update.go +++ b/cmd/groups/organizations/update/update.go @@ -1,8 +1,6 @@ package update import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" @@ -29,7 +27,7 @@ func NewCmdUpdateOrganization() *cobra.Command { if err != nil { return err } - return updateOrganizationToGroup(groupID, &opts) + return updateOrganizationToGroup(cmd, groupID, &opts) }, } @@ -42,12 +40,15 @@ func NewCmdUpdateOrganization() *cobra.Command { return &cmd } -func updateOrganizationToGroup(groupID int32, opts *UpdateOptions) (err error) { +func updateOrganizationToGroup(cmd *cobra.Command, groupID int32, opts *UpdateOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgDto := *taikuncore.NewUpdateGroupOrganizationDto(opts.Role) orgDto.SetProjects(opts.Projects) myApiClient := tk.NewClient() - response, err := myApiClient.Client.GroupsAPI.GroupsUpdateGroupOrganization(context.TODO(), groupID, opts.OrganizationID).UpdateGroupOrganizationDto(orgDto).Execute() + response, err := myApiClient.Client.GroupsAPI.GroupsUpdateGroupOrganization(ctx, groupID, opts.OrganizationID).UpdateGroupOrganizationDto(orgDto).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/groups/update/update.go b/cmd/groups/update/update.go index 38968317..7474951a 100644 --- a/cmd/groups/update/update.go +++ b/cmd/groups/update/update.go @@ -1,7 +1,6 @@ package update import ( - "context" "fmt" "github.com/itera-io/taikun-cli/utils/out" @@ -9,6 +8,7 @@ import ( tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type UpdateOptions struct { @@ -28,7 +28,7 @@ func NewCmdUpdateGroup() *cobra.Command { if err != nil { return err } - return updateGroup(groupID, &opts) + return updateGroup(cmd, groupID, &opts) }, } @@ -38,7 +38,10 @@ func NewCmdUpdateGroup() *cobra.Command { return &cmd } -func updateGroup(groupID int32, opts *UpdateOptions) (err error) { +func updateGroup(cmd *cobra.Command, groupID int32, opts *UpdateOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // input parameters sanity check if opts.GroupName == "" && opts.ClaimValue == "" { return fmt.Errorf("no parameters to update are passed") @@ -50,7 +53,7 @@ func updateGroup(groupID int32, opts *UpdateOptions) (err error) { ClaimValue: *taikuncore.NewNullableString(&opts.ClaimValue), } - response, err := myApiClient.Client.GroupsAPI.GroupsUpdate(context.TODO(), groupID).UpdateGroupDto(body).Execute() + response, err := myApiClient.Client.GroupsAPI.GroupsUpdate(ctx, groupID).UpdateGroupDto(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/groups/users/add/add.go b/cmd/groups/users/add/add.go index 08bbab9f..687f4d35 100644 --- a/cmd/groups/users/add/add.go +++ b/cmd/groups/users/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "fmt" "github.com/itera-io/taikun-cli/utils/out" @@ -9,6 +8,7 @@ import ( tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type AddOptions struct { @@ -29,7 +29,7 @@ func NewCmdAddUser() *cobra.Command { if err != nil { return err } - return addUsersToGroup(groupID, &opts) + return addUsersToGroup(cmd, groupID, &opts) }, } @@ -37,7 +37,10 @@ func NewCmdAddUser() *cobra.Command { return &cmd } -func addUsersToGroup(groupID int32, opts *AddOptions) (err error) { +func addUsersToGroup(cmd *cobra.Command, groupID int32, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // input parameters sanity check if len(opts.UserIDs) == 0 { return fmt.Errorf("no user IDs are specified") @@ -49,7 +52,7 @@ func addUsersToGroup(groupID int32, opts *AddOptions) (err error) { body = append(body, *taikuncore.NewCreateGroupUserDto(*taikuncore.NewNullableString(&userID))) } - response, err := myApiClient.Client.GroupsAPI.GroupsAddUsers(context.TODO(), groupID).CreateGroupUserDto(body).Execute() + response, err := myApiClient.Client.GroupsAPI.GroupsAddUsers(ctx, groupID).CreateGroupUserDto(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/groups/users/delete/delete.go b/cmd/groups/users/delete/delete.go index c687331f..560ef1c4 100644 --- a/cmd/groups/users/delete/delete.go +++ b/cmd/groups/users/delete/delete.go @@ -1,7 +1,6 @@ package delete import ( - "context" "fmt" "github.com/itera-io/taikun-cli/utils/out" @@ -9,6 +8,7 @@ import ( tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type DeleteOptions struct { @@ -29,7 +29,7 @@ func NewCmdDeleteUsers() *cobra.Command { if err != nil { return err } - return deleteUsersFromGroup(groupID, &opts) + return deleteUsersFromGroup(cmd, groupID, &opts) }, } @@ -37,7 +37,10 @@ func NewCmdDeleteUsers() *cobra.Command { return &cmd } -func deleteUsersFromGroup(groupID int32, opts *DeleteOptions) (err error) { +func deleteUsersFromGroup(cmd *cobra.Command, groupID int32, opts *DeleteOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // input parameters sanity check if len(opts.UserIDs) == 0 { return fmt.Errorf("no user IDs are specified") @@ -45,7 +48,7 @@ func deleteUsersFromGroup(groupID int32, opts *DeleteOptions) (err error) { myApiClient := tk.NewClient() body := *taikuncore.NewDeleteUserFromGroupCommand(groupID, opts.UserIDs) - response, err := myApiClient.Client.GroupsAPI.GroupsDeleteUsers(context.TODO()).DeleteUserFromGroupCommand(body).Execute() + response, err := myApiClient.Client.GroupsAPI.GroupsDeleteUsers(ctx).DeleteUserFromGroupCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/kubernetesprofile/add/add.go b/cmd/kubernetesprofile/add/add.go index 0821d42c..f35b3246 100644 --- a/cmd/kubernetesprofile/add/add.go +++ b/cmd/kubernetesprofile/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -80,7 +79,7 @@ func NewCmdAdd() *cobra.Command { Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { opts.Name = args[0] - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -100,7 +99,10 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -131,7 +133,7 @@ func addRun(opts *AddOptions) (err error) { } // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.KubernetesProfilesAPI.KubernetesprofilesCreate(context.TODO()).CreateKubernetesProfileCommand(body).Execute() + data, response, err := myApiClient.Client.KubernetesProfilesAPI.KubernetesprofilesCreate(ctx).CreateKubernetesProfileCommand(body).Execute() if err != nil { err = tk.CreateError(response, err) return diff --git a/cmd/kubernetesprofile/list/list.go b/cmd/kubernetesprofile/list/list.go index 07b978ce..3cca024a 100644 --- a/cmd/kubernetesprofile/list/list.go +++ b/cmd/kubernetesprofile/list/list.go @@ -1,8 +1,6 @@ package list import ( - "context" - "github.com/itera-io/taikun-cli/api" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/config" @@ -70,7 +68,7 @@ func NewCmdList() *cobra.Command { Use: "list", Short: "List kubernetes profiles", RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Args: cobra.NoArgs, Aliases: cmdutils.ListAliases, @@ -85,7 +83,10 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -93,7 +94,7 @@ func listRun(opts *ListOptions) (err error) { opts.OrganizationID = orgID myApiClient := tk.NewClient() - myRequest := myApiClient.Client.KubernetesProfilesAPI.KubernetesprofilesList(context.TODO()) + myRequest := myApiClient.Client.KubernetesProfilesAPI.KubernetesprofilesList(ctx) if opts.OrganizationID != 0 { myRequest = myRequest.OrganizationId(opts.OrganizationID) } diff --git a/cmd/kubernetesprofile/lock/lock.go b/cmd/kubernetesprofile/lock/lock.go index 6b6d17b3..e22a570a 100644 --- a/cmd/kubernetesprofile/lock/lock.go +++ b/cmd/kubernetesprofile/lock/lock.go @@ -1,13 +1,13 @@ package lock import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdLock() *cobra.Command { @@ -20,14 +20,17 @@ func NewCmdLock() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return lockRun(id) + return lockRun(cmd, id) }, } return cmd } -func lockRun(kubernetesProfileID int32) (err error) { +func lockRun(cmd *cobra.Command, kubernetesProfileID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -38,7 +41,7 @@ func lockRun(kubernetesProfileID int32) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.KubernetesProfilesAPI.KubernetesprofilesLockManager(context.TODO()).KubernetesProfilesLockManagerCommand(body).Execute() + response, err := myApiClient.Client.KubernetesProfilesAPI.KubernetesprofilesLockManager(ctx).KubernetesProfilesLockManagerCommand(body).Execute() if err != nil { err = tk.CreateError(response, err) return diff --git a/cmd/kubernetesprofile/remove/remove.go b/cmd/kubernetesprofile/remove/remove.go index adb07ccb..1ea7b87e 100644 --- a/cmd/kubernetesprofile/remove/remove.go +++ b/cmd/kubernetesprofile/remove/remove.go @@ -15,11 +15,15 @@ func NewCmdDelete() *cobra.Command { Short: "Delete one or more kubernetes profiles", Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() ids, err := cmdutils.ArgsToNumericalIDs(args) if err != nil { return cmderr.ErrIDArgumentNotANumber } - return cmdutils.DeleteMultiple(ids, deleteRun) + return cmdutils.DeleteMultiple(ids, func(id int32) error { + return deleteRun(ctx, id) + }) }, Aliases: cmdutils.DeleteAliases, } @@ -27,12 +31,12 @@ func NewCmdDelete() *cobra.Command { return cmd } -func deleteRun(kubernetesProfileID int32) (err error) { +func deleteRun(ctx context.Context, kubernetesProfileID int32) (err error) { // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - response, err := myApiClient.Client.KubernetesProfilesAPI.KubernetesprofilesDelete(context.TODO(), kubernetesProfileID).Execute() + response, err := myApiClient.Client.KubernetesProfilesAPI.KubernetesprofilesDelete(ctx, kubernetesProfileID).Execute() if err != nil { err = tk.CreateError(response, err) return diff --git a/cmd/kubernetesprofile/unlock/unlock.go b/cmd/kubernetesprofile/unlock/unlock.go index a1a4fbd1..a5d83b4a 100644 --- a/cmd/kubernetesprofile/unlock/unlock.go +++ b/cmd/kubernetesprofile/unlock/unlock.go @@ -1,13 +1,13 @@ package unlock import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdUnlock() *cobra.Command { @@ -20,14 +20,17 @@ func NewCmdUnlock() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return unlockRun(id) + return unlockRun(cmd, id) }, } return cmd } -func unlockRun(kubernetesProfileID int32) (err error) { +func unlockRun(cmd *cobra.Command, kubernetesProfileID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -38,7 +41,7 @@ func unlockRun(kubernetesProfileID int32) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.KubernetesProfilesAPI.KubernetesprofilesLockManager(context.TODO()).KubernetesProfilesLockManagerCommand(body).Execute() + response, err := myApiClient.Client.KubernetesProfilesAPI.KubernetesprofilesLockManager(ctx).KubernetesProfilesLockManagerCommand(body).Execute() if err != nil { err = tk.CreateError(response, err) return diff --git a/cmd/organization/add/add.go b/cmd/organization/add/add.go index 9bd06cbc..2aba2f30 100644 --- a/cmd/organization/add/add.go +++ b/cmd/organization/add/add.go @@ -1,8 +1,6 @@ package add import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -93,7 +91,7 @@ func NewCmdAdd() *cobra.Command { Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { opts.Name = args[0] - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -111,7 +109,10 @@ func NewCmdAdd() *cobra.Command { return cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.OrganizationCreateCommand{ Name: *taikuncore.NewNullableString(&opts.Name), @@ -123,7 +124,7 @@ func addRun(opts *AddOptions) (err error) { body.AccountId = *taikuncore.NewNullableInt32(&opts.AccountID) } - data, response, err := myApiClient.Client.OrganizationsAPI.OrganizationsCreate(context.TODO()).OrganizationCreateCommand(body).Execute() + data, response, err := myApiClient.Client.OrganizationsAPI.OrganizationsCreate(ctx).OrganizationCreateCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/organization/info/info.go b/cmd/organization/info/info.go index 761d40e4..2821c088 100644 --- a/cmd/organization/info/info.go +++ b/cmd/organization/info/info.go @@ -1,7 +1,6 @@ package info import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/cmd/organization/list" @@ -30,7 +29,7 @@ func NewCmdInfo() *cobra.Command { return } infoFields.ShowAll() - return infoRun(&opts) + return infoRun(cmd, &opts) }, } @@ -40,9 +39,12 @@ func NewCmdInfo() *cobra.Command { } // infoRun calls the API and gets an object with information which it prints -func infoRun(opts *InfoOptions) (err error) { +func infoRun(cmd *cobra.Command, opts *InfoOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - data, response, err := myApiClient.Client.OrganizationsAPI.OrganizationsList(context.TODO()).Id(opts.OrganizationID).Execute() + data, response, err := myApiClient.Client.OrganizationsAPI.OrganizationsList(ctx).Id(opts.OrganizationID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/organization/list/list.go b/cmd/organization/list/list.go index c23081b0..13b05a51 100644 --- a/cmd/organization/list/list.go +++ b/cmd/organization/list/list.go @@ -54,7 +54,7 @@ func NewCmdList() *cobra.Command { Use: "list", Short: "List organizations", RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Args: cobra.NoArgs, Aliases: cmdutils.ListAliases, @@ -67,10 +67,11 @@ func NewCmdList() *cobra.Command { return &cmd } -// listRun sends multiple queries to the API and returns a list of organizations. -// Organizations are returned in the UserForListDto structs generated in models. -func listRun(opts *ListOptions) (err error) { - organizations, err := ListOrganizations(opts) +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + + organizations, err := ListOrganizations(ctx, opts) if err != nil { return err } @@ -78,10 +79,9 @@ func listRun(opts *ListOptions) (err error) { return out.PrintResults(organizations, ListFields) } -// List organizations, also used in list catalogs -func ListOrganizations(opts *ListOptions) (organizations []taikuncore.OrganizationDetailsDto, err error) { +func ListOrganizations(ctx context.Context, opts *ListOptions) (organizations []taikuncore.OrganizationDetailsDto, err error) { myApiClient := tk.NewClient() - myRequest := myApiClient.Client.OrganizationsAPI.OrganizationsList(context.TODO()) + myRequest := myApiClient.Client.OrganizationsAPI.OrganizationsList(ctx) // Set Sorting if set in command line options if config.SortBy != "" { myRequest = myRequest.SortBy(config.SortBy).SortDirection(*api.GetSortDirection()) diff --git a/cmd/organization/remove/remove.go b/cmd/organization/remove/remove.go index edae9613..3f165bf3 100644 --- a/cmd/organization/remove/remove.go +++ b/cmd/organization/remove/remove.go @@ -15,11 +15,15 @@ func NewCmdDelete() *cobra.Command { Short: "Delete one or more organizations", Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() ids, err := cmdutils.ArgsToNumericalIDs(args) if err != nil { return cmderr.ErrIDArgumentNotANumber } - return cmdutils.DeleteMultiple(ids, deleteRun) + return cmdutils.DeleteMultiple(ids, func(id int32) error { + return deleteRun(ctx, id) + }) }, Aliases: cmdutils.DeleteAliases, } @@ -27,9 +31,9 @@ func NewCmdDelete() *cobra.Command { return cmd } -func deleteRun(orgID int32) (err error) { +func deleteRun(ctx context.Context, orgID int32) (err error) { myApiClient := tk.NewClient() - response, err := myApiClient.Client.OrganizationsAPI.OrganizationsDelete(context.TODO(), orgID).Execute() + response, err := myApiClient.Client.OrganizationsAPI.OrganizationsDelete(ctx, orgID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/organization/utils.go b/cmd/organization/utils.go index 944fbcc4..f7bd3ef2 100644 --- a/cmd/organization/utils.go +++ b/cmd/organization/utils.go @@ -3,12 +3,13 @@ package organization import ( "context" "fmt" + tk "github.com/itera-io/taikungoclient" ) -func GetDefaultOrganizationID() (id int32, err error) { +func GetDefaultOrganizationID(ctx context.Context) (id int32, err error) { myApiClient := tk.NewClient() - data, response, err := myApiClient.Client.UsersAPI.UsersUserInfo(context.TODO()).Execute() + data, response, err := myApiClient.Client.UsersAPI.UsersUserInfo(ctx).Execute() if err != nil { return -1, tk.CreateError(response, err) } @@ -20,8 +21,8 @@ func GetDefaultOrganizationID() (id int32, err error) { return } -func GetOrganizationIDFromCloudCredential(ccid int32, client *tk.Client) (int32, error) { - data, response, err := client.Client.CloudCredentialAPI.CloudcredentialsOrgList(context.TODO()).IsAdmin(false).Id(ccid).Execute() +func GetOrganizationIDFromCloudCredential(ctx context.Context, ccid int32, client *tk.Client) (int32, error) { + data, response, err := client.Client.CloudCredentialAPI.CloudcredentialsOrgList(ctx).IsAdmin(false).Id(ccid).Execute() if err != nil { return -1, tk.CreateError(response, err) } else if len(data) != 1 { diff --git a/cmd/policyprofile/add/add.go b/cmd/policyprofile/add/add.go index 4ccb317f..e23272f4 100644 --- a/cmd/policyprofile/add/add.go +++ b/cmd/policyprofile/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -71,7 +70,7 @@ func NewCmdAdd() *cobra.Command { Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { opts.Name = args[0] - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -92,7 +91,10 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -117,7 +119,7 @@ func addRun(opts *AddOptions) (err error) { } // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.OpaProfilesAPI.OpaprofilesCreate(context.TODO()).CreateOpaProfileCommand(body).Execute() + data, response, err := myApiClient.Client.OpaProfilesAPI.OpaprofilesCreate(ctx).CreateOpaProfileCommand(body).Execute() if err != nil { err = tk.CreateError(response, err) return diff --git a/cmd/policyprofile/list/list.go b/cmd/policyprofile/list/list.go index 16505c81..3161c4a0 100644 --- a/cmd/policyprofile/list/list.go +++ b/cmd/policyprofile/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/api" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/config" @@ -63,7 +62,7 @@ func NewCmdList() *cobra.Command { Use: "list", Short: "List policy profiles", RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Args: cobra.NoArgs, Aliases: cmdutils.ListAliases, @@ -78,7 +77,10 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -86,7 +88,7 @@ func listRun(opts *ListOptions) (err error) { opts.OrganizationID = orgID myApiClient := tk.NewClient() - myRequest := myApiClient.Client.OpaProfilesAPI.OpaprofilesList(context.TODO()) + myRequest := myApiClient.Client.OpaProfilesAPI.OpaprofilesList(ctx) if opts.OrganizationID != 0 { myRequest = myRequest.OrganizationId(opts.OrganizationID) } diff --git a/cmd/policyprofile/lock/lock.go b/cmd/policyprofile/lock/lock.go index c7244217..5715f2e2 100644 --- a/cmd/policyprofile/lock/lock.go +++ b/cmd/policyprofile/lock/lock.go @@ -1,13 +1,13 @@ package lock import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdLock() *cobra.Command { @@ -20,14 +20,17 @@ func NewCmdLock() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return lockRun(id) + return lockRun(cmd, id) }, } return cmd } -func lockRun(policyProfileID int32) (err error) { +func lockRun(cmd *cobra.Command, policyProfileID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -38,7 +41,7 @@ func lockRun(policyProfileID int32) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.OpaProfilesAPI.OpaprofilesLockManager(context.TODO()).OpaProfileLockManagerCommand(body).Execute() + response, err := myApiClient.Client.OpaProfilesAPI.OpaprofilesLockManager(ctx).OpaProfileLockManagerCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/policyprofile/remove/remove.go b/cmd/policyprofile/remove/remove.go index b1081af1..fe4c3508 100644 --- a/cmd/policyprofile/remove/remove.go +++ b/cmd/policyprofile/remove/remove.go @@ -15,11 +15,15 @@ func NewCmdDelete() *cobra.Command { Short: "Delete one or more policy profiles", Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() ids, err := cmdutils.ArgsToNumericalIDs(args) if err != nil { return cmderr.ErrIDArgumentNotANumber } - return cmdutils.DeleteMultiple(ids, deleteRun) + return cmdutils.DeleteMultiple(ids, func(id int32) error { + return deleteRun(ctx, id) + }) }, Aliases: cmdutils.DeleteAliases, } @@ -27,12 +31,12 @@ func NewCmdDelete() *cobra.Command { return cmd } -func deleteRun(policyProfileID int32) (err error) { +func deleteRun(ctx context.Context, policyProfileID int32) (err error) { // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - response, err := myApiClient.Client.OpaProfilesAPI.OpaprofilesDelete(context.TODO(), policyProfileID).Execute() + response, err := myApiClient.Client.OpaProfilesAPI.OpaprofilesDelete(ctx, policyProfileID).Execute() if err != nil { err = tk.CreateError(response, err) return diff --git a/cmd/policyprofile/unlock/unlock.go b/cmd/policyprofile/unlock/unlock.go index 7a20cee1..164abed3 100644 --- a/cmd/policyprofile/unlock/unlock.go +++ b/cmd/policyprofile/unlock/unlock.go @@ -1,13 +1,13 @@ package unlock import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdUnlock() *cobra.Command { @@ -20,14 +20,17 @@ func NewCmdUnlock() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return unlockRun(id) + return unlockRun(cmd, id) }, } return cmd } -func unlockRun(policyProfileID int32) (err error) { +func unlockRun(cmd *cobra.Command, policyProfileID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -38,7 +41,7 @@ func unlockRun(policyProfileID int32) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.OpaProfilesAPI.OpaprofilesLockManager(context.TODO()).OpaProfileLockManagerCommand(body).Execute() + response, err := myApiClient.Client.OpaProfilesAPI.OpaprofilesLockManager(ctx).OpaProfileLockManagerCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/add/add.go b/cmd/project/add/add.go index d23cadc1..6df596ed 100644 --- a/cmd/project/add/add.go +++ b/cmd/project/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "fmt" "time" @@ -140,7 +139,7 @@ func NewCmdAdd() *cobra.Command { } } - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -187,10 +186,13 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - err = setDefaultAddOptions(opts, myApiClient) + err = setDefaultAddOptions(cmd, opts, myApiClient) if err != nil { return err } @@ -261,7 +263,7 @@ func addRun(opts *AddOptions) (err error) { body.SetMaxSpotPrice(opts.SpotMaxPrice) } - data, response, err := myApiClient.Client.ProjectsAPI.ProjectsCreate(context.TODO()).CreateProjectCommand(body).Execute() + data, response, err := myApiClient.Client.ProjectsAPI.ProjectsCreate(ctx).CreateProjectCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } @@ -269,7 +271,9 @@ func addRun(opts *AddOptions) (err error) { } -func setDefaultAddOptions(opts *AddOptions, client *tk.Client) (err error) { +func setDefaultAddOptions(cmd *cobra.Command, opts *AddOptions, client *tk.Client) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() // Try to resolve org ID from flag/env var; fall back to cloud credential derivation. orgID, resolveErr := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) var organizationID int32 @@ -277,28 +281,28 @@ func setDefaultAddOptions(opts *AddOptions, client *tk.Client) (err error) { organizationID = orgID } else { // Get organization ID from cloud credential ID - organizationID, err = organization.GetOrganizationIDFromCloudCredential(opts.CloudCredentialID, client) + organizationID, err = organization.GetOrganizationIDFromCloudCredential(ctx, opts.CloudCredentialID, client) if err != nil { return err } } if opts.AccessProfileID == 0 { - opts.AccessProfileID, err = getDefaultAccessProfileID(organizationID) + opts.AccessProfileID, err = getDefaultAccessProfileID(cmd, organizationID) if err != nil { return } } if opts.AlertingProfileID == 0 { - opts.AlertingProfileID, err = getDefaultAlertingProfileID(organizationID) + opts.AlertingProfileID, err = getDefaultAlertingProfileID(cmd, organizationID) if err != nil { return } } if opts.KubernetesProfileID == 0 { - opts.KubernetesProfileID, err = getDefaultKubernetesProfileID(organizationID) + opts.KubernetesProfileID, err = getDefaultKubernetesProfileID(cmd, organizationID) if err != nil { return err } @@ -307,9 +311,12 @@ func setDefaultAddOptions(opts *AddOptions, client *tk.Client) (err error) { return } -func getDefaultAccessProfileID(organizationID int32) (id int32, err error) { +func getDefaultAccessProfileID(cmd *cobra.Command, organizationID int32) (id int32, err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - data, response, err := myApiClient.Client.AccessProfilesAPI.AccessprofilesList(context.TODO()).OrganizationId(organizationID).Execute() + data, response, err := myApiClient.Client.AccessProfilesAPI.AccessprofilesList(ctx).OrganizationId(organizationID).Execute() if err != nil { err = tk.CreateError(response, err) return @@ -325,9 +332,12 @@ func getDefaultAccessProfileID(organizationID int32) (id int32, err error) { return } -func getDefaultAlertingProfileID(organizationID int32) (id int32, err error) { +func getDefaultAlertingProfileID(cmd *cobra.Command, organizationID int32) (id int32, err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiclient := tk.NewClient() - data, response, err := myApiclient.Client.AlertingProfilesAPI.AlertingprofilesList(context.TODO()).OrganizationId(organizationID).Execute() + data, response, err := myApiclient.Client.AlertingProfilesAPI.AlertingprofilesList(ctx).OrganizationId(organizationID).Execute() if err != nil { err = tk.CreateError(response, err) return @@ -343,9 +353,12 @@ func getDefaultAlertingProfileID(organizationID int32) (id int32, err error) { return } -func getDefaultKubernetesProfileID(organizationID int32) (id int32, err error) { +func getDefaultKubernetesProfileID(cmd *cobra.Command, organizationID int32) (id int32, err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - data, response, err := myApiClient.Client.KubernetesProfilesAPI.KubernetesprofilesList(context.TODO()).OrganizationId(organizationID).Execute() + data, response, err := myApiClient.Client.KubernetesProfilesAPI.KubernetesprofilesList(ctx).OrganizationId(organizationID).Execute() if err != nil { err = tk.CreateError(response, err) return diff --git a/cmd/project/alert/attach/attach.go b/cmd/project/alert/attach/attach.go index 9f0fccbf..84d781aa 100644 --- a/cmd/project/alert/attach/attach.go +++ b/cmd/project/alert/attach/attach.go @@ -1,7 +1,6 @@ package attach import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" @@ -27,7 +26,7 @@ func NewCmdAttach() *cobra.Command { if err != nil { return } - return attachRun(&opts) + return attachRun(cmd, &opts) }, } @@ -37,13 +36,16 @@ func NewCmdAttach() *cobra.Command { return &cmd } -func attachRun(opts *AttachOptions) (err error) { +func attachRun(cmd *cobra.Command, opts *AttachOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.AttachDetachAlertingProfileCommand{ ProjectId: &opts.ProjectID, AlertingProfileId: *taikuncore.NewNullableInt32(&opts.AlertingProfileID), } - response, err := myApiClient.Client.AlertingProfilesAPI.AlertingprofilesAttach(context.TODO()).AttachDetachAlertingProfileCommand(body).Execute() + response, err := myApiClient.Client.AlertingProfilesAPI.AlertingprofilesAttach(ctx).AttachDetachAlertingProfileCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/alert/detach/detach.go b/cmd/project/alert/detach/detach.go index 433b42ac..bbec5ec0 100644 --- a/cmd/project/alert/detach/detach.go +++ b/cmd/project/alert/detach/detach.go @@ -1,12 +1,12 @@ package detach import ( - "context" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type DetachOptions struct { @@ -25,19 +25,22 @@ func NewCmdDetach() *cobra.Command { if err != nil { return } - return detachRun(&opts) + return detachRun(cmd, &opts) }, } return &cmd } -func detachRun(opts *DetachOptions) (err error) { +func detachRun(cmd *cobra.Command, opts *DetachOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.AttachDetachAlertingProfileCommand{ ProjectId: &opts.ProjectID, } - response, err := myApiClient.Client.AlertingProfilesAPI.AlertingprofilesDetach(context.TODO()).AttachDetachAlertingProfileCommand(body).Execute() + response, err := myApiClient.Client.AlertingProfilesAPI.AlertingprofilesDetach(ctx).AttachDetachAlertingProfileCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/autoscaler/disable/disable.go b/cmd/project/autoscaler/disable/disable.go index 46196149..0a02650f 100644 --- a/cmd/project/autoscaler/disable/disable.go +++ b/cmd/project/autoscaler/disable/disable.go @@ -1,7 +1,6 @@ package disable import ( - "context" "fmt" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -27,15 +26,18 @@ func NewCmdDisable() *cobra.Command { if err != nil { return } - return disableRun(&opts) + return disableRun(cmd, &opts) }, } return &cmd } -func disableRun(opts *DisableOptions) (err error) { - autoscalingEnabled, err := cmdutils.IsAutoscalingEnabled(opts.ProjectID) +func disableRun(cmd *cobra.Command, opts *DisableOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + + autoscalingEnabled, err := cmdutils.IsAutoscalingEnabled(cmd, opts.ProjectID) if err != nil { return err } @@ -48,7 +50,7 @@ func disableRun(opts *DisableOptions) (err error) { body := taikuncore.DisableAutoscalingCommand{ ProjectId: &opts.ProjectID, } - response, err := myApiClient.Client.AutoscalingAPI.AutoscalingDisable(context.TODO()).DisableAutoscalingCommand(body).Execute() + response, err := myApiClient.Client.AutoscalingAPI.AutoscalingDisable(ctx).DisableAutoscalingCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/autoscaler/edit/edit.go b/cmd/project/autoscaler/edit/edit.go index cce467f2..f5ace570 100644 --- a/cmd/project/autoscaler/edit/edit.go +++ b/cmd/project/autoscaler/edit/edit.go @@ -1,7 +1,6 @@ package edit import ( - "context" "fmt" "github.com/itera-io/taikun-cli/cmd/cmdutils" @@ -32,7 +31,7 @@ func NewCmdEdit() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return editRun(&opts) + return editRun(cmd, &opts) }, } @@ -42,8 +41,11 @@ func NewCmdEdit() *cobra.Command { return &cmd } -func editRun(opts *EditOptions) (err error) { - autoscalingEnabled, err := cmdutils.IsAutoscalingEnabled(opts.ProjectID) +func editRun(cmd *cobra.Command, opts *EditOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + + autoscalingEnabled, err := cmdutils.IsAutoscalingEnabled(cmd, opts.ProjectID) if err != nil { return err } @@ -58,7 +60,7 @@ func editRun(opts *EditOptions) (err error) { MinSize: &opts.MinSize, MaxSize: &opts.MaxSize, } - response, err := myApiClient.Client.AutoscalingAPI.AutoscalingEdit(context.TODO()).EditAutoscalingCommand(body).Execute() + response, err := myApiClient.Client.AutoscalingAPI.AutoscalingEdit(ctx).EditAutoscalingCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/autoscaler/enable/enable.go b/cmd/project/autoscaler/enable/enable.go index e2de9685..10d18723 100644 --- a/cmd/project/autoscaler/enable/enable.go +++ b/cmd/project/autoscaler/enable/enable.go @@ -1,7 +1,6 @@ package enable import ( - "context" "fmt" "github.com/itera-io/taikun-cli/utils/out" @@ -36,7 +35,7 @@ func NewCmdEnable() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return enableRun(&opts) + return enableRun(cmd, &opts) }, } @@ -56,8 +55,11 @@ func NewCmdEnable() *cobra.Command { return &cmd } -func enableRun(opts *EnableOptions) (err error) { - autoscalingEnabled, err := cmdutils.IsAutoscalingEnabled(opts.ProjectID) +func enableRun(cmd *cobra.Command, opts *EnableOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + + autoscalingEnabled, err := cmdutils.IsAutoscalingEnabled(cmd, opts.ProjectID) if err != nil { return err } @@ -77,7 +79,7 @@ func enableRun(opts *EnableOptions) (err error) { Flavor: *taikuncore.NewNullableString(&opts.Flavor), SpotEnabled: &opts.Spot, } - response, err := myApiClient.Client.AutoscalingAPI.AutoscalingEnable(context.TODO()).EnableAutoscalingCommand(body).Execute() + response, err := myApiClient.Client.AutoscalingAPI.AutoscalingEnable(ctx).EnableAutoscalingCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/backup/disable/disable.go b/cmd/project/backup/disable/disable.go index 2bed2c93..3bc892bf 100644 --- a/cmd/project/backup/disable/disable.go +++ b/cmd/project/backup/disable/disable.go @@ -1,13 +1,13 @@ package disable import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type DisableOptions struct { @@ -26,19 +26,22 @@ func NewCmdDisable() *cobra.Command { if err != nil { return } - return disableRun(&opts) + return disableRun(cmd, &opts) }, } return &cmd } -func disableRun(opts *DisableOptions) (err error) { +func disableRun(cmd *cobra.Command, opts *DisableOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.DeploymentDisableBackupCommand{ ProjectId: &opts.ProjectID, } - _, err = myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentDisableBackup(context.TODO()).DeploymentDisableBackupCommand(body).Execute() + _, err = myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentDisableBackup(ctx).DeploymentDisableBackupCommand(body).Execute() if err != nil { //return tk.CreateError(response, err) return cmderr.ErrProjectBackupAlreadyDisabled diff --git a/cmd/project/backup/enable/enable.go b/cmd/project/backup/enable/enable.go index 2f3f8426..a6787d39 100644 --- a/cmd/project/backup/enable/enable.go +++ b/cmd/project/backup/enable/enable.go @@ -1,7 +1,6 @@ package enable import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -28,7 +27,7 @@ func NewCmdEnable() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return enableRun(&opts) + return enableRun(cmd, &opts) }, } @@ -42,13 +41,16 @@ func NewCmdEnable() *cobra.Command { return &cmd } -func enableRun(opts *EnableOptions) (err error) { +func enableRun(cmd *cobra.Command, opts *EnableOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.DeploymentEnableBackupCommand{ ProjectId: &opts.ProjectID, S3CredentialId: &opts.BackupCredentialID, } - response, err := myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentEnableBackup(context.TODO()).DeploymentEnableBackupCommand(body).Execute() + response, err := myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentEnableBackup(ctx).DeploymentEnableBackupCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/backup/list/list.go b/cmd/project/backup/list/list.go index fbc14c58..cff1bea3 100644 --- a/cmd/project/backup/list/list.go +++ b/cmd/project/backup/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/api" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/config" @@ -57,7 +56,7 @@ func NewCmdList() *cobra.Command { if err != nil { return } - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -69,9 +68,12 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - myRequest := myApiClient.Client.BackupPolicyAPI.BackupListAllBackups(context.TODO(), opts.ProjectID) + myRequest := myApiClient.Client.BackupPolicyAPI.BackupListAllBackups(ctx, opts.ProjectID) if config.SortBy != "" { myRequest = myRequest.SortBy(config.SortBy).SortDirection(*api.GetSortDirection()) diff --git a/cmd/project/backup/remove/remove.go b/cmd/project/backup/remove/remove.go index 28677733..9d029cbf 100644 --- a/cmd/project/backup/remove/remove.go +++ b/cmd/project/backup/remove/remove.go @@ -26,7 +26,9 @@ func NewCmdDelete() *cobra.Command { if err != nil { return } - return deleteRun(opts) + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + return deleteRun(ctx, opts) }, Aliases: cmdutils.DeleteAliases, } @@ -37,13 +39,13 @@ func NewCmdDelete() *cobra.Command { return &cmd } -func deleteRun(opts DeleteOption) (err error) { +func deleteRun(ctx context.Context, opts DeleteOption) (err error) { myApiClient := tk.NewClient() body := taikuncore.DeleteBackupCommand{ ProjectId: &opts.ProjectID, Name: *taikuncore.NewNullableString(&opts.Name), } - response, err := myApiClient.Client.BackupPolicyAPI.BackupDeleteBackup(context.TODO()).DeleteBackupCommand(body).Execute() + response, err := myApiClient.Client.BackupPolicyAPI.BackupDeleteBackup(ctx).DeleteBackupCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/backupsource/add/add.go b/cmd/project/backupsource/add/add.go index f5ec8332..238f1e53 100644 --- a/cmd/project/backupsource/add/add.go +++ b/cmd/project/backupsource/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" @@ -39,7 +38,7 @@ func NewCmdAdd() *cobra.Command { if err != nil { return } - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -49,7 +48,10 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -59,7 +61,7 @@ func addRun(opts *AddOptions) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.BackupPolicyAPI.BackupImportBackupStorage(context.TODO()).ImportBackupStorageLocationCommand(body).Execute() + response, err := myApiClient.Client.BackupPolicyAPI.BackupImportBackupStorage(ctx).ImportBackupStorageLocationCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/backupsource/list/list.go b/cmd/project/backupsource/list/list.go index f99aebb2..60ba2385 100644 --- a/cmd/project/backupsource/list/list.go +++ b/cmd/project/backupsource/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/api" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/config" @@ -57,7 +56,7 @@ func NewCmdList() *cobra.Command { if err != nil { return err } - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -68,9 +67,12 @@ func NewCmdList() *cobra.Command { return cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - myRequest := myApiClient.Client.BackupPolicyAPI.BackupListAllBackupStorages(context.TODO(), opts.ProjectID) + myRequest := myApiClient.Client.BackupPolicyAPI.BackupListAllBackupStorages(ctx, opts.ProjectID) if config.SortBy != "" { myRequest = myRequest.SortBy(config.SortBy).SortDirection(*api.GetSortDirection()) } diff --git a/cmd/project/backupsource/remove/remove.go b/cmd/project/backupsource/remove/remove.go index 90e98eab..e6a9e23b 100644 --- a/cmd/project/backupsource/remove/remove.go +++ b/cmd/project/backupsource/remove/remove.go @@ -26,7 +26,9 @@ func NewCmdDelete() *cobra.Command { if err != nil { return } - return deleteRun(opts) + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + return deleteRun(ctx, opts) }, Aliases: cmdutils.DeleteAliases, } @@ -37,7 +39,7 @@ func NewCmdDelete() *cobra.Command { return &cmd } -func deleteRun(opts DeleteOption) (err error) { +func deleteRun(ctx context.Context, opts DeleteOption) (err error) { // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -47,7 +49,7 @@ func deleteRun(opts DeleteOption) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.BackupPolicyAPI.BackupDeleteBackupLocation(context.TODO()).DeleteBackupStorageLocationCommand(body).Execute() + response, err := myApiClient.Client.BackupPolicyAPI.BackupDeleteBackupLocation(ctx).DeleteBackupStorageLocationCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/complete/complete.go b/cmd/project/complete/complete.go index eb0805af..031b6375 100644 --- a/cmd/project/complete/complete.go +++ b/cmd/project/complete/complete.go @@ -1,18 +1,21 @@ package complete import ( - "context" tk "github.com/itera-io/taikungoclient" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) // KubernetesVersionCompletionFunc Returns list of Taikun supported Kubernetes versions for a project func KubernetesVersionCompletionFunc(cmd *cobra.Command, args []string, toComplete string) []string { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - data, _, err := myApiClient.Client.KubernetesAPI.KubernetesGetSupportedList(context.TODO()).Execute() + data, _, err := myApiClient.Client.KubernetesAPI.KubernetesGetSupportedList(ctx).Execute() if err != nil { return []string{} } diff --git a/cmd/project/disablemonitoring/disablemonitoring.go b/cmd/project/disablemonitoring/disablemonitoring.go index 19922af8..3726fb72 100644 --- a/cmd/project/disablemonitoring/disablemonitoring.go +++ b/cmd/project/disablemonitoring/disablemonitoring.go @@ -1,13 +1,13 @@ package disablemonitoring import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type DisableMonitoringOptions struct { @@ -26,14 +26,17 @@ func NewCmdDisableMonitoring() *cobra.Command { if err != nil { return } - return disableMonitoringRun(&opts) + return disableMonitoringRun(cmd, &opts) }, } return &cmd } -func disableMonitoringRun(opts *DisableMonitoringOptions) (err error) { +func disableMonitoringRun(cmd *cobra.Command, opts *DisableMonitoringOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() body := taikuncore.DeploymentDisableMonitoringCommand{ @@ -41,7 +44,7 @@ func disableMonitoringRun(opts *DisableMonitoringOptions) (err error) { } // Execute a query into the API + graceful exit - _, err = myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentDisableMonitoring(context.TODO()).DeploymentDisableMonitoringCommand(body).Execute() + _, err = myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentDisableMonitoring(ctx).DeploymentDisableMonitoringCommand(body).Execute() if err != nil { return cmderr.ErrProjectMonitoringAlreadyDisabled } diff --git a/cmd/project/enablemonitoring/enablemonitoring.go b/cmd/project/enablemonitoring/enablemonitoring.go index 558b0d0c..9b3bdb03 100644 --- a/cmd/project/enablemonitoring/enablemonitoring.go +++ b/cmd/project/enablemonitoring/enablemonitoring.go @@ -1,13 +1,13 @@ package enablemonitoring import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type EnableMonitoringOptions struct { @@ -26,14 +26,17 @@ func NewCmdEnableMonitoring() *cobra.Command { if err != nil { return } - return enableMonitoringRun(&opts) + return enableMonitoringRun(cmd, &opts) }, } return &cmd } -func enableMonitoringRun(opts *EnableMonitoringOptions) (err error) { +func enableMonitoringRun(cmd *cobra.Command, opts *EnableMonitoringOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -42,7 +45,7 @@ func enableMonitoringRun(opts *EnableMonitoringOptions) (err error) { } // Execute a query into the API + graceful exit - _, err = myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentEnableMonitoring(context.TODO()).DeploymentEnableMonitoringCommand(body).Execute() + _, err = myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentEnableMonitoring(ctx).DeploymentEnableMonitoringCommand(body).Execute() if err != nil { return cmderr.ErrProjectMonitoringAlreadyEnabled } diff --git a/cmd/project/flavor/bind/bind.go b/cmd/project/flavor/bind/bind.go index 8c463e46..b8fc92e1 100644 --- a/cmd/project/flavor/bind/bind.go +++ b/cmd/project/flavor/bind/bind.go @@ -1,7 +1,6 @@ package bind import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" @@ -26,7 +25,7 @@ func NewCmdBind() *cobra.Command { if err != nil { return } - return bindRun(&opts) + return bindRun(cmd, &opts) }, Args: cobra.ExactArgs(1), } @@ -37,13 +36,16 @@ func NewCmdBind() *cobra.Command { return cmd } -func bindRun(opts *BindOptions) (err error) { +func bindRun(cmd *cobra.Command, opts *BindOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.BindFlavorToProjectCommand{ ProjectId: &opts.ProjectID, Flavors: opts.Flavors, } - response, err := myApiClient.Client.FlavorsAPI.FlavorsBindToProject(context.TODO()).BindFlavorToProjectCommand(body).Execute() + response, err := myApiClient.Client.FlavorsAPI.FlavorsBindToProject(ctx).BindFlavorToProjectCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/flavor/list/list.go b/cmd/project/flavor/list/list.go index 3b2b3a74..15452c64 100644 --- a/cmd/project/flavor/list/list.go +++ b/cmd/project/flavor/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/api" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" @@ -56,7 +55,7 @@ func NewCmdList() *cobra.Command { return cmderr.ErrIDArgumentNotANumber } opts.ProjectID = projectID - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -68,9 +67,12 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - myRequest := myApiClient.Client.FlavorsAPI.FlavorsSelectedFlavorsForProject(context.TODO()).ProjectId(opts.ProjectID) + myRequest := myApiClient.Client.FlavorsAPI.FlavorsSelectedFlavorsForProject(ctx).ProjectId(opts.ProjectID) if config.SortBy != "" { myRequest = myRequest.SortBy(config.SortBy).SortDirection(*api.GetSortDirection()) } diff --git a/cmd/project/flavor/unbind/unbind.go b/cmd/project/flavor/unbind/unbind.go index 54e7ae18..69d3866a 100644 --- a/cmd/project/flavor/unbind/unbind.go +++ b/cmd/project/flavor/unbind/unbind.go @@ -1,13 +1,13 @@ package unbind import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdUnbind() *cobra.Command { @@ -23,7 +23,7 @@ func NewCmdUnbind() *cobra.Command { } bindings[i] = binding } - return unbindRun(bindings) + return unbindRun(cmd, bindings) }, Args: cobra.MinimumNArgs(1), } @@ -31,12 +31,15 @@ func NewCmdUnbind() *cobra.Command { return cmd } -func unbindRun(bindings []int32) (err error) { +func unbindRun(cmd *cobra.Command, bindings []int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.UnbindFlavorFromProjectCommand{ Ids: bindings, } - response, err := myApiClient.Client.FlavorsAPI.FlavorsUnbindFromProject(context.TODO()).UnbindFlavorFromProjectCommand(body).Execute() + response, err := myApiClient.Client.FlavorsAPI.FlavorsUnbindFromProject(ctx).UnbindFlavorFromProjectCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/image/bind/bind.go b/cmd/project/image/bind/bind.go index 32d651c5..5334d7a1 100644 --- a/cmd/project/image/bind/bind.go +++ b/cmd/project/image/bind/bind.go @@ -1,7 +1,6 @@ package bind import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -28,7 +27,7 @@ func NewCmdBind() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return bindRun(&opts) + return bindRun(cmd, &opts) }, } @@ -38,7 +37,10 @@ func NewCmdBind() *cobra.Command { return &cmd } -func bindRun(opts *BindOptions) (err error) { +func bindRun(cmd *cobra.Command, opts *BindOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -49,7 +51,7 @@ func bindRun(opts *BindOptions) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.ImagesAPI.ImagesBindImagesToProject(context.TODO()).BindImageToProjectCommand(body).Execute() + response, err := myApiClient.Client.ImagesAPI.ImagesBindImagesToProject(ctx).BindImageToProjectCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/image/list/list.go b/cmd/project/image/list/list.go index 37c3d195..3bfcd8de 100644 --- a/cmd/project/image/list/list.go +++ b/cmd/project/image/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -57,7 +56,7 @@ func NewCmdList() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return listRun(&opts) + return listRun(cmd, &opts) }, } @@ -66,12 +65,15 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.ImagesAPI.ImagesSelectedImagesForProject(context.TODO()).ProjectId(opts.ProjectID).Execute() + data, response, err := myApiClient.Client.ImagesAPI.ImagesSelectedImagesForProject(ctx).ProjectId(opts.ProjectID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/image/unbind/unbind.go b/cmd/project/image/unbind/unbind.go index 2e1beb92..4a0e0399 100644 --- a/cmd/project/image/unbind/unbind.go +++ b/cmd/project/image/unbind/unbind.go @@ -1,7 +1,6 @@ package unbind import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -26,14 +25,17 @@ func NewCmdUnbind() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return unbindRun(&opts) + return unbindRun(cmd, &opts) }, } return &cmd } -func unbindRun(opts *UnbindOptions) (err error) { +func unbindRun(cmd *cobra.Command, opts *UnbindOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -43,7 +45,7 @@ func unbindRun(opts *UnbindOptions) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.ImagesAPI.ImagesUnbindImagesFromProject(context.TODO()).DeleteImageFromProjectCommand(body).Execute() + response, err := myApiClient.Client.ImagesAPI.ImagesUnbindImagesFromProject(ctx).DeleteImageFromProjectCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/info/info.go b/cmd/project/info/info.go index 2686cddd..497a95b8 100644 --- a/cmd/project/info/info.go +++ b/cmd/project/info/info.go @@ -1,8 +1,6 @@ package info import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -177,7 +175,7 @@ func NewCmdInfo() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return infoRun(&opts) + return infoRun(cmd, &opts) }, } @@ -186,9 +184,12 @@ func NewCmdInfo() *cobra.Command { return &cmd } -func infoRun(opts *InfoOptions) (err error) { +func infoRun(cmd *cobra.Command, opts *InfoOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - data, response, err := myApiClient.Client.ServersAPI.ServersDetails(context.TODO(), opts.ProjectID).Execute() + data, response, err := myApiClient.Client.ServersAPI.ServersDetails(ctx, opts.ProjectID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/k8s/add/add.go b/cmd/project/k8s/add/add.go index 7d07ece3..629be114 100644 --- a/cmd/project/k8s/add/add.go +++ b/cmd/project/k8s/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "errors" "fmt" "strings" @@ -94,7 +93,7 @@ func NewCmdAdd() *cobra.Command { if err := cmdutils.CheckFlagValue("role", opts.Role, types.ServerRoles); err != nil { return err } - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -124,7 +123,10 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() diskSizeValue := types.GiBToBInt64(opts.DiskSize) serverRole := types.GetServerRole(opts.Role) @@ -144,7 +146,7 @@ func addRun(opts *AddOptions) (err error) { // Only set if optional Proxmox parameter is present if opts.ProxmoxDisk != 0 { - proxmoxRole, err1 := getProxmoxRole(opts.ProjectID) + proxmoxRole, err1 := getProxmoxRole(cmd, opts.ProjectID) if err1 != nil { return err1 } @@ -163,7 +165,7 @@ func addRun(opts *AddOptions) (err error) { return } } - data, response, err := myApiClient.Client.ServersAPI.ServersCreate(context.TODO()).ServerForCreateDto(body).Execute() + data, response, err := myApiClient.Client.ServersAPI.ServersCreate(ctx).ServerForCreateDto(body).Execute() if err != nil { return tk.CreateError(response, err) } @@ -196,9 +198,12 @@ func parseKubernetesNodeLabelsFlag(labelsData []string) ([]taikuncore.Kubernetes // Proxmox Role type for a k8s server depends on Proxmox type specified in the Kubernetes profile. // The names in profile don't match the names we send to the server, for it, we have this function. -func getProxmoxRole(projectId int32) (returnRole *taikuncore.ProxmoxRole, returnErr error) { +func getProxmoxRole(cmd *cobra.Command, projectId int32) (returnRole *taikuncore.ProxmoxRole, returnErr error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myclient := tk.NewClient() - data, response, err := myclient.Client.ServersAPI.ServersDetails(context.TODO(), projectId).Execute() + data, response, err := myclient.Client.ServersAPI.ServersDetails(ctx, projectId).Execute() if err != nil { returnErr = tk.CreateError(response, err) return diff --git a/cmd/project/k8s/commit/commit.go b/cmd/project/k8s/commit/commit.go index ef93f7b2..e453c8ca 100644 --- a/cmd/project/k8s/commit/commit.go +++ b/cmd/project/k8s/commit/commit.go @@ -1,12 +1,12 @@ package commit import ( - "context" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type CommitOptions struct { @@ -25,17 +25,20 @@ func NewCmdCommit() *cobra.Command { if err != nil { return } - return commitRun(&opts) + return commitRun(cmd, &opts) }, } return &cmd } -func commitRun(opts *CommitOptions) (err error) { +func commitRun(cmd *cobra.Command, opts *CommitOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() commitCommand := taikuncore.ProjectDeploymentCommitCommand{ProjectId: &opts.ProjectID} - response, err := myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentCommit(context.TODO()).ProjectDeploymentCommitCommand(commitCommand).Execute() + response, err := myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentCommit(ctx).ProjectDeploymentCommitCommand(commitCommand).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/k8s/list/list.go b/cmd/project/k8s/list/list.go index 1ae332d0..06d27e67 100644 --- a/cmd/project/k8s/list/list.go +++ b/cmd/project/k8s/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/api" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/config" @@ -106,7 +105,7 @@ func NewCmdList() *cobra.Command { if err != nil { return } - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -117,8 +116,8 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { - projectServers, err := ListServers(opts) +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + projectServers, err := ListServers(cmd, opts) if err == nil { flavorJsonPropertyName := "flavor" @@ -133,9 +132,12 @@ func listRun(opts *ListOptions) (err error) { } -func ListServers(opts *ListOptions) (projectServers []taikuncore.ServerListDto, err error) { +func ListServers(cmd *cobra.Command, opts *ListOptions) (projectServers []taikuncore.ServerListDto, err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - myApiRequest := myApiClient.Client.ServersAPI.ServersDetails(context.TODO(), opts.ProjectID) + myApiRequest := myApiClient.Client.ServersAPI.ServersDetails(ctx, opts.ProjectID) if config.SortBy != "" { myApiRequest = myApiRequest.SortBy(config.SortBy).SortDirection(*api.GetSortDirection()) } diff --git a/cmd/project/k8s/policy/disable/disable.go b/cmd/project/k8s/policy/disable/disable.go index 95978f76..bc0c7865 100644 --- a/cmd/project/k8s/policy/disable/disable.go +++ b/cmd/project/k8s/policy/disable/disable.go @@ -1,11 +1,11 @@ package disable import ( - "context" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type DisableOptions struct { @@ -24,19 +24,22 @@ func NewCmdDisable() *cobra.Command { if err != nil { return } - return disableRun(&opts) + return disableRun(cmd, &opts) }, } return &cmd } -func disableRun(opts *DisableOptions) (err error) { +func disableRun(cmd *cobra.Command, opts *DisableOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.DeploymentDisableOpaCommand{ ProjectId: &opts.ProjectID, } - response, err := myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentDisableOpa(context.TODO()).DeploymentDisableOpaCommand(body).Execute() + response, err := myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentDisableOpa(ctx).DeploymentDisableOpaCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/k8s/policy/enable/enable.go b/cmd/project/k8s/policy/enable/enable.go index 3b9bf3e4..1962204f 100644 --- a/cmd/project/k8s/policy/enable/enable.go +++ b/cmd/project/k8s/policy/enable/enable.go @@ -1,7 +1,6 @@ package enable import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" @@ -27,7 +26,7 @@ func NewCmdEnable() *cobra.Command { if err != nil { return } - return enableRun(&opts) + return enableRun(cmd, &opts) }, } @@ -37,13 +36,16 @@ func NewCmdEnable() *cobra.Command { return &cmd } -func enableRun(opts *EnableOptions) (err error) { +func enableRun(cmd *cobra.Command, opts *EnableOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.DeploymentOpaEnableCommand{ ProjectId: &opts.ProjectID, OpaCredentialId: &opts.PolicyProfileID, } - response, err := myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentEnableOpa(context.TODO()).DeploymentOpaEnableCommand(body).Execute() + response, err := myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentEnableOpa(ctx).DeploymentOpaEnableCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/k8s/reboot/reboot.go b/cmd/project/k8s/reboot/reboot.go index 208ed6f4..a50dde99 100644 --- a/cmd/project/k8s/reboot/reboot.go +++ b/cmd/project/k8s/reboot/reboot.go @@ -1,12 +1,12 @@ package reboot import ( - "context" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type RebootOptions struct { @@ -26,7 +26,7 @@ func NewCmdReboot() *cobra.Command { if err != nil { return } - return rebootRun(&opts) + return rebootRun(cmd, &opts) }, } cmd.Flags().BoolVarP(&opts.RebootType, "hard", "f", false, "Force hard reboot of server") @@ -34,7 +34,10 @@ func NewCmdReboot() *cobra.Command { return &cmd } -func rebootRun(opts *RebootOptions) (err error) { +func rebootRun(cmd *cobra.Command, opts *RebootOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.RebootServerCommand{} @@ -45,7 +48,7 @@ func rebootRun(opts *RebootOptions) (err error) { body.SetType("soft") } - response, err := myApiClient.Client.ServersAPI.ServersReboot(context.TODO()).RebootServerCommand(body).Execute() + response, err := myApiClient.Client.ServersAPI.ServersReboot(ctx).RebootServerCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/k8s/remove/remove.go b/cmd/project/k8s/remove/remove.go index 1584b293..efc00e34 100644 --- a/cmd/project/k8s/remove/remove.go +++ b/cmd/project/k8s/remove/remove.go @@ -1,7 +1,6 @@ package remove import ( - "context" "errors" "fmt" @@ -42,7 +41,7 @@ func NewCmdDelete() *cobra.Command { return errors.New("must set one of --server-ids and --all-servers flags") } } - return deleteRun(&opts) + return deleteRun(cmd, &opts) }, Aliases: cmdutils.DeleteAliases, } @@ -53,14 +52,16 @@ func NewCmdDelete() *cobra.Command { return &cmd } -func deleteRun(opts *DeleteOptions) (err error) { +func deleteRun(cmd *cobra.Command, opts *DeleteOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() myApiClient := tk.NewClient() body := taikuncore.ProjectDeploymentDeleteServersCommand{ ProjectId: &opts.ProjectID, } if opts.DeleteAll { - allServers, err := list.ListServers(&list.ListOptions{ProjectID: opts.ProjectID}) + allServers, err := list.ListServers(cmd, &list.ListOptions{ProjectID: opts.ProjectID}) if err != nil { return err } @@ -77,7 +78,7 @@ func deleteRun(opts *DeleteOptions) (err error) { } else { body.SetServerIds(opts.ServerIDs) } - response, err := myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentDelete(context.TODO()).ProjectDeploymentDeleteServersCommand(body).Execute() + response, err := myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentDelete(ctx).ProjectDeploymentDeleteServersCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/k8s/repair/repair.go b/cmd/project/k8s/repair/repair.go index a125f1c6..40dd55a7 100644 --- a/cmd/project/k8s/repair/repair.go +++ b/cmd/project/k8s/repair/repair.go @@ -1,12 +1,12 @@ package repair import ( - "context" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type RepairOptions struct { @@ -25,18 +25,21 @@ func NewCmdRepair() *cobra.Command { if err != nil { return } - return repairRun(&opts) + return repairRun(cmd, &opts) }, } return &cmd } -func repairRun(opts *RepairOptions) (err error) { +func repairRun(cmd *cobra.Command, opts *RepairOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() var body taikuncore.ProjectDeploymentRepairCommand body.SetProjectId(opts.ProjectID) - response, err := myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentRepair(context.TODO()).ProjectDeploymentRepairCommand(body).Execute() + response, err := myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentRepair(ctx).ProjectDeploymentRepairCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/k8s/status/status.go b/cmd/project/k8s/status/status.go index ed45b774..85416167 100644 --- a/cmd/project/k8s/status/status.go +++ b/cmd/project/k8s/status/status.go @@ -1,11 +1,11 @@ package status import ( - "context" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type StatusOptions struct { @@ -24,16 +24,19 @@ func NewCmdStatus() *cobra.Command { if err != nil { return } - return statusRun(&opts) + return statusRun(cmd, &opts) }, } return &cmd } -func statusRun(opts *StatusOptions) (err error) { +func statusRun(cmd *cobra.Command, opts *StatusOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - data, response, err := myApiClient.Client.ServersAPI.ServersStatus(context.TODO(), opts.ServerID).Execute() + data, response, err := myApiClient.Client.ServersAPI.ServersStatus(ctx, opts.ServerID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/k8s/upgrade/upgrade.go b/cmd/project/k8s/upgrade/upgrade.go index 59d64bc8..cf25e4db 100644 --- a/cmd/project/k8s/upgrade/upgrade.go +++ b/cmd/project/k8s/upgrade/upgrade.go @@ -1,11 +1,11 @@ package upgrade import ( - "context" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type UpgradeOptions struct { @@ -24,16 +24,19 @@ func NewCmdUpgrade() *cobra.Command { if err != nil { return } - return upgradeRun(&opts) + return upgradeRun(cmd, &opts) }, } return &cmd } -func upgradeRun(opts *UpgradeOptions) (err error) { +func upgradeRun(cmd *cobra.Command, opts *UpgradeOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - response, err := myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentUpgrade(context.TODO(), opts.ProjectID).Execute() + response, err := myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentUpgrade(ctx, opts.ProjectID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/kubeconfig/add/add.go b/cmd/project/kubeconfig/add/add.go index fb573529..381e7292 100644 --- a/cmd/project/kubeconfig/add/add.go +++ b/cmd/project/kubeconfig/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "fmt" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" @@ -77,7 +76,7 @@ func NewCmdAdd() *cobra.Command { if err := cmdutils.CheckFlagValue("access-scope", opts.AccessScope, types.KubeconfigAccessScopes); err != nil { return err } - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -105,7 +104,10 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.CreateKubeConfigCommand{} body.SetName(opts.Name) @@ -114,7 +116,7 @@ func addRun(opts *AddOptions) (err error) { body.SetIsAccessibleForManager(opts.AccessScope == types.KubeconfigAccessManagers) body.SetKubeConfigRoleId(types.GetKubeconfigRole(opts.Role)) - data, response, err := myApiClient.Client.KubeConfigAPI.KubeconfigCreate(context.TODO()).CreateKubeConfigCommand(body).Execute() + data, response, err := myApiClient.Client.KubeConfigAPI.KubeconfigCreate(ctx).CreateKubeConfigCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/kubeconfig/download/download.go b/cmd/project/kubeconfig/download/download.go index fa67f867..af030cb3 100644 --- a/cmd/project/kubeconfig/download/download.go +++ b/cmd/project/kubeconfig/download/download.go @@ -1,7 +1,6 @@ package download import ( - "context" "fmt" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" @@ -30,7 +29,7 @@ func NewCmdDownload() *cobra.Command { if err != nil { return } - return downloadRun(&opts) + return downloadRun(cmd, &opts) }, } @@ -42,11 +41,14 @@ func NewCmdDownload() *cobra.Command { return &cmd } -func downloadRun(opts *DownloadOptions) (err error) { +func downloadRun(cmd *cobra.Command, opts *DownloadOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() if opts.OutputFile == "" { - kubeconfigName, err := getKubeconfigName(opts) + kubeconfigName, err := getKubeconfigName(cmd, opts) if err != nil { return err } @@ -61,7 +63,7 @@ func downloadRun(opts *DownloadOptions) (err error) { Id: &opts.KubeconfigID, ProjectId: &opts.ProjectID, } - data, response, err := myApiClient.Client.KubeConfigAPI.KubeconfigDownload(context.TODO()).DownloadKubeConfigCommand(body).Execute() + data, response, err := myApiClient.Client.KubeConfigAPI.KubeconfigDownload(ctx).DownloadKubeConfigCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } @@ -72,10 +74,13 @@ func downloadRun(opts *DownloadOptions) (err error) { } -func getKubeconfigName(opts *DownloadOptions) (name string, err error) { +func getKubeconfigName(cmd *cobra.Command, opts *DownloadOptions) (name string, err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - data, response, err := myApiClient.Client.KubeConfigAPI.KubeconfigList(context.TODO()).ProjectId(opts.ProjectID).Id(opts.KubeconfigID).Execute() - //data, response, err := myApiClient.Client.KubeConfigAPI.KubeconfigList(context.TODO()).Id(kubeconfigID).Execute() + data, response, err := myApiClient.Client.KubeConfigAPI.KubeconfigList(ctx).ProjectId(opts.ProjectID).Id(opts.KubeconfigID).Execute() + //data, response, err := myApiClient.Client.KubeConfigAPI.KubeconfigList(ctx).Id(kubeconfigID).Execute() if err != nil { err = tk.CreateError(response, err) return diff --git a/cmd/project/kubeconfig/list/list.go b/cmd/project/kubeconfig/list/list.go index 6df99d7f..7b0add8c 100644 --- a/cmd/project/kubeconfig/list/list.go +++ b/cmd/project/kubeconfig/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -66,7 +65,7 @@ func NewCmdList() *cobra.Command { if err != nil { return } - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -78,9 +77,12 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - myRequest := myApiClient.Client.KubeConfigAPI.KubeconfigList(context.TODO()).ProjectId(opts.ProjectID) + myRequest := myApiClient.Client.KubeConfigAPI.KubeconfigList(ctx).ProjectId(opts.ProjectID) var kubeconfigs []interface{} for { diff --git a/cmd/project/kubeconfig/remove/remove.go b/cmd/project/kubeconfig/remove/remove.go index 56de1a8b..c6d33f5d 100644 --- a/cmd/project/kubeconfig/remove/remove.go +++ b/cmd/project/kubeconfig/remove/remove.go @@ -16,11 +16,15 @@ func NewCmdDelete() *cobra.Command { Short: "Delete one or more kubeconfigs", Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() ids, err := cmdutils.ArgsToNumericalIDs(args) if err != nil { return cmderr.ErrIDArgumentNotANumber } - return cmdutils.DeleteMultiple(ids, deleteRun) + return cmdutils.DeleteMultiple(ids, func(id int32) error { + return deleteRun(ctx, id) + }) }, Aliases: cmdutils.DeleteAliases, } @@ -28,12 +32,12 @@ func NewCmdDelete() *cobra.Command { return &cmd } -func deleteRun(kubeconfigID int32) (err error) { +func deleteRun(ctx context.Context, kubeconfigID int32) (err error) { myApiClient := tk.NewClient() body := taikuncore.DeleteKubeConfigCommand{ Id: &kubeconfigID, } - response, err := myApiClient.Client.KubeConfigAPI.KubeconfigDelete(context.TODO()).DeleteKubeConfigCommand(body).Execute() + response, err := myApiClient.Client.KubeConfigAPI.KubeconfigDelete(ctx).DeleteKubeConfigCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/list-subnets/list-subnets.go b/cmd/project/list-subnets/list-subnets.go index 52f2127c..4006b3ac 100644 --- a/cmd/project/list-subnets/list-subnets.go +++ b/cmd/project/list-subnets/list-subnets.go @@ -1,8 +1,6 @@ package list_subnets import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -33,7 +31,7 @@ func NewCmdListSubnets() *cobra.Command { if err != nil { return } - return listSubnetsRun(projectID) + return listSubnetsRun(cmd, projectID) }, Aliases: cmdutils.ListAliases, } @@ -41,9 +39,12 @@ func NewCmdListSubnets() *cobra.Command { return &cmd } -func listSubnetsRun(projectID int32) (err error) { +func listSubnetsRun(cmd *cobra.Command, projectID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - req := myApiClient.Client.ServersAPI.ServersDetails(context.TODO(), projectID) + req := myApiClient.Client.ServersAPI.ServersDetails(ctx, projectID) details, response, err := req.Execute() if err != nil { return tk.CreateError(response, err) diff --git a/cmd/project/list/list.go b/cmd/project/list/list.go index 211b32b6..b9dcb9be 100644 --- a/cmd/project/list/list.go +++ b/cmd/project/list/list.go @@ -1,8 +1,6 @@ package list import ( - "context" - "github.com/itera-io/taikun-cli/api" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/config" @@ -96,7 +94,7 @@ func NewCmdList() *cobra.Command { Short: "List projects", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -110,7 +108,10 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -118,7 +119,7 @@ func listRun(opts *ListOptions) (err error) { opts.OrganizationID = orgID myApiClient := tk.NewClient() - myRequest := myApiClient.Client.ProjectsAPI.ProjectsList(context.TODO()) + myRequest := myApiClient.Client.ProjectsAPI.ProjectsList(ctx) if opts.OrganizationID != 0 { myRequest = myRequest.OrganizationId(opts.OrganizationID) } diff --git a/cmd/project/lock/lock.go b/cmd/project/lock/lock.go index f89e45c5..1b7fc8a2 100644 --- a/cmd/project/lock/lock.go +++ b/cmd/project/lock/lock.go @@ -1,13 +1,13 @@ package lock import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdLock() *cobra.Command { @@ -20,14 +20,17 @@ func NewCmdLock() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return lockRun(projectID) + return lockRun(cmd, projectID) }, } return cmd } -func lockRun(projectID int32) (err error) { +func lockRun(cmd *cobra.Command, projectID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -37,7 +40,7 @@ func lockRun(projectID int32) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.ProjectsAPI.ProjectsLockManager(context.TODO()).ProjectLockManagerCommand(body).Execute() + response, err := myApiClient.Client.ProjectsAPI.ProjectsLockManager(ctx).ProjectLockManagerCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/purge/purge.go b/cmd/project/purge/purge.go index 75dc30f7..1fa6b6f3 100644 --- a/cmd/project/purge/purge.go +++ b/cmd/project/purge/purge.go @@ -1,12 +1,12 @@ package purge import ( - "context" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdPurge() *cobra.Command { @@ -14,7 +14,7 @@ func NewCmdPurge() *cobra.Command { Use: "purge ", Short: "Deletes all k8s servers and all virtual clusters for this project", RunE: func(cmd *cobra.Command, args []string) error { - return purgeRun(args[0]) + return purgeRun(cmd, args[0]) }, Args: cobra.ExactArgs(1), } @@ -22,7 +22,10 @@ func NewCmdPurge() *cobra.Command { return cmd } -func purgeRun(projectIdString string) (err error) { +func purgeRun(cmd *cobra.Command, projectIdString string) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() // Project ID @@ -32,7 +35,7 @@ func purgeRun(projectIdString string) (err error) { } // Server IDs - data, response, err := myApiClient.Client.ServersAPI.ServersDetails(context.TODO(), projectId).Execute() + data, response, err := myApiClient.Client.ServersAPI.ServersDetails(ctx, projectId).Execute() if err != nil { return tk.CreateError(response, err) } @@ -50,7 +53,7 @@ func purgeRun(projectIdString string) (err error) { DeleteAutoscalingServers: &alwaysTellThe, } - response, err = myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentDelete(context.TODO()).ProjectDeploymentDeleteServersCommand(body).Execute() + response, err = myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentDelete(ctx).ProjectDeploymentDeleteServersCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/quota/edit/edit.go b/cmd/project/quota/edit/edit.go index a84c28fd..342b157e 100644 --- a/cmd/project/quota/edit/edit.go +++ b/cmd/project/quota/edit/edit.go @@ -1,7 +1,6 @@ package edit import ( - "context" "fmt" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" @@ -9,6 +8,7 @@ import ( tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) const ( @@ -69,10 +69,13 @@ func NewCmdEdit() *cobra.Command { } func editRun(cmd *cobra.Command, opts *EditOptions) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + client := tk.NewClient() // Load existing quotas - data, response, err := client.Client.ProjectQuotasAPI.ProjectquotasList(context.TODO()).Id(opts.projectId).Execute() + data, response, err := client.Client.ProjectQuotasAPI.ProjectquotasList(ctx).Id(opts.projectId).Execute() if err != nil { return tk.CreateError(response, err) } @@ -145,7 +148,7 @@ func editRun(cmd *cobra.Command, opts *EditOptions) error { body.SetVmRam(types.GiBToB(vmRam)) // Send the update request - response, err = client.Client.ProjectQuotasAPI.ProjectquotasUpdate(context.TODO()).UpdateQuotaCommand(body).Execute() + response, err = client.Client.ProjectQuotasAPI.ProjectquotasUpdate(ctx).UpdateQuotaCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/quota/list/list.go b/cmd/project/quota/list/list.go index 62a240c7..0f6ddc20 100644 --- a/cmd/project/quota/list/list.go +++ b/cmd/project/quota/list/list.go @@ -1,8 +1,6 @@ package list import ( - "context" - "github.com/itera-io/taikun-cli/api" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/config" @@ -58,7 +56,7 @@ func NewCmdList() *cobra.Command { Use: "list", Short: "List project quotas", RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Args: cobra.NoArgs, Aliases: cmdutils.ListAliases, @@ -73,7 +71,10 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -81,7 +82,7 @@ func listRun(opts *ListOptions) (err error) { opts.OrganizationID = orgID myApiClient := tk.NewClient() - myRequest := myApiClient.Client.ProjectQuotasAPI.ProjectquotasList(context.TODO()) + myRequest := myApiClient.Client.ProjectQuotasAPI.ProjectquotasList(ctx) if opts.OrganizationID != 0 { myRequest = myRequest.OrganizationId(opts.OrganizationID) } diff --git a/cmd/project/remove/remove.go b/cmd/project/remove/remove.go index 11b19f4b..a81bf36c 100644 --- a/cmd/project/remove/remove.go +++ b/cmd/project/remove/remove.go @@ -28,6 +28,8 @@ func NewCmdDelete() *cobra.Command { Short: "Delete one or more projects", Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() optsList := make([]*DeleteOptions, len(args)) for i, arg := range args { projectID, err := types.Atoi32(arg) @@ -39,7 +41,7 @@ func NewCmdDelete() *cobra.Command { ProjectID: projectID, } } - return deleteMultiple(optsList) + return deleteMultiple(ctx, optsList) }, Aliases: cmdutils.DeleteAliases, } @@ -49,11 +51,11 @@ func NewCmdDelete() *cobra.Command { return cmd } -func deleteMultiple(optsList []*DeleteOptions) error { +func deleteMultiple(ctx context.Context, optsList []*DeleteOptions) error { errorOccured := false for _, opts := range optsList { - if err := deleteRun(opts); err != nil { + if err := deleteRun(ctx, opts); err != nil { errorOccured = true fmt.Fprintln(os.Stderr, err) @@ -68,13 +70,13 @@ func deleteMultiple(optsList []*DeleteOptions) error { return nil } -func deleteRun(opts *DeleteOptions) (err error) { +func deleteRun(ctx context.Context, opts *DeleteOptions) (err error) { myApiClient := tk.NewClient() body := taikuncore.DeleteProjectCommand{ ProjectId: &opts.ProjectID, IsForceDelete: &opts.Force, } - request, err := myApiClient.Client.ProjectsAPI.ProjectsDelete(context.TODO()).DeleteProjectCommand(body).Execute() + request, err := myApiClient.Client.ProjectsAPI.ProjectsDelete(ctx).DeleteProjectCommand(body).Execute() if err != nil { return tk.CreateError(request, err) } diff --git a/cmd/project/restore/add/add.go b/cmd/project/restore/add/add.go index b2d7a442..61bb11bf 100644 --- a/cmd/project/restore/add/add.go +++ b/cmd/project/restore/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" @@ -45,7 +44,7 @@ func NewCmdAdd() *cobra.Command { if err != nil { return err } - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -60,7 +59,10 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.RestoreBackupCommand{ ProjectId: &opts.ProjectID, @@ -69,7 +71,7 @@ func addRun(opts *AddOptions) (err error) { IncludeNamespaces: opts.IncludeNamespaces, ExcludeNamespaces: opts.ExcludeNamespaces, } - response, err := myApiClient.Client.BackupPolicyAPI.BackupRestoreBackup(context.TODO()).RestoreBackupCommand(body).Execute() + response, err := myApiClient.Client.BackupPolicyAPI.BackupRestoreBackup(ctx).RestoreBackupCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/restore/list/list.go b/cmd/project/restore/list/list.go index dda8ad87..51cca4a6 100644 --- a/cmd/project/restore/list/list.go +++ b/cmd/project/restore/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/api" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/config" @@ -66,7 +65,7 @@ func NewCmdList() *cobra.Command { if err != nil { return err } - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -77,9 +76,12 @@ func NewCmdList() *cobra.Command { return cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - myRequest := myApiClient.Client.BackupPolicyAPI.BackupListAllRestores(context.TODO(), opts.ProjectID) + myRequest := myApiClient.Client.BackupPolicyAPI.BackupListAllRestores(ctx, opts.ProjectID) if config.SortBy != "" { myRequest = myRequest.SortBy(config.SortBy).SortDirection(*api.GetSortDirection()) } diff --git a/cmd/project/restore/remove/remove.go b/cmd/project/restore/remove/remove.go index 8ebff26c..639d37ad 100644 --- a/cmd/project/restore/remove/remove.go +++ b/cmd/project/restore/remove/remove.go @@ -26,7 +26,9 @@ func NewCmdDelete() *cobra.Command { if err != nil { return } - return deleteRun(opts) + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + return deleteRun(ctx, opts) }, Aliases: cmdutils.DeleteAliases, } @@ -37,13 +39,13 @@ func NewCmdDelete() *cobra.Command { return &cmd } -func deleteRun(opts DeleteOption) (err error) { +func deleteRun(ctx context.Context, opts DeleteOption) (err error) { myApiClient := tk.NewClient() body := taikuncore.DeleteRestoreCommand{ ProjectId: &opts.ProjectID, Name: *taikuncore.NewNullableString(&opts.Name), } - response, err := myApiClient.Client.BackupPolicyAPI.BackupDeleteRestore(context.TODO()).DeleteRestoreCommand(body).Execute() + response, err := myApiClient.Client.BackupPolicyAPI.BackupDeleteRestore(ctx).DeleteRestoreCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/set/expiration/expiration.go b/cmd/project/set/expiration/expiration.go index b49155f2..f6efa993 100644 --- a/cmd/project/set/expiration/expiration.go +++ b/cmd/project/set/expiration/expiration.go @@ -1,7 +1,6 @@ package expiration import ( - "context" "fmt" "time" @@ -11,6 +10,7 @@ import ( tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type ExtendLifetimeOptions struct { @@ -37,7 +37,7 @@ func NewCmdExpiration() *cobra.Command { return cmderr.ErrUnknownDateFormat } } - return extendProjectLifetime(&opts) + return extendProjectLifetime(cmd, &opts) }, } @@ -48,7 +48,10 @@ func NewCmdExpiration() *cobra.Command { return &cmd } -func extendProjectLifetime(opts *ExtendLifetimeOptions) (err error) { +func extendProjectLifetime(cmd *cobra.Command, opts *ExtendLifetimeOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -68,7 +71,7 @@ func extendProjectLifetime(opts *ExtendLifetimeOptions) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.ProjectsAPI.ProjectsExtendLifetime(context.TODO()).ProjectExtendLifeTimeCommand(body).Execute() + response, err := myApiClient.Client.ProjectsAPI.ProjectsExtendLifetime(ctx).ProjectExtendLifeTimeCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/spot/full/full.go b/cmd/project/spot/full/full.go index bc86c432..b2d11e0c 100644 --- a/cmd/project/spot/full/full.go +++ b/cmd/project/spot/full/full.go @@ -1,13 +1,13 @@ package full import ( - "context" "fmt" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type FullOptions struct { @@ -28,7 +28,7 @@ func NewCmdFull() *cobra.Command { if err != nil { return } - return fullRun(&opts) + return fullRun(cmd, &opts) }, } @@ -40,7 +40,10 @@ func NewCmdFull() *cobra.Command { return &cmd } -func fullRun(opts *FullOptions) (err error) { +func fullRun(cmd *cobra.Command, opts *FullOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.FullSpotOperationCommand{ Id: &opts.ProjectID, @@ -54,7 +57,7 @@ func fullRun(opts *FullOptions) (err error) { return fmt.Errorf("unknown mode. Either disable or enable") } - response, err := myApiClient.Client.ProjectsAPI.ProjectsToggleFullSpot(context.TODO()).FullSpotOperationCommand(body).Execute() + response, err := myApiClient.Client.ProjectsAPI.ProjectsToggleFullSpot(ctx).FullSpotOperationCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/spot/vms/vms.go b/cmd/project/spot/vms/vms.go index 40b1bf8a..f64d4f74 100644 --- a/cmd/project/spot/vms/vms.go +++ b/cmd/project/spot/vms/vms.go @@ -1,13 +1,13 @@ package vms import ( - "context" "fmt" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type VmsOptions struct { @@ -28,7 +28,7 @@ func NewCmdVms() *cobra.Command { if err != nil { return } - return vmsRun(&opts) + return vmsRun(cmd, &opts) }, } @@ -40,7 +40,10 @@ func NewCmdVms() *cobra.Command { return &cmd } -func vmsRun(opts *VmsOptions) (err error) { +func vmsRun(cmd *cobra.Command, opts *VmsOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.SpotVmOperationCommand{ Id: &opts.ProjectID, @@ -54,7 +57,7 @@ func vmsRun(opts *VmsOptions) (err error) { return fmt.Errorf("unknown mode. Either disable or enable") } - response, err := myApiClient.Client.ProjectsAPI.ProjectsToggleSpotVms(context.TODO()).SpotVmOperationCommand(body).Execute() + response, err := myApiClient.Client.ProjectsAPI.ProjectsToggleSpotVms(ctx).SpotVmOperationCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/spot/worker/worker.go b/cmd/project/spot/worker/worker.go index 6939d891..da79fa11 100644 --- a/cmd/project/spot/worker/worker.go +++ b/cmd/project/spot/worker/worker.go @@ -1,13 +1,13 @@ package worker import ( - "context" "fmt" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type WorkerOptions struct { @@ -28,7 +28,7 @@ func NewCmdWorker() *cobra.Command { if err != nil { return } - return workerRun(&opts) + return workerRun(cmd, &opts) }, } @@ -40,7 +40,10 @@ func NewCmdWorker() *cobra.Command { return &cmd } -func workerRun(opts *WorkerOptions) (err error) { +func workerRun(cmd *cobra.Command, opts *WorkerOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.SpotWorkerOperationCommand{ Id: &opts.ProjectID, @@ -54,7 +57,7 @@ func workerRun(opts *WorkerOptions) (err error) { return fmt.Errorf("unknown mode. Either disable or enable") } - response, err := myApiClient.Client.ProjectsAPI.ProjectsToggleSpotWorkers(context.TODO()).SpotWorkerOperationCommand(body).Execute() + response, err := myApiClient.Client.ProjectsAPI.ProjectsToggleSpotWorkers(ctx).SpotWorkerOperationCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/unlock/unlock.go b/cmd/project/unlock/unlock.go index 6ca978af..4a7cdbf9 100644 --- a/cmd/project/unlock/unlock.go +++ b/cmd/project/unlock/unlock.go @@ -1,13 +1,13 @@ package unlock import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdUnlock() *cobra.Command { @@ -20,14 +20,17 @@ func NewCmdUnlock() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return unlockRun(projectID) + return unlockRun(cmd, projectID) }, } return cmd } -func unlockRun(projectID int32) (err error) { +func unlockRun(cmd *cobra.Command, projectID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -37,7 +40,7 @@ func unlockRun(projectID int32) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.ProjectsAPI.ProjectsLockManager(context.TODO()).ProjectLockManagerCommand(body).Execute() + response, err := myApiClient.Client.ProjectsAPI.ProjectsLockManager(ctx).ProjectLockManagerCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/vm/add/add.go b/cmd/project/vm/add/add.go index e60e4138..a4929129 100644 --- a/cmd/project/vm/add/add.go +++ b/cmd/project/vm/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "errors" "fmt" "strings" @@ -94,7 +93,7 @@ func NewCmdAdd() *cobra.Command { if err != nil { return } - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -157,7 +156,10 @@ func parseTagsOption(tagsOption []string) ([]taikuncore.StandAloneMetaDataDto, e return tags, nil } -func addRun(opts *AddOptions) error { +func addRun(cmd *cobra.Command, opts *AddOptions) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() tags, err := parseTagsOption(opts.Tags) @@ -197,7 +199,7 @@ func addRun(opts *AddOptions) error { } // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.StandaloneAPI.StandaloneCreate(context.TODO()).CreateStandAloneVmCommand(body).Execute() + data, response, err := myApiClient.Client.StandaloneAPI.StandaloneCreate(ctx).CreateStandAloneVmCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/vm/add/complete/complete.go b/cmd/project/vm/add/complete/complete.go index 7e7bd9ba..3b46c092 100644 --- a/cmd/project/vm/add/complete/complete.go +++ b/cmd/project/vm/add/complete/complete.go @@ -1,11 +1,11 @@ package complete import ( - "context" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func VolumeTypeCompletionFunc(cmd *cobra.Command, args []string, toComplete string) (completions []string) { @@ -20,7 +20,7 @@ func VolumeTypeCompletionFunc(cmd *cobra.Command, args []string, toComplete stri return } - volumeTypes, err := getOpenStackVolumeTypes(projectID) + volumeTypes, err := getOpenStackVolumeTypes(cmd, projectID) if err == nil { completions = append(completions, volumeTypes...) } @@ -28,7 +28,10 @@ func VolumeTypeCompletionFunc(cmd *cobra.Command, args []string, toComplete stri return } -func getOpenStackVolumeTypes(projectID int32) (volumeTypes []string, err error) { +func getOpenStackVolumeTypes(cmd *cobra.Command, projectID int32) (volumeTypes []string, err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -38,7 +41,7 @@ func getOpenStackVolumeTypes(projectID int32) (volumeTypes []string, err error) } // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.OpenstackCloudCredentialAPI.OpenstackVolumes(context.TODO()).OpenstackVolumeTypeListQuery(body).Execute() + data, response, err := myApiClient.Client.OpenstackCloudCredentialAPI.OpenstackVolumes(ctx).OpenstackVolumeTypeListQuery(body).Execute() if err != nil { err = tk.CreateError(response, err) return diff --git a/cmd/project/vm/commit/commit.go b/cmd/project/vm/commit/commit.go index 85218a3c..05dfb226 100644 --- a/cmd/project/vm/commit/commit.go +++ b/cmd/project/vm/commit/commit.go @@ -1,13 +1,13 @@ package commit import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type CommitOptions struct { @@ -26,14 +26,17 @@ func NewCmdCommit() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return commitRun(&opts) + return commitRun(cmd, &opts) }, } return &cmd } -func commitRun(opts *CommitOptions) (err error) { +func commitRun(cmd *cobra.Command, opts *CommitOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -43,7 +46,7 @@ func commitRun(opts *CommitOptions) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentCommitVm(context.TODO()).DeploymentCommitVmCommand(body).Execute() + response, err := myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentCommitVm(ctx).DeploymentCommitVmCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/vm/disk/add/add.go b/cmd/project/vm/disk/add/add.go index 2a1afa19..9dfb56dc 100644 --- a/cmd/project/vm/disk/add/add.go +++ b/cmd/project/vm/disk/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "github.com/itera-io/taikun-cli/utils/out" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" @@ -59,7 +58,7 @@ func NewCmdAdd() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -77,7 +76,10 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -92,7 +94,7 @@ func addRun(opts *AddOptions) (err error) { } // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.StandaloneVMDisksAPI.StandalonevmdisksCreate(context.TODO()).CreateStandAloneDiskCommand(body).Execute() + data, response, err := myApiClient.Client.StandaloneVMDisksAPI.StandalonevmdisksCreate(ctx).CreateStandAloneDiskCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/vm/disk/list/list.go b/cmd/project/vm/disk/list/list.go index 761752d4..a8bda84e 100644 --- a/cmd/project/vm/disk/list/list.go +++ b/cmd/project/vm/disk/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -58,7 +57,7 @@ func NewCmdList() *cobra.Command { if err != nil { return } - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -71,12 +70,15 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.StandaloneAPI.StandaloneDetails(context.TODO(), opts.ProjectID).Id(opts.StandaloneVMID).Execute() + data, response, err := myApiClient.Client.StandaloneAPI.StandaloneDetails(ctx, opts.ProjectID).Id(opts.StandaloneVMID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/vm/disk/remove/remove.go b/cmd/project/vm/disk/remove/remove.go index 96815d1a..ce5993e0 100644 --- a/cmd/project/vm/disk/remove/remove.go +++ b/cmd/project/vm/disk/remove/remove.go @@ -27,7 +27,9 @@ func NewCmdDelete() *cobra.Command { if err != nil { return } - return deleteRun(&opts) + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + return deleteRun(ctx, &opts) }, Aliases: cmdutils.DeleteAliases, } @@ -38,7 +40,7 @@ func NewCmdDelete() *cobra.Command { return &cmd } -func deleteRun(opts *DeleteOptions) (err error) { +func deleteRun(ctx context.Context, opts *DeleteOptions) (err error) { // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -49,7 +51,7 @@ func deleteRun(opts *DeleteOptions) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentDeleteVmDisks(context.TODO()).DeleteVmDiskCommand(body).Execute() + response, err := myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentDeleteVmDisks(ctx).DeleteVmDiskCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/vm/disk/resize/resize.go b/cmd/project/vm/disk/resize/resize.go index 627f42da..ed33f1c1 100644 --- a/cmd/project/vm/disk/resize/resize.go +++ b/cmd/project/vm/disk/resize/resize.go @@ -1,7 +1,6 @@ package resize import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -28,7 +27,7 @@ func NewCmdResize() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return resizeRun(&opts) + return resizeRun(cmd, &opts) }, } @@ -38,7 +37,10 @@ func NewCmdResize() *cobra.Command { return &cmd } -func resizeRun(opts *ResizeOptions) (err error) { +func resizeRun(cmd *cobra.Command, opts *ResizeOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -49,7 +51,7 @@ func resizeRun(opts *ResizeOptions) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.StandaloneVMDisksAPI.StandalonevmdisksUpdateSize(context.TODO()).UpdateStandaloneVmDiskSizeCommand(body).Execute() + response, err := myApiClient.Client.StandaloneVMDisksAPI.StandalonevmdisksUpdateSize(ctx).UpdateStandaloneVmDiskSizeCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/vm/list/list.go b/cmd/project/vm/list/list.go index d010e268..f39f974d 100644 --- a/cmd/project/vm/list/list.go +++ b/cmd/project/vm/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/api" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/config" @@ -92,7 +91,7 @@ func NewCmdList() *cobra.Command { if err != nil { return } - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -103,8 +102,8 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { - vms, err := ListVMs(opts) +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + vms, err := ListVMs(cmd, opts) if err == nil { return out.PrintResults(vms, listFields) } @@ -112,11 +111,14 @@ func listRun(opts *ListOptions) (err error) { return } -func ListVMs(opts *ListOptions) (vms []taikuncore.StandaloneVmsListForDetailsDto, err error) { +func ListVMs(cmd *cobra.Command, opts *ListOptions) (vms []taikuncore.StandaloneVmsListForDetailsDto, err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() - myRequest := myApiClient.Client.StandaloneAPI.StandaloneDetails(context.TODO(), opts.ProjectID) + myRequest := myApiClient.Client.StandaloneAPI.StandaloneDetails(ctx, opts.ProjectID) if config.SortBy != "" { myRequest = myRequest.SortBy(config.SortBy).SortDirection(*api.GetSortDirection()) } diff --git a/cmd/project/vm/publicip/disable/disable.go b/cmd/project/vm/publicip/disable/disable.go index 11ec8abf..b8822cb2 100644 --- a/cmd/project/vm/publicip/disable/disable.go +++ b/cmd/project/vm/publicip/disable/disable.go @@ -1,13 +1,13 @@ package disable import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type DisableOptions struct { @@ -26,14 +26,17 @@ func NewCmdDisable() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return disableRun(&opts) + return disableRun(cmd, &opts) }, } return &cmd } -func disableRun(opts *DisableOptions) (err error) { +func disableRun(cmd *cobra.Command, opts *DisableOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -45,7 +48,7 @@ func disableRun(opts *DisableOptions) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.StandaloneAPI.StandaloneIpManagement(context.TODO()).StandAloneVmIpManagementCommand(body).Execute() + response, err := myApiClient.Client.StandaloneAPI.StandaloneIpManagement(ctx).StandAloneVmIpManagementCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/vm/publicip/enable/enable.go b/cmd/project/vm/publicip/enable/enable.go index 76fe3d05..14338260 100644 --- a/cmd/project/vm/publicip/enable/enable.go +++ b/cmd/project/vm/publicip/enable/enable.go @@ -1,13 +1,13 @@ package enable import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type EnableOptions struct { @@ -26,14 +26,17 @@ func NewCmdEnable() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return enableRun(&opts) + return enableRun(cmd, &opts) }, } return &cmd } -func enableRun(opts *EnableOptions) (err error) { +func enableRun(cmd *cobra.Command, opts *EnableOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -45,7 +48,7 @@ func enableRun(opts *EnableOptions) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.StandaloneAPI.StandaloneIpManagement(context.TODO()).StandAloneVmIpManagementCommand(body).Execute() + response, err := myApiClient.Client.StandaloneAPI.StandaloneIpManagement(ctx).StandAloneVmIpManagementCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/vm/reboot/reboot.go b/cmd/project/vm/reboot/reboot.go index ab252a95..c9ece097 100644 --- a/cmd/project/vm/reboot/reboot.go +++ b/cmd/project/vm/reboot/reboot.go @@ -1,13 +1,13 @@ package reboot import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type RebootOptions struct { @@ -27,7 +27,7 @@ func NewCmdReboot() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return rebootRun(&opts) + return rebootRun(cmd, &opts) }, } @@ -36,7 +36,10 @@ func NewCmdReboot() *cobra.Command { return &cmd } -func rebootRun(opts *RebootOptions) (err error) { +func rebootRun(cmd *cobra.Command, opts *RebootOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -50,7 +53,7 @@ func rebootRun(opts *RebootOptions) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.StandaloneActionsAPI.StandaloneactionsReboot(context.TODO()).RebootStandAloneVmCommand(body).Execute() + response, err := myApiClient.Client.StandaloneActionsAPI.StandaloneactionsReboot(ctx).RebootStandAloneVmCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/vm/remove/remove.go b/cmd/project/vm/remove/remove.go index 3cf2cee0..7f0cce38 100644 --- a/cmd/project/vm/remove/remove.go +++ b/cmd/project/vm/remove/remove.go @@ -1,7 +1,6 @@ package remove import ( - "context" "errors" "fmt" tk "github.com/itera-io/taikungoclient" @@ -42,7 +41,7 @@ func NewCmdDelete() *cobra.Command { return errors.New("must set one of --vm-ids and --all-project flags") } } - return deleteRun(&opts) + return deleteRun(cmd, &opts) }, Aliases: cmdutils.DeleteAliases, } @@ -53,14 +52,16 @@ func NewCmdDelete() *cobra.Command { return &cmd } -func deleteRun(opts *DeleteOptions) error { +func deleteRun(cmd *cobra.Command, opts *DeleteOptions) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() myApiClient := tk.NewClient() body := taikuncore.ProjectDeploymentDeleteVmsCommand{ ProjectId: &opts.ProjectID, } if opts.DeleteAll { - allVMs, err := list.ListVMs(&list.ListOptions{ProjectID: opts.ProjectID}) + allVMs, err := list.ListVMs(cmd, &list.ListOptions{ProjectID: opts.ProjectID}) if err != nil { return err } @@ -78,7 +79,7 @@ func deleteRun(opts *DeleteOptions) error { body.VmIds = opts.VMIDs } - response, err := myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentDeleteVms(context.TODO()).ProjectDeploymentDeleteVmsCommand(body).Execute() + response, err := myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentDeleteVms(ctx).ProjectDeploymentDeleteVmsCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/vm/repair/repair.go b/cmd/project/vm/repair/repair.go index 61a2e6a4..d44ce354 100644 --- a/cmd/project/vm/repair/repair.go +++ b/cmd/project/vm/repair/repair.go @@ -1,13 +1,13 @@ package repair import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type RepairOptions struct { @@ -26,14 +26,17 @@ func NewCmdRepair() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return repairRun(&opts) + return repairRun(cmd, &opts) }, } return &cmd } -func repairRun(opts *RepairOptions) (err error) { +func repairRun(cmd *cobra.Command, opts *RepairOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -43,7 +46,7 @@ func repairRun(opts *RepairOptions) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentRepairVm(context.TODO()).ProjectDeploymentRepairVmCommand(body).Execute() + response, err := myApiClient.Client.ProjectDeploymentAPI.ProjectDeploymentRepairVm(ctx).ProjectDeploymentRepairVmCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/vm/shelve/shelve.go b/cmd/project/vm/shelve/shelve.go index 52c5b049..6bdebe9f 100644 --- a/cmd/project/vm/shelve/shelve.go +++ b/cmd/project/vm/shelve/shelve.go @@ -1,13 +1,13 @@ package shelve import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type ShelveOptions struct { @@ -26,14 +26,17 @@ func NewCmdShelve() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return shelveRun(&opts) + return shelveRun(cmd, &opts) }, } return &cmd } -func shelveRun(opts *ShelveOptions) (err error) { +func shelveRun(cmd *cobra.Command, opts *ShelveOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -43,7 +46,7 @@ func shelveRun(opts *ShelveOptions) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.StandaloneActionsAPI.StandaloneactionsShelve(context.TODO()).ShelveStandAloneVmCommand(body).Execute() + response, err := myApiClient.Client.StandaloneActionsAPI.StandaloneactionsShelve(ctx).ShelveStandAloneVmCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/vm/start/start.go b/cmd/project/vm/start/start.go index db346fe4..f2b0e609 100644 --- a/cmd/project/vm/start/start.go +++ b/cmd/project/vm/start/start.go @@ -1,13 +1,13 @@ package start import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type StartOptions struct { @@ -26,14 +26,17 @@ func NewCmdStart() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return startRun(&opts) + return startRun(cmd, &opts) }, } return &cmd } -func startRun(opts *StartOptions) (err error) { +func startRun(cmd *cobra.Command, opts *StartOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -43,7 +46,7 @@ func startRun(opts *StartOptions) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.StandaloneActionsAPI.StandaloneactionsStart(context.TODO()).StartStandaloneVmCommand(body).Execute() + response, err := myApiClient.Client.StandaloneActionsAPI.StandaloneactionsStart(ctx).StartStandaloneVmCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/vm/status/status.go b/cmd/project/vm/status/status.go index 26f2706e..5a6747e2 100644 --- a/cmd/project/vm/status/status.go +++ b/cmd/project/vm/status/status.go @@ -1,12 +1,12 @@ package status import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type StatusOptions struct { @@ -25,19 +25,22 @@ func NewCmdStatus() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return statusRun(&opts) + return statusRun(cmd, &opts) }, } return &cmd } -func statusRun(opts *StatusOptions) (err error) { +func statusRun(cmd *cobra.Command, opts *StatusOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.StandaloneActionsAPI.StandaloneactionsStatus(context.TODO(), opts.StandaloneVMID).Execute() + data, response, err := myApiClient.Client.StandaloneActionsAPI.StandaloneactionsStatus(ctx, opts.StandaloneVMID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/vm/stop/stop.go b/cmd/project/vm/stop/stop.go index 1e0168c3..2fe44a21 100644 --- a/cmd/project/vm/stop/stop.go +++ b/cmd/project/vm/stop/stop.go @@ -1,13 +1,13 @@ package stop import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type StopOptions struct { @@ -26,14 +26,17 @@ func NewCmdStop() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return stopRun(&opts) + return stopRun(cmd, &opts) }, } return &cmd } -func stopRun(opts *StopOptions) (err error) { +func stopRun(cmd *cobra.Command, opts *StopOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -43,7 +46,7 @@ func stopRun(opts *StopOptions) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.StandaloneActionsAPI.StandaloneactionsStop(context.TODO()).StopStandaloneVmCommand(body).Execute() + response, err := myApiClient.Client.StandaloneActionsAPI.StandaloneactionsStop(ctx).StopStandaloneVmCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/project/vm/unshelve/unshelve.go b/cmd/project/vm/unshelve/unshelve.go index ab2a55a7..318a865a 100644 --- a/cmd/project/vm/unshelve/unshelve.go +++ b/cmd/project/vm/unshelve/unshelve.go @@ -1,13 +1,13 @@ package unshelve import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type UnshelveOptions struct { @@ -26,14 +26,17 @@ func NewCmdUnshelve() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return unshelveRun(&opts) + return unshelveRun(cmd, &opts) }, } return &cmd } -func unshelveRun(opts *UnshelveOptions) (err error) { +func unshelveRun(cmd *cobra.Command, opts *UnshelveOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -43,7 +46,7 @@ func unshelveRun(opts *UnshelveOptions) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.StandaloneActionsAPI.StandaloneactionsUnshelve(context.TODO()).UnshelveStandaloneVmCommand(body).Execute() + response, err := myApiClient.Client.StandaloneActionsAPI.StandaloneactionsUnshelve(ctx).UnshelveStandaloneVmCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/repository/delete/delete.go b/cmd/repository/delete/delete.go index 7143f051..b4c29dab 100644 --- a/cmd/repository/delete/delete.go +++ b/cmd/repository/delete/delete.go @@ -1,7 +1,6 @@ package deleterepo import ( - "context" "fmt" "github.com/itera-io/taikun-cli/cmd/cmdutils" @@ -24,7 +23,7 @@ func NewCmdDelete() *cobra.Command { Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { opts.RepoName = args[0] - return deleteRun(opts) + return deleteRun(cmd, opts) }, } @@ -33,7 +32,10 @@ func NewCmdDelete() *cobra.Command { return &cmd } -func deleteRun(opts DeleteOptions) (err error) { +func deleteRun(cmd *cobra.Command, opts DeleteOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -46,7 +48,7 @@ func deleteRun(opts DeleteOptions) (err error) { foundId = -1 // Try private - privateCommand := myApiClient.Client.AppRepositoriesAPI.RepositoryAvailableList(context.TODO()).IsPrivate(true).Search(opts.RepoName) + privateCommand := myApiClient.Client.AppRepositoriesAPI.RepositoryAvailableList(ctx).IsPrivate(true).Search(opts.RepoName) if orgID != 0 { privateCommand = privateCommand.OrganizationId(orgID) } @@ -68,7 +70,7 @@ func deleteRun(opts DeleteOptions) (err error) { AppRepoId: &foundId, } - response, err = myApiClient.Client.AppRepositoriesAPI.RepositoryDelete(context.TODO()).DeleteRepositoryCommand(command).Execute() + response, err = myApiClient.Client.AppRepositoriesAPI.RepositoryDelete(ctx).DeleteRepositoryCommand(command).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/repository/disable/disable.go b/cmd/repository/disable/disable.go index 08c9798e..d6dcebf3 100644 --- a/cmd/repository/disable/disable.go +++ b/cmd/repository/disable/disable.go @@ -1,7 +1,6 @@ package disable import ( - "context" "fmt" "github.com/itera-io/taikun-cli/cmd/cmdutils" @@ -25,7 +24,7 @@ func NewCmdDisable() *cobra.Command { Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { opts.RepoName = args[0] - return disableRun(opts) + return disableRun(cmd, opts) }, } @@ -34,7 +33,10 @@ func NewCmdDisable() *cobra.Command { return &cmd } -func disableRun(opts DisableOptions) (err error) { +func disableRun(cmd *cobra.Command, opts DisableOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -47,7 +49,7 @@ func disableRun(opts DisableOptions) (err error) { foundId = "" // Try recommended - recommendCommand := myApiClient.Client.AppRepositoriesAPI.RepositoryRecommendedList(context.TODO()) + recommendCommand := myApiClient.Client.AppRepositoriesAPI.RepositoryRecommendedList(ctx) if orgID != 0 { recommendCommand = recommendCommand.OrganizationId(orgID) } @@ -63,7 +65,7 @@ func disableRun(opts DisableOptions) (err error) { } // Try public if foundId == "" { - publicCommand := myApiClient.Client.AppRepositoriesAPI.RepositoryAvailableList(context.TODO()).IsPrivate(false).Search(opts.RepoName) + publicCommand := myApiClient.Client.AppRepositoriesAPI.RepositoryAvailableList(ctx).IsPrivate(false).Search(opts.RepoName) if orgID != 0 { publicCommand = publicCommand.OrganizationId(orgID) } @@ -81,7 +83,7 @@ func disableRun(opts DisableOptions) (err error) { // Try private if foundId == "" { - privateCommand := myApiClient.Client.AppRepositoriesAPI.RepositoryAvailableList(context.TODO()).IsPrivate(true).Search(opts.RepoName) + privateCommand := myApiClient.Client.AppRepositoriesAPI.RepositoryAvailableList(ctx).IsPrivate(true).Search(opts.RepoName) if orgID != 0 { privateCommand = privateCommand.OrganizationId(orgID) } @@ -109,7 +111,7 @@ func disableRun(opts DisableOptions) (err error) { command.OrganizationId = &orgID } - response, err = myApiClient.Client.AppRepositoriesAPI.RepositoryUnbind(context.TODO()).UnbindAppRepositoryCommand(command).Execute() + response, err = myApiClient.Client.AppRepositoriesAPI.RepositoryUnbind(ctx).UnbindAppRepositoryCommand(command).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/repository/enable/enable.go b/cmd/repository/enable/enable.go index 9e7f887e..bc8f023e 100644 --- a/cmd/repository/enable/enable.go +++ b/cmd/repository/enable/enable.go @@ -1,8 +1,6 @@ package enable import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" tk "github.com/itera-io/taikungoclient" @@ -25,7 +23,7 @@ func NewCmdEnable() *cobra.Command { Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { opts.RepoName = args[0] - return enableRun(opts) + return enableRun(cmd, opts) }, } @@ -35,7 +33,10 @@ func NewCmdEnable() *cobra.Command { return &cmd } -func enableRun(opts EnableOptions) (err error) { +func enableRun(cmd *cobra.Command, opts EnableOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -55,7 +56,7 @@ func enableRun(opts EnableOptions) (err error) { command.OrganizationId = *taikuncore.NewNullableInt32(&orgID) } - response, err := myApiClient.Client.AppRepositoriesAPI.RepositoryBind(context.TODO()).BindAppRepositoryCommand(command).Execute() + response, err := myApiClient.Client.AppRepositoriesAPI.RepositoryBind(ctx).BindAppRepositoryCommand(command).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/repository/import/import.go b/cmd/repository/import/import.go index 53a00310..e919444f 100644 --- a/cmd/repository/import/import.go +++ b/cmd/repository/import/import.go @@ -1,8 +1,6 @@ package importrepo import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" tk "github.com/itera-io/taikungoclient" @@ -25,7 +23,7 @@ func NewCmdEnable() *cobra.Command { Short: "Import a repository. Specify repository name and a URL and import it.", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - return enableRun(args[0], opts) + return enableRun(cmd, args[0], opts) }, } @@ -41,7 +39,10 @@ func NewCmdEnable() *cobra.Command { return &cmd } -func enableRun(name string, opts importOpts) (err error) { +func enableRun(cmd *cobra.Command, name string, opts importOpts) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -64,7 +65,7 @@ func enableRun(name string, opts importOpts) (err error) { command.SetPassword(opts.Password) } - response, err := myApiClient.Client.AppRepositoriesAPI.RepositoryImport(context.TODO()).ImportRepoCommand(command).Execute() + response, err := myApiClient.Client.AppRepositoriesAPI.RepositoryImport(ctx).ImportRepoCommand(command).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/repository/list-private/list.go b/cmd/repository/list-private/list.go index 88d210b5..2644a153 100644 --- a/cmd/repository/list-private/list.go +++ b/cmd/repository/list-private/list.go @@ -1,8 +1,6 @@ package list_private import ( - "context" - "github.com/itera-io/taikun-cli/api" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/config" @@ -38,7 +36,7 @@ func NewCmdList() *cobra.Command { Short: "List available private repositories", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -51,7 +49,10 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -61,7 +62,7 @@ func listRun(opts *ListOptions) (err error) { var repositories = make([]taikuncore.ArtifactRepositoryDto, 0) // Private - myRequest := myApiClient.Client.AppRepositoriesAPI.RepositoryAvailableList(context.TODO()).IsPrivate(true) + myRequest := myApiClient.Client.AppRepositoriesAPI.RepositoryAvailableList(ctx).IsPrivate(true) if config.SortBy != "" { myRequest = myRequest.SortBy(config.SortBy).SortDirection(*api.GetSortDirection()) } diff --git a/cmd/repository/list-public/list.go b/cmd/repository/list-public/list.go index 034c8a0b..c11f6b12 100644 --- a/cmd/repository/list-public/list.go +++ b/cmd/repository/list-public/list.go @@ -1,8 +1,6 @@ package list_public import ( - "context" - "github.com/itera-io/taikun-cli/api" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/config" @@ -38,7 +36,7 @@ func NewCmdList() *cobra.Command { Short: "List available public repositories", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -51,7 +49,10 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -61,7 +62,7 @@ func listRun(opts *ListOptions) (err error) { var repositories = make([]taikuncore.ArtifactRepositoryDto, 0) // Public - myRequest := myApiClient.Client.AppRepositoriesAPI.RepositoryAvailableList(context.TODO()).IsPrivate(false) + myRequest := myApiClient.Client.AppRepositoriesAPI.RepositoryAvailableList(ctx).IsPrivate(false) if config.SortBy != "" { myRequest = myRequest.SortBy(config.SortBy).SortDirection(*api.GetSortDirection()) } diff --git a/cmd/repository/list-recommend/list.go b/cmd/repository/list-recommend/list.go index 039a831f..0d3a37a6 100644 --- a/cmd/repository/list-recommend/list.go +++ b/cmd/repository/list-recommend/list.go @@ -1,8 +1,6 @@ package list_recommend import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -35,7 +33,7 @@ func NewCmdList() *cobra.Command { Short: "List available managed repositories", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -46,7 +44,10 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -56,7 +57,7 @@ func listRun(opts *ListOptions) (err error) { var repositories = make([]taikuncore.ArtifactRepositoryDto, 0) // Recommended - myquery := myApiClient.Client.AppRepositoriesAPI.RepositoryRecommendedList(context.TODO()) + myquery := myApiClient.Client.AppRepositoriesAPI.RepositoryRecommendedList(ctx) if orgID != 0 { myquery = myquery.OrganizationId(orgID) } diff --git a/cmd/robot/checker/checker.go b/cmd/robot/checker/checker.go index f2eba33e..73a18cb8 100644 --- a/cmd/robot/checker/checker.go +++ b/cmd/robot/checker/checker.go @@ -1,7 +1,6 @@ package checker import ( - "context" "fmt" "time" @@ -27,7 +26,7 @@ func NewCmdCreateRobotChecker() *cobra.Command { Short: "Dry-run for create a new robot user", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - return checkCreateRobot(args[0], &opts) + return checkCreateRobot(cmd, args[0], &opts) }, } @@ -42,7 +41,10 @@ func NewCmdCreateRobotChecker() *cobra.Command { return &cmd } -func checkCreateRobot(robotName string, opts *CreateOptions) (err error) { +func checkCreateRobot(cmd *cobra.Command, robotName string, opts *CreateOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // converting time from string to time.time expiresAt, err := time.Parse(time.RFC3339, opts.ExpiresAt) if err != nil { @@ -59,7 +61,7 @@ func checkCreateRobot(robotName string, opts *CreateOptions) (err error) { ExpiresAt: *taikuncore.NewNullableTime(&expiresAt), } - response, err := myApiClient.Client.RobotAPI.RobotChecker(context.TODO()).CheckerRobotCommand(body).Execute() + response, err := myApiClient.Client.RobotAPI.RobotChecker(ctx).CheckerRobotCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/robot/create/create.go b/cmd/robot/create/create.go index 72942ec8..79b09131 100644 --- a/cmd/robot/create/create.go +++ b/cmd/robot/create/create.go @@ -1,7 +1,6 @@ package create import ( - "context" "fmt" "time" @@ -32,7 +31,7 @@ func NewCmdCreateRobot() *cobra.Command { Short: "Create a new robot user", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - return createRobot(args[0], &opts) + return createRobot(cmd, args[0], &opts) }, } @@ -49,7 +48,10 @@ func NewCmdCreateRobot() *cobra.Command { return &cmd } -func createRobot(robotName string, opts *CreateOptions) (err error) { +func createRobot(cmd *cobra.Command, robotName string, opts *CreateOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // converting time from string to time.time expiresAt, err := time.Parse(time.RFC3339, opts.ExpiresAt) if err != nil { @@ -68,7 +70,7 @@ func createRobot(robotName string, opts *CreateOptions) (err error) { Ips: opts.IPs, } - data, response, err := myApiClient.Client.RobotAPI.RobotCreate(context.TODO()).CreateRobotUserCommand(body).Execute() + data, response, err := myApiClient.Client.RobotAPI.RobotCreate(ctx).CreateRobotUserCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/robot/delete/delete.go b/cmd/robot/delete/delete.go index cff1fd20..04435847 100644 --- a/cmd/robot/delete/delete.go +++ b/cmd/robot/delete/delete.go @@ -1,11 +1,10 @@ package delete import ( - "context" - "github.com/itera-io/taikun-cli/utils/out" tk "github.com/itera-io/taikungoclient" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdDeleteRobot() *cobra.Command { @@ -14,17 +13,20 @@ func NewCmdDeleteRobot() *cobra.Command { Short: "Delete group", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - return deleteRobot(args[0]) + return deleteRobot(cmd, args[0]) }, } return &cmd } -func deleteRobot(robotID string) (err error) { +func deleteRobot(cmd *cobra.Command, robotID string) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - response, err := myApiClient.Client.RobotAPI.RobotDelete(context.TODO(), robotID).Execute() + response, err := myApiClient.Client.RobotAPI.RobotDelete(ctx, robotID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/robot/details/details.go b/cmd/robot/details/details.go index 92fa3cae..575450c2 100644 --- a/cmd/robot/details/details.go +++ b/cmd/robot/details/details.go @@ -1,8 +1,6 @@ package details import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -37,7 +35,7 @@ func NewCmdDetails() *cobra.Command { Short: "Get robot user details", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return detailsRun() + return detailsRun(cmd) }, } @@ -46,9 +44,12 @@ func NewCmdDetails() *cobra.Command { return &cmd } -func detailsRun() (err error) { +func detailsRun(cmd *cobra.Command) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - data, response, err := myApiClient.Client.RobotAPI.RobotDetails(context.TODO()).Execute() + data, response, err := myApiClient.Client.RobotAPI.RobotDetails(ctx).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/robot/list/list.go b/cmd/robot/list/list.go index 94f70ae6..f029a4f3 100644 --- a/cmd/robot/list/list.go +++ b/cmd/robot/list/list.go @@ -1,8 +1,6 @@ package list import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -52,7 +50,7 @@ func NewCmdListRobots() *cobra.Command { if err != nil { return err } - return listRobots(accountID, &opts) + return listRobots(cmd, accountID, &opts) }, } @@ -62,11 +60,14 @@ func NewCmdListRobots() *cobra.Command { return &cmd } -func listRobots(accountID int32, opts *ListOptions) (err error) { +func listRobots(cmd *cobra.Command, accountID int32, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() var robots = make([]taikuncore.RobotUsersListDto, 0) - req := myApiClient.Client.RobotAPI.RobotList(context.TODO()).AccountId(accountID).OrganizationId(opts.OrganizationID) + req := myApiClient.Client.RobotAPI.RobotList(ctx).AccountId(accountID).OrganizationId(opts.OrganizationID) data, response, err := req.Execute() if err != nil { return tk.CreateError(response, err) diff --git a/cmd/robot/regenerate/regenerate.go b/cmd/robot/regenerate/regenerate.go index 1dde71e6..43d4c774 100644 --- a/cmd/robot/regenerate/regenerate.go +++ b/cmd/robot/regenerate/regenerate.go @@ -1,13 +1,13 @@ package regenerate import ( - "context" "fmt" "time" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type RegenerateOptions struct { @@ -22,7 +22,7 @@ func NewCmdRobotRegenerateTokens() *cobra.Command { Short: "Regenerate tokens for a robot user", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - return regenerateRobotTokens(args[0], &opts) + return regenerateRobotTokens(cmd, args[0], &opts) }, } @@ -32,7 +32,10 @@ func NewCmdRobotRegenerateTokens() *cobra.Command { return &cmd } -func regenerateRobotTokens(robotID string, opts *RegenerateOptions) (err error) { +func regenerateRobotTokens(cmd *cobra.Command, robotID string, opts *RegenerateOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // converting time from string to time.time expiresAt, err := time.Parse(time.RFC3339, opts.ExpiresAt) if err != nil { @@ -46,7 +49,7 @@ func regenerateRobotTokens(robotID string, opts *RegenerateOptions) (err error) ExpiresAt: *taikuncore.NewNullableTime(&expiresAt), } - data, response, err := myApiClient.Client.RobotAPI.RobotRegenerate(context.TODO()).RegenerateRobotTokenCommand(body).Execute() + data, response, err := myApiClient.Client.RobotAPI.RobotRegenerate(ctx).RegenerateRobotTokenCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/robot/scope-list/scope-list.go b/cmd/robot/scope-list/scope-list.go index 8ece332d..bef90326 100644 --- a/cmd/robot/scope-list/scope-list.go +++ b/cmd/robot/scope-list/scope-list.go @@ -1,13 +1,12 @@ package scope_list import ( - "context" - "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" "github.com/itera-io/taikun-cli/utils/out/fields" tk "github.com/itera-io/taikungoclient" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) var listFields = fields.New( @@ -25,17 +24,20 @@ func NewCmdScopeList() *cobra.Command { Short: "List available scopes for robot users", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return listRobots() + return listRobots(cmd) }, } return &cmd } -func listRobots() (err error) { +func listRobots(cmd *cobra.Command) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - data, response, err := myApiClient.Client.RobotAPI.RobotScopeList(context.TODO()).Execute() + data, response, err := myApiClient.Client.RobotAPI.RobotScopeList(ctx).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/robot/status/status.go b/cmd/robot/status/status.go index d48a2d12..35e2bb1e 100644 --- a/cmd/robot/status/status.go +++ b/cmd/robot/status/status.go @@ -1,12 +1,11 @@ package status import ( - "context" - "github.com/itera-io/taikun-cli/utils/out" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type StatusOptions struct { @@ -21,7 +20,7 @@ func NewCmdRobotStatus() *cobra.Command { Short: "Activate or deactivate robot user", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - return changeRobotStatus(args[0], &opts) + return changeRobotStatus(cmd, args[0], &opts) }, } @@ -31,7 +30,10 @@ func NewCmdRobotStatus() *cobra.Command { return &cmd } -func changeRobotStatus(robotID string, opts *StatusOptions) (err error) { +func changeRobotStatus(cmd *cobra.Command, robotID string, opts *StatusOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.RobotUserStatusManagementCommand{ @@ -39,7 +41,7 @@ func changeRobotStatus(robotID string, opts *StatusOptions) (err error) { Mode: *taikuncore.NewNullableString(&opts.Mode), } - response, err := myApiClient.Client.RobotAPI.RobotStatus(context.TODO()).RobotUserStatusManagementCommand(body).Execute() + response, err := myApiClient.Client.RobotAPI.RobotStatus(ctx).RobotUserStatusManagementCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/robot/update-scope/update-scope.go b/cmd/robot/update-scope/update-scope.go index ddf7afbd..255b1095 100644 --- a/cmd/robot/update-scope/update-scope.go +++ b/cmd/robot/update-scope/update-scope.go @@ -1,12 +1,11 @@ package updatescope import ( - "context" - "github.com/itera-io/taikun-cli/utils/out" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type UpdateScopeOptions struct { @@ -21,7 +20,7 @@ func NewCmdUpdateScope() *cobra.Command { Short: "Update robot user scope", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - return updateRobotScope(args[0], &opts) + return updateRobotScope(cmd, args[0], &opts) }, } @@ -31,7 +30,10 @@ func NewCmdUpdateScope() *cobra.Command { return &cmd } -func updateRobotScope(robotID string, opts *UpdateScopeOptions) (err error) { +func updateRobotScope(cmd *cobra.Command, robotID string, opts *UpdateScopeOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.UpdateRobotScopeCommand{ @@ -39,7 +41,7 @@ func updateRobotScope(robotID string, opts *UpdateScopeOptions) (err error) { Scopes: opts.Scopes, } - response, err := myApiClient.Client.RobotAPI.RobotUpdateScope(context.TODO()).UpdateRobotScopeCommand(body).Execute() + response, err := myApiClient.Client.RobotAPI.RobotUpdateScope(ctx).UpdateRobotScopeCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/robot/update/update.go b/cmd/robot/update/update.go index 6b524ec0..cdff4453 100644 --- a/cmd/robot/update/update.go +++ b/cmd/robot/update/update.go @@ -1,7 +1,6 @@ package update import ( - "context" "fmt" "time" @@ -9,6 +8,7 @@ import ( tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type UpdateOptions struct { @@ -26,7 +26,7 @@ func NewCmdUpdate() *cobra.Command { Short: "Update robot user", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - return updateRobot(args[0], &opts) + return updateRobot(cmd, args[0], &opts) }, } @@ -38,7 +38,10 @@ func NewCmdUpdate() *cobra.Command { return &cmd } -func updateRobot(robotID string, opts *UpdateOptions) (err error) { +func updateRobot(cmd *cobra.Command, robotID string, opts *UpdateOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.EditRobotUserCommand{ @@ -62,7 +65,7 @@ func updateRobot(robotID string, opts *UpdateOptions) (err error) { body.SetExpiresAt(expiresAt) } - response, err := myApiClient.Client.RobotAPI.RobotUpdate(context.TODO()).EditRobotUserCommand(body).Execute() + response, err := myApiClient.Client.RobotAPI.RobotUpdate(ctx).EditRobotUserCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/root/root.go b/cmd/root/root.go index d2fde6ad..ebe22192 100644 --- a/cmd/root/root.go +++ b/cmd/root/root.go @@ -37,6 +37,7 @@ func NewCmdRoot() *cobra.Command { Long: `Manage Taikun resources from the command line.`, SilenceUsage: true, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + config.APITimeout = config.ParseTimeout() if !config.OutputFormatIsValid() { return cmderr.ErrUnknownOutputFormat } diff --git a/cmd/showback/credential/add/add.go b/cmd/showback/credential/add/add.go index 26f48b19..b4bf1472 100644 --- a/cmd/showback/credential/add/add.go +++ b/cmd/showback/credential/add/add.go @@ -1,8 +1,6 @@ package add import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -64,7 +62,7 @@ func NewCmdAdd() *cobra.Command { Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { opts.Name = args[0] - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -85,7 +83,10 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -105,7 +106,7 @@ func addRun(opts *AddOptions) (err error) { } // Execute a query into the API + graceful exit - data, response, err := myApiClient.ShowbackClient.ShowbackCredentialsAPI.ShowbackcredentialsCreate(context.TODO()).CreateShowbackCredentialCommand(body).Execute() + data, response, err := myApiClient.ShowbackClient.ShowbackCredentialsAPI.ShowbackcredentialsCreate(ctx).CreateShowbackCredentialCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/showback/credential/list/list.go b/cmd/showback/credential/list/list.go index 41d1998d..fd0acf96 100644 --- a/cmd/showback/credential/list/list.go +++ b/cmd/showback/credential/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/api" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/config" @@ -58,7 +57,7 @@ func NewCmdList() *cobra.Command { Short: "List showback credentials", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -72,7 +71,10 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -83,7 +85,7 @@ func listRun(opts *ListOptions) (err error) { myApiClient := tk.NewClient() // Prepare the arguments for the query - myRequest := myApiClient.ShowbackClient.ShowbackCredentialsAPI.ShowbackcredentialsList(context.TODO()) + myRequest := myApiClient.ShowbackClient.ShowbackCredentialsAPI.ShowbackcredentialsList(ctx) if config.SortBy != "" { myRequest = myRequest.SortBy(config.SortBy).SortDirection(*api.GetSortDirection()) } diff --git a/cmd/showback/credential/lock/lock.go b/cmd/showback/credential/lock/lock.go index fd90f831..0a68f525 100644 --- a/cmd/showback/credential/lock/lock.go +++ b/cmd/showback/credential/lock/lock.go @@ -1,14 +1,13 @@ package lock import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikunshowback "github.com/itera-io/taikungoclient/showbackclient" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdLock() *cobra.Command { @@ -21,14 +20,17 @@ func NewCmdLock() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return lockRun(showbackCredentialID) + return lockRun(cmd, showbackCredentialID) }, } return &cmd } -func lockRun(showbackCredentialID int32) (err error) { +func lockRun(cmd *cobra.Command, showbackCredentialID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -39,7 +41,7 @@ func lockRun(showbackCredentialID int32) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.ShowbackClient.ShowbackCredentialsAPI.ShowbackcredentialsLockManagement(context.TODO()).ShowbackCredentialLockCommand(body).Execute() + response, err := myApiClient.ShowbackClient.ShowbackCredentialsAPI.ShowbackcredentialsLockManagement(ctx).ShowbackCredentialLockCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/showback/credential/remove/remove.go b/cmd/showback/credential/remove/remove.go index 38f22a58..0a379491 100644 --- a/cmd/showback/credential/remove/remove.go +++ b/cmd/showback/credential/remove/remove.go @@ -15,11 +15,15 @@ func NewCmdDelete() *cobra.Command { Short: "Delete one or more showback credentials", Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() ids, err := cmdutils.ArgsToNumericalIDs(args) if err != nil { return cmderr.ErrIDArgumentNotANumber } - return cmdutils.DeleteMultiple(ids, deleteRun) + return cmdutils.DeleteMultiple(ids, func(id int32) error { + return deleteRun(ctx, id) + }) }, Aliases: cmdutils.DeleteAliases, } @@ -27,12 +31,12 @@ func NewCmdDelete() *cobra.Command { return &cmd } -func deleteRun(showbackCredentialID int32) (err error) { +func deleteRun(ctx context.Context, showbackCredentialID int32) (err error) { // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - response, err := myApiClient.ShowbackClient.ShowbackCredentialsAPI.ShowbackcredentialsDelete(context.TODO(), showbackCredentialID).Execute() + response, err := myApiClient.ShowbackClient.ShowbackCredentialsAPI.ShowbackcredentialsDelete(ctx, showbackCredentialID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/showback/credential/unlock/unlock.go b/cmd/showback/credential/unlock/unlock.go index 04f1ba21..87e2d210 100644 --- a/cmd/showback/credential/unlock/unlock.go +++ b/cmd/showback/credential/unlock/unlock.go @@ -1,14 +1,13 @@ package unlock import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikunshowback "github.com/itera-io/taikungoclient/showbackclient" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdUnlock() *cobra.Command { @@ -21,14 +20,17 @@ func NewCmdUnlock() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return unlockRun(showbackCredentialID) + return unlockRun(cmd, showbackCredentialID) }, } return &cmd } -func unlockRun(showbackCredentialID int32) (err error) { +func unlockRun(cmd *cobra.Command, showbackCredentialID int32) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -39,7 +41,7 @@ func unlockRun(showbackCredentialID int32) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.ShowbackClient.ShowbackCredentialsAPI.ShowbackcredentialsLockManagement(context.TODO()).ShowbackCredentialLockCommand(body).Execute() + response, err := myApiClient.ShowbackClient.ShowbackCredentialsAPI.ShowbackcredentialsLockManagement(ctx).ShowbackCredentialLockCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/showback/rule/add/add.go b/cmd/showback/rule/add/add.go index c9efffa1..ad831401 100644 --- a/cmd/showback/rule/add/add.go +++ b/cmd/showback/rule/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -86,7 +85,7 @@ func NewCmdAdd() *cobra.Command { if err := cmdutils.CheckFlagValue("type", opts.Type, types.EPrometheusTypes); err != nil { return err } - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -119,7 +118,10 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) error { +func addRun(cmd *cobra.Command, opts *AddOptions) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -152,7 +154,7 @@ func addRun(opts *AddOptions) error { } // Execute a query into the API + graceful exit - data, response, err := myApiClient.ShowbackClient.ShowbackRulesAPI.ShowbackrulesCreate(context.TODO()).CreateShowbackRuleCommand(body).Execute() + data, response, err := myApiClient.ShowbackClient.ShowbackRulesAPI.ShowbackrulesCreate(ctx).CreateShowbackRuleCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/showback/rule/label/add/add.go b/cmd/showback/rule/label/add/add.go index 39e3bb22..7efcf261 100644 --- a/cmd/showback/rule/label/add/add.go +++ b/cmd/showback/rule/label/add/add.go @@ -1,8 +1,6 @@ package add import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/cmd/showback/rule/label/list" @@ -31,7 +29,7 @@ func NewCmdAdd() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -44,12 +42,15 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) error { +func addRun(cmd *cobra.Command, opts *AddOptions) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Prepare the arguments for the query - showbackRule, err := list.GetShowbackRuleByID(opts.ShowbackRuleID) + showbackRule, err := list.GetShowbackRuleByID(cmd, opts.ShowbackRuleID) if err != nil { return err } @@ -71,7 +72,7 @@ func addRun(opts *AddOptions) error { } // Execute a query into the API + graceful exit - response, err := myApiClient.ShowbackClient.ShowbackRulesAPI.ShowbackrulesUpdate(context.TODO()).UpdateShowbackRuleCommand(body).Execute() + response, err := myApiClient.ShowbackClient.ShowbackRulesAPI.ShowbackrulesUpdate(ctx).UpdateShowbackRuleCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/showback/rule/label/clear/clear.go b/cmd/showback/rule/label/clear/clear.go index 816333d3..b453a5a0 100644 --- a/cmd/showback/rule/label/clear/clear.go +++ b/cmd/showback/rule/label/clear/clear.go @@ -1,8 +1,6 @@ package clear import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/showback/rule/label/list" "github.com/itera-io/taikun-cli/utils/out" @@ -10,6 +8,7 @@ import ( tk "github.com/itera-io/taikungoclient" taikunshowback "github.com/itera-io/taikungoclient/showbackclient" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type ClearOptions struct { @@ -28,19 +27,22 @@ func NewCmdClear() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return clearRun(&opts) + return clearRun(cmd, &opts) }, } return &cmd } -func clearRun(opts *ClearOptions) error { +func clearRun(cmd *cobra.Command, opts *ClearOptions) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Prepare the arguments for the query - showbackRule, err := list.GetShowbackRuleByID(opts.ShowbackRuleID) + showbackRule, err := list.GetShowbackRuleByID(cmd, opts.ShowbackRuleID) if err != nil { return err } @@ -58,7 +60,7 @@ func clearRun(opts *ClearOptions) error { } // Execute a query into the API + graceful exit - response, err := myApiClient.ShowbackClient.ShowbackRulesAPI.ShowbackrulesUpdate(context.TODO()).UpdateShowbackRuleCommand(body).Execute() + response, err := myApiClient.ShowbackClient.ShowbackRulesAPI.ShowbackrulesUpdate(ctx).UpdateShowbackRuleCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/showback/rule/label/list/list.go b/cmd/showback/rule/label/list/list.go index 944294c6..98287998 100644 --- a/cmd/showback/rule/label/list/list.go +++ b/cmd/showback/rule/label/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -41,7 +40,7 @@ func NewCmdList() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -52,8 +51,8 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { - showbackRule, err := GetShowbackRuleByID(opts.ShowbackRuleID) +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + showbackRule, err := GetShowbackRuleByID(cmd, opts.ShowbackRuleID) if err != nil { return } @@ -67,12 +66,15 @@ func listRun(opts *ListOptions) (err error) { return out.PrintResults(labels, listFields) } -func GetShowbackRuleByID(showbackRuleID int32) (showbackRule *taikunshowback.ShowbackRulesListDto, err error) { +func GetShowbackRuleByID(cmd *cobra.Command, showbackRuleID int32) (showbackRule *taikunshowback.ShowbackRulesListDto, err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - data, response, err := myApiClient.ShowbackClient.ShowbackRulesAPI.ShowbackrulesList(context.TODO()).Id(showbackRuleID).Execute() + data, response, err := myApiClient.ShowbackClient.ShowbackRulesAPI.ShowbackrulesList(ctx).Id(showbackRuleID).Execute() if err != nil { return nil, tk.CreateError(response, err) } diff --git a/cmd/showback/rule/list/list.go b/cmd/showback/rule/list/list.go index 2b76c72c..54844825 100644 --- a/cmd/showback/rule/list/list.go +++ b/cmd/showback/rule/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/api" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/config" @@ -67,7 +66,7 @@ func NewCmdList() *cobra.Command { Short: "List showback rules", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -81,7 +80,10 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -92,7 +94,7 @@ func listRun(opts *ListOptions) (err error) { myApiClient := tk.NewClient() // Prepare the arguments for the query - myRequest := myApiClient.ShowbackClient.ShowbackRulesAPI.ShowbackrulesList(context.TODO()) + myRequest := myApiClient.ShowbackClient.ShowbackRulesAPI.ShowbackrulesList(ctx) if config.SortBy != "" { myRequest = myRequest.SortBy(config.SortBy).SortDirection(*api.GetSortDirection()) } diff --git a/cmd/showback/rule/remove/remove.go b/cmd/showback/rule/remove/remove.go index 45936647..7e5f6450 100644 --- a/cmd/showback/rule/remove/remove.go +++ b/cmd/showback/rule/remove/remove.go @@ -15,11 +15,15 @@ func NewCmdDelete() *cobra.Command { Short: "Delete one or more showback rules", Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() ids, err := cmdutils.ArgsToNumericalIDs(args) if err != nil { return cmderr.ErrIDArgumentNotANumber } - return cmdutils.DeleteMultiple(ids, deleteRun) + return cmdutils.DeleteMultiple(ids, func(id int32) error { + return deleteRun(ctx, id) + }) }, Aliases: cmdutils.DeleteAliases, } @@ -27,12 +31,12 @@ func NewCmdDelete() *cobra.Command { return &cmd } -func deleteRun(showbackRuleID int32) (err error) { +func deleteRun(ctx context.Context, showbackRuleID int32) (err error) { // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - response, err := myApiClient.ShowbackClient.ShowbackRulesAPI.ShowbackrulesDelete(context.TODO(), showbackRuleID).Execute() + response, err := myApiClient.ShowbackClient.ShowbackRulesAPI.ShowbackrulesDelete(ctx, showbackRuleID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/slackconfiguration/add/add.go b/cmd/slackconfiguration/add/add.go index e23e3e96..4b5edcd6 100644 --- a/cmd/slackconfiguration/add/add.go +++ b/cmd/slackconfiguration/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -40,7 +39,7 @@ func NewCmdAdd() *cobra.Command { if err := cmdutils.CheckFlagValue("type", opts.Type, types.SlackTypes); err != nil { return err } - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -61,7 +60,10 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -83,7 +85,7 @@ func addRun(opts *AddOptions) (err error) { } // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.SlackAPI.SlackCreate(context.TODO()).CreateSlackConfigurationCommand(body).Execute() + data, response, err := myApiClient.Client.SlackAPI.SlackCreate(ctx).CreateSlackConfigurationCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/slackconfiguration/list/list.go b/cmd/slackconfiguration/list/list.go index 16066d40..8a96fa3b 100644 --- a/cmd/slackconfiguration/list/list.go +++ b/cmd/slackconfiguration/list/list.go @@ -1,8 +1,6 @@ package list import ( - "context" - "github.com/itera-io/taikun-cli/api" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/config" @@ -53,7 +51,7 @@ func NewCmdList() *cobra.Command { Short: "List Slack configurations", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -67,7 +65,10 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -78,7 +79,7 @@ func listRun(opts *ListOptions) (err error) { myApiClient := tk.NewClient() // Prepare the arguments for the query - myRequest := myApiClient.Client.SlackAPI.SlackList(context.TODO()) + myRequest := myApiClient.Client.SlackAPI.SlackList(ctx) if opts.OrganizationID != 0 { myRequest = myRequest.OrganizationId(opts.OrganizationID) } diff --git a/cmd/slackconfiguration/remove/remove.go b/cmd/slackconfiguration/remove/remove.go index ef9c93ea..790df996 100644 --- a/cmd/slackconfiguration/remove/remove.go +++ b/cmd/slackconfiguration/remove/remove.go @@ -16,11 +16,15 @@ func NewCmdDelete() *cobra.Command { Short: "Delete one or more Slack configurations", Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() ids, err := cmdutils.ArgsToNumericalIDs(args) if err != nil { return cmderr.ErrIDArgumentNotANumber } - return cmdutils.DeleteMultiple(ids, deleteRun) + return cmdutils.DeleteMultiple(ids, func(id int32) error { + return deleteRun(ctx, id) + }) }, Aliases: cmdutils.DeleteAliases, } @@ -28,7 +32,7 @@ func NewCmdDelete() *cobra.Command { return &cmd } -func deleteRun(slackConfigID int32) (err error) { +func deleteRun(ctx context.Context, slackConfigID int32) (err error) { // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -38,7 +42,7 @@ func deleteRun(slackConfigID int32) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.SlackAPI.SlackDeleteMultiple(context.TODO()).DeleteSlackConfigCommand(body).Execute() + response, err := myApiClient.Client.SlackAPI.SlackDeleteMultiple(ctx).DeleteSlackConfigCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/standaloneprofile/add/add.go b/cmd/standaloneprofile/add/add.go index 50c41d16..0b92bb15 100644 --- a/cmd/standaloneprofile/add/add.go +++ b/cmd/standaloneprofile/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -40,7 +39,7 @@ func NewCmdAdd() *cobra.Command { Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { opts.Name = args[0] - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -55,7 +54,10 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -75,7 +77,7 @@ func addRun(opts *AddOptions) (err error) { } // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.StandaloneProfileAPI.StandaloneprofileCreate(context.TODO()).StandAloneProfileCreateCommand(body).Execute() + data, response, err := myApiClient.Client.StandaloneProfileAPI.StandaloneprofileCreate(ctx).StandAloneProfileCreateCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/standaloneprofile/list/list.go b/cmd/standaloneprofile/list/list.go index a3a5df81..9ef6033e 100644 --- a/cmd/standaloneprofile/list/list.go +++ b/cmd/standaloneprofile/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/api" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/config" @@ -49,7 +48,7 @@ func NewCmdList() *cobra.Command { Short: "List standalone profiles", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -63,7 +62,10 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err @@ -71,7 +73,7 @@ func listRun(opts *ListOptions) (err error) { opts.OrganizationID = orgID myApiClient := tk.NewClient() - myRequest := myApiClient.Client.StandaloneProfileAPI.StandaloneprofileList(context.TODO()) + myRequest := myApiClient.Client.StandaloneProfileAPI.StandaloneprofileList(ctx) if opts.OrganizationID != 0 { myRequest = myRequest.OrganizationId(opts.OrganizationID) } diff --git a/cmd/standaloneprofile/lock/lock.go b/cmd/standaloneprofile/lock/lock.go index 1027b1e4..ba4a3acf 100644 --- a/cmd/standaloneprofile/lock/lock.go +++ b/cmd/standaloneprofile/lock/lock.go @@ -1,13 +1,13 @@ package lock import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type LockOptions struct { @@ -26,14 +26,17 @@ func NewCmdLock() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return lockRun(&opts) + return lockRun(cmd, &opts) }, } return &cmd } -func lockRun(opts *LockOptions) (err error) { +func lockRun(cmd *cobra.Command, opts *LockOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -44,7 +47,7 @@ func lockRun(opts *LockOptions) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.StandaloneProfileAPI.StandaloneprofileLockManagement(context.TODO()).StandAloneProfileLockManagementCommand(body).Execute() + response, err := myApiClient.Client.StandaloneProfileAPI.StandaloneprofileLockManagement(ctx).StandAloneProfileLockManagementCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/standaloneprofile/remove/remove.go b/cmd/standaloneprofile/remove/remove.go index a3b8e098..0e798bdb 100644 --- a/cmd/standaloneprofile/remove/remove.go +++ b/cmd/standaloneprofile/remove/remove.go @@ -16,11 +16,15 @@ func NewCmdDelete() *cobra.Command { Short: "Delete one or more standalone profiles", Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() ids, err := cmdutils.ArgsToNumericalIDs(args) if err != nil { return cmderr.ErrIDArgumentNotANumber } - return cmdutils.DeleteMultiple(ids, deleteRun) + return cmdutils.DeleteMultiple(ids, func(id int32) error { + return deleteRun(ctx, id) + }) }, Aliases: cmdutils.DeleteAliases, } @@ -28,7 +32,7 @@ func NewCmdDelete() *cobra.Command { return &cmd } -func deleteRun(standaloneProfileID int32) (err error) { +func deleteRun(ctx context.Context, standaloneProfileID int32) (err error) { // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -38,7 +42,7 @@ func deleteRun(standaloneProfileID int32) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.StandaloneProfileAPI.StandaloneprofileDelete(context.TODO()).DeleteStandAloneProfileCommand(body).Execute() + response, err := myApiClient.Client.StandaloneProfileAPI.StandaloneprofileDelete(ctx).DeleteStandAloneProfileCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/standaloneprofile/rename/rename.go b/cmd/standaloneprofile/rename/rename.go index d1f87a6c..0e3f9926 100644 --- a/cmd/standaloneprofile/rename/rename.go +++ b/cmd/standaloneprofile/rename/rename.go @@ -1,7 +1,6 @@ package rename import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" @@ -28,7 +27,7 @@ func NewCmdRename() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return renameRun(&opts) + return renameRun(cmd, &opts) }, } @@ -38,7 +37,10 @@ func NewCmdRename() *cobra.Command { return &cmd } -func renameRun(opts *RenameOptions) (err error) { +func renameRun(cmd *cobra.Command, opts *RenameOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -49,7 +51,7 @@ func renameRun(opts *RenameOptions) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.StandaloneProfileAPI.StandaloneprofileEdit(context.TODO()).StandAloneProfileUpdateCommand(body).Execute() + response, err := myApiClient.Client.StandaloneProfileAPI.StandaloneprofileEdit(ctx).StandAloneProfileUpdateCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/standaloneprofile/securitygroup/add/add.go b/cmd/standaloneprofile/securitygroup/add/add.go index 1bbc752d..8809be64 100644 --- a/cmd/standaloneprofile/securitygroup/add/add.go +++ b/cmd/standaloneprofile/securitygroup/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "errors" "fmt" tk "github.com/itera-io/taikungoclient" @@ -72,7 +71,7 @@ func NewCmdAdd() *cobra.Command { return fmt.Errorf("must set --min-port and --max-port with %s protocol", opts.Protocol) } } - return addRun(&opts) + return addRun(cmd, &opts) }, } @@ -95,7 +94,10 @@ func NewCmdAdd() *cobra.Command { return &cmd } -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -111,7 +113,7 @@ func addRun(opts *AddOptions) (err error) { } // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.SecurityGroupAPI.SecuritygroupCreate(context.TODO()).CreateSecurityGroupCommand(body).Execute() + data, response, err := myApiClient.Client.SecurityGroupAPI.SecuritygroupCreate(ctx).CreateSecurityGroupCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/standaloneprofile/securitygroup/list/list.go b/cmd/standaloneprofile/securitygroup/list/list.go index 928778e9..75046138 100644 --- a/cmd/standaloneprofile/securitygroup/list/list.go +++ b/cmd/standaloneprofile/securitygroup/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -53,7 +52,7 @@ func NewCmdList() *cobra.Command { if err != nil { return } - return listRun(&opts) + return listRun(cmd, &opts) }, Aliases: cmdutils.ListAliases, } @@ -63,12 +62,15 @@ func NewCmdList() *cobra.Command { return &cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - data, response, err := myApiClient.Client.SecurityGroupAPI.SecuritygroupList(context.TODO(), opts.StandAloneProfileID).Execute() + data, response, err := myApiClient.Client.SecurityGroupAPI.SecuritygroupList(ctx, opts.StandAloneProfileID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/standaloneprofile/securitygroup/remove/remove.go b/cmd/standaloneprofile/securitygroup/remove/remove.go index 731eb8c1..c90f02cd 100644 --- a/cmd/standaloneprofile/securitygroup/remove/remove.go +++ b/cmd/standaloneprofile/securitygroup/remove/remove.go @@ -15,11 +15,15 @@ func NewCmdDelete() *cobra.Command { Short: "Delete one or more security groups", Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() ids, err := cmdutils.ArgsToNumericalIDs(args) if err != nil { return cmderr.ErrIDArgumentNotANumber } - return cmdutils.DeleteMultiple(ids, deleteRun) + return cmdutils.DeleteMultiple(ids, func(id int32) error { + return deleteRun(ctx, id) + }) }, Aliases: cmdutils.DeleteAliases, } @@ -27,12 +31,12 @@ func NewCmdDelete() *cobra.Command { return &cmd } -func deleteRun(securityGroupID int32) (err error) { +func deleteRun(ctx context.Context, securityGroupID int32) (err error) { // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - response, err := myApiClient.Client.SecurityGroupAPI.SecuritygroupDelete(context.TODO(), securityGroupID).Execute() + response, err := myApiClient.Client.SecurityGroupAPI.SecuritygroupDelete(ctx, securityGroupID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/standaloneprofile/unlock/unlock.go b/cmd/standaloneprofile/unlock/unlock.go index 7da44c75..578c4dff 100644 --- a/cmd/standaloneprofile/unlock/unlock.go +++ b/cmd/standaloneprofile/unlock/unlock.go @@ -1,13 +1,13 @@ package unlock import ( - "context" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/types" tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type UnlockOptions struct { @@ -26,14 +26,17 @@ func NewCmdUnlock() *cobra.Command { if err != nil { return cmderr.ErrIDArgumentNotANumber } - return unlockRun(&opts) + return unlockRun(cmd, &opts) }, } return &cmd } -func unlockRun(opts *UnlockOptions) (err error) { +func unlockRun(cmd *cobra.Command, opts *UnlockOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() @@ -44,7 +47,7 @@ func unlockRun(opts *UnlockOptions) (err error) { } // Execute a query into the API + graceful exit - response, err := myApiClient.Client.StandaloneProfileAPI.StandaloneprofileLockManagement(context.TODO()).StandAloneProfileLockManagementCommand(body).Execute() + response, err := myApiClient.Client.StandaloneProfileAPI.StandaloneprofileLockManagement(ctx).StandAloneProfileLockManagementCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/user/add/add.go b/cmd/user/add/add.go index a8a83db6..e0b4627a 100644 --- a/cmd/user/add/add.go +++ b/cmd/user/add/add.go @@ -1,8 +1,6 @@ package add import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" @@ -85,7 +83,7 @@ func NewCmdAdd() *cobra.Command { Short: "Add a user", RunE: func(cmd *cobra.Command, args []string) error { opts.Username = args[0] - return addRun(&opts) + return addRun(cmd, &opts) }, Args: cobra.ExactArgs(1), } @@ -108,7 +106,10 @@ func NewCmdAdd() *cobra.Command { } // addRun calls the API with a custom body from arguments. It than prints the result. -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() body := taikuncore.CreateUserCommand{} body.SetDisplayName(opts.DisplayName) @@ -116,7 +117,7 @@ func addRun(opts *AddOptions) (err error) { body.SetAccountId(opts.AccountID) body.SetUsername(opts.Username) - data, response, err := myApiClient.Client.UsersAPI.UsersCreate(context.TODO()).CreateUserCommand(body).Execute() + data, response, err := myApiClient.Client.UsersAPI.UsersCreate(ctx).CreateUserCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/user/complete/complete.go b/cmd/user/complete/complete.go index 8caf7953..79f0b103 100644 --- a/cmd/user/complete/complete.go +++ b/cmd/user/complete/complete.go @@ -1,8 +1,6 @@ package complete import ( - "context" - "github.com/itera-io/taikun-cli/cmd/cmdutils" tk "github.com/itera-io/taikungoclient" "github.com/spf13/cobra" @@ -11,8 +9,10 @@ import ( func CompleteArgsWithUserID(cmd *cobra.Command) { cmdutils.SetArgsCompletionFunc(cmd, func(cmd *cobra.Command, args []string, toComplete string) []string { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() myApiClient := tk.NewClient() - data, _, err := myApiClient.Client.UsersAPI.UsersDropdown(context.TODO()).Execute() + data, _, err := myApiClient.Client.UsersAPI.UsersDropdown(ctx).Execute() if err != nil { return nil } diff --git a/cmd/user/info/info.go b/cmd/user/info/info.go index 36b9bf66..2d5a68bd 100644 --- a/cmd/user/info/info.go +++ b/cmd/user/info/info.go @@ -1,13 +1,12 @@ package info import ( - "context" - "github.com/itera-io/taikun-cli/utils/out" "github.com/itera-io/taikun-cli/utils/out/field" "github.com/itera-io/taikun-cli/utils/out/fields" tk "github.com/itera-io/taikungoclient" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) var infoFields = fields.New( @@ -54,16 +53,19 @@ func NewCmdInfo() *cobra.Command { Short: "Retrieve information about the current user", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return infoRun() + return infoRun(cmd) }, } return &cmd } -func infoRun() error { +func infoRun(cmd *cobra.Command) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - data, response, err := myApiClient.Client.UsersAPI.UsersUserInfo(context.TODO()).Execute() + data, response, err := myApiClient.Client.UsersAPI.UsersUserInfo(ctx).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/user/list/list.go b/cmd/user/list/list.go index b43f4857..3ede9944 100644 --- a/cmd/user/list/list.go +++ b/cmd/user/list/list.go @@ -50,7 +50,7 @@ func NewCmdList() *cobra.Command { Use: "list", Short: "List users", RunE: func(cmd *cobra.Command, args []string) error { - return listRun(&opts) + return listRun(cmd, &opts) }, Args: cobra.NoArgs, Aliases: cmdutils.ListAliases, @@ -63,14 +63,15 @@ func NewCmdList() *cobra.Command { return cmd } -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { orgID, err := cmdutils.ResolveOrgID(opts.OrganizationID, cmdutils.IsRobotAuth()) if err != nil { return err } opts.OrganizationID = orgID - - users, err := ListUsers(opts) + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + users, err := ListUsers(ctx, opts) if err != nil { return err } @@ -78,18 +79,18 @@ func listRun(opts *ListOptions) (err error) { return out.PrintResults(users, ListFields) } -func ListUsers(opts *ListOptions) ([]interface{}, error) { +func ListUsers(ctx context.Context, opts *ListOptions) ([]interface{}, error) { myApiClient := tk.NewClient() // Get current user's account ID for detail fetches - userInfo, response, err := myApiClient.Client.UsersAPI.UsersUserInfo(context.TODO()).Execute() + userInfo, response, err := myApiClient.Client.UsersAPI.UsersUserInfo(ctx).Execute() if err != nil { return nil, tk.CreateError(response, err) } accountID := userInfo.Data.Account.AccountId // Fetch user IDs via dropdown (paginated) - myRequest := myApiClient.Client.UsersAPI.UsersDropdown(context.TODO()) + myRequest := myApiClient.Client.UsersAPI.UsersDropdown(ctx) if opts.OrganizationID != 0 { myRequest = myRequest.OrganizationId(opts.OrganizationID) } @@ -120,7 +121,7 @@ func ListUsers(opts *ListOptions) ([]interface{}, error) { results := make([]interface{}, 0, len(dropdownUsers)) for _, u := range dropdownUsers { userID := u.GetId() - detail, response, err := myApiClient.Client.AccountsAPI.AccountsAccountUserDetails(context.TODO(), accountID, userID).Execute() + detail, response, err := myApiClient.Client.AccountsAPI.AccountsAccountUserDetails(ctx, accountID, userID).Execute() if err != nil { _ = tk.CreateError(response, err) continue diff --git a/cmd/user/project/list/list.go b/cmd/user/project/list/list.go index 99b1c903..83772530 100644 --- a/cmd/user/project/list/list.go +++ b/cmd/user/project/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "fmt" "github.com/itera-io/taikun-cli/cmd/cmdutils" @@ -58,7 +57,7 @@ func NewCmdList() *cobra.Command { Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { opts.UserID = args[0] - return listRun(&opts) + return listRun(cmd, &opts) }, } @@ -69,9 +68,12 @@ func NewCmdList() *cobra.Command { } // listRun calls the API, gets the Users and prints their bound projects. -func listRun(opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() - data, response, err := myApiClient.Client.ProjectsAPI.ProjectsDropdown(context.TODO()).UserId(opts.UserID).Execute() + data, response, err := myApiClient.Client.ProjectsAPI.ProjectsDropdown(ctx).UserId(opts.UserID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/user/remove/remove.go b/cmd/user/remove/remove.go index 9e06d9b6..7a7747c7 100644 --- a/cmd/user/remove/remove.go +++ b/cmd/user/remove/remove.go @@ -2,6 +2,7 @@ package remove import ( "context" + "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/cmd/user/complete" "github.com/itera-io/taikun-cli/utils/out" @@ -15,7 +16,11 @@ func NewCmdDelete() *cobra.Command { Short: "Delete one or more users", Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - return cmdutils.DeleteMultipleStringID(args, deleteRun) + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + return cmdutils.DeleteMultipleStringID(args, func(id string) error { + return deleteRun(ctx, id) + }) }, Aliases: cmdutils.DeleteAliases, } @@ -25,9 +30,9 @@ func NewCmdDelete() *cobra.Command { return &cmd } -func deleteRun(userID string) (err error) { +func deleteRun(ctx context.Context, userID string) (err error) { myApiClient := tk.NewClient() - response, err := myApiClient.Client.UsersAPI.UsersDelete(context.TODO(), userID).Execute() + response, err := myApiClient.Client.UsersAPI.UsersDelete(ctx, userID).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/virtualcluster/add/add.go b/cmd/virtualcluster/add/add.go index dcd229c9..2f3629f0 100644 --- a/cmd/virtualcluster/add/add.go +++ b/cmd/virtualcluster/add/add.go @@ -1,7 +1,6 @@ package add import ( - "context" "fmt" "github.com/itera-io/taikun-cli/cmd/cmderr" "github.com/itera-io/taikun-cli/utils/out" @@ -9,6 +8,7 @@ import ( tk "github.com/itera-io/taikungoclient" taikuncore "github.com/itera-io/taikungoclient/client" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) type AddOptions struct { @@ -30,7 +30,7 @@ func NewCmdAdd() *cobra.Command { } opts.virtualClusterName = args[1] - return addRun(&opts) + return addRun(cmd, &opts) }, Args: cobra.ExactArgs(2), } @@ -41,12 +41,15 @@ func NewCmdAdd() *cobra.Command { } // addRun calls the API with a custom body from arguments. It than prints the result. -func addRun(opts *AddOptions) (err error) { +func addRun(cmd *cobra.Command, opts *AddOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + myApiClient := tk.NewClient() if opts.alertingProfileId == 0 { // Get alerting profile ID of parent - data, response, err := myApiClient.Client.ProjectsAPI.ProjectsList(context.TODO()).Id(opts.parentProjectId).Execute() + data, response, err := myApiClient.Client.ProjectsAPI.ProjectsList(ctx).Id(opts.parentProjectId).Execute() if err != nil { return tk.CreateError(response, err) } @@ -64,7 +67,7 @@ func addRun(opts *AddOptions) (err error) { AlertingProfileId: *taikuncore.NewNullableInt32(&opts.alertingProfileId), } - response, err := myApiClient.Client.VirtualClusterAPI.VirtualClusterCreate(context.TODO()).CreateVirtualClusterCommand(body).Execute() + response, err := myApiClient.Client.VirtualClusterAPI.VirtualClusterCreate(ctx).CreateVirtualClusterCommand(body).Execute() if err != nil { return tk.CreateError(response, err) } diff --git a/cmd/virtualcluster/list/list.go b/cmd/virtualcluster/list/list.go index 5034df70..916c34d8 100644 --- a/cmd/virtualcluster/list/list.go +++ b/cmd/virtualcluster/list/list.go @@ -1,7 +1,6 @@ package list import ( - "context" "github.com/itera-io/taikun-cli/api" "github.com/itera-io/taikun-cli/cmd/cmdutils" "github.com/itera-io/taikun-cli/config" @@ -59,7 +58,7 @@ func NewCmdList() *cobra.Command { Use: "list [project-id]", Short: "List virtual clusters for a project", RunE: func(cmd *cobra.Command, args []string) error { - return listRun(args[0], &opts) // List user by ID + return listRun(cmd, args[0], &opts) // List user by ID }, Args: cobra.ExactArgs(1), Aliases: cmdutils.ListAliases, @@ -73,7 +72,10 @@ func NewCmdList() *cobra.Command { } // listRun calls the API, gets the Users and prints them in a table. -func listRun(projectIdString string, opts *ListOptions) (err error) { +func listRun(cmd *cobra.Command, projectIdString string, opts *ListOptions) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + projectId, err := types.Atoi32(projectIdString) if err != nil { return err @@ -81,7 +83,7 @@ func listRun(projectIdString string, opts *ListOptions) (err error) { // Prepare the request myApiClient := tk.NewClient() - myRequest := myApiClient.Client.VirtualClusterAPI.VirtualClusterList(context.TODO(), projectId) + myRequest := myApiClient.Client.VirtualClusterAPI.VirtualClusterList(ctx, projectId) // Set Sorting if set in command line options if config.SortBy != "" { diff --git a/cmd/virtualcluster/remove/remove.go b/cmd/virtualcluster/remove/remove.go index 405e243b..59592a64 100644 --- a/cmd/virtualcluster/remove/remove.go +++ b/cmd/virtualcluster/remove/remove.go @@ -26,6 +26,8 @@ func NewCmdDelete() *cobra.Command { Short: "Delete one or more virtual projects", Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() optsList := make([]*DeleteOptions, len(args)) for i, arg := range args { projectID, err := types.Atoi32(arg) @@ -36,7 +38,7 @@ func NewCmdDelete() *cobra.Command { ProjectID: projectID, } } - return deleteMultiple(optsList) + return deleteMultiple(ctx, optsList) }, Aliases: cmdutils.DeleteAliases, } @@ -44,11 +46,11 @@ func NewCmdDelete() *cobra.Command { return cmd } -func deleteMultiple(optsList []*DeleteOptions) error { +func deleteMultiple(ctx context.Context, optsList []*DeleteOptions) error { errorOccured := false for _, opts := range optsList { - if err := deleteRun(opts); err != nil { + if err := deleteRun(ctx, opts); err != nil { errorOccured = true _, _ = fmt.Fprintln(os.Stderr, err) @@ -63,13 +65,13 @@ func deleteMultiple(optsList []*DeleteOptions) error { return nil } -func deleteRun(opts *DeleteOptions) (err error) { +func deleteRun(ctx context.Context, opts *DeleteOptions) (err error) { myApiClient := tk.NewClient() body := taikuncore.DeleteVirtualClusterCommand{ ProjectId: &opts.ProjectID, } - request, err := myApiClient.Client.VirtualClusterAPI.VirtualClusterDelete(context.TODO()).DeleteVirtualClusterCommand(body).Execute() + request, err := myApiClient.Client.VirtualClusterAPI.VirtualClusterDelete(ctx).DeleteVirtualClusterCommand(body).Execute() if err != nil { return tk.CreateError(request, err) } diff --git a/cmd/whoami/whoami.go b/cmd/whoami/whoami.go index 159cd2ab..7a00e525 100644 --- a/cmd/whoami/whoami.go +++ b/cmd/whoami/whoami.go @@ -1,10 +1,10 @@ package whoami import ( - "context" "fmt" tk "github.com/itera-io/taikungoclient" "github.com/spf13/cobra" + "github.com/itera-io/taikun-cli/cmd/cmdutils" ) func NewCmdWhoami() *cobra.Command { @@ -13,18 +13,21 @@ func NewCmdWhoami() *cobra.Command { Short: "Print username", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return whoamiRun() + return whoamiRun(cmd) }, } return cmd } -func whoamiRun() (err error) { +func whoamiRun(cmd *cobra.Command) (err error) { + ctx, cancel := cmdutils.APIContext(cmd) + defer cancel() + // Create and authenticated client to the Taikun API myApiClient := tk.NewClient() // Execute a query into the API + graceful exit - data, _, err := myApiClient.Client.UsersAPI.UsersUserInfo(context.TODO()).Execute() + data, _, err := myApiClient.Client.UsersAPI.UsersUserInfo(ctx).Execute() if err != nil { return err } diff --git a/config/context.go b/config/context.go new file mode 100644 index 00000000..38247c83 --- /dev/null +++ b/config/context.go @@ -0,0 +1,28 @@ +package config + +import ( + "os" + "strconv" + "time" +) + +const ( + EnvTimeout = "TAIKUN_API_TIMEOUT" + DefaultTimeout = 120 +) + +var APITimeout time.Duration + +func ParseTimeout() time.Duration { + raw := os.Getenv(EnvTimeout) + if raw == "" { + return time.Duration(DefaultTimeout) * time.Second + } + + seconds, err := strconv.Atoi(raw) + if err != nil || seconds <= 0 { + return time.Duration(DefaultTimeout) * time.Second + } + + return time.Duration(seconds) * time.Second +} diff --git a/go.mod b/go.mod index 0d7a84cd..b59a74c0 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/itera-io/taikun-cli -go 1.24.0 +go 1.25.0 require ( github.com/go-openapi/strfmt v0.24.0 @@ -21,8 +21,8 @@ require ( github.com/oklog/ulid v1.3.1 // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/spf13/pflag v1.0.10 // indirect - go.mongodb.org/mongo-driver v1.17.4 // indirect - golang.org/x/net v0.46.0 // indirect - golang.org/x/sys v0.37.0 // indirect - golang.org/x/text v0.30.0 // indirect + go.mongodb.org/mongo-driver v1.17.9 // indirect + golang.org/x/net v0.56.0 // indirect + golang.org/x/sys v0.46.0 // indirect + golang.org/x/text v0.38.0 // indirect ) diff --git a/go.sum b/go.sum index a46994d8..a3bc7514 100644 --- a/go.sum +++ b/go.sum @@ -36,14 +36,14 @@ github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk= github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= -go.mongodb.org/mongo-driver v1.17.4 h1:jUorfmVzljjr0FLzYQsGP8cgN/qzzxlY9Vh0C9KFXVw= -go.mongodb.org/mongo-driver v1.17.4/go.mod h1:Hy04i7O2kC4RS06ZrhPRqj/u4DTYkFDAAccj+rVKqgQ= -golang.org/x/net v0.46.0 h1:giFlY12I07fugqwPuWJi68oOnpfqFnJIJzaIIm2JVV4= -golang.org/x/net v0.46.0/go.mod h1:Q9BGdFy1y4nkUwiLvT5qtyhAnEHgnQ/zd8PfU6nc210= -golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ= -golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= -golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k= -golang.org/x/text v0.30.0/go.mod h1:yDdHFIX9t+tORqspjENWgzaCVXgk0yYnYuSZ8UzzBVM= +go.mongodb.org/mongo-driver v1.17.9 h1:IexDdCuuNJ3BHrELgBlyaH9p60JXAvdzWR128q+U5tU= +go.mongodb.org/mongo-driver v1.17.9/go.mod h1:LlOhpH5NUEfhxcAwG0UEkMqwYcc4JU18gtCdGudk/tQ= +golang.org/x/net v0.56.0 h1:Rw8j/hFzGvJUZwNBXnAtf5sVDVt+65SK2C7IxCxZt5o= +golang.org/x/net v0.56.0/go.mod h1:D3Ku6r+V6JROoZK144D2XfMHFcMq/0zSfLelVTCFKec= +golang.org/x/sys v0.46.0 h1:noSf2Fq6F8DBgS+LysIkx7rIExoNHJsxOAtPp4rthXw= +golang.org/x/sys v0.46.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= +golang.org/x/text v0.38.0 h1:sXmwo9DwP3OK9EZ7PqAdaooSGozfl/3a6/xJcbzPRhE= +golang.org/x/text v0.38.0/go.mod h1:YXZt3QhHUKYT53r2lLKFIVi6Ao1jdzrTR/KQ09qyxF4= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 574cfa32..8e222f86 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,12 @@ package main import ( - "github.com/itera-io/taikun-cli/cmd/root" + "context" "os" + "os/signal" + "syscall" + + "github.com/itera-io/taikun-cli/cmd/root" ) // Goreleaser sets this variable when building binaries @@ -11,13 +15,17 @@ var ( ) func main() { + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) + defer stop() + rootCmd := root.NewCmdRoot() + rootCmd.SetContext(ctx) // Activate and format a version root flag based on Goreleaser variables. rootCmd.Version = version rootCmd.SetVersionTemplate("Taikun CLI {{printf \"version %s\" .Version}}\n") - if err := rootCmd.Execute(); err != nil { + if err := rootCmd.ExecuteContext(ctx); err != nil { os.Exit(1) } }