-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In this PR we add support for the Broker API. The Broker API will be used for two calls: - to get Subscription Details, from which we extract the Subscription ID and the Morpheus URL - to exchange the IAM API Client token for Morpheus access and refresh tokens The Morpheus URL and both tokens will then be returned to the calling terraform provider code. The Changes: - we add pkg/models/broker.go to define three structs, two for use with each broker API call and the third returned to the calling terraform provider code - we add pkg/client/broker.go to define the BrokerAPIService which implements GetMorpheusDetails, to make both broker API calls and return the details - we add a boolean removeVmaasCMPBasePath to pkg/client/api.go which is used to toggle removal of VmaasCmpAPIBasePath from the path if true (in the case of the broker API) or include it if false (in the case of the cmp API) - we add two methods to the APIClientHandler interface which will be used in broker client creation by the terraform provider code: - SetMetaFnAndVersion() which will be used to set the meta, tokenFunc and cmpVersion fields in APIClient, this avoids a call to the CMP API to get version information when initialising the Broker Client - GetSCMVersion() to return the CMP version when initialising the Broker Client after the CMP client, we add this as opposed to exporting the existing getVersion() to avoid approx 190 line changes - we add two constants to pkg/common used to set the Broker API paths
- Loading branch information
1 parent
61cb94a
commit ba17997
Showing
5 changed files
with
126 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// (C) Copyright 2024 Hewlett Packard Enterprise Development LP | ||
|
||
package client | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
|
||
consts "github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/common" | ||
"github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/models" | ||
) | ||
|
||
// BrokerAPIService is a service that provides methods to interact with the broker API | ||
type BrokerAPIService struct { | ||
Client APIClientHandler | ||
Cfg Configuration | ||
} | ||
|
||
// GetMorpheusDetails returns Morpheus details to terraform | ||
func (a *BrokerAPIService) GetMorpheusDetails(ctx context.Context) (models.MorpheusDetails, error) { | ||
// Get the service instance ID and Morpheus URL | ||
ServiceSubscriptionDetailsResp := models.SubscriptionDetailsResponse{} | ||
serviceSubscriptionDetailsAPI := &api{ | ||
method: "GET", | ||
path: consts.SubscriptionDetails, | ||
client: a.Client, | ||
removeVmaasCMPBasePath: true, | ||
|
||
jsonParser: func(body []byte) error { | ||
return json.Unmarshal(body, &ServiceSubscriptionDetailsResp) | ||
}, | ||
} | ||
|
||
// 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) | ||
} | ||
|
||
// Get the Morpheus token | ||
MorpheusTokenResp := models.MorpheusTokenResponse{} | ||
log.Printf(consts.MorpheusToken, ServiceSubscriptionDetailsResp.ServiceInstanceID) | ||
morpheusTokenAPI := &api{ | ||
method: "GET", | ||
path: fmt.Sprintf(consts.MorpheusToken, ServiceSubscriptionDetailsResp.ServiceInstanceID), | ||
client: a.Client, | ||
removeVmaasCMPBasePath: true, | ||
|
||
jsonParser: func(body []byte) error { | ||
return json.Unmarshal(body, &MorpheusTokenResp) | ||
}, | ||
} | ||
|
||
// No query params needed | ||
if err := morpheusTokenAPI.do(ctx, nil, nil); err != nil { | ||
return models.MorpheusDetails{}, fmt.Errorf("error getting Morpheus token: %v", err) | ||
} | ||
|
||
// build response | ||
ret := models.MorpheusDetails{ | ||
AccessToken: MorpheusTokenResp.AccessToken, | ||
RefreshToken: MorpheusTokenResp.RefreshToken, | ||
URL: ServiceSubscriptionDetailsResp.URL, | ||
} | ||
|
||
return ret, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// (C) Copyright 2024 Hewlett Packard Enterprise Development LP | ||
|
||
package models | ||
|
||
// Broker structs go here | ||
|
||
// SubscriptionDetailsResponse is the response for Subscription Details from the broker | ||
type SubscriptionDetailsResponse struct { | ||
ServiceInstanceID string `json:"ServiceInstanceID"` | ||
URL string `json:"URL"` | ||
} | ||
|
||
// MorpheusTokenResponse is the response for Morpheus Token from the broker | ||
type MorpheusTokenResponse struct { | ||
AccessToken string `json:"access_token"` | ||
RefreshToken string `json:"refresh_token"` | ||
} | ||
|
||
// MorpheusDetails is what we return to terraform | ||
type MorpheusDetails struct { | ||
AccessToken string `json:"access_token"` | ||
RefreshToken string `json:"refresh_token"` | ||
URL string `json:"URL"` | ||
} |