mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-27 00:46:25 -06:00
provider/test: Add more variants of maps
This commit adds a binary for the test provider, and adds a variety of different types of map to the schema.
This commit is contained in:
parent
074545e536
commit
75ef7ab636
15
builtin/bins/provider-test/main.go
Normal file
15
builtin/bins/provider-test/main.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/hashicorp/terraform/builtin/providers/test"
|
||||||
|
"github.com/hashicorp/terraform/plugin"
|
||||||
|
"github.com/hashicorp/terraform/terraform"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
plugin.Serve(&plugin.ServeOpts{
|
||||||
|
ProviderFunc: func() terraform.ResourceProvider {
|
||||||
|
return test.Provider()
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
@ -13,38 +13,94 @@ func testResource() *schema.Resource {
|
|||||||
Update: testResourceUpdate,
|
Update: testResourceUpdate,
|
||||||
Delete: testResourceDelete,
|
Delete: testResourceDelete,
|
||||||
Schema: map[string]*schema.Schema{
|
Schema: map[string]*schema.Schema{
|
||||||
"required": &schema.Schema{
|
"required": {
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Required: true,
|
Required: true,
|
||||||
},
|
},
|
||||||
"optional": &schema.Schema{
|
"optional": {
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
},
|
},
|
||||||
"optional_bool": &schema.Schema{
|
"optional_bool": {
|
||||||
Type: schema.TypeBool,
|
Type: schema.TypeBool,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
},
|
},
|
||||||
"optional_force_new": &schema.Schema{
|
"optional_force_new": {
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
ForceNew: true,
|
ForceNew: true,
|
||||||
},
|
},
|
||||||
"optional_computed_map": &schema.Schema{
|
"optional_computed_map": {
|
||||||
Type: schema.TypeMap,
|
Type: schema.TypeMap,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
Computed: true,
|
Computed: true,
|
||||||
},
|
},
|
||||||
"computed_read_only": &schema.Schema{
|
"computed_read_only": {
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Computed: true,
|
Computed: true,
|
||||||
ForceNew: true,
|
ForceNew: true,
|
||||||
},
|
},
|
||||||
"computed_read_only_force_new": &schema.Schema{
|
"computed_read_only_force_new": {
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Computed: true,
|
Computed: true,
|
||||||
ForceNew: true,
|
ForceNew: true,
|
||||||
},
|
},
|
||||||
|
"computed_list": {
|
||||||
|
Type: schema.TypeList,
|
||||||
|
Computed: true,
|
||||||
|
Elem: &schema.Schema{
|
||||||
|
Type: schema.TypeString,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"set": {
|
||||||
|
Type: schema.TypeSet,
|
||||||
|
Optional: true,
|
||||||
|
Elem: &schema.Schema{
|
||||||
|
Type: schema.TypeString,
|
||||||
|
},
|
||||||
|
Set: schema.HashString,
|
||||||
|
},
|
||||||
|
"computed_set": {
|
||||||
|
Type: schema.TypeSet,
|
||||||
|
Computed: true,
|
||||||
|
Elem: &schema.Schema{
|
||||||
|
Type: schema.TypeString,
|
||||||
|
},
|
||||||
|
Set: schema.HashString,
|
||||||
|
},
|
||||||
|
"map": {
|
||||||
|
Type: schema.TypeMap,
|
||||||
|
Optional: true,
|
||||||
|
},
|
||||||
|
"optional_map": {
|
||||||
|
Type: schema.TypeMap,
|
||||||
|
Optional: true,
|
||||||
|
},
|
||||||
|
"required_map": {
|
||||||
|
Type: schema.TypeMap,
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
"map_that_look_like_set": {
|
||||||
|
Type: schema.TypeMap,
|
||||||
|
Optional: true,
|
||||||
|
Elem: &schema.Schema{
|
||||||
|
Type: schema.TypeString,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"computed_map": {
|
||||||
|
Type: schema.TypeMap,
|
||||||
|
Computed: true,
|
||||||
|
},
|
||||||
|
"list_of_map": {
|
||||||
|
Type: schema.TypeList,
|
||||||
|
Optional: true,
|
||||||
|
Elem: &schema.Schema{
|
||||||
|
Type: schema.TypeMap,
|
||||||
|
Elem: &schema.Schema{
|
||||||
|
Type: schema.TypeString,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -56,6 +112,10 @@ func testResourceCreate(d *schema.ResourceData, meta interface{}) error {
|
|||||||
if _, ok := d.GetOk("required"); !ok {
|
if _, ok := d.GetOk("required"); !ok {
|
||||||
return fmt.Errorf("Missing attribute 'required', but it's required!")
|
return fmt.Errorf("Missing attribute 'required', but it's required!")
|
||||||
}
|
}
|
||||||
|
if _, ok := d.GetOk("required_map"); !ok {
|
||||||
|
return fmt.Errorf("Missing attribute 'required_map', but it's required!")
|
||||||
|
}
|
||||||
|
|
||||||
return testResourceRead(d, meta)
|
return testResourceRead(d, meta)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,6 +125,9 @@ func testResourceRead(d *schema.ResourceData, meta interface{}) error {
|
|||||||
if _, ok := d.GetOk("optional_computed_map"); !ok {
|
if _, ok := d.GetOk("optional_computed_map"); !ok {
|
||||||
d.Set("optional_computed_map", map[string]string{})
|
d.Set("optional_computed_map", map[string]string{})
|
||||||
}
|
}
|
||||||
|
d.Set("computed_map", map[string]string{"key1": "value1"})
|
||||||
|
d.Set("computed_list", []string{"listval1", "listval2"})
|
||||||
|
d.Set("computed_set", []string{"setval1", "setval2"})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,5 +136,6 @@ func testResourceUpdate(d *schema.ResourceData, meta interface{}) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testResourceDelete(d *schema.ResourceData, meta interface{}) error {
|
func testResourceDelete(d *schema.ResourceData, meta interface{}) error {
|
||||||
|
d.SetId("")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,9 @@ func TestResource_basic(t *testing.T) {
|
|||||||
Config: strings.TrimSpace(`
|
Config: strings.TrimSpace(`
|
||||||
resource "test_resource" "foo" {
|
resource "test_resource" "foo" {
|
||||||
required = "yep"
|
required = "yep"
|
||||||
|
required_map = {
|
||||||
|
key = "value"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
`),
|
`),
|
||||||
Check: func(s *terraform.State) error {
|
Check: func(s *terraform.State) error {
|
||||||
@ -36,10 +39,13 @@ func TestResource_ignoreChangesRequired(t *testing.T) {
|
|||||||
resource.TestStep{
|
resource.TestStep{
|
||||||
Config: strings.TrimSpace(`
|
Config: strings.TrimSpace(`
|
||||||
resource "test_resource" "foo" {
|
resource "test_resource" "foo" {
|
||||||
required = "yep"
|
required = "yep"
|
||||||
lifecycle {
|
required_map = {
|
||||||
ignore_changes = ["required"]
|
key = "value"
|
||||||
}
|
}
|
||||||
|
lifecycle {
|
||||||
|
ignore_changes = ["required"]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
`),
|
`),
|
||||||
Check: func(s *terraform.State) error {
|
Check: func(s *terraform.State) error {
|
||||||
@ -59,6 +65,9 @@ func TestResource_ignoreChangesEmpty(t *testing.T) {
|
|||||||
Config: strings.TrimSpace(`
|
Config: strings.TrimSpace(`
|
||||||
resource "test_resource" "foo" {
|
resource "test_resource" "foo" {
|
||||||
required = "yep"
|
required = "yep"
|
||||||
|
required_map = {
|
||||||
|
key = "value"
|
||||||
|
}
|
||||||
optional_force_new = "one"
|
optional_force_new = "one"
|
||||||
lifecycle {
|
lifecycle {
|
||||||
ignore_changes = []
|
ignore_changes = []
|
||||||
@ -73,6 +82,9 @@ resource "test_resource" "foo" {
|
|||||||
Config: strings.TrimSpace(`
|
Config: strings.TrimSpace(`
|
||||||
resource "test_resource" "foo" {
|
resource "test_resource" "foo" {
|
||||||
required = "yep"
|
required = "yep"
|
||||||
|
required_map = {
|
||||||
|
key = "value"
|
||||||
|
}
|
||||||
optional_force_new = "two"
|
optional_force_new = "two"
|
||||||
lifecycle {
|
lifecycle {
|
||||||
ignore_changes = []
|
ignore_changes = []
|
||||||
@ -96,6 +108,9 @@ func TestResource_ignoreChangesForceNew(t *testing.T) {
|
|||||||
Config: strings.TrimSpace(`
|
Config: strings.TrimSpace(`
|
||||||
resource "test_resource" "foo" {
|
resource "test_resource" "foo" {
|
||||||
required = "yep"
|
required = "yep"
|
||||||
|
required_map = {
|
||||||
|
key = "value"
|
||||||
|
}
|
||||||
optional_force_new = "one"
|
optional_force_new = "one"
|
||||||
lifecycle {
|
lifecycle {
|
||||||
ignore_changes = ["optional_force_new"]
|
ignore_changes = ["optional_force_new"]
|
||||||
@ -110,6 +125,9 @@ resource "test_resource" "foo" {
|
|||||||
Config: strings.TrimSpace(`
|
Config: strings.TrimSpace(`
|
||||||
resource "test_resource" "foo" {
|
resource "test_resource" "foo" {
|
||||||
required = "yep"
|
required = "yep"
|
||||||
|
required_map = {
|
||||||
|
key = "value"
|
||||||
|
}
|
||||||
optional_force_new = "two"
|
optional_force_new = "two"
|
||||||
lifecycle {
|
lifecycle {
|
||||||
ignore_changes = ["optional_force_new"]
|
ignore_changes = ["optional_force_new"]
|
||||||
@ -135,6 +153,9 @@ func TestResource_ignoreChangesForceNewBoolean(t *testing.T) {
|
|||||||
Config: strings.TrimSpace(`
|
Config: strings.TrimSpace(`
|
||||||
resource "test_resource" "foo" {
|
resource "test_resource" "foo" {
|
||||||
required = "yep"
|
required = "yep"
|
||||||
|
required_map = {
|
||||||
|
key = "value"
|
||||||
|
}
|
||||||
optional_force_new = "one"
|
optional_force_new = "one"
|
||||||
optional_bool = true
|
optional_bool = true
|
||||||
lifecycle {
|
lifecycle {
|
||||||
@ -150,6 +171,9 @@ resource "test_resource" "foo" {
|
|||||||
Config: strings.TrimSpace(`
|
Config: strings.TrimSpace(`
|
||||||
resource "test_resource" "foo" {
|
resource "test_resource" "foo" {
|
||||||
required = "yep"
|
required = "yep"
|
||||||
|
required_map = {
|
||||||
|
key = "value"
|
||||||
|
}
|
||||||
optional_force_new = "two"
|
optional_force_new = "two"
|
||||||
optional_bool = true
|
optional_bool = true
|
||||||
lifecycle {
|
lifecycle {
|
||||||
@ -174,6 +198,9 @@ func TestResource_ignoreChangesMap(t *testing.T) {
|
|||||||
Config: strings.TrimSpace(`
|
Config: strings.TrimSpace(`
|
||||||
resource "test_resource" "foo" {
|
resource "test_resource" "foo" {
|
||||||
required = "yep"
|
required = "yep"
|
||||||
|
required_map = {
|
||||||
|
key = "value"
|
||||||
|
}
|
||||||
optional_computed_map {
|
optional_computed_map {
|
||||||
foo = "bar"
|
foo = "bar"
|
||||||
}
|
}
|
||||||
@ -190,6 +217,9 @@ resource "test_resource" "foo" {
|
|||||||
Config: strings.TrimSpace(`
|
Config: strings.TrimSpace(`
|
||||||
resource "test_resource" "foo" {
|
resource "test_resource" "foo" {
|
||||||
required = "yep"
|
required = "yep"
|
||||||
|
required_map = {
|
||||||
|
key = "value"
|
||||||
|
}
|
||||||
optional_computed_map {
|
optional_computed_map {
|
||||||
foo = "bar"
|
foo = "bar"
|
||||||
no = "update"
|
no = "update"
|
||||||
|
Loading…
Reference in New Issue
Block a user