forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 7
/
resource_policy.go
82 lines (60 loc) · 1.55 KB
/
resource_policy.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
78
79
80
81
82
package vault
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/vault/api"
)
func policyResource() *schema.Resource {
return &schema.Resource{
Create: policyWrite,
Update: policyWrite,
Delete: policyDelete,
Read: policyRead,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name of the policy",
},
"policy": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The policy document",
},
},
}
}
func policyWrite(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
name := d.Get("name").(string)
policy := d.Get("policy").(string)
log.Printf("[DEBUG] Writing policy %s to Vault", name)
err := client.Sys().PutPolicy(name, policy)
if err != nil {
return fmt.Errorf("error writing to Vault: %s", err)
}
d.SetId(name)
return nil
}
func policyDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
name := d.Id()
log.Printf("[DEBUG] Deleting policy %s from Vault", name)
err := client.Sys().DeletePolicy(name)
if err != nil {
return fmt.Errorf("error deleting from Vault: %s", err)
}
return nil
}
func policyRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
name := d.Id()
policy, err := client.Sys().GetPolicy(name)
if err != nil {
return fmt.Errorf("error reading from Vault: %s", err)
}
d.Set("policy", policy)
return nil
}