-
Notifications
You must be signed in to change notification settings - Fork 112
/
resource_consul_acl_policy.go
167 lines (137 loc) · 4.04 KB
/
resource_consul_acl_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package consul
import (
"fmt"
"log"
"strings"
consulapi "github.com/hashicorp/consul/api"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func resourceConsulACLPolicy() *schema.Resource {
return &schema.Resource{
Create: resourceConsulACLPolicyCreate,
Read: resourceConsulACLPolicyRead,
Update: resourceConsulACLPolicyUpdate,
Delete: resourceConsulACLPolicyDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "The ACL policy name.",
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "The ACL policy description.",
},
"rules": {
Type: schema.TypeString,
Required: true,
Description: "The ACL policy rules.",
},
"datacenters": {
Type: schema.TypeSet,
Optional: true,
Description: "The ACL policy datacenters.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"namespace": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"partition": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The partition the ACL policy is associated with.",
},
},
}
}
func resourceConsulACLPolicyCreate(d *schema.ResourceData, meta interface{}) error {
client, _, wOpts := getClient(d, meta)
log.Printf("[DEBUG] Creating ACL policy")
aclPolicy := consulapi.ACLPolicy{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Rules: d.Get("rules").(string),
Namespace: wOpts.Namespace,
}
if v, ok := d.GetOk("datacenters"); ok {
vs := v.(*schema.Set).List()
s := make([]string, len(vs))
for i, raw := range vs {
s[i] = raw.(string)
}
aclPolicy.Datacenters = s
}
policy, _, err := client.ACL().PolicyCreate(&aclPolicy, wOpts)
if err != nil {
return fmt.Errorf("error creating ACL policy: %s", err)
}
log.Printf("[DEBUG] Created ACL policy %q", policy.ID)
d.SetId(policy.ID)
return resourceConsulACLPolicyRead(d, meta)
}
func resourceConsulACLPolicyRead(d *schema.ResourceData, meta interface{}) error {
client, qOpts, _ := getClient(d, meta)
id := d.Id()
aclPolicy, _, err := client.ACL().PolicyRead(id, qOpts)
if err != nil {
if strings.Contains(err.Error(), "ACL not found") {
d.SetId("")
return nil
}
return fmt.Errorf("failed to read policy '%s': %v", id, err)
}
sw := newStateWriter(d)
sw.set("name", aclPolicy.Name)
sw.set("description", aclPolicy.Description)
sw.set("rules", aclPolicy.Rules)
sw.set("datacenters", aclPolicy.Datacenters)
sw.set("namespace", aclPolicy.Namespace)
sw.set("partition", aclPolicy.Partition)
return sw.error()
}
func resourceConsulACLPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
client, _, wOpts := getClient(d, meta)
id := d.Id()
log.Printf("[DEBUG] Updating ACL policy %q", id)
aclPolicy := consulapi.ACLPolicy{
ID: id,
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Rules: d.Get("rules").(string),
Namespace: wOpts.Namespace,
}
if v, ok := d.GetOk("datacenters"); ok {
vs := v.(*schema.Set).List()
s := make([]string, len(vs))
for i, raw := range vs {
s[i] = raw.(string)
}
aclPolicy.Datacenters = s
}
_, _, err := client.ACL().PolicyUpdate(&aclPolicy, wOpts)
if err != nil {
return fmt.Errorf("error updating ACL policy %q: %s", id, err)
}
log.Printf("[DEBUG] Updated ACL policy %q", id)
return resourceConsulACLPolicyRead(d, meta)
}
func resourceConsulACLPolicyDelete(d *schema.ResourceData, meta interface{}) error {
client, _, wOpts := getClient(d, meta)
id := d.Id()
log.Printf("[DEBUG] Deleting ACL policy %q", id)
_, err := client.ACL().PolicyDelete(id, wOpts)
if err != nil {
return fmt.Errorf("error deleting ACL policy %q: %s", id, err)
}
log.Printf("[DEBUG] Deleted ACL policy %q", id)
return nil
}