-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
resource_kafka_acl.go
197 lines (174 loc) · 4.71 KB
/
resource_kafka_acl.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package kafka
import (
"context"
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func kafkaACLResource() *schema.Resource {
//lintignore:R011
return &schema.Resource{
CreateContext: aclCreate,
ReadContext: aclRead,
DeleteContext: aclDelete,
Importer: &schema.ResourceImporter{
StateContext: importACL,
},
SchemaVersion: 1,
MigrateState: migrateKafkaAclState,
Schema: map[string]*schema.Schema{
"resource_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The name of the resource",
},
"resource_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"resource_pattern_type_filter": {
Type: schema.TypeString,
Default: "Literal",
Optional: true,
ForceNew: true,
},
"acl_principal": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"acl_host": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"acl_operation": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"acl_permission_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func aclCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*LazyClient)
a := aclInfo(d)
log.Printf("[INFO] Creating ACL %s", a)
err := c.CreateACL(a)
if err != nil {
log.Println("[ERROR] Failed to create ACL")
return diag.FromErr(err)
}
d.SetId(a.String())
return nil
}
func aclDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*LazyClient)
a := aclInfo(d)
log.Printf("[INFO] Deleting ACL %s", a)
err := c.DeleteACL(a)
if err != nil {
return diag.FromErr(err)
}
return nil
}
func aclRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Println("[INFO] Reading ACL")
c := meta.(*LazyClient)
a := aclInfo(d)
log.Printf("[INFO] Reading ACL %s", a)
currentACLs, err := c.ListACLs()
if err != nil {
return diag.FromErr(err)
}
aclNotFound := true
for _, foundACLs := range currentACLs {
// find only ACLs where ResourceName matches
if foundACLs.ResourceName != a.Resource.Name {
continue
}
if len(foundACLs.Acls) < 1 {
break
}
log.Printf("[INFO] Found (%d) ACL(s) for Resource %s: %+v.", len(foundACLs.Acls), foundACLs.ResourceName, foundACLs)
for _, acl := range foundACLs.Acls {
aclID := StringlyTypedACL{
ACL: ACL{
Principal: acl.Principal,
Host: acl.Host,
Operation: ACLOperationToString(acl.Operation),
PermissionType: ACLPermissionTypeToString(acl.PermissionType),
},
Resource: Resource{
Type: ACLResourceToString(foundACLs.ResourceType),
Name: foundACLs.ResourceName,
PatternTypeFilter: foundACLs.ResourcePatternType.String(),
},
}
// exact match
if a.String() == aclID.String() {
return nil
}
}
}
if aclNotFound {
log.Printf("[INFO] Did not find ACL %s", a.String())
d.SetId("")
}
return nil
}
func importACL(ctx context.Context, d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "|")
if len(parts) == 7 {
errSet := errSetter{d: d}
errSet.Set("acl_principal", parts[0])
errSet.Set("acl_host", parts[1])
errSet.Set("acl_operation", parts[2])
errSet.Set("acl_permission_type", parts[3])
errSet.Set("resource_type", parts[4])
errSet.Set("resource_name", parts[5])
errSet.Set("resource_pattern_type_filter", parts[6])
if errSet.err != nil {
return nil, errSet.err
}
} else {
return nil, fmt.Errorf("Failed importing resource; expected format is acl_principal|acl_host|acl_operation|acl_permission_type|resource_type|resource_name|resource_pattern_type_filter - got %v segments instead of 7", len(parts))
}
return []*schema.ResourceData{d}, nil
}
type errSetter struct {
err error
d *schema.ResourceData
}
func (es *errSetter) Set(key string, value interface{}) {
if es.err != nil {
return
}
//lintignore:R001
es.err = es.d.Set(key, value)
}
func aclInfo(d *schema.ResourceData) StringlyTypedACL {
s := StringlyTypedACL{
ACL: ACL{
Principal: d.Get("acl_principal").(string),
Host: d.Get("acl_host").(string),
Operation: d.Get("acl_operation").(string),
PermissionType: d.Get("acl_permission_type").(string),
},
Resource: Resource{
Type: d.Get("resource_type").(string),
Name: d.Get("resource_name").(string),
PatternTypeFilter: d.Get("resource_pattern_type_filter").(string),
},
}
return s
}