github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/builtin/providers/test/resource_nested_set.go (about)

     1  package test
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  func testResourceNestedSet() *schema.Resource {
    11  	return &schema.Resource{
    12  		Create: testResourceNestedSetCreate,
    13  		Read:   testResourceNestedSetRead,
    14  		Delete: testResourceNestedSetDelete,
    15  		Update: testResourceNestedSetUpdate,
    16  
    17  		Importer: &schema.ResourceImporter{
    18  			State: schema.ImportStatePassthrough,
    19  		},
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"optional": {
    23  				Type:     schema.TypeBool,
    24  				Optional: true,
    25  			},
    26  			"force_new": {
    27  				Type:     schema.TypeString,
    28  				Optional: true,
    29  				ForceNew: true,
    30  			},
    31  			"type_list": {
    32  				Type:     schema.TypeList,
    33  				Optional: true,
    34  				Elem: &schema.Resource{
    35  					Schema: map[string]*schema.Schema{
    36  						"value": {
    37  							Type:     schema.TypeString,
    38  							ForceNew: true,
    39  							Optional: true,
    40  						},
    41  					},
    42  				},
    43  			},
    44  			"single": {
    45  				Type:     schema.TypeSet,
    46  				Optional: true,
    47  				MaxItems: 1,
    48  				Elem: &schema.Resource{
    49  					Schema: map[string]*schema.Schema{
    50  						"value": {
    51  							Type:     schema.TypeString,
    52  							ForceNew: true,
    53  							Required: true,
    54  						},
    55  
    56  						"optional": {
    57  							Type:     schema.TypeString,
    58  							ForceNew: true,
    59  							Optional: true,
    60  							Computed: true,
    61  						},
    62  					},
    63  				},
    64  			},
    65  			"multi": {
    66  				Type:     schema.TypeSet,
    67  				Optional: true,
    68  				Elem: &schema.Resource{
    69  					Schema: map[string]*schema.Schema{
    70  						"set": {
    71  							Type:     schema.TypeSet,
    72  							Optional: true,
    73  							Elem: &schema.Resource{
    74  								Schema: map[string]*schema.Schema{
    75  									"required": {
    76  										Type:     schema.TypeString,
    77  										Required: true,
    78  										ForceNew: true,
    79  									},
    80  									"optional_int": {
    81  										Type:     schema.TypeInt,
    82  										Optional: true,
    83  									},
    84  									"bool": {
    85  										Type:     schema.TypeBool,
    86  										Optional: true,
    87  									},
    88  								},
    89  							},
    90  						},
    91  
    92  						"optional": {
    93  							Type: schema.TypeString,
    94  							// commenting this causes it to get missed during apply
    95  							//ForceNew: true,
    96  							Optional: true,
    97  						},
    98  						"bool": {
    99  							Type:     schema.TypeBool,
   100  							Optional: true,
   101  						},
   102  					},
   103  				},
   104  			},
   105  			"with_list": {
   106  				Type:     schema.TypeSet,
   107  				Optional: true,
   108  				Elem: &schema.Resource{
   109  					Schema: map[string]*schema.Schema{
   110  						"required": {
   111  							Type:     schema.TypeString,
   112  							Required: true,
   113  						},
   114  						"list": {
   115  							Type:     schema.TypeList,
   116  							Optional: true,
   117  							Elem: &schema.Schema{
   118  								Type: schema.TypeString,
   119  							},
   120  						},
   121  						"list_block": {
   122  							Type:     schema.TypeList,
   123  							Optional: true,
   124  							Elem: &schema.Resource{
   125  								Schema: map[string]*schema.Schema{
   126  									"unused": {
   127  										Type:     schema.TypeString,
   128  										Optional: true,
   129  									},
   130  								},
   131  							},
   132  						},
   133  					},
   134  				},
   135  			},
   136  		},
   137  	}
   138  }
   139  
   140  func testResourceNestedSetCreate(d *schema.ResourceData, meta interface{}) error {
   141  	id := fmt.Sprintf("%x", rand.Int63())
   142  	d.SetId(id)
   143  
   144  	// replicate some awkward handling of a computed value in a set
   145  	set := d.Get("single").(*schema.Set)
   146  	l := set.List()
   147  	if len(l) == 1 {
   148  		if s, ok := l[0].(map[string]interface{}); ok {
   149  			if v, _ := s["optional"].(string); v == "" {
   150  				s["optional"] = id
   151  			}
   152  		}
   153  	}
   154  
   155  	d.Set("single", set)
   156  
   157  	return testResourceNestedSetRead(d, meta)
   158  }
   159  
   160  func testResourceNestedSetRead(d *schema.ResourceData, meta interface{}) error {
   161  	return nil
   162  }
   163  
   164  func testResourceNestedSetDelete(d *schema.ResourceData, meta interface{}) error {
   165  	d.SetId("")
   166  	return nil
   167  }
   168  
   169  func testResourceNestedSetUpdate(d *schema.ResourceData, meta interface{}) error {
   170  	return nil
   171  }