From c9ca794ee564baf859dd228f9e95441360e7879c Mon Sep 17 00:00:00 2001 From: Mahesh N Date: Fri, 8 Nov 2024 15:10:20 +0530 Subject: [PATCH] update atf --- internal/acceptance_test/helper.go | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/internal/acceptance_test/helper.go b/internal/acceptance_test/helper.go index 3505caee..d8686915 100644 --- a/internal/acceptance_test/helper.go +++ b/internal/acceptance_test/helper.go @@ -7,6 +7,7 @@ import ( "log" "os" "strconv" + "time" api_client "github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/client" "github.com/HewlettPackard/hpegl-vmaas-terraform-resources/pkg/constants" @@ -61,14 +62,26 @@ func getAPIClient() (*api_client.APIClient, api_client.Configuration) { DefaultQueryParams: map[string]string{}, } cmpAPIClient := api_client.NewAPIClient(&cfg) + cmpAPIClient.CMPToken = cmpDetails.AccessToken + cmpAPIClient.TokenExpiry = cmpDetails.ValidTill cmpAPIClient.SetMetaFnAndVersion(nil, 0, func(ctx *context.Context, meta interface{}) { // Initialise token handler - cmpDetails, err := brokerClient.GetCMPDetails(*ctx) - if err != nil { - log.Printf("[ERROR] Unable to fetch token for CMP client: %s", err) - } else { - *ctx = context.WithValue(*ctx, api_client.ContextAccessToken, cmpDetails.AccessToken) + tokenExpiry := cmpAPIClient.TokenExpiry/1000 - 15 + token := cmpAPIClient.CMPToken + // Token is about to expire and get new + if tokenExpiry < time.Now().Unix() { + cmpDetails, err := brokerClient.GetCMPDetails(*ctx) + if err != nil { + log.Printf("[ERROR] Unable to fetch token for CMP client: %s", err) + panic("Unable to renew token") + } else { + token = cmpDetails.AccessToken + cmpAPIClient.CMPToken = cmpDetails.AccessToken + cmpAPIClient.TokenExpiry = cmpDetails.ValidTill + } + } + *ctx = context.WithValue(*ctx, api_client.ContextAccessToken, token) }) ctx = context.WithValue(ctx, api_client.ContextAccessToken, cmpDetails.AccessToken) err = cmpAPIClient.SetCMPVersion(ctx)