1
+ // Package alicloud. This file is generated automatically. Please do not modify it manually, thank you!
1
2
package alicloud
2
3
3
4
import (
5
+ "encoding/json"
4
6
"fmt"
7
+ "github.com/PaesslerAG/jsonpath"
8
+ "log"
5
9
"time"
6
10
7
11
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
8
12
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9
13
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
10
- "github.com/hashicorp/terraform-plugin-sdk/helper/validation"
11
14
)
12
15
13
- func resourceAlicloudMessageServiceSubscription () * schema.Resource {
16
+ func resourceAliCloudMessageServiceSubscription () * schema.Resource {
14
17
return & schema.Resource {
15
- Create : resourceAlicloudMessageServiceSubscriptionCreate ,
16
- Read : resourceAlicloudMessageServiceSubscriptionRead ,
17
- Update : resourceAlicloudMessageServiceSubscriptionUpdate ,
18
- Delete : resourceAlicloudMessageServiceSubscriptionDelete ,
18
+ Create : resourceAliCloudMessageServiceSubscriptionCreate ,
19
+ Read : resourceAliCloudMessageServiceSubscriptionRead ,
20
+ Update : resourceAliCloudMessageServiceSubscriptionUpdate ,
21
+ Delete : resourceAliCloudMessageServiceSubscriptionDelete ,
19
22
Importer : & schema.ResourceImporter {
20
23
State : schema .ImportStatePassthrough ,
21
24
},
22
25
Timeouts : & schema.ResourceTimeout {
23
- Create : schema .DefaultTimeout (3 * time .Minute ),
24
- Update : schema .DefaultTimeout (3 * time .Minute ),
25
- Delete : schema .DefaultTimeout (3 * time .Minute ),
26
+ Create : schema .DefaultTimeout (5 * time .Minute ),
27
+ Update : schema .DefaultTimeout (5 * time .Minute ),
28
+ Delete : schema .DefaultTimeout (5 * time .Minute ),
26
29
},
27
30
Schema : map [string ]* schema.Schema {
28
- "topic_name" : {
31
+ "create_time" : {
32
+ Type : schema .TypeInt ,
33
+ Computed : true ,
34
+ },
35
+ "dlq_policy" : {
36
+ Type : schema .TypeList ,
37
+ Optional : true ,
38
+ Computed : true ,
39
+ MaxItems : 1 ,
40
+ Elem : & schema.Resource {
41
+ Schema : map [string ]* schema.Schema {
42
+ "dead_letter_target_queue" : {
43
+ Type : schema .TypeString ,
44
+ Optional : true ,
45
+ },
46
+ "enabled" : {
47
+ Type : schema .TypeBool ,
48
+ Optional : true ,
49
+ },
50
+ },
51
+ },
52
+ },
53
+ "endpoint" : {
29
54
Type : schema .TypeString ,
30
55
Required : true ,
31
56
ForceNew : true ,
32
57
},
33
- "subscription_name " : {
58
+ "filter_tag " : {
34
59
Type : schema .TypeString ,
35
- Required : true ,
60
+ Optional : true ,
36
61
ForceNew : true ,
37
62
},
38
- "endpoint " : {
63
+ "notify_content_format " : {
39
64
Type : schema .TypeString ,
40
- Required : true ,
65
+ Optional : true ,
41
66
ForceNew : true ,
67
+ Computed : true ,
42
68
},
43
- "push_type" : {
44
- Type : schema .TypeString ,
45
- Required : true ,
46
- ForceNew : true ,
47
- ValidateFunc : validation .StringInSlice ([]string {"http" , "queue" , "mpush" , "alisms" , "email" }, false ),
69
+ "notify_strategy" : {
70
+ Type : schema .TypeString ,
71
+ Optional : true ,
72
+ Computed : true ,
48
73
},
49
- "filter_tag" : {
50
- Type : schema .TypeString ,
51
- Optional : true ,
52
- ForceNew : true ,
53
- ValidateFunc : validation .StringLenBetween (0 , 16 ),
74
+ "push_type" : {
75
+ Type : schema .TypeString ,
76
+ Required : true ,
54
77
},
55
- "notify_content_format" : {
56
- Type : schema .TypeString ,
57
- Optional : true ,
58
- Computed : true ,
59
- ForceNew : true ,
60
- ValidateFunc : validation .StringInSlice ([]string {"XML" , "JSON" , "SIMPLIFIED" }, false ),
78
+ "subscription_name" : {
79
+ Type : schema .TypeString ,
80
+ Required : true ,
81
+ ForceNew : true ,
61
82
},
62
- "notify_strategy" : {
63
- Type : schema .TypeString ,
64
- Optional : true ,
65
- Computed : true ,
66
- ValidateFunc : validation .StringInSlice ([]string {"BACKOFF_RETRY" , "EXPONENTIAL_DECAY_RETRY" }, false ),
83
+ "topic_name" : {
84
+ Type : schema .TypeString ,
85
+ Required : true ,
86
+ ForceNew : true ,
67
87
},
68
88
},
69
89
}
70
90
}
71
91
72
- func resourceAlicloudMessageServiceSubscriptionCreate (d * schema.ResourceData , meta interface {}) error {
92
+ func resourceAliCloudMessageServiceSubscriptionCreate (d * schema.ResourceData , meta interface {}) error {
93
+
73
94
client := meta .(* connectivity.AliyunClient )
74
- var response map [ string ] interface {}
95
+
75
96
action := "Subscribe"
76
- request := make (map [string ]interface {})
97
+ var request map [string ]interface {}
98
+ var response map [string ]interface {}
99
+ query := make (map [string ]interface {})
77
100
var err error
78
-
101
+ request = make ( map [ string ] interface {})
79
102
request ["TopicName" ] = d .Get ("topic_name" )
80
103
request ["SubscriptionName" ] = d .Get ("subscription_name" )
81
- request ["Endpoint" ] = d .Get ("endpoint" )
82
- request ["PushType" ] = d .Get ("push_type" )
83
104
84
- if v , ok := d .GetOk ("filter_tag " ); ok {
85
- request ["MessageTag " ] = v
105
+ if v , ok := d .GetOk ("notify_strategy " ); ok {
106
+ request ["NotifyStrategy " ] = v
86
107
}
87
-
88
108
if v , ok := d .GetOk ("notify_content_format" ); ok {
89
109
request ["NotifyContentFormat" ] = v
90
110
}
111
+ if v , ok := d .GetOk ("filter_tag" ); ok {
112
+ request ["MessageTag" ] = v
113
+ }
114
+ request ["Endpoint" ] = d .Get ("endpoint" )
115
+ request ["PushType" ] = d .Get ("push_type" )
116
+ objectDataLocalMap := make (map [string ]interface {})
91
117
92
- if v , ok := d .GetOk ("notify_strategy" ); ok {
93
- request ["NotifyStrategy" ] = v
118
+ if v := d .Get ("dlq_policy" ); ! IsNil (v ) {
119
+ deadLetterTargetQueue1 , _ := jsonpath .Get ("$[0].dead_letter_target_queue" , v )
120
+ if deadLetterTargetQueue1 != nil && deadLetterTargetQueue1 != "" {
121
+ objectDataLocalMap ["DeadLetterTargetQueue" ] = deadLetterTargetQueue1
122
+ }
123
+ enabled1 , _ := jsonpath .Get ("$[0].enabled" , v )
124
+ if enabled1 != nil && enabled1 != "" {
125
+ objectDataLocalMap ["Enabled" ] = enabled1
126
+ }
127
+
128
+ objectDataLocalMapJson , err := json .Marshal (objectDataLocalMap )
129
+ if err != nil {
130
+ return WrapError (err )
131
+ }
132
+ request ["DlqPolicy" ] = string (objectDataLocalMapJson )
94
133
}
95
134
96
- wait := incrementalWait (3 * time .Second , 3 * time .Second )
97
- err = resource .Retry (client . GetRetryTimeout ( d .Timeout (schema .TimeoutCreate ) ), func () * resource.RetryError {
98
- response , err = client .RpcPost ("Mns-open" , "2022-01-19" , action , nil , request , false )
135
+ wait := incrementalWait (3 * time .Second , 5 * time .Second )
136
+ err = resource .Retry (d .Timeout (schema .TimeoutCreate ), func () * resource.RetryError {
137
+ response , err = client .RpcPost ("Mns-open" , "2022-01-19" , action , query , request , true )
99
138
if err != nil {
100
139
if NeedRetry (err ) {
101
140
wait ()
@@ -111,46 +150,69 @@ func resourceAlicloudMessageServiceSubscriptionCreate(d *schema.ResourceData, me
111
150
return WrapErrorf (err , DefaultErrorMsg , "alicloud_message_service_subscription" , action , AlibabaCloudSdkGoERROR )
112
151
}
113
152
114
- d .SetId (fmt .Sprint ( request [ "TopicName" ], ":" , request ["SubscriptionName" ]))
153
+ d .SetId (fmt .Sprintf ( "%v:%v" , request [ "TopicName" ] , request ["SubscriptionName" ]))
115
154
116
- return resourceAlicloudMessageServiceSubscriptionRead (d , meta )
155
+ return resourceAliCloudMessageServiceSubscriptionRead (d , meta )
117
156
}
118
157
119
- func resourceAlicloudMessageServiceSubscriptionRead (d * schema.ResourceData , meta interface {}) error {
158
+ func resourceAliCloudMessageServiceSubscriptionRead (d * schema.ResourceData , meta interface {}) error {
120
159
client := meta .(* connectivity.AliyunClient )
121
- mnsOpenService := MnsOpenService {client }
160
+ messageServiceServiceV2 := MessageServiceServiceV2 {client }
122
161
123
- object , err := mnsOpenService .DescribeMessageServiceSubscription (d .Id ())
162
+ objectRaw , err := messageServiceServiceV2 .DescribeMessageServiceSubscription (d .Id ())
124
163
if err != nil {
125
- if NotFoundError (err ) {
164
+ if ! d .IsNewResource () && NotFoundError (err ) {
165
+ log .Printf ("[DEBUG] Resource alicloud_message_service_subscription DescribeMessageServiceSubscription Failed!!! %s" , err )
126
166
d .SetId ("" )
127
167
return nil
128
168
}
129
169
return WrapError (err )
130
170
}
131
171
132
- d .Set ("topic_name" , object ["TopicName" ])
133
- d .Set ("subscription_name" , object ["SubscriptionName" ])
134
- d .Set ("endpoint" , object ["Endpoint" ])
135
- d .Set ("filter_tag" , object ["FilterTag" ])
136
- d .Set ("notify_content_format" , object ["NotifyContentFormat" ])
137
- d .Set ("notify_strategy" , object ["NotifyStrategy" ])
172
+ d .Set ("create_time" , objectRaw ["CreateTime" ])
173
+ d .Set ("endpoint" , objectRaw ["Endpoint" ])
174
+ d .Set ("filter_tag" , objectRaw ["FilterTag" ])
175
+ d .Set ("notify_content_format" , objectRaw ["NotifyContentFormat" ])
176
+ d .Set ("notify_strategy" , objectRaw ["NotifyStrategy" ])
177
+ d .Set ("subscription_name" , objectRaw ["SubscriptionName" ])
178
+ d .Set ("topic_name" , objectRaw ["TopicName" ])
179
+
180
+ dlqPolicyMaps := make ([]map [string ]interface {}, 0 )
181
+ dlqPolicyMap := make (map [string ]interface {})
182
+ dlqPolicyRaw := make (map [string ]interface {})
183
+ if objectRaw ["DlqPolicy" ] != nil {
184
+ dlqPolicyRaw = objectRaw ["DlqPolicy" ].(map [string ]interface {})
185
+ }
186
+ if len (dlqPolicyRaw ) > 0 {
187
+ dlqPolicyMap ["dead_letter_target_queue" ] = dlqPolicyRaw ["DeadLetterTargetQueue" ]
188
+ dlqPolicyMap ["enabled" ] = dlqPolicyRaw ["Enabled" ]
189
+
190
+ dlqPolicyMaps = append (dlqPolicyMaps , dlqPolicyMap )
191
+ }
192
+ if err := d .Set ("dlq_policy" , dlqPolicyMaps ); err != nil {
193
+ return err
194
+ }
138
195
139
196
return nil
140
197
}
141
198
142
- func resourceAlicloudMessageServiceSubscriptionUpdate (d * schema.ResourceData , meta interface {}) error {
199
+ func resourceAliCloudMessageServiceSubscriptionUpdate (d * schema.ResourceData , meta interface {}) error {
143
200
client := meta .(* connectivity.AliyunClient )
201
+ var request map [string ]interface {}
144
202
var response map [string ]interface {}
203
+ var query map [string ]interface {}
145
204
update := false
205
+
206
+ var err error
146
207
parts , err := ParseResourceId (d .Id (), 2 )
147
208
if err != nil {
148
209
return WrapError (err )
149
210
}
150
- request := map [string ]interface {}{
151
- "TopicName" : parts [0 ],
152
- "SubscriptionName" : parts [1 ],
153
- }
211
+ action := "SetSubscriptionAttributes"
212
+ request = make (map [string ]interface {})
213
+ query = make (map [string ]interface {})
214
+ request ["TopicName" ] = parts [0 ]
215
+ request ["SubscriptionName" ] = parts [1 ]
154
216
155
217
if d .HasChange ("notify_strategy" ) {
156
218
update = true
@@ -159,11 +221,35 @@ func resourceAlicloudMessageServiceSubscriptionUpdate(d *schema.ResourceData, me
159
221
request ["NotifyStrategy" ] = v
160
222
}
161
223
224
+ if d .HasChange ("dlq_policy" ) {
225
+ update = true
226
+ objectDataLocalMap := make (map [string ]interface {})
227
+
228
+ if v := d .Get ("dlq_policy" ); v != nil {
229
+ enabled1 , _ := jsonpath .Get ("$[0].enabled" , v )
230
+ if enabled1 != nil && (d .HasChange ("dlq_policy.0.enabled" ) || enabled1 != "" ) {
231
+ objectDataLocalMap ["Enabled" ] = enabled1
232
+
233
+ if objectDataLocalMap ["Enabled" ].(bool ) {
234
+ deadLetterTargetQueue1 , _ := jsonpath .Get ("$[0].dead_letter_target_queue" , v )
235
+ if deadLetterTargetQueue1 != nil && (d .HasChange ("dlq_policy.0.dead_letter_target_queue" ) || deadLetterTargetQueue1 != "" ) {
236
+ objectDataLocalMap ["DeadLetterTargetQueue" ] = deadLetterTargetQueue1
237
+ }
238
+ }
239
+ }
240
+
241
+ objectDataLocalMapJson , err := json .Marshal (objectDataLocalMap )
242
+ if err != nil {
243
+ return WrapError (err )
244
+ }
245
+ request ["DlqPolicy" ] = string (objectDataLocalMapJson )
246
+ }
247
+ }
248
+
162
249
if update {
163
- action := "SetSubscriptionAttributes"
164
- wait := incrementalWait (3 * time .Second , 3 * time .Second )
165
- err = resource .Retry (client .GetRetryTimeout (d .Timeout (schema .TimeoutUpdate )), func () * resource.RetryError {
166
- response , err = client .RpcPost ("Mns-open" , "2022-01-19" , action , nil , request , false )
250
+ wait := incrementalWait (3 * time .Second , 5 * time .Second )
251
+ err = resource .Retry (d .Timeout (schema .TimeoutUpdate ), func () * resource.RetryError {
252
+ response , err = client .RpcPost ("Mns-open" , "2022-01-19" , action , query , request , true )
167
253
if err != nil {
168
254
if NeedRetry (err ) {
169
255
wait ()
@@ -174,33 +260,33 @@ func resourceAlicloudMessageServiceSubscriptionUpdate(d *schema.ResourceData, me
174
260
return nil
175
261
})
176
262
addDebug (action , response , request )
177
-
178
263
if err != nil {
179
264
return WrapErrorf (err , DefaultErrorMsg , d .Id (), action , AlibabaCloudSdkGoERROR )
180
265
}
181
266
}
182
267
183
- return resourceAlicloudMessageServiceSubscriptionRead (d , meta )
268
+ return resourceAliCloudMessageServiceSubscriptionRead (d , meta )
184
269
}
185
270
186
- func resourceAlicloudMessageServiceSubscriptionDelete (d * schema.ResourceData , meta interface {}) error {
187
- client := meta .(* connectivity.AliyunClient )
188
- action := "Unsubscribe"
189
- var response map [string ]interface {}
190
- var err error
271
+ func resourceAliCloudMessageServiceSubscriptionDelete (d * schema.ResourceData , meta interface {}) error {
191
272
273
+ client := meta .(* connectivity.AliyunClient )
192
274
parts , err := ParseResourceId (d .Id (), 2 )
193
275
if err != nil {
194
276
return WrapError (err )
195
277
}
196
- request := map [string ]interface {}{
197
- "TopicName" : parts [0 ],
198
- "SubscriptionName" : parts [1 ],
199
- }
278
+ action := "Unsubscribe"
279
+ var request map [string ]interface {}
280
+ var response map [string ]interface {}
281
+ query := make (map [string ]interface {})
282
+ request = make (map [string ]interface {})
283
+ request ["SubscriptionName" ] = parts [1 ]
284
+ request ["TopicName" ] = parts [0 ]
285
+
286
+ wait := incrementalWait (3 * time .Second , 5 * time .Second )
287
+ err = resource .Retry (d .Timeout (schema .TimeoutDelete ), func () * resource.RetryError {
288
+ response , err = client .RpcPost ("Mns-open" , "2022-01-19" , action , query , request , true )
200
289
201
- wait := incrementalWait (3 * time .Second , 3 * time .Second )
202
- err = resource .Retry (client .GetRetryTimeout (d .Timeout (schema .TimeoutDelete )), func () * resource.RetryError {
203
- response , err = client .RpcPost ("Mns-open" , "2022-01-19" , action , nil , request , false )
204
290
if err != nil {
205
291
if NeedRetry (err ) {
206
292
wait ()
0 commit comments