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