Skip to content

Commit a129b0f

Browse files
committed
[new ckafka acl, user resources and datasources]
1. add new ckafka user resource 2. add new ckafka acl resource 3. add new ckafka user datasource 4. add new ckafka acl datasource
1 parent 7a96451 commit a129b0f

24 files changed

+4539
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ FEATURES:
99
* **New Resource**: `tencentcloud_sqlserver_db_account_attachment`
1010
* **New Resource**: `tencentcloud_vpc_acl`
1111
* **New Resource**: `tencentcloud_vpc_acl_attachment`
12+
* **New Resource**: `tencentcloud_ckafka_acl`
13+
* **New Resource**: `tencentcloud_ckafka_user`
1214
* **New Data Source**: `tencentcloud_sqlserver_instance`
1315
* **New Data Source**: `tencentcloud_sqlserver_readonly_groups`
1416
* **New Data Source**: `tencentcloud_vpc_acls`
17+
* **New Data Source**: `tencentcloud_ckafka_acls`
18+
* **New Data Source**: `tencentcloud_ckafka_users`
1519

1620
DEPRECATED:
1721

examples/tencentcloud-ckafka/main.tf

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
resource "tencentcloud_ckafka_user" "foo" {
2+
instance_id = "ckafka-f9ife4zz"
3+
account_name = "test"
4+
password = "test1234"
5+
}
6+
7+
data "tencentcloud_ckafka_users" "foo" {
8+
instance_id = tencentcloud_ckafka_user.foo.instance_id
9+
account_name = tencentcloud_ckafka_user.foo.account_name
10+
}
11+
12+
resource "tencentcloud_ckafka_acl" foo {
13+
instance_id = "ckafka-f9ife4zz"
14+
resource_type = "TOPIC"
15+
resource_name = "topic-tf-test"
16+
operation_type = "WRITE"
17+
permission_type = "ALLOW"
18+
host = "10.10.10.0"
19+
principal = tencentcloud_ckafka_user.foo.account_name
20+
}
21+
22+
data "tencentcloud_ckafka_acls" "foo" {
23+
instance_id = tencentcloud_ckafka_acl.foo.instance_id
24+
resource_type = tencentcloud_ckafka_acl.foo.resource_type
25+
resource_name = tencentcloud_ckafka_acl.foo.resource_name
26+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
terraform {
2+
required_version = ">= 0.12"
3+
}

tencentcloud/connectivity/client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
cdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdb/v20170320"
1515
cdn "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdn/v20180606"
1616
cfs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfs/v20190719"
17+
ckafka "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka/v20190819"
1718
clb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317"
1819
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
1920
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
@@ -68,6 +69,7 @@ type TencentCloudClient struct {
6869
esConn *es.Client
6970
sqlserverConn *sqlserver.Client
7071
postgreConn *postgre.Client
72+
ckafkaConn *ckafka.Client
7173
}
7274

7375
// NewClientProfile returns a new ClientProfile
@@ -428,3 +430,16 @@ func (me *TencentCloudClient) UseSqlserverClient() *sqlserver.Client {
428430

429431
return me.sqlserverConn
430432
}
433+
434+
// UseCkafkaClient returns ckafka client for service
435+
func (me *TencentCloudClient) UseCkafkaClient() *ckafka.Client {
436+
if me.ckafkaConn != nil {
437+
return me.ckafkaConn
438+
}
439+
440+
cpf := me.NewClientProfile(300)
441+
me.ckafkaConn, _ = ckafka.NewClient(me.Credential, me.Region, cpf)
442+
me.ckafkaConn.WithHttpTransport(&LogRoundTripper{})
443+
444+
return me.ckafkaConn
445+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
Use this data source to query detailed acl information of Ckafka
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_ckafka_acls" "foo" {
8+
instance_id = "ckafka-f9ife4zz"
9+
resource_type = "TOPIC"
10+
resource_name = "topic-tf-test"
11+
host = "2"
12+
}
13+
```
14+
*/
15+
package tencentcloud
16+
17+
import (
18+
"context"
19+
"strings"
20+
21+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
22+
"github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/internal/helper"
23+
)
24+
25+
func dataSourceTencentCloudCkafkaAcls() *schema.Resource {
26+
return &schema.Resource{
27+
Read: dataSourceTencentCloudCkafkaAclsRead,
28+
29+
Schema: map[string]*schema.Schema{
30+
"instance_id": {
31+
Type: schema.TypeString,
32+
Required: true,
33+
Description: "Id of the ckafka instance.",
34+
},
35+
"resource_type": {
36+
Type: schema.TypeString,
37+
Required: true,
38+
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.",
39+
},
40+
"resource_name": {
41+
Type: schema.TypeString,
42+
Required: true,
43+
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.",
44+
},
45+
"host": {
46+
Type: schema.TypeString,
47+
Optional: true,
48+
Description: "Host substr used for querying.",
49+
},
50+
"result_output_file": {
51+
Type: schema.TypeString,
52+
Optional: true,
53+
Description: "Used to save results.",
54+
},
55+
"acl_list": {
56+
Type: schema.TypeList,
57+
Computed: true,
58+
Description: "A list of ckafka acls. Each element contains the following attributes:",
59+
Elem: &schema.Resource{
60+
Schema: map[string]*schema.Schema{
61+
"resource_type": {
62+
Type: schema.TypeString,
63+
Computed: true,
64+
Description: "ACL resource type.",
65+
},
66+
"resource_name": {
67+
Type: schema.TypeString,
68+
Computed: true,
69+
Description: "ACL resource name, which is related to `resource_type`.",
70+
},
71+
"operation_type": {
72+
Type: schema.TypeString,
73+
Computed: true,
74+
Description: "ACL operation mode.",
75+
},
76+
"permission_type": {
77+
Type: schema.TypeString,
78+
Computed: true,
79+
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.",
80+
},
81+
"host": {
82+
Type: schema.TypeString,
83+
Computed: true,
84+
Description: "IP address allowed to access.",
85+
},
86+
"principal": {
87+
Type: schema.TypeString,
88+
Computed: true,
89+
Description: "User which can access. `*` means that any user can access.",
90+
},
91+
},
92+
},
93+
},
94+
},
95+
}
96+
}
97+
98+
func dataSourceTencentCloudCkafkaAclsRead(d *schema.ResourceData, meta interface{}) error {
99+
defer logElapsed("data_source.tencentcloud_ckafka_acls.read")()
100+
101+
logId := getLogId(contextNil)
102+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
103+
104+
params := make(map[string]interface{})
105+
params["instance_id"] = d.Get("instance_id").(string)
106+
params["resource_type"] = d.Get("resource_type").(string)
107+
params["resource_name"] = d.Get("resource_name").(string)
108+
if v, ok := d.GetOk("host"); ok {
109+
params["host"] = v.(string)
110+
}
111+
112+
ckafkaService := CkafkaService{
113+
client: meta.(*TencentCloudClient).apiV3Conn,
114+
}
115+
aclInfos, err := ckafkaService.DescribeAclByFilter(ctx, params)
116+
if err != nil {
117+
return err
118+
}
119+
aclList := make([]map[string]interface{}, 0, len(aclInfos))
120+
ids := make([]string, 0, len(aclInfos))
121+
for _, acl := range aclInfos {
122+
aclList = append(aclList, map[string]interface{}{
123+
"resource_type": CKAFKA_ACL_RESOURCE_TYPE_TO_STRING[*acl.ResourceType],
124+
"resource_name": *acl.ResourceName,
125+
"operation_type": CKAFKA_ACL_OPERATION_TO_STRING[*acl.Operation],
126+
"permission_type": CKAFKA_PERMISSION_TYPE_TO_STRING[*acl.PermissionType],
127+
"host": *acl.Host,
128+
"principal": strings.TrimLeft(*acl.Principal, CKAFKA_ACL_PRINCIPAL_STR),
129+
})
130+
131+
ids = append(ids, params["instance_id"].(string)+FILED_SP+CKAFKA_PERMISSION_TYPE_TO_STRING[*acl.PermissionType]+
132+
FILED_SP+strings.TrimLeft(*acl.Principal, CKAFKA_ACL_PRINCIPAL_STR)+FILED_SP+*acl.Host+FILED_SP+
133+
CKAFKA_ACL_OPERATION_TO_STRING[*acl.Operation]+FILED_SP+CKAFKA_ACL_RESOURCE_TYPE_TO_STRING[*acl.ResourceType]+
134+
FILED_SP+*acl.ResourceName)
135+
}
136+
137+
d.SetId(helper.DataResourceIdsHash(ids))
138+
d.Set("acl_list", aclList)
139+
140+
output, ok := d.GetOk("result_output_file")
141+
if ok && output.(string) != "" {
142+
if e := writeToFile(output.(string), aclList); e != nil {
143+
return e
144+
}
145+
}
146+
147+
return nil
148+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudDataSourceCkafkaAcls(t *testing.T) {
10+
resource.Test(t, resource.TestCase{
11+
PreCheck: func() { testAccPreCheck(t) },
12+
Providers: testAccProviders,
13+
CheckDestroy: testAccCheckCkafkaAclDestroy,
14+
Steps: []resource.TestStep{
15+
{
16+
Config: testAccTencentCloudDataSourceCkafkaAcl,
17+
Check: resource.ComposeAggregateTestCheckFunc(
18+
testAccCheckCkafkaAclExists("tencentcloud_ckafka_acl.foo"),
19+
resource.TestCheckResourceAttrSet("data.tencentcloud_ckafka_acls.foo", "acl_list.0.operation_type"),
20+
resource.TestCheckResourceAttrSet("data.tencentcloud_ckafka_acls.foo", "acl_list.0.permission_type"),
21+
resource.TestCheckResourceAttrSet("data.tencentcloud_ckafka_acls.foo", "acl_list.0.resource_name"),
22+
resource.TestCheckResourceAttrSet("data.tencentcloud_ckafka_acls.foo", "acl_list.0.resource_type"),
23+
resource.TestCheckResourceAttrSet("data.tencentcloud_ckafka_acls.foo", "acl_list.0.host"),
24+
resource.TestCheckResourceAttrSet("data.tencentcloud_ckafka_acls.foo", "acl_list.0.principal"),
25+
),
26+
},
27+
},
28+
})
29+
}
30+
31+
const testAccTencentCloudDataSourceCkafkaAcl = testAccCkafkaAcl + `
32+
data "tencentcloud_ckafka_acls" "foo" {
33+
instance_id = tencentcloud_ckafka_acl.foo.instance_id
34+
resource_type = tencentcloud_ckafka_acl.foo.resource_type
35+
resource_name = tencentcloud_ckafka_acl.foo.resource_name
36+
}
37+
`
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
Use this data source to query detailed user information of Ckafka
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_ckafka_users" "foo" {
8+
instance_id = "ckafka-f9ife4zz"
9+
account_name = "test"
10+
}
11+
```
12+
*/
13+
package tencentcloud
14+
15+
import (
16+
"context"
17+
18+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
19+
"github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/internal/helper"
20+
)
21+
22+
func dataSourceTencentCloudCkafkaUsers() *schema.Resource {
23+
return &schema.Resource{
24+
Read: dataSourceTencentCloudCkafkaUsersRead,
25+
26+
Schema: map[string]*schema.Schema{
27+
"instance_id": {
28+
Type: schema.TypeString,
29+
Required: true,
30+
Description: "Id of the ckafka instance.",
31+
},
32+
"account_name": {
33+
Type: schema.TypeString,
34+
Optional: true,
35+
Description: "Account name used when query ckafka users' infos. Could be a substr of user name.",
36+
},
37+
"result_output_file": {
38+
Type: schema.TypeString,
39+
Optional: true,
40+
Description: "Used to save results.",
41+
},
42+
"user_list": {
43+
Type: schema.TypeList,
44+
Computed: true,
45+
Description: "A list of ckafka users. Each element contains the following attributes:",
46+
Elem: &schema.Resource{
47+
Schema: map[string]*schema.Schema{
48+
"account_name": {
49+
Type: schema.TypeString,
50+
Computed: true,
51+
Description: "Account name of user.",
52+
},
53+
"create_time": {
54+
Type: schema.TypeString,
55+
Computed: true,
56+
Description: "Creation time of the account.",
57+
},
58+
"update_time": {
59+
Type: schema.TypeString,
60+
Computed: true,
61+
Description: "The last update time of the account.",
62+
},
63+
},
64+
},
65+
},
66+
},
67+
}
68+
}
69+
70+
func dataSourceTencentCloudCkafkaUsersRead(d *schema.ResourceData, meta interface{}) error {
71+
defer logElapsed("data_source.tencentcloud_ckafka_users.read")()
72+
73+
logId := getLogId(contextNil)
74+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
75+
76+
params := make(map[string]interface{})
77+
params["instance_id"] = d.Get("instance_id").(string)
78+
if v, ok := d.GetOk("account_name"); ok {
79+
params["account_name"] = v.(string)
80+
}
81+
82+
ckafkaService := CkafkaService{
83+
client: meta.(*TencentCloudClient).apiV3Conn,
84+
}
85+
userInfos, err := ckafkaService.DescribeUserByFilter(ctx, params)
86+
if err != nil {
87+
return err
88+
}
89+
userList := make([]map[string]interface{}, 0, len(userInfos))
90+
ids := make([]string, 0, len(userInfos))
91+
for _, user := range userInfos {
92+
userList = append(userList, map[string]interface{}{
93+
"account_name": *user.Name,
94+
"create_time": *user.CreateTime,
95+
"update_time": *user.UpdateTime,
96+
})
97+
98+
ids = append(ids, params["instance_id"].(string)+FILED_SP+*user.Name)
99+
}
100+
101+
d.SetId(helper.DataResourceIdsHash(ids))
102+
d.Set("user_list", userList)
103+
104+
output, ok := d.GetOk("result_output_file")
105+
if ok && output.(string) != "" {
106+
if e := writeToFile(output.(string), userList); e != nil {
107+
return e
108+
}
109+
}
110+
111+
return nil
112+
}

0 commit comments

Comments
 (0)