Skip to content

Commit

Permalink
Merge pull request #255 from eamonnotoole/morpheus-token-data-source
Browse files Browse the repository at this point in the history
Add morpheus_details data-source
  • Loading branch information
reubenur-rahman authored Sep 25, 2024
2 parents fc10c14 + 65508cd commit e455a03
Show file tree
Hide file tree
Showing 16 changed files with 363 additions and 18 deletions.
2 changes: 2 additions & 0 deletions acc-testcases/data-sources/morpheus_details.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
acc:
- config:
10 changes: 10 additions & 0 deletions examples/data-sources/hpegl_vmaas_morpheus_details/minimal.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# (C) Copyright 2024 Hewlett Packard Enterprise Development LP

data "hpegl_vmaas_morpheus_details" "morpheus_details" {}

provider "morpheus" {
url = data.hpegl_vmaas_morpheus_details.morpheus_details.url
access_token = data.hpegl_vmaas_morpheus_details.morpheus_details.access_token
}


46 changes: 46 additions & 0 deletions examples/data-sources/hpegl_vmaas_morpheus_details/multiple.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# (C) Copyright 2024 Hewlett Packard Enterprise Development LP

# Location 1
provider "hpegl" {
vmaas {
location = var.location_1
space_name = var.space_1
}

alias = "location_1"
}

data "hpegl_vmaas_morpheus_details" "location_1" {
provider = hpegl.location_1
}

provider "morpheus" {
url = data.hpegl_vmaas_morpheus_details.location_1.url
access_token = data.hpegl_vmaas_morpheus_details.location_1.access_token

alias = "morpheus_location_1"
}


# Location 2
provider "hpegl" {
vmaas {
location = var.location_2
space_name = var.space_2
}

alias = "location_2"
}

data "hpegl_vmaas_morpheus_details" "location_2" {
provider = hpegl.location_2
}

provider "morpheus" {
url = data.hpegl_vmaas_morpheus_details.location_2.url
access_token = data.hpegl_vmaas_morpheus_details.location_2.access_token

alias = "morpheus_location_2"
}


2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/HewlettPackard/hpegl-vmaas-terraform-resources
go 1.21

