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
94 lines
2.0 KiB
Go
94 lines
2.0 KiB
Go
package external
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os/exec"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
)
|
|
|
|
func dataSource() *schema.Resource {
|
|
return &schema.Resource{
|
|
Read: dataSourceRead,
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
"program": &schema.Schema{
|
|
Type: schema.TypeList,
|
|
Required: true,
|
|
Elem: &schema.Schema{
|
|
Type: schema.TypeString,
|
|
},
|
|
},
|
|
|
|
"query": &schema.Schema{
|
|
Type: schema.TypeMap,
|
|
Optional: true,
|
|
Elem: &schema.Schema{
|
|
Type: schema.TypeString,
|
|
},
|
|
},
|
|
|
|
"result": &schema.Schema{
|
|
Type: schema.TypeMap,
|
|
Computed: true,
|
|
Elem: &schema.Schema{
|
|
Type: schema.TypeString,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func dataSourceRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
programI := d.Get("program").([]interface{})
|
|
query := d.Get("query").(map[string]interface{})
|
|
|
|
// This would be a ValidateFunc if helper/schema allowed these
|
|
// to be applied to lists.
|
|
if err := validateProgramAttr(programI); err != nil {
|
|
return err
|
|
}
|
|
|
|
program := make([]string, len(programI))
|
|
for i, vI := range programI {
|
|
program[i] = vI.(string)
|
|
}
|
|
|
|
cmd := exec.Command(program[0], program[1:]...)
|
|
|
|
queryJson, err := json.Marshal(query)
|
|
if err != nil {
|
|
// Should never happen, since we know query will always be a map
|
|
// from string to string, as guaranteed by d.Get and our schema.
|
|
return err
|
|
}
|
|
|
|
cmd.Stdin = bytes.NewReader(queryJson)
|
|
|
|
resultJson, err := cmd.Output()
|
|
if err != nil {
|
|
if exitErr, ok := err.(*exec.ExitError); ok {
|
|
if exitErr.Stderr != nil && len(exitErr.Stderr) > 0 {
|
|
return fmt.Errorf("failed to execute %q: %s", program[0], string(exitErr.Stderr))
|
|
}
|
|
return fmt.Errorf("command %q failed with no error message", program[0])
|
|
} else {
|
|
return fmt.Errorf("failed to execute %q: %s", program[0], err)
|
|
}
|
|
}
|
|
|
|
result := map[string]string{}
|
|
err = json.Unmarshal(resultJson, &result)
|
|
if err != nil {
|
|
return fmt.Errorf("command %q produced invalid JSON: %s", program[0], err)
|
|
}
|
|
|
|
d.Set("result", result)
|
|
|
|
d.SetId("-")
|
|
return nil
|
|
}
|