1
1
package rum
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
5
6
"log"
6
7
7
8
"github.com/aws/aws-sdk-go/aws"
8
9
"github.com/aws/aws-sdk-go/aws/arn"
9
10
"github.com/aws/aws-sdk-go/service/cloudwatchrum"
10
11
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
12
+ "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
13
+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11
14
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12
15
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
13
16
"github.com/hashicorp/terraform-provider-aws/internal/conns"
@@ -19,10 +22,11 @@ import (
19
22
20
23
func ResourceAppMonitor () * schema.Resource {
21
24
return & schema.Resource {
22
- Create : resourceAppMonitorCreate ,
23
- Read : resourceAppMonitorRead ,
24
- Update : resourceAppMonitorUpdate ,
25
- Delete : resourceAppMonitorDelete ,
25
+ CreateWithoutTimeout : resourceAppMonitorCreate ,
26
+ ReadWithoutTimeout : resourceAppMonitorRead ,
27
+ UpdateWithoutTimeout : resourceAppMonitorUpdate ,
28
+ DeleteWithoutTimeout : resourceAppMonitorDelete ,
29
+
26
30
Importer : & schema.ResourceImporter {
27
31
State : schema .ImportStatePassthrough ,
28
32
},
@@ -122,7 +126,7 @@ func ResourceAppMonitor() *schema.Resource {
122
126
}
123
127
}
124
128
125
- func resourceAppMonitorCreate (d * schema.ResourceData , meta interface {}) error {
129
+ func resourceAppMonitorCreate (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
126
130
conn := meta .(* conns.AWSClient ).RUMConn
127
131
defaultTagsConfig := meta .(* conns.AWSClient ).DefaultTagsConfig
128
132
tags := defaultTagsConfig .MergeTags (tftags .New (d .Get ("tags" ).(map [string ]interface {})))
@@ -142,35 +146,36 @@ func resourceAppMonitorCreate(d *schema.ResourceData, meta interface{}) error {
142
146
input .Tags = Tags (tags .IgnoreAWS ())
143
147
}
144
148
145
- _ , err := conn .CreateAppMonitor ( input )
149
+ _ , err := conn .CreateAppMonitorWithContext ( ctx , input )
146
150
147
151
if err != nil {
148
- return fmt .Errorf ("error creating CloudWatch RUM App Monitor %s : %w " , name , err )
152
+ return diag .Errorf ("creating CloudWatch RUM App Monitor (%s) : %s " , name , err )
149
153
}
150
154
151
155
d .SetId (name )
152
156
153
- return resourceAppMonitorRead (d , meta )
157
+ return resourceAppMonitorRead (ctx , d , meta )
154
158
}
155
159
156
- func resourceAppMonitorRead (d * schema.ResourceData , meta interface {}) error {
160
+ func resourceAppMonitorRead (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
157
161
conn := meta .(* conns.AWSClient ).RUMConn
158
162
defaultTagsConfig := meta .(* conns.AWSClient ).DefaultTagsConfig
159
163
ignoreTagsConfig := meta .(* conns.AWSClient ).IgnoreTagsConfig
160
164
161
- appMon , err := FindAppMonitorByName (conn , d .Id ())
165
+ appMon , err := FindAppMonitorByName (ctx , conn , d .Id ())
166
+
162
167
if ! d .IsNewResource () && tfresource .NotFound (err ) {
163
- log .Printf ("[WARN] Unable to find CloudWatch RUM App Monitor (%s); removing from state" , d .Id ())
168
+ log .Printf ("[WARN] CloudWatch RUM App Monitor %s not found, removing from state" , d .Id ())
164
169
d .SetId ("" )
165
170
return nil
166
171
}
167
172
168
173
if err != nil {
169
- return fmt .Errorf ("error reading CloudWatch RUM App Monitor (%s): %w " , d .Id (), err )
174
+ return diag .Errorf ("reading CloudWatch RUM App Monitor (%s): %s " , d .Id (), err )
170
175
}
171
176
172
177
if err := d .Set ("app_monitor_configuration" , []interface {}{flattenAppMonitorConfiguration (appMon .AppMonitorConfiguration )}); err != nil {
173
- return fmt .Errorf ("setting app_monitor_configuration: %w " , err )
178
+ return diag .Errorf ("setting app_monitor_configuration: %s " , err )
174
179
}
175
180
d .Set ("app_monitor_id" , appMon .Id )
176
181
arn := arn.ARN {
@@ -190,71 +195,98 @@ func resourceAppMonitorRead(d *schema.ResourceData, meta interface{}) error {
190
195
191
196
//lintignore:AWSR002
192
197
if err := d .Set ("tags" , tags .RemoveDefaultConfig (defaultTagsConfig ).Map ()); err != nil {
193
- return fmt .Errorf ("error setting tags: %w " , err )
198
+ return diag .Errorf ("setting tags: %s " , err )
194
199
}
195
200
196
201
if err := d .Set ("tags_all" , tags .Map ()); err != nil {
197
- return fmt .Errorf ("error setting tags_all: %w " , err )
202
+ return diag .Errorf ("setting tags_all: %s " , err )
198
203
}
199
204
200
205
return nil
201
206
}
202
207
203
- func resourceAppMonitorUpdate (d * schema.ResourceData , meta interface {}) error {
208
+ func resourceAppMonitorUpdate (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
204
209
conn := meta .(* conns.AWSClient ).RUMConn
205
210
206
211
if d .HasChangesExcept ("tags" , "tags_all" ) {
207
212
input := & cloudwatchrum.UpdateAppMonitorInput {
208
213
Name : aws .String (d .Id ()),
209
214
}
210
215
211
- if d .HasChange ("cw_log_enabled" ) {
212
- input .CwLogEnabled = aws .Bool (d .Get ("cw_log_enabled" ).(bool ))
213
- }
214
-
215
216
if d .HasChange ("app_monitor_configuration" ) {
216
217
input .AppMonitorConfiguration = expandAppMonitorConfiguration (d .Get ("app_monitor_configuration" ).([]interface {})[0 ].(map [string ]interface {}))
217
218
}
218
219
220
+ if d .HasChange ("cw_log_enabled" ) {
221
+ input .CwLogEnabled = aws .Bool (d .Get ("cw_log_enabled" ).(bool ))
222
+ }
223
+
219
224
if d .HasChange ("domain" ) {
220
225
input .Domain = aws .String (d .Get ("domain" ).(string ))
221
226
}
222
227
223
- log . Printf ( "[DEBUG] cloudwatchrum AppMonitor update config: %s" , input . String () )
224
- _ , err := conn . UpdateAppMonitor ( input )
228
+ _ , err := conn . UpdateAppMonitorWithContext ( ctx , input )
229
+
225
230
if err != nil {
226
- return fmt .Errorf ("error updating CloudWatch RUM App Monitor: %w" , err )
231
+ return diag .Errorf ("updating CloudWatch RUM App Monitor (%s) : %s" , d . Id () , err )
227
232
}
228
233
}
229
234
230
235
if d .HasChange ("tags_all" ) {
231
236
o , n := d .GetChange ("tags_all" )
232
237
233
- if err := UpdateTags ( conn , d .Get ("arn" ).(string ), o , n ); err != nil {
234
- return fmt .Errorf ("error updating CloudWatch RUM App Monitor (%s) tags: %w " , d .Id (), err )
238
+ if err := UpdateTagsWithContext ( ctx , conn , d .Get ("arn" ).(string ), o , n ); err != nil {
239
+ return diag .Errorf ("updating CloudWatch RUM App Monitor (%s) tags: %s " , d .Id (), err )
235
240
}
236
241
}
237
242
238
- return resourceAppMonitorRead (d , meta )
243
+ return resourceAppMonitorRead (ctx , d , meta )
239
244
}
240
245
241
- func resourceAppMonitorDelete (d * schema.ResourceData , meta interface {}) error {
246
+ func resourceAppMonitorDelete (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
242
247
conn := meta .(* conns.AWSClient ).RUMConn
243
248
244
- input := & cloudwatchrum.DeleteAppMonitorInput {
249
+ log .Printf ("[DEBUG] Deleting CloudWatch RUM App Monitor: %s" , d .Id ())
250
+ _ , err := conn .DeleteAppMonitorWithContext (ctx , & cloudwatchrum.DeleteAppMonitorInput {
245
251
Name : aws .String (d .Id ()),
252
+ })
253
+
254
+ if tfawserr .ErrCodeEquals (err , cloudwatchrum .ErrCodeResourceNotFoundException ) {
255
+ return nil
246
256
}
247
257
248
- if _ , err := conn .DeleteAppMonitor (input ); err != nil {
249
- if tfawserr .ErrCodeEquals (err , cloudwatchrum .ErrCodeResourceNotFoundException ) {
250
- return nil
251
- }
252
- return fmt .Errorf ("error deleting CloudWatch RUM App Monitor (%s): %w" , d .Id (), err )
258
+ if err != nil {
259
+ return diag .Errorf ("deleting CloudWatch RUM App Monitor (%s): %s" , d .Id (), err )
253
260
}
254
261
255
262
return nil
256
263
}
257
264
265
+ func FindAppMonitorByName (ctx context.Context , conn * cloudwatchrum.CloudWatchRUM , name string ) (* cloudwatchrum.AppMonitor , error ) {
266
+ input := & cloudwatchrum.GetAppMonitorInput {
267
+ Name : aws .String (name ),
268
+ }
269
+
270
+ output , err := conn .GetAppMonitorWithContext (ctx , input )
271
+
272
+ if tfawserr .ErrCodeEquals (err , cloudwatchrum .ErrCodeResourceNotFoundException ) {
273
+ return nil , & resource.NotFoundError {
274
+ LastError : err ,
275
+ LastRequest : input ,
276
+ }
277
+ }
278
+
279
+ if err != nil {
280
+ return nil , err
281
+ }
282
+
283
+ if output == nil || output .AppMonitor == nil {
284
+ return nil , tfresource .NewEmptyResultError (input )
285
+ }
286
+
287
+ return output .AppMonitor , nil
288
+ }
289
+
258
290
func expandAppMonitorConfiguration (tfMap map [string ]interface {}) * cloudwatchrum.AppMonitorConfiguration {
259
291
if tfMap == nil {
260
292
return nil
0 commit comments