@@ -4,16 +4,23 @@ Provide a resource to create a tdmq namespace.
44Example Usage
55
66```hcl
7- resource "tencentcloud_tdmq_instance" "foo" {
8- cluster_name = "example"
9- remark = "this is description."
7+ resource "tencentcloud_tdmq_instance" "example" {
8+ cluster_name = "tf_example"
9+ remark = "remark."
10+ tags = {
11+ "createdBy" = "terraform"
12+ }
1013}
1114
12- resource "tencentcloud_tdmq_namespace" "bar" {
13- environ_name = "example"
14- msg_ttl = 300
15- cluster_id = "${tencentcloud_tdmq_instance.foo.id}"
16- remark = "this is description."
15+ resource "tencentcloud_tdmq_namespace" "example" {
16+ environ_name = "tf_example"
17+ msg_ttl = 300
18+ cluster_id = tencentcloud_tdmq_instance.example.id
19+ retention_policy {
20+ time_in_minutes = 60
21+ size_in_mb = 10
22+ }
23+ remark = "remark."
1724}
1825```
1926
@@ -30,28 +37,14 @@ package tencentcloud
3037import (
3138 "context"
3239 "fmt"
40+ "strings"
3341
3442 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
3543 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
3644 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
3745 tdmq "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tdmq/v20200217"
3846)
3947
40- func RetentionPolicy () map [string ]* schema.Schema {
41- return map [string ]* schema.Schema {
42- "time_in_minutes" : {
43- Type : schema .TypeInt ,
44- Optional : true ,
45- Description : "the time of message to retain." ,
46- },
47- "size_in_mb" : {
48- Type : schema .TypeInt ,
49- Optional : true ,
50- Description : "the size of message to retain." ,
51- },
52- }
53- }
54-
5548func resourceTencentCloudTdmqNamespace () * schema.Resource {
5649 return & schema.Resource {
5750 Create : resourceTencentCloudTdmqNamespaceCreate ,
@@ -84,9 +77,27 @@ func resourceTencentCloudTdmqNamespace() *schema.Resource {
8477 Description : "Description of the namespace." ,
8578 },
8679 "retention_policy" : {
87- Type : schema .TypeMap ,
80+ Type : schema .TypeList ,
8881 Optional : true ,
82+ Computed : true ,
83+ MaxItems : 1 ,
8984 Description : "The Policy of message to retain. Format like: `{time_in_minutes: Int, size_in_mb: Int}`. `time_in_minutes`: the time of message to retain; `size_in_mb`: the size of message to retain." ,
85+ Elem : & schema.Resource {
86+ Schema : map [string ]* schema.Schema {
87+ "time_in_minutes" : {
88+ Type : schema .TypeInt ,
89+ Optional : true ,
90+ Computed : true ,
91+ Description : "the time of message to retain." ,
92+ },
93+ "size_in_mb" : {
94+ Type : schema .TypeInt ,
95+ Optional : true ,
96+ Computed : true ,
97+ Description : "the size of message to retain." ,
98+ },
99+ },
100+ },
90101 },
91102 },
92103 }
@@ -125,20 +136,21 @@ func resourceTencentCloudTdmqNamespaceCreate(d *schema.ResourceData, meta interf
125136 }
126137
127138 if temp , ok := d .GetOk ("retention_policy" ); ok {
128- v := temp .(map [string ]interface {})
129- timeInMinutes := int64 (v ["time_in_minutes" ].(int ))
130- sizeInMb := int64 (v ["size_in_mb" ].(int ))
131-
132- retentionPolicy .TimeInMinutes = & timeInMinutes
133- retentionPolicy .SizeInMB = & sizeInMb
139+ policy := temp .([]interface {})
140+ for _ , item := range policy {
141+ value := item .(map [string ]interface {})
142+ timeInMinutes := int64 (value ["time_in_minutes" ].(int ))
143+ sizeInMB := int64 (value ["size_in_mb" ].(int ))
144+ retentionPolicy .TimeInMinutes = & timeInMinutes
145+ retentionPolicy .SizeInMB = & sizeInMB
146+ }
134147 }
135148 environId , err := tdmqService .CreateTdmqNamespace (ctx , environ_name , msg_ttl , clusterId , remark , retentionPolicy )
136149 if err != nil {
137150 return err
138151 }
139152
140- d .SetId (environId )
141-
153+ d .SetId (strings .Join ([]string {environId , clusterId }, FILED_SP ))
142154 return resourceTencentCloudTdmqNamespaceRead (d , meta )
143155}
144156
@@ -149,8 +161,12 @@ func resourceTencentCloudTdmqNamespaceRead(d *schema.ResourceData, meta interfac
149161 logId := getLogId (contextNil )
150162 ctx := context .WithValue (context .TODO (), logIdKey , logId )
151163
152- environId := d .Id ()
153- clusterId := d .Get ("cluster_id" ).(string )
164+ idSplit := strings .Split (d .Id (), FILED_SP )
165+ if len (idSplit ) != 2 {
166+ return fmt .Errorf ("id is broken,%s" , idSplit )
167+ }
168+ environId := idSplit [0 ]
169+ clusterId := idSplit [1 ]
154170
155171 tdmqService := TdmqService {client : meta .(* TencentCloudClient ).apiV3Conn }
156172
@@ -165,13 +181,16 @@ func resourceTencentCloudTdmqNamespaceRead(d *schema.ResourceData, meta interfac
165181 }
166182
167183 _ = d .Set ("environ_name" , info .EnvironmentId )
184+ _ = d .Set ("cluster_id" , clusterId )
168185 _ = d .Set ("msg_ttl" , info .MsgTTL )
169186 _ = d .Set ("remark" , info .Remark )
170187
188+ tmpList := make ([]map [string ]interface {}, 0 )
171189 retentionPolicy := make (map [string ]interface {}, 2 )
172190 retentionPolicy ["time_in_minutes" ] = info .RetentionPolicy .TimeInMinutes
173191 retentionPolicy ["size_in_mb" ] = info .RetentionPolicy .SizeInMB
174- _ = d .Set ("retention_policy" , retentionPolicy )
192+ tmpList = append (tmpList , retentionPolicy )
193+ _ = d .Set ("retention_policy" , tmpList )
175194 return nil
176195 })
177196 if err != nil {
@@ -186,15 +205,19 @@ func resourceTencentCloudTdmqNamespaceUpdate(d *schema.ResourceData, meta interf
186205 logId := getLogId (contextNil )
187206 ctx := context .WithValue (context .TODO (), logIdKey , logId )
188207
189- environId := d .Id ()
190- clusterId := d .Get ("cluster_id" ).(string )
208+ idSplit := strings .Split (d .Id (), FILED_SP )
209+ if len (idSplit ) != 2 {
210+ return fmt .Errorf ("id is broken,%s" , idSplit )
211+ }
212+ environId := idSplit [0 ]
213+ clusterId := idSplit [1 ]
191214
192215 service := TdmqService {client : meta .(* TencentCloudClient ).apiV3Conn }
193216
194217 var (
195- msgTtl uint64
196- remark string
197- retentPolicy * tdmq.RetentionPolicy
218+ msgTtl uint64
219+ remark string
220+ retentionPolicy = new ( tdmq.RetentionPolicy )
198221 )
199222
200223 old , now := d .GetChange ("msg_ttl" )
@@ -213,17 +236,19 @@ func resourceTencentCloudTdmqNamespaceUpdate(d *schema.ResourceData, meta interf
213236
214237 _ , now = d .GetChange ("retention_policy" )
215238 if d .HasChange ("retention_policy" ) {
216- temp := now .(map [string ]interface {})
217- time := temp ["time_in_minutes" ].(int64 )
218- size := temp ["size_in_mb" ].(int64 )
219- retentPolicy = & tdmq.RetentionPolicy {
220- TimeInMinutes : & time ,
221- SizeInMB : & size ,
239+ policy := now .([]interface {})
240+
241+ for _ , item := range policy {
242+ value := item .(map [string ]interface {})
243+ timeInMinutes := int64 (value ["time_in_minutes" ].(int ))
244+ sizeInMB := int64 (value ["size_in_mb" ].(int ))
245+ retentionPolicy .TimeInMinutes = & timeInMinutes
246+ retentionPolicy .SizeInMB = & sizeInMB
222247 }
223248 }
224249
225250 d .Partial (true )
226- if err := service .ModifyTdmqNamespaceAttribute (ctx , environId , msgTtl , remark , clusterId , retentPolicy ); err != nil {
251+ if err := service .ModifyTdmqNamespaceAttribute (ctx , environId , msgTtl , remark , clusterId , retentionPolicy ); err != nil {
227252 return err
228253 }
229254
@@ -237,8 +262,12 @@ func resourceTencentCloudTdmqNamespaceDelete(d *schema.ResourceData, meta interf
237262 logId := getLogId (contextNil )
238263 ctx := context .WithValue (context .TODO (), logIdKey , logId )
239264
240- environId := d .Id ()
241- clusterId := d .Get ("cluster_id" ).(string )
265+ idSplit := strings .Split (d .Id (), FILED_SP )
266+ if len (idSplit ) != 2 {
267+ return fmt .Errorf ("id is broken,%s" , idSplit )
268+ }
269+ environId := idSplit [0 ]
270+ clusterId := idSplit [1 ]
242271
243272 service := TdmqService {client : meta .(* TencentCloudClient ).apiV3Conn }
244273
0 commit comments