github.com/lymingtonprecision/terraform@v0.9.9-0.20170613092852-62acef9611a9/builtin/providers/test/resource.go (about)

     1  package test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/terraform/helper/schema"
     7  )
     8  
     9  func testResource() *schema.Resource {
    10  	return &schema.Resource{
    11  		Create: testResourceCreate,
    12  		Read:   testResourceRead,
    13  		Update: testResourceUpdate,
    14  		Delete: testResourceDelete,
    15  
    16  		Importer: &schema.ResourceImporter{
    17  			State: schema.ImportStatePassthrough,
    18  		},
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"required": {
    22  				Type:     schema.TypeString,
    23  				Required: true,
    24  			},
    25  			"optional": {
    26  				Type:     schema.TypeString,
    27  				Optional: true,
    28  			},
    29  			"optional_bool": {
    30  				Type:     schema.TypeBool,
    31  				Optional: true,
    32  			},
    33  			"optional_force_new": {
    34  				Type:     schema.TypeString,
    35  				Optional: true,
    36  				ForceNew: true,
    37  			},
    38  			"optional_computed_map": {
    39  				Type:     schema.TypeMap,
    40  				Optional: true,
    41  				Computed: true,
    42  			},
    43  			"optional_computed_force_new": {
    44  				Type:     schema.TypeString,
    45  				Optional: true,
    46  				Computed: true,
    47  				ForceNew: true,
    48  			},
    49  			"computed_read_only": {
    50  				Type:     schema.TypeString,
    51  				Computed: true,
    52  				ForceNew: true,
    53  			},
    54  			"computed_from_required": {
    55  				Type:     schema.TypeString,
    56  				Computed: true,
    57  				ForceNew: true,
    58  			},
    59  			"computed_read_only_force_new": {
    60  				Type:     schema.TypeString,
    61  				Computed: true,
    62  				ForceNew: true,
    63  			},
    64  			"computed_list": {
    65  				Type:     schema.TypeList,
    66  				Computed: true,
    67  				Elem: &schema.Schema{
    68  					Type: schema.TypeString,
    69  				},
    70  			},
    71  			"set": {
    72  				Type:     schema.TypeSet,
    73  				Optional: true,
    74  				Elem: &schema.Schema{
    75  					Type: schema.TypeString,
    76  				},
    77  				Set: schema.HashString,
    78  			},
    79  			"computed_set": {
    80  				Type:     schema.TypeSet,
    81  				Computed: true,
    82  				Elem: &schema.Schema{
    83  					Type: schema.TypeString,
    84  				},
    85  				Set: schema.HashString,
    86  			},
    87  			"map": {
    88  				Type:     schema.TypeMap,
    89  				Optional: true,
    90  			},
    91  			"optional_map": {
    92  				Type:     schema.TypeMap,
    93  				Optional: true,
    94  			},
    95  			"required_map": {
    96  				Type:     schema.TypeMap,
    97  				Required: true,
    98  			},
    99  			"map_that_look_like_set": {
   100  				Type:     schema.TypeMap,
   101  				Optional: true,
   102  				Elem: &schema.Schema{
   103  					Type: schema.TypeString,
   104  				},
   105  			},
   106  			"computed_map": {
   107  				Type:     schema.TypeMap,
   108  				Computed: true,
   109  			},
   110  			"list": {
   111  				Type:     schema.TypeList,
   112  				Optional: true,
   113  				Elem: &schema.Schema{
   114  					Type: schema.TypeString,
   115  				},
   116  			},
   117  			"list_of_map": {
   118  				Type:     schema.TypeList,
   119  				Optional: true,
   120  				Elem: &schema.Schema{
   121  					Type: schema.TypeMap,
   122  					Elem: &schema.Schema{
   123  						Type: schema.TypeString,
   124  					},
   125  				},
   126  			},
   127  		},
   128  	}
   129  }
   130  
   131  func testResourceCreate(d *schema.ResourceData, meta interface{}) error {
   132  	d.SetId("testId")
   133  
   134  	// Required must make it through to Create
   135  	if _, ok := d.GetOk("required"); !ok {
   136  		return fmt.Errorf("Missing attribute 'required', but it's required!")
   137  	}
   138  	if _, ok := d.GetOk("required_map"); !ok {
   139  		return fmt.Errorf("Missing attribute 'required_map', but it's required!")
   140  	}
   141  
   142  	d.Set("computed_from_required", d.Get("required"))
   143  
   144  	return testResourceRead(d, meta)
   145  }
   146  
   147  func testResourceRead(d *schema.ResourceData, meta interface{}) error {
   148  	d.Set("computed_read_only", "value_from_api")
   149  	d.Set("computed_read_only_force_new", "value_from_api")
   150  	if _, ok := d.GetOk("optional_computed_map"); !ok {
   151  		d.Set("optional_computed_map", map[string]string{})
   152  	}
   153  	d.Set("computed_map", map[string]string{"key1": "value1"})
   154  	d.Set("computed_list", []string{"listval1", "listval2"})
   155  	d.Set("computed_set", []string{"setval1", "setval2"})
   156  	return nil
   157  }
   158  
   159  func testResourceUpdate(d *schema.ResourceData, meta interface{}) error {
   160  	return nil
   161  }
   162  
   163  func testResourceDelete(d *schema.ResourceData, meta interface{}) error {
   164  	d.SetId("")
   165  	return nil
   166  }