-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add implementation for "secret" datasource
Update the boiler plate to actually implement the required API calls. Add tests, examples, etc. These changes mean that we can now read existing "secrets" and add them to, eg, hypervisor server definitions. ``` data "hpegl_pc_secret" "my_secret" { name = "mysecret1" } . . . esx_root_credential_id = data.hpegl_pc_secret.my_secret.id ```
- Loading branch information
1 parent
07c1864
commit d966ad0
Showing
9 changed files
with
325 additions
and
2 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
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
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,12 @@ | ||
// (C) Copyright 2024 Hewlett Packard Enterprise Development LP | ||
//go:build simulation | ||
|
||
package secret | ||
|
||
import ( | ||
"github.com/HewlettPackard/hpegl-pcbe-terraform-resources/internal/simulator" | ||
) | ||
|
||
func init() { | ||
simulator.Secret() | ||
} |
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 @@ | ||
{"count":1,"total":1,"offset":0,"items":[{"customerId":"123456da3faa11efb5afca2247c626ef","service":"Private Cloud Business Edition","id":"cccfcad1-85b7-4162-b16e-f7cadc2c46b5","name":"mysecret1","type":"/data-services/secret","resourceUri":"/data-services/v1beta1/secrets/cccfcad1-85b7-4162-b16e-f7cadc2c46b5","generation":5,"updatedAt":"2024-12-04T14:39:23.912452+00:00","createdAt":"2024-10-22T15:56:55.824878+00:00","groups":[{"id":"123456da3faa11efb5afca2247c626ef","name":"Default Group"}],"label":"PCBE-NIMBLE-SYSTEM","domain":{"name":"CONFIGURATION","properties":{"CREATED_BY":"[email protected]","LAST_UPDATED_BY":"[email protected]","LIFECYCLE_EVENT_KEY":"pluginSecrets"}},"classifier":{"name":"SECRET"},"subclassifier":{"name":"BASIC_AUTH","properties":{"USERNAME":"admin"}},"status":"NOT_APPLIED","statusUpdatedAt":"2024-12-04T14:39:23.912452+00:00","assignmentsCount":0,"policy":"pcbeBasicAuth.nimbleSystem"}]} |
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,39 @@ | ||
// (C) Copyright 2024 Hewlett Packard Enterprise Development LP | ||
|
||
package simulator | ||
|
||
import ( | ||
_ "embed" | ||
|
||
"github.com/h2non/gock" | ||
) | ||
|
||
// TODO: (API) Replace fake data with real data when possible | ||
// | ||
//go:embed fixtures/secrets/getByName.json | ||
var secretByName string | ||
|
||
func simulateSecretGetByName() { | ||
secretName := "mysecret1" | ||
|
||
gock.New("http://localhost"). | ||
Get("/data-services/v1beta1/secrets"). | ||
MatchParam("filter", "name eq "+secretName). | ||
MatchHeader("Authorization", "Bearer abcdefghijklmnopqrstuvwxyz-0123456789"). | ||
Reply(200). | ||
SetHeader("Content-Type", "application/json"). | ||
BodyString(secretByName) | ||
|
||
gock.New("http://localhost"). | ||
Get("/data-services/v1beta1/secrets"). | ||
MatchParam("filter", "name eq "+secretName). | ||
MatchHeader("Authorization", "Bearer expired-token"). | ||
Reply(401). | ||
SetHeader("Content-Type", "text/plain"). | ||
BodyString("Jwt is not in the form of Header.Payload.Signature " + | ||
"with two dots and 3 sections") | ||
} | ||
|
||
func Secret() { | ||
simulateSecretGetByName() | ||
} |
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,167 @@ | ||
// (C) Copyright 2024 Hewlett Packard Enterprise Development LP | ||
|
||
package acceptance | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/HewlettPackard/hpegl-pcbe-terraform-resources/internal/provider" | ||
"github.com/google/uuid" | ||
"github.com/hashicorp/terraform-plugin-framework/providerserver" | ||
"github.com/hashicorp/terraform-plugin-go/tfprotov6" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/terraform" | ||
) | ||
|
||
const ( | ||
providerConfig = ` | ||
terraform { | ||
required_providers { | ||
hpegl = { | ||
source = "github.com/HewlettPackard/hpegl-pcbe-terraform-resources" | ||
} | ||
} | ||
} | ||
provider "hpegl" { | ||
pc { | ||
host = "http://localhost:8080" | ||
token = "abcdefghijklmnopqrstuvwxyz-0123456789" | ||
http_dump = true | ||
poll_interval = 0.001 | ||
max_polls = 10 | ||
} | ||
} | ||
` | ||
) | ||
|
||
var simulation = false | ||
|
||
var testAccProtoV6ProviderFactories = map[string]func() ( | ||
tfprotov6.ProviderServer, error, | ||
){ | ||
"scaffolding": providerserver.NewProtocol6WithError( | ||
provider.New("test")(), | ||
), | ||
} | ||
|
||
func checkUUIDAttr(resource string, attr string) func(*terraform.State) error { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[resource] | ||
if !ok { | ||
return fmt.Errorf("resource not found: %s", resource) | ||
} | ||
|
||
attrValue := rs.Primary.Attributes[attr] | ||
_, err := uuid.Parse(attrValue) | ||
|
||
return err | ||
} | ||
} | ||
|
||
func TestAccSecretDataSource(t *testing.T) { | ||
config := providerConfig + ` | ||
data "hpegl_pc_secret" "test" { | ||
name = "mysecret1" | ||
} | ||
` | ||
|
||
checks := []resource.TestCheckFunc{ | ||
resource.TestCheckResourceAttr( | ||
"data.hpegl_pc_secret.test", | ||
"name", | ||
"mysecret1", | ||
), | ||
checkUUIDAttr("data.hpegl_pc_secret.test", "id"), | ||
} | ||
|
||
if simulation { | ||
// In simulation mode the ID value is known in advance | ||
checks = append(checks, | ||
resource.TestCheckResourceAttr( | ||
"data.hpegl_pc_secret.test", | ||
"id", | ||
"cccfcad1-85b7-4162-b16e-f7cadc2c46b5", | ||
), | ||
resource.TestCheckResourceAttr( | ||
"data.hpegl_pc_secret.test", | ||
"name", | ||
"mysecret1", | ||
), | ||
) | ||
} | ||
|
||
checkFn := resource.ComposeAggregateTestCheckFunc(checks...) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: checkFn, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccSecretDataSourceMissingName(t *testing.T) { | ||
config := providerConfig + ` | ||
data "hpegl_pc_secret" "test" { | ||
} | ||
` | ||
expected := `The argument "name" is required, but no definition was found.` | ||
resource.Test(t, resource.TestCase{ | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
ExpectError: regexp.MustCompile(expected), | ||
PlanOnly: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccSystemDataSourceBadAuth(t *testing.T) { | ||
providerConfigBadAuth := ` | ||
terraform { | ||
required_providers { | ||
hpegl = { | ||
source = "github.com/HewlettPackard/hpegl-pcbe-terraform-resources" | ||
} | ||
} | ||
} | ||
provider "hpegl" { | ||
pc { | ||
host = "http://localhost:8080" | ||
token = "expired-token" | ||
http_dump = true | ||
poll_interval = 0.001 | ||
max_polls = 10 | ||
} | ||
} | ||
` | ||
config := providerConfigBadAuth + ` | ||
data "hpegl_pc_secret" "test" { | ||
name = "mysecret1" | ||
} | ||
` | ||
// TODO: return more informative error message - including | ||
// http response code (requires change to request handler) | ||
expected := `text does not support structured data` | ||
resource.Test(t, resource.TestCase{ | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
ExpectError: regexp.MustCompile(expected), | ||
PlanOnly: true, | ||
}, | ||
}, | ||
}) | ||
} |
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,9 @@ | ||
// (C) Copyright 2024 Hewlett Packard Enterprise Development LP | ||
//go:build simulation | ||
// +build simulation | ||
|
||
package acceptance | ||
|
||
func init() { | ||
simulation = true | ||
} |