-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathresource_tc_ckafka_user.go
141 lines (119 loc) · 4.06 KB
/
resource_tc_ckafka_user.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
package ckafka
import (
"context"
"fmt"
"strings"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func ResourceTencentCloudCkafkaUser() *schema.Resource {
return &schema.Resource{
Create: resourceTencentCloudCkafkaUserCreate,
Read: resourceTencentCloudCkafkaUserRead,
Update: resourceTencentCloudCkafkaUserUpdate,
Delete: resourceTencentCloudCkafkaUserDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "ID of the ckafka instance.",
},
"account_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Account name used to access to ckafka instance.",
},
"password": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
Description: "Password of the account.",
},
// computed
"create_time": {
Type: schema.TypeString,
Computed: true,
Description: "Creation time of the account.",
},
"update_time": {
Type: schema.TypeString,
Computed: true,
Description: "The last update time of the account.",
},
},
}
}
func resourceTencentCloudCkafkaUserCreate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_ckafka_user.create")()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
instanceId := d.Get("instance_id").(string)
accountName := d.Get("account_name").(string)
password := d.Get("password").(string)
ckafkaService := CkafkaService{
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
}
if err := ckafkaService.CreateUser(ctx, instanceId, accountName, password); err != nil {
return fmt.Errorf("[CRITAL]%s create ckafka user failed, reason:%+v", logId, err)
}
d.SetId(instanceId + tccommon.FILED_SP + accountName)
return resourceTencentCloudCkafkaUserRead(d, meta)
}
func resourceTencentCloudCkafkaUserRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_ckafka_user.read")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
ckafkaService := CkafkaService{
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
}
id := d.Id()
info, has, err := ckafkaService.DescribeUserByUserId(ctx, id)
if err != nil {
return err
}
if !has {
d.SetId("")
return nil
}
items := strings.Split(id, tccommon.FILED_SP)
_ = d.Set("instance_id", items[0])
_ = d.Set("account_name", info.Name)
_ = d.Set("create_time", info.CreateTime)
_ = d.Set("update_time", info.UpdateTime)
return nil
}
func resourceTencentCloudCkafkaUserUpdate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_ckafka_user.update")()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
ckafkaService := CkafkaService{
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
}
instanceId := d.Get("instance_id").(string)
user := d.Get("account_name").(string)
if d.HasChange("password") {
old, new := d.GetChange("password")
if err := ckafkaService.ModifyPassword(ctx, instanceId, user, old.(string), new.(string)); err != nil {
return err
}
}
return resourceTencentCloudCkafkaUserRead(d, meta)
}
func resourceTencentCloudCkafkaUserDelete(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_ckafka_user.delete")()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
ckafkaService := CkafkaService{
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
}
if err := ckafkaService.DeleteUser(ctx, d.Id()); err != nil {
return err
}
return nil
}