Skip to content

Commit 47161f8

Browse files
committed
fix(mnq): v2 for sns
1 parent b225d40 commit 47161f8

19 files changed

+2005
-12735
lines changed

internal/services/mnq/helpers_mnq_sns.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import (
88
"regexp"
99
"strings"
1010

11-
"github.com/aws/aws-sdk-go/aws"
12-
"github.com/aws/aws-sdk-go/aws/credentials"
13-
"github.com/aws/aws-sdk-go/aws/session"
14-
"github.com/aws/aws-sdk-go/service/sns"
11+
"github.com/aws/aws-sdk-go-v2/config"
12+
"github.com/aws/aws-sdk-go-v2/credentials"
13+
"github.com/aws/aws-sdk-go-v2/service/sns"
1514
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
1615
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
1716
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -20,7 +19,7 @@ import (
2019
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
2120
)
2221

23-
func SNSClientWithRegion(d *schema.ResourceData, m interface{}) (*sns.SNS, scw.Region, error) {
22+
func SNSClientWithRegion(d *schema.ResourceData, m interface{}) (*sns.Client, scw.Region, error) {
2423
region, err := meta.ExtractRegion(d, m)
2524
if err != nil {
2625
return nil, "", err
@@ -38,7 +37,7 @@ func SNSClientWithRegion(d *schema.ResourceData, m interface{}) (*sns.SNS, scw.R
3837
return snsClient, region, err
3938
}
4039

41-
func SNSClientWithRegionFromID(d *schema.ResourceData, m interface{}, regionalID string) (*sns.SNS, scw.Region, error) {
40+
func SNSClientWithRegionFromID(d *schema.ResourceData, m interface{}, regionalID string) (*sns.Client, scw.Region, error) {
4241
tab := strings.SplitN(regionalID, "/", 2)
4342
if len(tab) != 2 {
4443
return nil, "", errors.New("invalid ID format, expected parts separated by slashes")
@@ -60,22 +59,26 @@ func SNSClientWithRegionFromID(d *schema.ResourceData, m interface{}, regionalID
6059
return snsClient, region, err
6160
}
6261

63-
func NewSNSClient(httpClient *http.Client, region string, endpoint string, accessKey string, secretKey string) (*sns.SNS, error) {
64-
config := &aws.Config{}
65-
config.WithRegion(region)
66-
config.WithCredentials(credentials.NewStaticCredentials(accessKey, secretKey, ""))
67-
config.WithEndpoint(strings.ReplaceAll(endpoint, "{region}", region))
68-
config.WithHTTPClient(httpClient)
62+
func NewSNSClient(httpClient *http.Client, region string, endpoint string, accessKey string, secretKey string) (*sns.Client, error) {
63+
customEndpoint := strings.ReplaceAll(endpoint, "{region}", region)
64+
customConfig, err := config.LoadDefaultConfig(
65+
context.TODO(),
66+
config.WithRegion(region),
67+
config.WithBaseEndpoint(customEndpoint),
68+
config.WithHTTPClient(httpClient),
69+
config.WithCredentialsProvider(
70+
credentials.NewStaticCredentialsProvider(accessKey, secretKey, ""),
71+
),
72+
)
73+
6974
if logging.IsDebugOrHigher() {
70-
config.WithLogLevel(aws.LogDebugWithHTTPBody)
75+
customConfig.Logger = &httpDebugLogger{}
7176
}
7277

73-
s, err := session.NewSession(config)
7478
if err != nil {
7579
return nil, err
7680
}
77-
78-
return sns.New(s), nil
81+
return sns.NewFromConfig(customConfig), nil
7982
}
8083

8184
func composeMNQSubscriptionID(region scw.Region, projectID string, topicName string, subscriptionID string) string {

internal/services/mnq/sns_topic.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func ResourceMNQSNSTopicCreate(ctx context.Context, d *schema.ResourceData, m in
125125
Attributes: attributes,
126126
}
127127

128-
output, err := snsClient.CreateTopicWithContext(ctx, input)
128+
output, err := snsClient.CreateTopic(ctx, input)
129129
if err != nil {
130130
return diag.FromErr(fmt.Errorf("failed to create SNS Topic: %w", err))
131131
}
@@ -150,7 +150,7 @@ func ResourceMNQSNSTopicRead(ctx context.Context, d *schema.ResourceData, m inte
150150
return diag.FromErr(fmt.Errorf("failed to parse id: %w", err))
151151
}
152152

153-
topicAttributes, err := snsClient.GetTopicAttributesWithContext(ctx, &sns.GetTopicAttributesInput{
153+
topicAttributes, err := snsClient.GetTopicAttributes(ctx, &sns.GetTopicAttributesInput{
154154
TopicArn: scw.StringPtr(ComposeSNSARN(region, projectID, topicName)),
155155
})
156156
if err != nil {
@@ -197,17 +197,17 @@ func ResourceMNQSNSTopicUpdate(ctx context.Context, d *schema.ResourceData, m in
197197
return diag.FromErr(fmt.Errorf("failed to get attributes from schema: %w", err))
198198
}
199199

200-
updatedAttributes := map[string]*string{}
200+
updatedAttributes := map[string]string{}
201201

202202
for _, changedAttribute := range changedAttributes {
203203
updatedAttributes[changedAttribute] = attributes[changedAttribute]
204204
}
205205

206206
if len(updatedAttributes) > 0 {
207207
for attributeName, attributeValue := range updatedAttributes {
208-
_, err := snsClient.SetTopicAttributes(&sns.SetTopicAttributesInput{
208+
_, err := snsClient.SetTopicAttributes(ctx, &sns.SetTopicAttributesInput{
209209
AttributeName: scw.StringPtr(attributeName),
210-
AttributeValue: attributeValue,
210+
AttributeValue: &attributeValue,
211211
TopicArn: &topicARN,
212212
})
213213
if err != nil {
@@ -230,7 +230,7 @@ func ResourceMNQSNSTopicDelete(ctx context.Context, d *schema.ResourceData, m in
230230
return diag.FromErr(err)
231231
}
232232

233-
_, err = snsClient.DeleteTopicWithContext(ctx, &sns.DeleteTopicInput{
233+
_, err = snsClient.DeleteTopic(ctx, &sns.DeleteTopicInput{
234234
TopicArn: scw.StringPtr(ComposeSNSARN(region, projectID, topicName)),
235235
})
236236
if err != nil {

internal/services/mnq/sns_topic_subscription.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/aws/aws-sdk-go/service/sns"
7+
"github.com/aws/aws-sdk-go-v2/service/sns"
88
"github.com/hashicorp/go-cty/cty"
99
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -140,11 +140,11 @@ func ResourceMNQSNSTopicSubscriptionCreate(ctx context.Context, d *schema.Resour
140140
Attributes: attributes,
141141
Endpoint: types.ExpandStringPtr(d.Get("endpoint")),
142142
Protocol: types.ExpandStringPtr(d.Get("protocol")),
143-
ReturnSubscriptionArn: scw.BoolPtr(true),
143+
ReturnSubscriptionArn: true,
144144
TopicArn: &topicARN,
145145
}
146146

147-
output, err := snsClient.SubscribeWithContext(ctx, input)
147+
output, err := snsClient.Subscribe(ctx, input)
148148
if err != nil {
149149
return diag.FromErr(fmt.Errorf("failed to create SNS Topic: %w", err))
150150
}
@@ -174,7 +174,7 @@ func ResourceMNQSNSTopicSubscriptionRead(ctx context.Context, d *schema.Resource
174174
return diag.FromErr(fmt.Errorf("failed to parse id: %w", err))
175175
}
176176

177-
subAttributes, err := snsClient.GetSubscriptionAttributesWithContext(ctx, &sns.GetSubscriptionAttributesInput{
177+
subAttributes, err := snsClient.GetSubscriptionAttributes(ctx, &sns.GetSubscriptionAttributesInput{
178178
SubscriptionArn: scw.StringPtr(arn.String()),
179179
})
180180
if err != nil {
@@ -207,7 +207,7 @@ func ResourceMNQSNSTopicSubscriptionDelete(ctx context.Context, d *schema.Resour
207207
return diag.FromErr(fmt.Errorf("failed to parse id: %w", err))
208208
}
209209

210-
_, err = snsClient.UnsubscribeWithContext(ctx, &sns.UnsubscribeInput{
210+
_, err = snsClient.Unsubscribe(ctx, &sns.UnsubscribeInput{
211211
SubscriptionArn: scw.StringPtr(arn.String()),
212212
})
213213
if err != nil {

internal/services/mnq/sns_topic_subscription_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package mnq_test
22

33
import (
4+
"context"
45
"fmt"
56
"testing"
67

7-
"github.com/aws/aws-sdk-go/service/sns"
8+
"github.com/aws/aws-sdk-go-v2/service/sns"
89
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
910
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
@@ -16,11 +17,11 @@ import (
1617
func TestAccSNSTopicSubscription_Basic(t *testing.T) {
1718
tt := acctest.NewTestTools(t)
1819
defer tt.Cleanup()
19-
20+
ctx := context.Background()
2021
resource.ParallelTest(t, resource.TestCase{
2122
PreCheck: func() { acctest.PreCheck(t) },
2223
ProviderFactories: tt.ProviderFactories,
23-
CheckDestroy: isSNSTopicSubscriptionDestroyed(tt),
24+
CheckDestroy: isSNSTopicSubscriptionDestroyed(ctx, tt),
2425
Steps: []resource.TestStep{
2526
{
2627
Config: `
@@ -67,12 +68,12 @@ func TestAccSNSTopicSubscription_Basic(t *testing.T) {
6768
}
6869
`,
6970
Check: resource.ComposeTestCheckFunc(
70-
isSNSTopicSubscriptionPresent(tt, "scaleway_mnq_sns_topic_subscription.by_id"),
71+
isSNSTopicSubscriptionPresent(ctx, tt, "scaleway_mnq_sns_topic_subscription.by_id"),
7172
acctest.CheckResourceAttrUUID("scaleway_mnq_sns_topic_subscription.by_id", "id"),
7273
resource.TestCheckResourceAttr("scaleway_mnq_sns_topic_subscription.by_id", "protocol", "http"),
7374
resource.TestCheckResourceAttr("scaleway_mnq_sns_topic_subscription.by_id", "endpoint", "http://scaleway.com"),
7475

75-
isSNSTopicSubscriptionPresent(tt, "scaleway_mnq_sns_topic_subscription.by_arn"),
76+
isSNSTopicSubscriptionPresent(ctx, tt, "scaleway_mnq_sns_topic_subscription.by_arn"),
7677
acctest.CheckResourceAttrUUID("scaleway_mnq_sns_topic_subscription.by_arn", "id"),
7778
resource.TestCheckResourceAttr("scaleway_mnq_sns_topic_subscription.by_arn", "protocol", "http"),
7879
resource.TestCheckResourceAttr("scaleway_mnq_sns_topic_subscription.by_arn", "endpoint", "http://scaleway.com"),
@@ -82,7 +83,7 @@ func TestAccSNSTopicSubscription_Basic(t *testing.T) {
8283
})
8384
}
8485

85-
func isSNSTopicSubscriptionPresent(tt *acctest.TestTools, n string) resource.TestCheckFunc {
86+
func isSNSTopicSubscriptionPresent(ctx context.Context, tt *acctest.TestTools, n string) resource.TestCheckFunc {
8687
return func(state *terraform.State) error {
8788
rs, ok := state.RootModule().Resources[n]
8889
if !ok {
@@ -99,7 +100,7 @@ func isSNSTopicSubscriptionPresent(tt *acctest.TestTools, n string) resource.Tes
99100
return err
100101
}
101102

102-
_, err = snsClient.GetSubscriptionAttributes(&sns.GetSubscriptionAttributesInput{
103+
_, err = snsClient.GetSubscriptionAttributes(ctx, &sns.GetSubscriptionAttributesInput{
103104
SubscriptionArn: scw.StringPtr(arn.String()),
104105
})
105106
if err != nil {
@@ -110,7 +111,7 @@ func isSNSTopicSubscriptionPresent(tt *acctest.TestTools, n string) resource.Tes
110111
}
111112
}
112113

113-
func isSNSTopicSubscriptionDestroyed(tt *acctest.TestTools) resource.TestCheckFunc {
114+
func isSNSTopicSubscriptionDestroyed(ctx context.Context, tt *acctest.TestTools) resource.TestCheckFunc {
114115
return func(state *terraform.State) error {
115116
for _, rs := range state.RootModule().Resources {
116117
if rs.Type != "scaleway_mnq_sns_topic_subscription" {
@@ -127,7 +128,7 @@ func isSNSTopicSubscriptionDestroyed(tt *acctest.TestTools) resource.TestCheckFu
127128
return err
128129
}
129130

130-
_, err = snsClient.GetSubscriptionAttributes(&sns.GetSubscriptionAttributesInput{
131+
_, err = snsClient.GetSubscriptionAttributes(ctx, &sns.GetSubscriptionAttributesInput{
131132
SubscriptionArn: scw.StringPtr(arn.String()),
132133
})
133134
if err != nil {

internal/services/mnq/sns_topic_test.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package mnq_test
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"strings"
78
"testing"
89

9-
sns "github.com/aws/aws-sdk-go/service/sns"
10+
sns "github.com/aws/aws-sdk-go-v2/service/sns"
1011
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1213
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
@@ -19,10 +20,12 @@ func TestAccSNSTopic_Basic(t *testing.T) {
1920
tt := acctest.NewTestTools(t)
2021
defer tt.Cleanup()
2122

23+
ctx := context.Background()
24+
2225
resource.ParallelTest(t, resource.TestCase{
2326
PreCheck: func() { acctest.PreCheck(t) },
2427
ProviderFactories: tt.ProviderFactories,
25-
CheckDestroy: isSNSTopicDestroyed(tt),
28+
CheckDestroy: isSNSTopicDestroyed(ctx, tt),
2629
Steps: []resource.TestStep{
2730
{
2831
Config: `
@@ -49,7 +52,7 @@ func TestAccSNSTopic_Basic(t *testing.T) {
4952
}
5053
`,
5154
Check: resource.ComposeTestCheckFunc(
52-
isSNSTopicPresent(tt, "scaleway_mnq_sns_topic.main"),
55+
isSNSTopicPresent(ctx, tt, "scaleway_mnq_sns_topic.main"),
5356
acctest.CheckResourceAttrUUID("scaleway_mnq_sns_topic.main", "id"),
5457
resource.TestCheckResourceAttr("scaleway_mnq_sns_topic.main", "name", "test-mnq-sns-topic-basic"),
5558
),
@@ -81,7 +84,7 @@ func TestAccSNSTopic_Basic(t *testing.T) {
8184
}
8285
`,
8386
Check: resource.ComposeTestCheckFunc(
84-
isSNSTopicPresent(tt, "scaleway_mnq_sns_topic.main"),
87+
isSNSTopicPresent(ctx, tt, "scaleway_mnq_sns_topic.main"),
8588
acctest.CheckResourceAttrUUID("scaleway_mnq_sns_topic.main", "id"),
8689
resource.TestCheckResourceAttr("scaleway_mnq_sns_topic.main", "name", "test-mnq-sns-topic-basic.fifo"),
8790
resource.TestCheckResourceAttr("scaleway_mnq_sns_topic.main", "content_based_deduplication", "true"),
@@ -114,7 +117,7 @@ func TestAccSNSTopic_Basic(t *testing.T) {
114117
}
115118
`,
116119
Check: resource.ComposeTestCheckFunc(
117-
isSNSTopicPresent(tt, "scaleway_mnq_sns_topic.main"),
120+
isSNSTopicPresent(ctx, tt, "scaleway_mnq_sns_topic.main"),
118121
acctest.CheckResourceAttrUUID("scaleway_mnq_sns_topic.main", "id"),
119122
func(state *terraform.State) error {
120123
topic, exists := state.RootModule().Resources["scaleway_mnq_sns_topic.main"]
@@ -139,7 +142,7 @@ func TestAccSNSTopic_Basic(t *testing.T) {
139142
})
140143
}
141144

142-
func isSNSTopicPresent(tt *acctest.TestTools, n string) resource.TestCheckFunc {
145+
func isSNSTopicPresent(ctx context.Context, tt *acctest.TestTools, n string) resource.TestCheckFunc {
143146
return func(state *terraform.State) error {
144147
rs, ok := state.RootModule().Resources[n]
145148
if !ok {
@@ -156,7 +159,7 @@ func isSNSTopicPresent(tt *acctest.TestTools, n string) resource.TestCheckFunc {
156159
return err
157160
}
158161

159-
_, err = snsClient.GetTopicAttributes(&sns.GetTopicAttributesInput{
162+
_, err = snsClient.GetTopicAttributes(ctx, &sns.GetTopicAttributesInput{
160163
TopicArn: scw.StringPtr(mnq.ComposeSNSARN(region, projectID, topicName)),
161164
})
162165
if err != nil {
@@ -167,7 +170,7 @@ func isSNSTopicPresent(tt *acctest.TestTools, n string) resource.TestCheckFunc {
167170
}
168171
}
169172

170-
func isSNSTopicDestroyed(tt *acctest.TestTools) resource.TestCheckFunc {
173+
func isSNSTopicDestroyed(ctx context.Context, tt *acctest.TestTools) resource.TestCheckFunc {
171174
return func(state *terraform.State) error {
172175
for _, rs := range state.RootModule().Resources {
173176
if rs.Type != "scaleway_mnq_sns_topic" {
@@ -184,7 +187,7 @@ func isSNSTopicDestroyed(tt *acctest.TestTools) resource.TestCheckFunc {
184187
return err
185188
}
186189

187-
_, err = snsClient.GetTopicAttributes(&sns.GetTopicAttributesInput{
190+
_, err = snsClient.GetTopicAttributes(ctx, &sns.GetTopicAttributesInput{
188191
TopicArn: scw.StringPtr(mnq.ComposeSNSARN(region, projectID, topicName)),
189192
})
190193
if err != nil {

0 commit comments

Comments
 (0)