-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathdata_source_tc_ckafka_acls.go
137 lines (123 loc) · 4.81 KB
/
data_source_tc_ckafka_acls.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
package ckafka
import (
"context"
"strings"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func DataSourceTencentCloudCkafkaAcls() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudCkafkaAclsRead,
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
Required: true,
Description: "Id of the ckafka instance.",
},
"resource_type": {
Type: schema.TypeString,
Required: true,
Description: "ACL resource type. Valid values are `UNKNOWN`, `ANY`, `TOPIC`, `GROUP`, `CLUSTER`, `TRANSACTIONAL_ID`. Currently, only `TOPIC` is available, and other fields will be used for future ACLs compatible with open-source Kafka.",
},
"resource_name": {
Type: schema.TypeString,
Required: true,
Description: "ACL resource name, which is related to `resource_type`. For example, if `resource_type` is `TOPIC`, this field indicates the topic name; if `resource_type` is `GROUP`, this field indicates the group name.",
},
"host": {
Type: schema.TypeString,
Optional: true,
Description: "Host substr used for querying.",
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
"acl_list": {
Type: schema.TypeList,
Computed: true,
Description: "A list of ckafka acls. Each element contains the following attributes:",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: "ACL resource type.",
},
"resource_name": {
Type: schema.TypeString,
Computed: true,
Description: "ACL resource name, which is related to `resource_type`.",
},
"operation_type": {
Type: schema.TypeString,
Computed: true,
Description: "ACL operation mode.",
},
"permission_type": {
Type: schema.TypeString,
Computed: true,
Description: "ACL permission type, valid values are `UNKNOWN`, `ANY`, `DENY`, `ALLOW`, and `ALLOW` by default. Currently, CKafka supports `ALLOW` (equivalent to allow list), and other fields will be used for future ACLs compatible with open-source Kafka.",
},
"host": {
Type: schema.TypeString,
Computed: true,
Description: "IP address allowed to access.",
},
"principal": {
Type: schema.TypeString,
Computed: true,
Description: "User which can access. `*` means that any user can access.",
},
},
},
},
},
}
}
func dataSourceTencentCloudCkafkaAclsRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_ckafka_acls.read")()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
params := make(map[string]interface{})
params["instance_id"] = d.Get("instance_id").(string)
params["resource_type"] = d.Get("resource_type").(string)
params["resource_name"] = d.Get("resource_name").(string)
if v, ok := d.GetOk("host"); ok {
params["host"] = v.(string)
}
ckafkaService := CkafkaService{
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
}
aclInfos, err := ckafkaService.DescribeAclByFilter(ctx, params)
if err != nil {
return err
}
aclList := make([]map[string]interface{}, 0, len(aclInfos))
ids := make([]string, 0, len(aclInfos))
for _, acl := range aclInfos {
aclList = append(aclList, map[string]interface{}{
"resource_type": CKAFKA_ACL_RESOURCE_TYPE_TO_STRING[*acl.ResourceType],
"resource_name": *acl.ResourceName,
"operation_type": CKAFKA_ACL_OPERATION_TO_STRING[*acl.Operation],
"permission_type": CKAFKA_PERMISSION_TYPE_TO_STRING[*acl.PermissionType],
"host": *acl.Host,
"principal": strings.TrimLeft(*acl.Principal, CKAFKA_ACL_PRINCIPAL_STR),
})
ids = append(ids, params["instance_id"].(string)+tccommon.FILED_SP+CKAFKA_PERMISSION_TYPE_TO_STRING[*acl.PermissionType]+
tccommon.FILED_SP+strings.TrimLeft(*acl.Principal, CKAFKA_ACL_PRINCIPAL_STR)+tccommon.FILED_SP+*acl.Host+tccommon.FILED_SP+
CKAFKA_ACL_OPERATION_TO_STRING[*acl.Operation]+tccommon.FILED_SP+CKAFKA_ACL_RESOURCE_TYPE_TO_STRING[*acl.ResourceType]+
tccommon.FILED_SP+*acl.ResourceName)
}
d.SetId(helper.DataResourceIdsHash(ids))
_ = d.Set("acl_list", aclList)
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := tccommon.WriteToFile(output.(string), aclList); e != nil {
return e
}
}
return nil
}