Skip to content

Commit

Permalink
set the token and expiry time
Browse files Browse the repository at this point in the history
  • Loading branch information
mahesh-hpe committed Nov 8, 2024
1 parent b5ca03b commit ee5be5a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
24 changes: 19 additions & 5 deletions pkg/utils/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ee5be5a

Please sign in to comment.