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