-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
resource_kafka_acl.go
121 lines (108 loc) · 2.57 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
package kafka
import (
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func kafkaACLResource() *schema.Resource {
return &schema.Resource{
Create: aclCreate,
Read: aclRead,
Delete: aclDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
SchemaVersion: 1,
MigrateState: migrateKafkaAclState,
Schema: map[string]*schema.Schema{
"resource_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The name of the resouce",
},
"resource_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"resource_pattern_type_filter": {
Type: schema.TypeString,
Required: false,
Optional: true,
Default: "Literal",
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(d *schema.ResourceData, meta interface{}) error {
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 err
}
d.SetId(a.String())
return nil
}
func aclDelete(d *schema.ResourceData, meta interface{}) error {
c := meta.(*LazyClient)
a := aclInfo(d)
log.Printf("[INFO] Deleting ACL %s", a)
return c.DeleteACL(a)
}
func aclRead(d *schema.ResourceData, meta interface{}) error {
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 err
}
for _, c := range currentAcls {
if c.ResourceName == a.Resource.Name {
log.Printf("[INFO] Found ACL %v", c)
return nil
}
}
return nil
}
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
}