1
1
package rum
2
2
3
3
import (
4
- "fmt "
4
+ "context "
5
5
"log"
6
6
7
7
"github.com/aws/aws-sdk-go/aws"
8
8
"github.com/aws/aws-sdk-go/service/cloudwatchrum"
9
9
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
10
+ "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11
+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10
12
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11
13
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
12
14
"github.com/hashicorp/terraform-provider-aws/internal/conns"
@@ -16,10 +18,11 @@ import (
16
18
17
19
func ResourceMetricsDestination () * schema.Resource {
18
20
return & schema.Resource {
19
- Create : resourceMetricsDestinationPut ,
20
- Read : resourceMetricsDestinationRead ,
21
- Update : resourceMetricsDestinationPut ,
22
- Delete : resourceMetricsDestinationDelete ,
21
+ CreateWithoutTimeout : resourceMetricsDestinationPut ,
22
+ ReadWithoutTimeout : resourceMetricsDestinationRead ,
23
+ UpdateWithoutTimeout : resourceMetricsDestinationPut ,
24
+ DeleteWithoutTimeout : resourceMetricsDestinationDelete ,
25
+
23
26
Importer : & schema.ResourceImporter {
24
27
State : schema .ImportStatePassthrough ,
25
28
},
@@ -48,7 +51,7 @@ func ResourceMetricsDestination() *schema.Resource {
48
51
}
49
52
}
50
53
51
- func resourceMetricsDestinationPut (d * schema.ResourceData , meta interface {}) error {
54
+ func resourceMetricsDestinationPut (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
52
55
conn := meta .(* conns.AWSClient ).RUMConn
53
56
54
57
name := d .Get ("app_monitor_name" ).(string )
@@ -65,40 +68,43 @@ func resourceMetricsDestinationPut(d *schema.ResourceData, meta interface{}) err
65
68
input .IamRoleArn = aws .String (v .(string ))
66
69
}
67
70
68
- _ , err := conn .PutRumMetricsDestination ( input )
71
+ _ , err := conn .PutRumMetricsDestinationWithContext ( ctx , input )
69
72
70
73
if err != nil {
71
- return fmt .Errorf ("error creating CloudWatch RUM Metric Destination %s : %w " , name , err )
74
+ return diag .Errorf ("putting CloudWatch RUM Metrics Destination (%s) : %s " , name , err )
72
75
}
73
76
74
- d .SetId (name )
77
+ if d .IsNewResource () {
78
+ d .SetId (name )
79
+ }
75
80
76
- return resourceMetricsDestinationRead (d , meta )
81
+ return resourceMetricsDestinationRead (ctx , d , meta )
77
82
}
78
83
79
- func resourceMetricsDestinationRead (d * schema.ResourceData , meta interface {}) error {
84
+ func resourceMetricsDestinationRead (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
80
85
conn := meta .(* conns.AWSClient ).RUMConn
81
86
82
- dest , err := FindMetricsDestinationByName (conn , d .Id ())
87
+ dest , err := FindMetricsDestinationByName (ctx , conn , d .Id ())
88
+
83
89
if ! d .IsNewResource () && tfresource .NotFound (err ) {
84
- log .Printf ("[WARN] Unable to find CloudWatch RUM Metric Destination (%s); removing from state" , d .Id ())
90
+ log .Printf ("[WARN] CloudWatch RUM Metrics Destination %s not found, removing from state" , d .Id ())
85
91
d .SetId ("" )
86
92
return nil
87
93
}
88
94
89
95
if err != nil {
90
- return fmt .Errorf ("error reading CloudWatch RUM Metric Destination (%s): %w " , d .Id (), err )
96
+ return diag .Errorf ("reading CloudWatch RUM Metrics Destination (%s): %s " , d .Id (), err )
91
97
}
92
98
93
- d .Set ("destination" , dest .Destination )
94
99
d .Set ("app_monitor_name" , d .Id ())
100
+ d .Set ("destination" , dest .Destination )
95
101
d .Set ("destination_arn" , dest .DestinationArn )
96
102
d .Set ("iam_role_arn" , dest .IamRoleArn )
97
103
98
104
return nil
99
105
}
100
106
101
- func resourceMetricsDestinationDelete (d * schema.ResourceData , meta interface {}) error {
107
+ func resourceMetricsDestinationDelete (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
102
108
conn := meta .(* conns.AWSClient ).RUMConn
103
109
104
110
input := & cloudwatchrum.DeleteRumMetricsDestinationInput {
@@ -110,12 +116,45 @@ func resourceMetricsDestinationDelete(d *schema.ResourceData, meta interface{})
110
116
input .DestinationArn = aws .String (v .(string ))
111
117
}
112
118
113
- if _ , err := conn .DeleteRumMetricsDestination (input ); err != nil {
114
- if tfawserr .ErrCodeEquals (err , cloudwatchrum .ErrCodeResourceNotFoundException ) {
115
- return nil
116
- }
117
- return fmt .Errorf ("error deleting CloudWatch RUM Metric Destination (%s): %w" , d .Id (), err )
119
+ log .Printf ("[DEBUG] Deleting CloudWatch RUM Metrics Destination: %s" , d .Id ())
120
+ _ , err := conn .DeleteRumMetricsDestinationWithContext (ctx , input )
121
+
122
+ if tfawserr .ErrCodeEquals (err , cloudwatchrum .ErrCodeResourceNotFoundException ) {
123
+ return nil
124
+ }
125
+
126
+ if err != nil {
127
+ return diag .Errorf ("deleting CloudWatch RUM Metrics Destination (%s): %s" , d .Id (), err )
118
128
}
119
129
120
130
return nil
121
131
}
132
+
133
+ func FindMetricsDestinationByName (ctx context.Context , conn * cloudwatchrum.CloudWatchRUM , name string ) (* cloudwatchrum.MetricDestinationSummary , error ) {
134
+ input := cloudwatchrum.ListRumMetricsDestinationsInput {
135
+ AppMonitorName : aws .String (name ),
136
+ }
137
+
138
+ output , err := conn .ListRumMetricsDestinations (& input )
139
+
140
+ if tfawserr .ErrCodeEquals (err , cloudwatchrum .ErrCodeResourceNotFoundException ) {
141
+ return nil , & resource.NotFoundError {
142
+ LastError : err ,
143
+ LastRequest : input ,
144
+ }
145
+ }
146
+
147
+ if err != nil {
148
+ return nil , err
149
+ }
150
+
151
+ if output == nil || len (output .Destinations ) == 0 || output .Destinations [0 ] == nil {
152
+ return nil , tfresource .NewEmptyResultError (input )
153
+ }
154
+
155
+ if count := len (output .Destinations ); count > 1 {
156
+ return nil , tfresource .NewTooManyResultsError (count , input )
157
+ }
158
+
159
+ return output .Destinations [0 ], nil
160
+ }
0 commit comments