Skip to content

Commit

Permalink
Merge pull request #267 from HewlettPackard/update_host
Browse files Browse the repository at this point in the history
Switch to cmp endpoint
  • Loading branch information
reubenur-rahman authored Nov 5, 2024
2 parents 1052e6e + 93b02c4 commit 7494108
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ acceptance:
@if [ "${case}" != "" ]; then \
TF_ACC=true go test -parallel 1 -count 1 -run $(case) -v -timeout=20000s -cover $(ACC_TEST_FILE_LOCATION); \
else \
TF_ACC=true go test -parallel 4 -v -timeout=50000s -cover $(ACC_TEST_FILE_LOCATION);\
TF_ACC=true go test -parallel 2 -v -timeout=50000s -cover $(ACC_TEST_FILE_LOCATION);\
fi

build: vendor $(NAME)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ module github.com/HewlettPackard/hpegl-vmaas-terraform-resources
go 1.21

require (
github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.13
github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.14
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
github.com/hashicorp/terraform-plugin-sdk/v2 v2.33.0
github.com/hewlettpackard/hpegl-provider-lib v0.0.20
github.com/hewlettpackard/hpegl-provider-lib v0.0.17
github.com/spf13/viper v1.18.2
github.com/tidwall/gjson v1.14.1
github.com/tshihad/tftags v0.0.10
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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.13 h1:UnTm9GYtLqRe3M5XRcmFAqKQElnIoX/MEgPF5VJWfqY=
github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.13/go.mod h1:tsKOAAbEQnpXAzqjcZUGzRiVp9QfRViKoTOdIltIMFI=
github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.14 h1:G3i8N70LBGo1tEFXCCBx5fd+cERBvnBCviFheVLlj80=
github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.14/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 Expand Up @@ -124,8 +124,8 @@ github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S
github.com/hashicorp/terraform-svchost v0.1.1/go.mod h1:mNsjQfZyf/Jhz35v6/0LWcv26+X7JPS+buii2c9/ctc=
github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE=
github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ=
github.com/hewlettpackard/hpegl-provider-lib v0.0.20 h1:QbZUQwr08aI2zELk/V+0OTaJe0LSVGklG99cQh4hUVA=
github.com/hewlettpackard/hpegl-provider-lib v0.0.20/go.mod h1:Bw2DhefBjqXQI6s5vBBGiWUkFz7fx4KkbGfwmo9wv3U=
github.com/hewlettpackard/hpegl-provider-lib v0.0.17 h1:6sh4XjWK2hg2q5V78W4AypV0IBbDy0CpGoMfzE1CK1c=
github.com/hewlettpackard/hpegl-provider-lib v0.0.17/go.mod h1:Bw2DhefBjqXQI6s5vBBGiWUkFz7fx4KkbGfwmo9wv3U=
github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/huandu/xstrings v1.3.3 h1:/Gcsuc1x8JVbJ9/rlye4xZnVAbEkGauT8lbebqcQws4=
Expand Down
67 changes: 46 additions & 21 deletions internal/acceptance_test/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,68 @@ import (
"github.com/hewlettpackard/hpegl-provider-lib/pkg/token/serviceclient"
)

func getAPIClient() (*api_client.APIClient, api_client.Configuration) {
// func getOldAPIClient() (*api_client.APIClient, api_client.Configuration) {
// headers, queryParam, iamVersion := getHeadersAndQueryParamsAndIAMVersion()
// cfg := api_client.Configuration{
// Host: os.Getenv("HPEGL_VMAAS_API_URL"),
// DefaultHeader: headers,
// DefaultQueryParams: queryParam,
// }
// apiClient := api_client.NewAPIClient(&cfg)
// err := apiClientSetMeta(apiClient, iamVersion)
// if err != nil {
// log.Printf("[WARN] Error: %s", err)
// }

// return apiClient, cfg
// }

func getBrokerAPIClient() (*api_client.APIClient, api_client.Configuration) {
headers, queryParam, iamVersion := getHeadersAndQueryParamsAndIAMVersion()

cfg := api_client.Configuration{
Host: os.Getenv("HPEGL_VMAAS_API_URL"),
Host: os.Getenv("HPEGL_VMAAS_BROKER_URL"),
DefaultHeader: headers,
DefaultQueryParams: queryParam,
}
apiClient := api_client.NewAPIClient(&cfg)
err := apiClientSetMeta(apiClient, iamVersion)
brokerAPIClient := api_client.NewAPIClient(&cfg)
err := apiClientSetMeta(brokerAPIClient, iamVersion)
if err != nil {
log.Printf("[WARN] Error: %s", err)
}

return apiClient, cfg
return brokerAPIClient, cfg
}

func getBrokerAPIClient() (*api_client.APIClient, api_client.Configuration) {
headers, queryParam, iamVersion := getHeadersAndQueryParamsAndIAMVersion()
// No need to set the default query params for broker API
func getAPIClient() (*api_client.APIClient, api_client.Configuration) {
ctx := context.Background()
brokerClient, _ := getBrokerAPIClient()
cmpDetails, err := brokerClient.GetCMPDetails(ctx)
if err != nil {
log.Printf("[ERROR] Error getting cmp details: %s", err)
}
cfg := api_client.Configuration{
Host: os.Getenv("HPEGL_VMAAS_BROKER_URL"),
DefaultHeader: headers,
Host: cmpDetails.URL,
DefaultHeader: map[string]string{},
DefaultQueryParams: map[string]string{},
}
brokerAPIClient := api_client.NewAPIClient(&cfg)
err := apiClientSetMeta(brokerAPIClient, iamVersion)
cmpAPIClient := api_client.NewAPIClient(&cfg)
cmpAPIClient.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, api_client.ContextAccessToken, cmpDetails.AccessToken)
}
})
ctx = context.WithValue(ctx, api_client.ContextAccessToken, cmpDetails.AccessToken)
err = cmpAPIClient.SetCMPVersion(ctx)
if err != nil {
log.Printf("[WARN] Error: %s", err)
}
log.Printf("[ERROR] Unable to set CMP version client: %s", err)