require (
github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.11
github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.12
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
Expand Down
4 changes: 2 additions & 2 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.11 h1:KnXixte97F83KVsqppc5olEjFmCmccIhIPfR0s3gSVw=
github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.11/go.mod h1:UzXizYR6sUVsvDRMcLC2E4eabJsPH/3nM8rlKL2BLSc=
github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.12 h1:AVRTfWXyDYEa5FdTVSDBH2MHw7Fy5X3W4mv89cnAwHc=
github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.12/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
29 changes: 29 additions & 0 deletions internal/acceptance_test/data_source_morpheus_details_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// (C) Copyright 2024 Hewlett Packard Enterprise Development LP

package acceptancetest

import (
"testing"

api_client "github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/client"
"github.com/HewlettPackard/hpegl-vmaas-terraform-resources/pkg/atf"
)

func TestAccDataSourceMorpheusDetails(t *testing.T) {
acc := &atf.Acc{
PreCheck: testAccPreCheck,
Providers: testAccProviders,
ResourceName: "hpegl_vmaas_morpheus_details",
GetAPI: func(attr map[string]string) (interface{}, error) {
cl, cfg := getBrokerAPIClient()
iClient := api_client.BrokerAPIService{
Client: cl,
Cfg: cfg,
}

return iClient.GetMorpheusDetails(getAccContext())
},
}

acc.RunDataSourceTests(t)
}
57 changes: 45 additions & 12 deletions internal/acceptance_test/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,45 @@ import (
)

func getAPIClient() (*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()
// No need to set the default query params for broker API
cfg := api_client.Configuration{
Host: os.Getenv("HPEGL_VMAAS_BROKER_URL"),
DefaultHeader: headers,
}
brokerAPIClient := api_client.NewAPIClient(&cfg)
err := apiClientSetMeta(brokerAPIClient, iamVersion)
if err != nil {
log.Printf("[WARN] Error: %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
}

func getHeadersAndQueryParamsAndIAMVersion() (map[string]string, map[string]string, string) {
var headers map[string]string
if utils.GetEnvBool("TF_ACC_MOCK_IAM") {
headers = make(map[string]string)
Expand All @@ -31,13 +70,12 @@ func getAPIClient() (*api_client.APIClient, api_client.Configuration) {
} else {
queryParam[constants.SpaceKey] = os.Getenv("HPEGL_VMAAS_SPACE_NAME")
}
cfg := api_client.Configuration{
Host: os.Getenv("HPEGL_VMAAS_API_URL"),
DefaultHeader: headers,
DefaultQueryParams: queryParam,
}
apiClient := api_client.NewAPIClient(&cfg)
err := apiClient.SetMeta(nil, func(ctx *context.Context, meta interface{}) {

return headers, queryParam, iamVersion
}

func apiClientSetMeta(apiClient *api_client.APIClient, iamVersion string) error {
return apiClient.SetMeta(nil, func(ctx *context.Context, meta interface{}) {
d := &utils.ResourceData{
Data: map[string]interface{}{
"iam_service_url": os.Getenv("HPEGL_IAM_SERVICE_URL"),
Expand Down Expand Up @@ -68,11 +106,6 @@ func getAPIClient() (*api_client.APIClient, api_client.Configuration) {
*ctx = context.WithValue(*ctx, api_client.ContextAccessToken, token)
}
})
if err != nil {
log.Printf("[WARN] Error: %s", err)
}

return apiClient, cfg
}

func toInt(s string) int {
Expand Down
19 changes: 19 additions & 0 deletions internal/cmp/broker_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// (C) Copyright 2024 Hewlett Packard Enterprise Development LP

package cmp

import apiClient "github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/client"

// BrokerClient - struct to hold the broker client details
type BrokerClient struct {
DSMorpheusDetails DataSource
}

// NewBrokerClient - function to create a new broker client
func NewBrokerClient(client *apiClient.APIClient, cfg apiClient.Configuration) *BrokerClient {
return &BrokerClient{
DSMorpheusDetails: newMorpheusBroker(
&apiClient.BrokerAPIService{Client: client, Cfg: cfg},
),
}
}
56 changes: 56 additions & 0 deletions internal/cmp/morpheus_datasource_broker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// (C) Copyright 2024 Hewlett Packard Enterprise Development LP

package cmp

import (
"context"
"fmt"
"strconv"
"time"

"github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk/pkg/client"
"github.com/HewlettPackard/hpegl-vmaas-terraform-resources/internal/utils"
)

// morpheusBroker is used to read morpheus details using the Broker API
type morpheusBroker struct {
bClient *client.BrokerAPIService
}

func newMorpheusBroker(bClient *client.BrokerAPIService) *morpheusBroker {
return &morpheusBroker{bClient: bClient}
}

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

// Get Morpheus Tokens and URL
morpheusDetails, err := m.bClient.GetMorpheusDetails(ctx)
if err != nil {
return err
}

// Convert the Unix timestamp to Duration in seconds expressed as a string
validDuration := time.Until(time.UnixMilli(morpheusDetails.ValidTill))
// We do the following since we cannot get a string representation of a Duration in seconds
validSeconds := validDuration.Round(time.Second).Seconds() // Round to the nearest second, in float64
validSecondsString := fmt.Sprintf("%ss", strconv.FormatFloat(validSeconds, 'f', -1, 64))

// Set all of the details
d.SetId(morpheusDetails.ID)

if err = d.Set("access_token", morpheusDetails.AccessToken); err != nil {
return err
}

if err = d.Set("valid_till", validSecondsString); err != nil {
return err
}

if err = d.Set("url", morpheusDetails.URL); err != nil {
return err
}

return nil
}
2 changes: 2 additions & 0 deletions internal/resources/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const (
DSDhcpServer = "hpegl_vmaas_dhcp_server"
DSInstanceStorageType = "hpegl_vmaas_instance_disk_type"

DSMorpheusDataSource = "hpegl_vmaas_morpheus_details"

// resource key
ResInstance = "hpegl_vmaas_instance"
ResInstanceClone = "hpegl_vmaas_instance_clone"
Expand Down
62 changes: 62 additions & 0 deletions internal/resources/data_source_morpheus_token_broker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// (C) Copyright 2024 Hewlett Packard Enterprise Development LP

package resources

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/HewlettPackard/hpegl-vmaas-terraform-resources/internal/utils"
"github.com/HewlettPackard/hpegl-vmaas-terraform-resources/pkg/client"
)

// MorpheusDetailsBroker returns a schema.Resource for the MorpheusDetails data source
func MorpheusDetailsBroker() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"access_token": {
Type: schema.TypeString,
Computed: true,
Description: "Morpheus access_token",
Sensitive: true,
},
"valid_till": {
Type: schema.TypeString,
Computed: true,
Description: "time until the token expires, in seconds",
Sensitive: false,
},
"url": {
Type: schema.TypeString,
Computed: true,
Description: "Morpheus URL",
Sensitive: false,
},
},
ReadContext: MorpheusDetailsBrokerReadContext,
Description: `The ` + DSMorpheusDataSource + ` data source can be used to get a details of the Morpheus instance
used by VMaaS. The details that can be retrieved are the access_token, valid_till (time until the token expires,
in seconds), and the URL of the Morpheus instance.`,
SchemaVersion: 0,
StateUpgraders: nil,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}

func MorpheusDetailsBrokerReadContext(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, err := client.GetClientFromMetaMap(meta)
if err != nil {
return diag.FromErr(err)
}

data := utils.NewData(d)
err = c.BrokerClient.DSMorpheusDetails.Read(ctx, data, meta)
if err != nil {
return diag.FromErr(err)
}

return nil
}
Loading

0 comments on commit e455a03

Please sign in to comment.