mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-09 23:25:33 -06:00
provider/null: null_data_source data source
A companion to the null_resource resource, this is here primarily to enable manual quick testing of data sources workflows without depending on any external services. The "inputs" map gets copied to the computed "outputs" map on read, "rand" gives a random number to exercise cases with constantly-changing values (an anti-pattern!), and "has_computed_default" is settable in config but computed if not set.
This commit is contained in:
parent
2ca10ad962
commit
f95dccf1b3
54
builtin/providers/null/data_source.go
Normal file
54
builtin/providers/null/data_source.go
Normal file
@ -0,0 +1,54 @@
|
||||
package null
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rand.Seed(time.Now().Unix())
|
||||
}
|
||||
|
||||
func dataSource() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Read: dataSourceRead,
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"inputs": &schema.Schema{
|
||||
Type: schema.TypeMap,
|
||||
Optional: true,
|
||||
},
|
||||
"outputs": &schema.Schema{
|
||||
Type: schema.TypeMap,
|
||||
Computed: true,
|
||||
},
|
||||
"random": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
"has_computed_default": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func dataSourceRead(d *schema.ResourceData, meta interface{}) error {
|
||||
|
||||
inputs := d.Get("inputs")
|
||||
d.Set("outputs", inputs)
|
||||
|
||||
d.Set("random", fmt.Sprintf("%d", rand.Int()))
|
||||
if d.Get("has_computed_default") == "" {
|
||||
d.Set("has_computed_default", "default")
|
||||
}
|
||||
|
||||
d.SetId("static")
|
||||
|
||||
return nil
|
||||
}
|
@ -13,5 +13,9 @@ func Provider() terraform.ResourceProvider {
|
||||
ResourcesMap: map[string]*schema.Resource{
|
||||
"null_resource": resource(),
|
||||
},
|
||||
|
||||
DataSourcesMap: map[string]*schema.Resource{
|
||||
"null_data_source": dataSource(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user