mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-08 15:13:56 -06:00
e772b45970
* "external" provider for gluing in external logic This provider will become a bit of glue to help people interface external programs with Terraform without writing a full Terraform provider. It will be nowhere near as capable as a first-class provider, but is intended as a light-touch way to integrate some pre-existing or custom system into Terraform. * Unit test for the "resourceProvider" utility function This small function determines the dependable name of a provider for a given resource name and optional provider alias. It's simple but it's a key part of how resource nodes get connected to provider nodes so worth specifying the intended behavior in the form of a test. * Allow a provider to export a resource with the provider's name If a provider only implements one resource of each type (managed vs. data) then it can be reasonable for the resource names to exactly match the provider name, if the provider name is descriptive enough for the purpose of the each resource to be obvious. * provider/external: data source A data source that executes a child process, expecting it to support a particular gateway protocol, and exports its result. This can be used as a straightforward way to retrieve data from sources that Terraform doesn't natively support.. * website: documentation for the "external" provider
125 lines
2.6 KiB
Go
125 lines
2.6 KiB
Go
package external
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path"
|
|
"regexp"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
"github.com/hashicorp/terraform/terraform"
|
|
)
|
|
|
|
const testDataSourceConfig_basic = `
|
|
data "external" "test" {
|
|
program = ["%s", "cheese"]
|
|
|
|
query = {
|
|
value = "pizza"
|
|
}
|
|
}
|
|
|
|
output "query_value" {
|
|
value = "${data.external.test.result["query_value"]}"
|
|
}
|
|
|
|
output "argument" {
|
|
value = "${data.external.test.result["argument"]}"
|
|
}
|
|
`
|
|
|
|
func TestDataSource_basic(t *testing.T) {
|
|
programPath, err := buildDataSourceTestProgram()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
return
|
|
}
|
|
|
|
resource.UnitTest(t, resource.TestCase{
|
|
Providers: testProviders,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: fmt.Sprintf(testDataSourceConfig_basic, programPath),
|
|
Check: func(s *terraform.State) error {
|
|
_, ok := s.RootModule().Resources["data.external.test"]
|
|
if !ok {
|
|
return fmt.Errorf("missing data resource")
|
|
}
|
|
|
|
outputs := s.RootModule().Outputs
|
|
|
|
if outputs["argument"] == nil {
|
|
return fmt.Errorf("missing 'argument' output")
|
|
}
|
|
if outputs["query_value"] == nil {
|
|
return fmt.Errorf("missing 'query_value' output")
|
|
}
|
|
|
|
if outputs["argument"].Value != "cheese" {
|
|
return fmt.Errorf(
|
|
"'argument' output is %q; want 'cheese'",
|
|
outputs["argument"].Value,
|
|
)
|
|
}
|
|
if outputs["query_value"].Value != "pizza" {
|
|
return fmt.Errorf(
|
|
"'query_value' output is %q; want 'pizza'",
|
|
outputs["query_value"].Value,
|
|
)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
const testDataSourceConfig_error = `
|
|
data "external" "test" {
|
|
program = ["%s"]
|
|
|
|
query = {
|
|
fail = "true"
|
|
}
|
|
}
|
|
`
|
|
|
|
func TestDataSource_error(t *testing.T) {
|
|
programPath, err := buildDataSourceTestProgram()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
return
|
|
}
|
|
|
|
resource.UnitTest(t, resource.TestCase{
|
|
Providers: testProviders,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: fmt.Sprintf(testDataSourceConfig_error, programPath),
|
|
ExpectError: regexp.MustCompile("I was asked to fail"),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func buildDataSourceTestProgram() (string, error) {
|
|
// We have a simple Go program that we use as a stub for testing.
|
|
cmd := exec.Command(
|
|
"go", "install",
|
|
"github.com/hashicorp/terraform/builtin/providers/external/test-programs/tf-acc-external-data-source",
|
|
)
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to build test stub program: %s", err)
|
|
}
|
|
|
|
programPath := path.Join(
|
|
os.Getenv("GOPATH"), "bin", "tf-acc-external-data-source",
|
|
)
|
|
return programPath, nil
|
|
}
|