From ee5be5ace7d687ae876ed1d71631df8e83356bed Mon Sep 17 00:00:00 2001 From: Mahesh N Date: Fri, 8 Nov 2024 15:03:37 +0530 Subject: [PATCH] set the token and expiry time --- go.mod | 2 +- go.sum | 2 ++ pkg/utils/meta.go | 24 +++++++++++++++++++----- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index cdee7a4d..683c9086 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/HewlettPackard/hpegl-vmaas-terraform-resources go 1.21 require ( - github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.16 + github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.17-0.20241108090636-05469301103d github.com/golang/mock v1.6.0 github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 github.com/hashicorp/terraform-plugin-docs v0.9.0 diff --git a/go.sum b/go.sum index 6b38fb0c..6b03e85a 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk= dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.16 h1:AxP9A1zwr8L7kahiiKOBb1uPJ9RiG2bXXhqrJuIysO4= github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.16/go.mod h1:tsKOAAbEQnpXAzqjcZUGzRiVp9QfRViKoTOdIltIMFI= +github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.17-0.20241108090636-05469301103d h1:0J7uB3CnWUqiyGVM4utguQ861TAU6/KPkj1S+bK0vyA= +github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.17-0.20241108090636-05469301103d/go.mod h1:tsKOAAbEQnpXAzqjcZUGzRiVp9QfRViKoTOdIltIMFI= github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI= github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= diff --git a/pkg/utils/meta.go b/pkg/utils/meta.go index 9eb6ca2d..0efc8af2 100644 --- a/pkg/utils/meta.go +++ b/pkg/utils/meta.go @@ -3,6 +3,7 @@ package utils import ( "context" "log" + "time" "github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/client" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -58,14 +59,27 @@ func SetCMPVars(apiClient, brokerClient *client.APIClient, cfg *client.Configura return } apiClient.SetHost(cmpDetails.URL) + apiClient.CMPToken = cmpDetails.AccessToken + apiClient.TokenExpiry = cmpDetails.ValidTill apiClient.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, client.ContextAccessToken, cmpDetails.AccessToken) + // Token expiry unix time in seconds and substract 15secs + tokenExpiry := apiClient.TokenExpiry/1000 - 15 + token := apiClient.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 + apiClient.CMPToken = cmpDetails.AccessToken + apiClient.TokenExpiry = cmpDetails.ValidTill + } + } + *ctx = context.WithValue(*ctx, client.ContextAccessToken, token) }) ctx := context.Background() ctx = context.WithValue(ctx, client.ContextAccessToken, cmpDetails.AccessToken)