Skip to content

Commit

Permalink
More changes requested by Mahesh.
Browse files Browse the repository at this point in the history
  • Loading branch information
eamonnotoole committed Sep 18, 2024
1 parent c6c630e commit 56de964
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
16 changes: 8 additions & 8 deletions pkg/client/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type BrokerAPIService struct {
}

// GetMorpheusDetails returns Morpheus details to terraform
func (a *BrokerAPIService) GetMorpheusDetails(ctx context.Context) (models.MorpheusDetails, error) {
func (a *BrokerAPIService) GetMorpheusDetails(ctx context.Context) (models.TFMorpheusDetails, error) {
// Get the service instance ID and Morpheus URL
ServiceSubscriptionDetailsResp := models.SubscriptionDetailsResponse{}
serviceSubscriptionDetailsAPI := &api{
Expand All @@ -35,7 +35,7 @@ func (a *BrokerAPIService) GetMorpheusDetails(ctx context.Context) (models.Morph

// Use the default query params
if err := serviceSubscriptionDetailsAPI.do(ctx, nil, a.Cfg.DefaultQueryParams); err != nil {
return models.MorpheusDetails{}, fmt.Errorf("error getting service subscription details: %v", err)
return models.TFMorpheusDetails{}, fmt.Errorf("error getting service subscription details: %v", err)
}

// Get the Morpheus token
Expand All @@ -53,15 +53,15 @@ func (a *BrokerAPIService) GetMorpheusDetails(ctx context.Context) (models.Morph

// No query params needed
if err := morpheusTokenAPI.do(ctx, nil, nil); err != nil {
return models.MorpheusDetails{}, fmt.Errorf("error getting Morpheus token: %v", err)
return models.TFMorpheusDetails{}, fmt.Errorf("error getting Morpheus token: %v", err)
}

// build response
ret := models.MorpheusDetails{
ID: ServiceSubscriptionDetailsResp.ServiceInstanceID,
AccessToken: MorpheusTokenResp.AccessToken,
AccessTokenExpires: MorpheusTokenResp.AccessTokenExpires,
URL: ServiceSubscriptionDetailsResp.URL,
ret := models.TFMorpheusDetails{
ID: ServiceSubscriptionDetailsResp.ServiceInstanceID,
AccessToken: MorpheusTokenResp.AccessToken,
ValidTill: MorpheusTokenResp.Expires,
URL: ServiceSubscriptionDetailsResp.URL,
}

return ret, nil
Expand Down
42 changes: 24 additions & 18 deletions pkg/client/broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import (
)

const (
testServiceInstanceID = "18ba6409-ac59-4eac-9414-0147e72d615e"
testAccessToken = "2b9fba7f-7c14-4773-a970-a9ad393811ac"
testMorpheusURL = "https://1234-mp.private.greenlake.hpe-gl-intg.com/"
testAccessTokenExpires = 1758034360176
testServiceInstanceID = "18ba6409-ac59-4eac-9414-0147e72d615e"
testAccessToken = "2b9fba7f-7c14-4773-a970-a9ad393811ac"
testRefreshToken = "7806acfb-f847-48b1-a6d5-6119dccb3ffe"
testMorpheusURL = "https://1234-mp.private.greenlake.hpe-gl-intg.com/"
testAccessTokenExpires = 1758034360176
testAccessTokenExpiresIn = 3600
)

func TestBrokerAPIService_GetMorpheusDetails(t *testing.T) {
Expand All @@ -48,16 +50,16 @@ func TestBrokerAPIService_GetMorpheusDetails(t *testing.T) {
tests := []struct {
name string
given func(m *MockAPIClientHandler)
want models.MorpheusDetails
want models.TFMorpheusDetails
wantErr bool
}{
{
name: "Test GetMorpheusDetails success",
want: models.MorpheusDetails{
ID: testServiceInstanceID,
AccessToken: testAccessToken,
AccessTokenExpires: testAccessTokenExpires,
URL: testMorpheusURL,
want: models.TFMorpheusDetails{
ID: testServiceInstanceID,
AccessToken: testAccessToken,
ValidTill: testAccessTokenExpires,
URL: testMorpheusURL,
},
wantErr: false,
given: func(m *MockAPIClientHandler) {
Expand Down Expand Up @@ -87,8 +89,10 @@ func TestBrokerAPIService_GetMorpheusDetails(t *testing.T) {
pathToken := mockHost + "/" + fmt.Sprintf(consts.MorpheusToken, testServiceInstanceID)
reqToken, _ := http.NewRequest(method, pathToken, nil)
tokenResp := models.MorpheusTokenResponse{
AccessToken: testAccessToken,
AccessTokenExpires: testAccessTokenExpires,
AccessToken: testAccessToken,
Expires: testAccessTokenExpires,
RefreshToken: testRefreshToken,
ExpiresIn: testAccessTokenExpiresIn,
}
body, err := json.Marshal(tokenResp)
assert.NoError(t, err)
Expand All @@ -107,7 +111,7 @@ func TestBrokerAPIService_GetMorpheusDetails(t *testing.T) {

{
name: "Test GetMorpheusDetails error in get subscription details prepare request",
want: models.MorpheusDetails{},
want: models.TFMorpheusDetails{},
wantErr: true,
given: func(m *MockAPIClientHandler) {
// Get subscription details
Expand All @@ -124,7 +128,7 @@ func TestBrokerAPIService_GetMorpheusDetails(t *testing.T) {

{
name: "Test GetMorpheusDetails error in get subscription details call API",
want: models.MorpheusDetails{},
want: models.TFMorpheusDetails{},
wantErr: true,
given: func(m *MockAPIClientHandler) {
// Get subscription details
Expand Down Expand Up @@ -152,7 +156,7 @@ func TestBrokerAPIService_GetMorpheusDetails(t *testing.T) {

{
name: "Test GetMorpheusDetails error in get Morpheus token prepare request",
want: models.MorpheusDetails{},
want: models.TFMorpheusDetails{},
wantErr: true,
given: func(m *MockAPIClientHandler) {
// Get subscription details
Expand Down Expand Up @@ -189,7 +193,7 @@ func TestBrokerAPIService_GetMorpheusDetails(t *testing.T) {

{
name: "Test GetMorpheusDetails error in get Morpheus token call API",
want: models.MorpheusDetails{},
want: models.TFMorpheusDetails{},
wantErr: true,
given: func(m *MockAPIClientHandler) {
// Get subscription details
Expand Down Expand Up @@ -218,8 +222,10 @@ func TestBrokerAPIService_GetMorpheusDetails(t *testing.T) {
pathToken := mockHost + "/" + fmt.Sprintf(consts.MorpheusToken, testServiceInstanceID)
reqToken, _ := http.NewRequest(method, pathToken, nil)
tokenResp := models.MorpheusTokenResponse{
AccessToken: testAccessToken,
AccessTokenExpires: testAccessTokenExpires,
AccessToken: testAccessToken,
Expires: testAccessTokenExpires,
RefreshToken: testRefreshToken,
ExpiresIn: testAccessTokenExpiresIn,
}
body, err := json.Marshal(tokenResp)
assert.NoError(t, err)
Expand Down
20 changes: 12 additions & 8 deletions pkg/models/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ type SubscriptionDetailsResponse struct {

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

// MorpheusDetails is what we return to terraform
type MorpheusDetails struct {
ID string `json:"id"` // This is the ServiceInstanceID, added here for use by the provider
AccessToken string `json:"access_token"`
AccessTokenExpires int `json:"access_token_expires"` // This is the Unix timestamp of when the token expires
URL string `json:"URL"`
// TFMorpheusDetails is what we return to terraform
type TFMorpheusDetails struct {
// ID is the ServiceInstanceID, added here for use by the provider when storing the data
ID string `json:"id"`
AccessToken string `json:"access_token"`
// ValidTill Unix timestamp of when the access_token expires in seconds
ValidTill int64 `json:"valid_till"`
URL string `json:"URL"`
}

0 comments on commit 56de964

Please sign in to comment.