// Return the configuration with the default query params
cfgForReturn := api_client.Configuration{
Host: os.Getenv("HPEGL_VMAAS_BROKER_URL"),
DefaultHeader: headers,
DefaultQueryParams: queryParam,
}

return brokerAPIClient, cfgForReturn
return cmpAPIClient, cfg
}

func getHeadersAndQueryParamsAndIAMVersion() (map[string]string, map[string]string, string) {
Expand Down
4 changes: 4 additions & 0 deletions internal/cmp/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
)

func setMeta(meta interface{}, apiClient client.APIClientHandler) {
// Not needed while using cmp directly

}
func setMetaHpegl(meta interface{}, apiClient client.APIClientHandler) {
err := apiClient.SetMeta(meta, auth.SetScmClientToken)
if err != nil {
log.Printf("[ERROR] error while setting meta information for cmp-sdk, error: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion internal/cmp/morpheus_datasource_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func newMorpheusBroker(bClient *client.BrokerAPIService) *morpheusBroker {

// Read reads the morpheus details using the Broker API
func (m *morpheusBroker) Read(ctx context.Context, d *utils.Data, meta interface{}) error {
setMeta(meta, m.bClient.Client)
setMetaHpegl(meta, m.bClient.Client)

// Get Morpheus Tokens and URL
morpheusDetails, err := m.bClient.GetMorpheusDetails(ctx)
Expand Down
4 changes: 2 additions & 2 deletions internal/cmp/resource_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ func (r *resNetwork) Create(ctx context.Context, d *utils.Data, meta interface{}
}
errCount := 0
cRetry := utils.CustomRetry{
Timeout: time.Minute * 10,
Timeout: time.Minute * 15,
RetryDelay: time.Second * 10,
InitialDelay: time.Second * 10,
InitialDelay: time.Second * 20,
Cond: func(response interface{}, err error) (bool, error) {
if err != nil {
errCount++
Expand Down
36 changes: 16 additions & 20 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,37 +68,33 @@ func (i InitialiseClient) NewClient(r *schema.ResourceData) (interface{}, error)
queryParam[constants.SpaceKey] = vmaasProviderSettings[constants.SPACENAME].(string)
}

// Create cmp client
cfg := api_client.Configuration{
Host: vmaasProviderSettings[constants.APIURL].(string),
DefaultHeader: getHeaders(),
DefaultQueryParams: queryParam,
}
apiClient := api_client.NewAPIClient(&cfg)
utils.SetMeta(apiClient, r)
client.CmpClient = cmp_client.NewClient(apiClient, cfg)

// Create broker client
brokerHeaders := getHeaders()
tenantID := r.Get(constants.TenantID).(string)
brokerHeaders["X-Tenant-ID"] = tenantID
// We don't add default query params to broker client
brokerCfgForAPIClient := api_client.Configuration{
Host: vmaasProviderSettings[constants.BROKERRURL].(string),
DefaultHeader: brokerHeaders,
}
brokerApiClient := api_client.NewAPIClient(&brokerCfgForAPIClient)
utils.SetMetaFnAndVersion(brokerApiClient, r, apiClient.GetSCMVersion())

// brokerCfg is passed down to NewBrokerClient. It is used mainly to set the query params
// for the call to get SubscriptionDetails
brokerCfg := api_client.Configuration{
Host: vmaasProviderSettings[constants.BROKERRURL].(string),
DefaultHeader: brokerHeaders,
DefaultQueryParams: queryParam,
}
client.BrokerClient = cmp_client.NewBrokerClient(brokerApiClient, brokerCfg)
brokerApiClient := api_client.NewAPIClient(&brokerCfgForAPIClient)
utils.SetMetaFnAndVersion(brokerApiClient, r, 0)
// Create cmp client
cfg := api_client.Configuration{
Host: "",
DefaultHeader: map[string]string{},
DefaultQueryParams: map[string]string{},
}
apiClient := api_client.NewAPIClient(&cfg)
err = utils.SetCMPVars(apiClient, brokerApiClient, &cfg)
if err != nil {
return nil, fmt.Errorf("[ERROR]: unable to set cmp metadata %v", err)
}
client.CmpClient = cmp_client.NewClient(apiClient, cfg)
utils.SetMetaFnAndVersion(brokerApiClient, r, apiClient.GetSCMVersion())

client.BrokerClient = cmp_client.NewBrokerClient(brokerApiClient, brokerCfgForAPIClient)
return client, nil
}

Expand Down
27 changes: 27 additions & 0 deletions pkg/utils/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,30 @@ func SetMetaFnAndVersion(apiClient *client.APIClient, r *schema.ResourceData, ve
}
})
}
func SetCMPVars(apiClient, brokerClient *client.APIClient, cfg *client.Configuration) (err error) {
cmpDetails, err := brokerClient.GetCMPDetails(context.Background())
if err != nil {
log.Printf("[ERROR] Unable to fetch token for CMP client: %s", err)
return
}
apiClient.SetHost(cmpDetails.URL)
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)
}
})
ctx := context.Background()
ctx = context.WithValue(ctx, client.ContextAccessToken, cmpDetails.AccessToken)
err = apiClient.SetCMPVersion(ctx)
if err != nil {
log.Printf("[ERROR] Unable to set CMP version client: %s", err)
return
}
cfg.Host = cmpDetails.URL

return err
}

0 comments on commit 7494108

Please sign in to comment.