From 2a67a31003c27a3a7e239c8092cd6bdaf19a1eee Mon Sep 17 00:00:00 2001 From: Mahesh N Date: Tue, 29 Oct 2024 16:45:36 +0530 Subject: [PATCH 1/9] Switch to cmp url --- pkg/client/api.go | 3 ++- pkg/client/client.go | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/pkg/client/api.go b/pkg/client/api.go index f8e5ce0..f03ad5c 100644 --- a/pkg/client/api.go +++ b/pkg/client/api.go @@ -55,7 +55,8 @@ func (a *api) do(ctx context.Context, request interface{}, queryParams map[strin // Set the path if !a.removeVmaasCMPBasePath { // Add the base path of the vmaas-cmp API if we are calling the vmaas-cmp API - a.path = fmt.Sprintf("%s/%s/%s", a.client.getHost(), consts.VmaasCmpAPIBasePath, a.path) + // a.path = fmt.Sprintf("%s/%s/%s", a.client.getHost(), consts.VmaasCmpAPIBasePath, a.path) + a.path = fmt.Sprintf("%s/%s", a.client.getHost(), a.path) } else { // Don't use the base path of the vmaas-cmp API if we are calling the broker API a.path = fmt.Sprintf("%s/%s", a.client.getHost(), a.path) diff --git a/pkg/client/client.go b/pkg/client/client.go index 89c6d1f..45c2733 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -47,6 +47,8 @@ type APIClientHandler interface { // In most cases there should be only one, shared, APIClient. type APIClient struct { cfg *Configuration + cmpURL string + cmpToken string cmpVersion int meta interface{} tokenFunc SetScmClientToken @@ -77,6 +79,16 @@ func (c *APIClient) getHost() string { func (c *APIClient) SetMeta(meta interface{}, fn SetScmClientToken) error { c.meta = meta c.tokenFunc = fn + cmpBroker := BrokerAPIService{ + Client: c, + Cfg: *c.cfg, + } + cmpDetails, err := cmpBroker.GetMorpheusDetails(context.Background()) + if err != nil { + return err + } + c.cmpURL = cmpDetails.URL + c.cmpToken = cmpDetails.AccessToken // if cmp version already set then skip if c.cmpVersion != 0 { return nil @@ -261,6 +273,10 @@ func (c *APIClient) prepareRequest( localVarRequest.Header.Set("Authorization", "Bearer "+auth) } } + if c.cmpToken != "" && c.cmpURL != "" { + c.cfg.Host = c.cmpURL + localVarRequest.Header.Set("Authorization", "Bearer "+c.cmpToken) + } for header, value := range c.cfg.DefaultHeader { if value != "" && value != " " { From d69ab92a2c1cbcc107f40538eb859e752d03b3c2 Mon Sep 17 00:00:00 2001 From: Mahesh N Date: Wed, 30 Oct 2024 12:57:25 +0530 Subject: [PATCH 2/9] add get cmp var function --- pkg/client/client.go | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index 45c2733..169f28c 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -16,6 +16,8 @@ import ( "net/url" "path/filepath" "strings" + + "github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/models" ) type SetScmClientToken func(ctx *context.Context, meta interface{}) @@ -41,14 +43,13 @@ type APIClientHandler interface { SetMetaFnAndVersion(meta interface{}, version int, fn SetScmClientToken) // GetSCMVersion returns the SCM version for use when creating the Broker client GetSCMVersion() int + GetCMPVars(ctx context.Context) (models.TFMorpheusDetails, error) } // APIClient manages communication with the GreenLake Private Cloud VMaaS CMP API API v1.0.0 // In most cases there should be only one, shared, APIClient. type APIClient struct { cfg *Configuration - cmpURL string - cmpToken string cmpVersion int meta interface{} tokenFunc SetScmClientToken @@ -79,16 +80,6 @@ func (c *APIClient) getHost() string { func (c *APIClient) SetMeta(meta interface{}, fn SetScmClientToken) error { c.meta = meta c.tokenFunc = fn - cmpBroker := BrokerAPIService{ - Client: c, - Cfg: *c.cfg, - } - cmpDetails, err := cmpBroker.GetMorpheusDetails(context.Background()) - if err != nil { - return err - } - c.cmpURL = cmpDetails.URL - c.cmpToken = cmpDetails.AccessToken // if cmp version already set then skip if c.cmpVersion != 0 { return nil @@ -113,6 +104,14 @@ func (c *APIClient) SetMeta(meta interface{}, fn SetScmClientToken) error { return nil } +func (c *APIClient) GetCMPVars(ctx context.Context) (models.TFMorpheusDetails, error) { + cmpBroker := BrokerAPIService{ + Client: c, + Cfg: *c.cfg, + } + return cmpBroker.GetMorpheusDetails(ctx) + +} func (c *APIClient) SetMetaFnAndVersion(meta interface{}, version int, fn SetScmClientToken) { c.meta = meta c.tokenFunc = fn @@ -273,10 +272,6 @@ func (c *APIClient) prepareRequest( localVarRequest.Header.Set("Authorization", "Bearer "+auth) } } - if c.cmpToken != "" && c.cmpURL != "" { - c.cfg.Host = c.cmpURL - localVarRequest.Header.Set("Authorization", "Bearer "+c.cmpToken) - } for header, value := range c.cfg.DefaultHeader { if value != "" && value != " " { From bc7bf5eafc6690f03567b2b26d9a67f649a41e0d Mon Sep 17 00:00:00 2001 From: Mahesh N Date: Wed, 30 Oct 2024 13:11:52 +0530 Subject: [PATCH 3/9] add SetHost function --- pkg/client/client.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index 169f28c..762874d 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -103,7 +103,9 @@ func (c *APIClient) SetMeta(meta interface{}, fn SetScmClientToken) error { return nil } - +func (c *APIClient) SetHost(host string) { + c.cfg.Host = host +} func (c *APIClient) GetCMPVars(ctx context.Context) (models.TFMorpheusDetails, error) { cmpBroker := BrokerAPIService{ Client: c, From e7c95d2717fadfa2a24012a4751a037cfb1eaf5d Mon Sep 17 00:00:00 2001 From: Mahesh N Date: Wed, 30 Oct 2024 14:11:53 +0530 Subject: [PATCH 4/9] add setcmpmeta --- pkg/client/client.go | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index 762874d..7d6baa3 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -44,6 +44,7 @@ type APIClientHandler interface { // GetSCMVersion returns the SCM version for use when creating the Broker client GetSCMVersion() int GetCMPVars(ctx context.Context) (models.TFMorpheusDetails, error) + SetCMPMeta(meta interface{}, fn SetScmClientToken) error } // APIClient manages communication with the GreenLake Private Cloud VMaaS CMP API API v1.0.0 @@ -103,9 +104,6 @@ func (c *APIClient) SetMeta(meta interface{}, fn SetScmClientToken) error { return nil } -func (c *APIClient) SetHost(host string) { - c.cfg.Host = host -} func (c *APIClient) GetCMPVars(ctx context.Context) (models.TFMorpheusDetails, error) { cmpBroker := BrokerAPIService{ Client: c, @@ -114,6 +112,32 @@ func (c *APIClient) GetCMPVars(ctx context.Context) (models.TFMorpheusDetails, e return cmpBroker.GetMorpheusDetails(ctx) } +func (c *APIClient) SetCMPMeta(meta interface{}, fn SetScmClientToken) (err error) { + c.meta = meta + c.tokenFunc = fn + // if cmp version already set then skip + if c.cmpVersion != 0 { + return nil + } + ctx := context.Background() + c.tokenFunc(&ctx, meta) + cmpClient := CmpStatus{ + Client: c, + Cfg: *c.cfg, + } + // Get status of cmp + statusResp, err := cmpClient.GetCmpVersion(ctx) + if err != nil { + return + } + versionInt, err := parseVersion(statusResp.Appliance.BuildVersion) + if err != nil { + return fmt.Errorf("failed to parse cmp build, error: %w", err) + } + c.cmpVersion = versionInt + + return +} func (c *APIClient) SetMetaFnAndVersion(meta interface{}, version int, fn SetScmClientToken) { c.meta = meta c.tokenFunc = fn From 3719e7819ba2e5375c0eda1dab0f86eb86dba45d Mon Sep 17 00:00:00 2001 From: Mahesh N Date: Wed, 30 Oct 2024 15:42:51 +0530 Subject: [PATCH 5/9] add sethost and trim / in the cmp url --- pkg/client/broker.go | 3 ++- pkg/client/client.go | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/client/broker.go b/pkg/client/broker.go index 84c1804..ec607f1 100644 --- a/pkg/client/broker.go +++ b/pkg/client/broker.go @@ -7,6 +7,7 @@ import ( "encoding/json" "fmt" "net/http" + "strings" consts "github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/common" "github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/models" @@ -61,7 +62,7 @@ func (a *BrokerAPIService) GetMorpheusDetails(ctx context.Context) (models.TFMor ID: ServiceSubscriptionDetailsResp.ServiceInstanceID, AccessToken: MorpheusTokenResp.AccessToken, ValidTill: MorpheusTokenResp.Expires, - URL: ServiceSubscriptionDetailsResp.URL, + URL: strings.TrimSuffix(ServiceSubscriptionDetailsResp.URL, "/"), } return ret, nil diff --git a/pkg/client/client.go b/pkg/client/client.go index 7d6baa3..ecc00e0 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -77,7 +77,9 @@ func NewAPIClient(cfg *Configuration) *APIClient { func (c *APIClient) getHost() string { return c.cfg.Host } - +func (c *APIClient) SetHost(host string) { + c.cfg.Host = host +} func (c *APIClient) SetMeta(meta interface{}, fn SetScmClientToken) error { c.meta = meta c.tokenFunc = fn From be6b464d9a85fe9601a5e5c9a7e733aba6786b11 Mon Sep 17 00:00:00 2001 From: Mahesh N Date: Wed, 30 Oct 2024 18:42:48 +0530 Subject: [PATCH 6/9] change basepath and add more functions --- pkg/client/api.go | 4 ++-- pkg/client/client.go | 28 ++++++++++++++++++---------- pkg/common/constants.go | 2 +- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/pkg/client/api.go b/pkg/client/api.go index f03ad5c..0ddb96c 100644 --- a/pkg/client/api.go +++ b/pkg/client/api.go @@ -55,8 +55,8 @@ func (a *api) do(ctx context.Context, request interface{}, queryParams map[strin // Set the path if !a.removeVmaasCMPBasePath { // Add the base path of the vmaas-cmp API if we are calling the vmaas-cmp API - // a.path = fmt.Sprintf("%s/%s/%s", a.client.getHost(), consts.VmaasCmpAPIBasePath, a.path) - a.path = fmt.Sprintf("%s/%s", a.client.getHost(), a.path) + a.path = fmt.Sprintf("%s/%s/%s", a.client.getHost(), consts.VmaasCmpAPIBasePath, a.path) + //a.path = fmt.Sprintf("%s/%s", a.client.getHost(), a.path) } else { // Don't use the base path of the vmaas-cmp API if we are calling the broker API a.path = fmt.Sprintf("%s/%s", a.client.getHost(), a.path) diff --git a/pkg/client/client.go b/pkg/client/client.go index ecc00e0..322b109 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -44,16 +44,18 @@ type APIClientHandler interface { // GetSCMVersion returns the SCM version for use when creating the Broker client GetSCMVersion() int GetCMPVars(ctx context.Context) (models.TFMorpheusDetails, error) - SetCMPMeta(meta interface{}, fn SetScmClientToken) error + SetCMPMeta(meta interface{}, brokerClient *APIClient, fn SetScmClientToken) error + SetCMPVersion(ctx context.Context) (err error) } // APIClient manages communication with the GreenLake Private Cloud VMaaS CMP API API v1.0.0 // In most cases there should be only one, shared, APIClient. type APIClient struct { - cfg *Configuration - cmpVersion int - meta interface{} - tokenFunc SetScmClientToken + cfg *Configuration + cmpVersion int + meta interface{} + tokenFunc SetScmClientToken + BrokerClient *APIClient } // defaultTokenFunc will use while defining httpClient. defaultTokenFunc @@ -77,8 +79,12 @@ func NewAPIClient(cfg *Configuration) *APIClient { func (c *APIClient) getHost() string { return c.cfg.Host } -func (c *APIClient) SetHost(host string) { +func (c *APIClient) SetHostandToken(host, token string) { c.cfg.Host = host + if c.cfg.DefaultHeader == nil { + c.cfg.DefaultHeader = map[string]string{} + } + c.cfg.AddDefaultHeader("Authorization", "Bearer "+token) } func (c *APIClient) SetMeta(meta interface{}, fn SetScmClientToken) error { c.meta = meta @@ -114,15 +120,18 @@ func (c *APIClient) GetCMPVars(ctx context.Context) (models.TFMorpheusDetails, e return cmpBroker.GetMorpheusDetails(ctx) } -func (c *APIClient) SetCMPMeta(meta interface{}, fn SetScmClientToken) (err error) { +func (c *APIClient) SetCMPMeta(meta interface{}, brokerClient *APIClient, fn SetScmClientToken) (err error) { c.meta = meta c.tokenFunc = fn // if cmp version already set then skip + c.BrokerClient = brokerClient + + return +} +func (c *APIClient) SetCMPVersion(ctx context.Context) (err error) { if c.cmpVersion != 0 { return nil } - ctx := context.Background() - c.tokenFunc(&ctx, meta) cmpClient := CmpStatus{ Client: c, Cfg: *c.cfg, @@ -137,7 +146,6 @@ func (c *APIClient) SetCMPMeta(meta interface{}, fn SetScmClientToken) (err erro return fmt.Errorf("failed to parse cmp build, error: %w", err) } c.cmpVersion = versionInt - return } func (c *APIClient) SetMetaFnAndVersion(meta interface{}, version int, fn SetScmClientToken) { diff --git a/pkg/common/constants.go b/pkg/common/constants.go index c09e85e..6d781aa 100644 --- a/pkg/common/constants.go +++ b/pkg/common/constants.go @@ -4,7 +4,7 @@ package common const ( // VmaasCmpAPIBasePath base cmp api path - VmaasCmpAPIBasePath = "v1" + VmaasCmpAPIBasePath = "api" // InstancesPath InstancesPath = "instances" // InstanceTypesPath From 2b4bf1278fd0ff24092282c3666bed16eb91b7eb Mon Sep 17 00:00:00 2001 From: Mahesh N Date: Thu, 31 Oct 2024 13:33:30 +0530 Subject: [PATCH 7/9] Code clean --- pkg/client/api.go | 3 +-- pkg/client/broker_test.go | 2 +- pkg/client/client.go | 20 ++++++++------------ pkg/common/constants.go | 4 +++- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/pkg/client/api.go b/pkg/client/api.go index 0ddb96c..e4b32fa 100644 --- a/pkg/client/api.go +++ b/pkg/client/api.go @@ -55,8 +55,7 @@ func (a *api) do(ctx context.Context, request interface{}, queryParams map[strin // Set the path if !a.removeVmaasCMPBasePath { // Add the base path of the vmaas-cmp API if we are calling the vmaas-cmp API - a.path = fmt.Sprintf("%s/%s/%s", a.client.getHost(), consts.VmaasCmpAPIBasePath, a.path) - //a.path = fmt.Sprintf("%s/%s", a.client.getHost(), a.path) + a.path = fmt.Sprintf("%s/%s/%s", a.client.getHost(), consts.CmpAPIBasePath, a.path) } else { // Don't use the base path of the vmaas-cmp API if we are calling the broker API a.path = fmt.Sprintf("%s/%s", a.client.getHost(), a.path) diff --git a/pkg/client/broker_test.go b/pkg/client/broker_test.go index 010469d..628efed 100644 --- a/pkg/client/broker_test.go +++ b/pkg/client/broker_test.go @@ -25,7 +25,7 @@ const ( 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/" + testMorpheusURL = "https://1234-mp.private.greenlake.hpe-gl-intg.com" testAccessTokenExpires = 1758034360176 testAccessTokenExpiresIn = 3600 ) diff --git a/pkg/client/client.go b/pkg/client/client.go index 322b109..3f9ee51 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -43,8 +43,9 @@ type APIClientHandler interface { SetMetaFnAndVersion(meta interface{}, version int, fn SetScmClientToken) // GetSCMVersion returns the SCM version for use when creating the Broker client GetSCMVersion() int - GetCMPVars(ctx context.Context) (models.TFMorpheusDetails, error) - SetCMPMeta(meta interface{}, brokerClient *APIClient, fn SetScmClientToken) error + SetHost(host string) + GetCMPDetails(ctx context.Context) (models.TFMorpheusDetails, error) + SetCMPMeta(meta interface{}, brokerClient *APIClient, fn SetScmClientToken) SetCMPVersion(ctx context.Context) (err error) } @@ -79,12 +80,8 @@ func NewAPIClient(cfg *Configuration) *APIClient { func (c *APIClient) getHost() string { return c.cfg.Host } -func (c *APIClient) SetHostandToken(host, token string) { +func (c *APIClient) SetHost(host string) { c.cfg.Host = host - if c.cfg.DefaultHeader == nil { - c.cfg.DefaultHeader = map[string]string{} - } - c.cfg.AddDefaultHeader("Authorization", "Bearer "+token) } func (c *APIClient) SetMeta(meta interface{}, fn SetScmClientToken) error { c.meta = meta @@ -112,7 +109,9 @@ func (c *APIClient) SetMeta(meta interface{}, fn SetScmClientToken) error { return nil } -func (c *APIClient) GetCMPVars(ctx context.Context) (models.TFMorpheusDetails, error) { + +// GetCMPDetails here APIClient is the brokerClient +func (c *APIClient) GetCMPDetails(ctx context.Context) (models.TFMorpheusDetails, error) { cmpBroker := BrokerAPIService{ Client: c, Cfg: *c.cfg, @@ -120,13 +119,10 @@ func (c *APIClient) GetCMPVars(ctx context.Context) (models.TFMorpheusDetails, e return cmpBroker.GetMorpheusDetails(ctx) } -func (c *APIClient) SetCMPMeta(meta interface{}, brokerClient *APIClient, fn SetScmClientToken) (err error) { +func (c *APIClient) SetCMPMeta(meta interface{}, brokerClient *APIClient, fn SetScmClientToken) { c.meta = meta c.tokenFunc = fn - // if cmp version already set then skip c.BrokerClient = brokerClient - - return } func (c *APIClient) SetCMPVersion(ctx context.Context) (err error) { if c.cmpVersion != 0 { diff --git a/pkg/common/constants.go b/pkg/common/constants.go index 6d781aa..e28a1a9 100644 --- a/pkg/common/constants.go +++ b/pkg/common/constants.go @@ -4,7 +4,9 @@ package common const ( // VmaasCmpAPIBasePath base cmp api path - VmaasCmpAPIBasePath = "api" + VmaasCmpAPIBasePath = "v1" + // CmpAPIBasePath base cmp api path + CmpAPIBasePath string = "api" // InstancesPath InstancesPath = "instances" // InstanceTypesPath From 09bdd6eaa383df9880ce76e6f1e5e97523100723 Mon Sep 17 00:00:00 2001 From: Mahesh N Date: Thu, 31 Oct 2024 13:43:30 +0530 Subject: [PATCH 8/9] update basepath variable --- pkg/client/api.go | 2 +- pkg/common/constants.go | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/pkg/client/api.go b/pkg/client/api.go index e4b32fa..f8e5ce0 100644 --- a/pkg/client/api.go +++ b/pkg/client/api.go @@ -55,7 +55,7 @@ func (a *api) do(ctx context.Context, request interface{}, queryParams map[strin // Set the path if !a.removeVmaasCMPBasePath { // Add the base path of the vmaas-cmp API if we are calling the vmaas-cmp API - a.path = fmt.Sprintf("%s/%s/%s", a.client.getHost(), consts.CmpAPIBasePath, a.path) + a.path = fmt.Sprintf("%s/%s/%s", a.client.getHost(), consts.VmaasCmpAPIBasePath, a.path) } else { // Don't use the base path of the vmaas-cmp API if we are calling the broker API a.path = fmt.Sprintf("%s/%s", a.client.getHost(), a.path) diff --git a/pkg/common/constants.go b/pkg/common/constants.go index e28a1a9..46d8b28 100644 --- a/pkg/common/constants.go +++ b/pkg/common/constants.go @@ -3,10 +3,8 @@ package common const ( - // VmaasCmpAPIBasePath base cmp api path - VmaasCmpAPIBasePath = "v1" - // CmpAPIBasePath base cmp api path - CmpAPIBasePath string = "api" + // VmaasCmpAPIBasePath base cmp api path - currently used to talk to CMP directly + VmaasCmpAPIBasePath = "api" // InstancesPath InstancesPath = "instances" // InstanceTypesPath From 8961e26004b0c6d510bef29435362525f01a554f Mon Sep 17 00:00:00 2001 From: Mahesh N Date: Tue, 5 Nov 2024 11:04:31 +0530 Subject: [PATCH 9/9] cleanup --- pkg/client/client.go | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index 3f9ee51..5239c06 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -44,19 +44,18 @@ type APIClientHandler interface { // GetSCMVersion returns the SCM version for use when creating the Broker client GetSCMVersion() int SetHost(host string) + // GetCMPDetails here the client is the one which has broker host set GetCMPDetails(ctx context.Context) (models.TFMorpheusDetails, error) - SetCMPMeta(meta interface{}, brokerClient *APIClient, fn SetScmClientToken) SetCMPVersion(ctx context.Context) (err error) } // APIClient manages communication with the GreenLake Private Cloud VMaaS CMP API API v1.0.0 // In most cases there should be only one, shared, APIClient. type APIClient struct { - cfg *Configuration - cmpVersion int - meta interface{} - tokenFunc SetScmClientToken - BrokerClient *APIClient + cfg *Configuration + cmpVersion int + meta interface{} + tokenFunc SetScmClientToken } // defaultTokenFunc will use while defining httpClient. defaultTokenFunc @@ -119,11 +118,7 @@ func (c *APIClient) GetCMPDetails(ctx context.Context) (models.TFMorpheusDetails return cmpBroker.GetMorpheusDetails(ctx) } -func (c *APIClient) SetCMPMeta(meta interface{}, brokerClient *APIClient, fn SetScmClientToken) { - c.meta = meta - c.tokenFunc = fn - c.BrokerClient = brokerClient -} + func (c *APIClient) SetCMPVersion(ctx context.Context) (err error) { if c.cmpVersion != 0 { return nil