Skip to content

Commit

Permalink
Merge pull request #78 from eamonnotoole/morpheus-token-data-source
Browse files Browse the repository at this point in the history
Add support for Broker API
  • Loading branch information
reubenur-rahman authored Sep 19, 2024
2 parents 38cfff9 + 56de964 commit fb2edb9
Show file tree
Hide file tree
Showing 8 changed files with 408 additions and 4 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=
14 changes: 12 additions & 2 deletions pkg/client/api.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// (C) Copyright 2021 Hewlett Packard Enterprise Development LP
// (C) Copyright 2021-2024 Hewlett Packard Enterprise Development LP

package client

Expand All @@ -25,6 +25,8 @@ type api struct {
jsonParser jsonPareserFunc
validations []validationFunc
compatibleVersion string
// removeVmaasCMPBasePath is used to remove the base path of the vmaas-cmp API, for use by the broker API
removeVmaasCMPBasePath bool
}

// do will call the API provided. this function will not return any response, but
Expand All @@ -49,7 +51,15 @@ func (a *api) do(ctx context.Context, request interface{}, queryParams map[strin
if a.path == "" || a.method == "" || a.client == nil || a.jsonParser == nil {
panic("api not properly configured")
}
a.path = fmt.Sprintf("%s/%s/%s", a.client.getHost(), consts.VmaasCmpAPIBasePath, a.path)

// 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)
} 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)
}

for _, validations := range a.validations {
err := validations()
Expand Down
68 changes: 68 additions & 0 deletions pkg/client/broker.go
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"
"net/http"

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.TFMorpheusDetails, error) {
// Get the service instance ID and Morpheus URL
ServiceSubscriptionDetailsResp := models.SubscriptionDetailsResponse{}
serviceSubscriptionDetailsAPI := &api{
method: http.MethodGet,
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.TFMorpheusDetails{}, fmt.Errorf("error getting service subscription details: %v", err)
}

// Get the Morpheus token
MorpheusTokenResp := models.MorpheusTokenResponse{}
morpheusTokenAPI := &api{
method: http.MethodGet,
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.TFMorpheusDetails{}, fmt.Errorf("error getting Morpheus token: %v", err)
}

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

return ret, nil
}
Loading

0 comments on commit fb2edb9

Please sign in to comment.