-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #255 from eamonnotoole/morpheus-token-data-source
Add morpheus_details data-source
- Loading branch information
Showing
16 changed files
with
363 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
acc: | ||
- config: |
10 changes: 10 additions & 0 deletions
10
examples/data-sources/hpegl_vmaas_morpheus_details/minimal.tf
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,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
46
examples/data-sources/hpegl_vmaas_morpheus_details/multiple.tf
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,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" | ||
} | ||
|
||
|
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
29 changes: 29 additions & 0 deletions
29
internal/acceptance_test/data_source_morpheus_details_test.go
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,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) | ||
} |
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,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}, | ||
), | ||
} | ||
} |
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,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 | ||
} |
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,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 | ||
} |
Oops, something went wrong.