github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/chef/resource_data_bag.go (about) 1 package chef 2 3 import ( 4 "github.com/hashicorp/terraform/helper/schema" 5 6 chefc "github.com/go-chef/chef" 7 ) 8 9 func resourceChefDataBag() *schema.Resource { 10 return &schema.Resource{ 11 Create: CreateDataBag, 12 Read: ReadDataBag, 13 Delete: DeleteDataBag, 14 15 Schema: map[string]*schema.Schema{ 16 "name": &schema.Schema{ 17 Type: schema.TypeString, 18 Required: true, 19 ForceNew: true, 20 }, 21 "api_uri": &schema.Schema{ 22 Type: schema.TypeString, 23 Computed: true, 24 }, 25 }, 26 } 27 } 28 29 func CreateDataBag(d *schema.ResourceData, meta interface{}) error { 30 client := meta.(*chefc.Client) 31 32 dataBag := &chefc.DataBag{ 33 Name: d.Get("name").(string), 34 } 35 36 result, err := client.DataBags.Create(dataBag) 37 if err != nil { 38 return err 39 } 40 41 d.SetId(dataBag.Name) 42 d.Set("api_uri", result.URI) 43 return nil 44 } 45 46 func ReadDataBag(d *schema.ResourceData, meta interface{}) error { 47 client := meta.(*chefc.Client) 48 49 // The Chef API provides no API to read a data bag's metadata, 50 // but we can try to read its items and use that as a proxy for 51 // whether it still exists. 52 53 name := d.Id() 54 55 _, err := client.DataBags.ListItems(name) 56 if err != nil { 57 if errRes, ok := err.(*chefc.ErrorResponse); ok { 58 if errRes.Response.StatusCode == 404 { 59 d.SetId("") 60 return nil 61 } 62 } 63 } 64 return err 65 } 66 67 func DeleteDataBag(d *schema.ResourceData, meta interface{}) error { 68 client := meta.(*chefc.Client) 69 70 name := d.Id() 71 72 _, err := client.DataBags.Delete(name) 73 if err == nil { 74 d.SetId("") 75 } 76 return err 77 }