Skip to content

Commit

Permalink
Add some more details to MorpheusDetails
Browse files Browse the repository at this point in the history
In this commit we add:
- access_token_expires_in
- refresh_token_expires
  • Loading branch information
eamonnotoole committed Sep 16, 2024
1 parent 5d9591b commit 0554cc6
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 27 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ go 1.21
require (
github.com/antihax/optional v1.0.0
github.com/golang/mock v1.6.0
github.com/stretchr/testify v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/tools v0.1.4 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
github.com/antihax/optional v1.0.0 h1:xK2lYat7ZLaVVcIuj82J8kIro4V6kDe0AUDFboUCwcg=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down Expand Up @@ -31,3 +37,7 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
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=
8 changes: 5 additions & 3 deletions pkg/client/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ func (a *BrokerAPIService) GetMorpheusDetails(ctx context.Context) (models.Morph

// build response
ret := models.MorpheusDetails{
AccessToken: MorpheusTokenResp.AccessToken,
RefreshToken: MorpheusTokenResp.RefreshToken,
URL: ServiceSubscriptionDetailsResp.URL,
AccessToken: MorpheusTokenResp.AccessToken,
RefreshToken: MorpheusTokenResp.RefreshToken,
AccessTokenExpiresIn: MorpheusTokenResp.AccessTokenExpiresIn,
RefreshTokenExpires: MorpheusTokenResp.RefreshTokenExpires,
URL: ServiceSubscriptionDetailsResp.URL,
}

return ret, nil
Expand Down
51 changes: 32 additions & 19 deletions pkg/client/broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package client
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -14,15 +15,19 @@ import (

"github.com/golang/mock/gomock"

"github.com/stretchr/testify/assert"

consts "github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/common"
"github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/models"
)

const (
testSubscriptionID = "18ba6409-ac59-4eac-9414-0147e72d615e"
testAccessToken = "2b9fba7f-7c14-4773-a970-a9ad393811ac"
testRefreshToken = "2b9fba7f-7c14-4773-a970-a9ad393811ac"
testMorpheusURL = "https://1234-mp.private.greenlake.hpe-gl-intg.com/"
testSubscriptionID = "18ba6409-ac59-4eac-9414-0147e72d615e"
testAccessToken = "2b9fba7f-7c14-4773-a970-a9ad393811ac"
testRefreshToken = "2b9fba7f-7c14-4773-a970-a9ad393811ac"
testMorpheusURL = "https://1234-mp.private.greenlake.hpe-gl-intg.com/"
testAccessTokenExpiresIn = 3600
testRefreshTokenExpires = 1758034360176
)

func TestBrokerAPIService_GetMorpheusDetails(t *testing.T) {
Expand Down Expand Up @@ -51,9 +56,11 @@ func TestBrokerAPIService_GetMorpheusDetails(t *testing.T) {
{
name: "Test GetMorpheusDetails success",
want: models.MorpheusDetails{
AccessToken: testAccessToken,
RefreshToken: testRefreshToken,
URL: testMorpheusURL,
AccessToken: testAccessToken,
RefreshToken: testRefreshToken,
AccessTokenExpiresIn: testAccessTokenExpiresIn,
RefreshTokenExpires: testRefreshTokenExpires,
URL: testMorpheusURL,
},
wantErr: false,
given: func(m *MockAPIClientHandler) {
Expand Down Expand Up @@ -82,12 +89,15 @@ func TestBrokerAPIService_GetMorpheusDetails(t *testing.T) {
m.EXPECT().getHost().Return(mockHost)
pathToken := mockHost + "/" + fmt.Sprintf(consts.MorpheusToken, testSubscriptionID)
reqToken, _ := http.NewRequest(method, pathToken, nil)
respBodyToken := io.NopCloser(bytes.NewReader([]byte(`
{
"access_token": "` + testAccessToken + `",
"refresh_token": "` + testRefreshToken + `"
}
`)))
tokenResp := models.MorpheusTokenResponse{
AccessToken: testAccessToken,
RefreshToken: testRefreshToken,
AccessTokenExpiresIn: testAccessTokenExpiresIn,
RefreshTokenExpires: testRefreshTokenExpires,
}
body, err := json.Marshal(tokenResp)
assert.NoError(t, err)
respBodyToken := io.NopCloser(bytes.NewReader(body))
// mock the context only since it is not validated in this function
m.EXPECT().getVersion().Return(999999)
m.EXPECT().prepareRequest(gomock.Any(), pathToken, method, nil, headers,
Expand Down Expand Up @@ -212,12 +222,15 @@ func TestBrokerAPIService_GetMorpheusDetails(t *testing.T) {
m.EXPECT().getHost().Return(mockHost)
pathToken := mockHost + "/" + fmt.Sprintf(consts.MorpheusToken, testSubscriptionID)
reqToken, _ := http.NewRequest(method, pathToken, nil)
respBodyToken := io.NopCloser(bytes.NewReader([]byte(`
{
"access_token": "` + testAccessToken + `",
"refresh_token": "` + testRefreshToken + `"
}
`)))
tokenResp := models.MorpheusTokenResponse{
AccessToken: testAccessToken,
RefreshToken: testRefreshToken,
AccessTokenExpiresIn: testAccessTokenExpiresIn,
RefreshTokenExpires: testRefreshTokenExpires,
}
body, err := json.Marshal(tokenResp)
assert.NoError(t, err)
respBodyToken := io.NopCloser(bytes.NewReader([]byte(body)))
// mock the context only since it is not validated in this function
m.EXPECT().getVersion().Return(999999)
m.EXPECT().prepareRequest(gomock.Any(), pathToken, method, nil, headers,
Expand Down
14 changes: 9 additions & 5 deletions pkg/models/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ type SubscriptionDetailsResponse struct {

// MorpheusTokenResponse is the response for Morpheus Token from the broker
type MorpheusTokenResponse struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
AccessTokenExpiresIn int `json:"expires_in"`
RefreshTokenExpires int `json:"expires"`
}

// MorpheusDetails is what we return to terraform
type MorpheusDetails struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
URL string `json:"URL"`
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
AccessTokenExpiresIn int `json:"access_token_expires_in"`
RefreshTokenExpires int `json:"refresh_token_expires"`
URL string `json:"URL"`
}

0 comments on commit 0554cc6

Please sign in to comment.