Skip to content

Commit 60a69be

Browse files
authored
feat(mnq): queue resource (#1903)
* feat(mnq): resource queue feat(mnq): add read and update queue sqs feat(mnq): sqs queues fix linter issues feat: add nats refactor: underscore unused function parameters docs: add documentation remove unused cassette remove code from another MR fix: ignore `time.Sleep` linter issue docs(mnq): fix sqs example namespace protocol fix: remove double error feat(mnq): set sqs specific url fix(mnq): if name_prefix is not set then it will never be feat(cockpit): don't require token scopes (#1905) feat(mnq): generate queue name feat(mnq): use NATS jetstream and improve tests refactor: replace `retryWhenAWSErrCodeEquals` by `retryOnAWSCode` * fix: typo * refactor: use fifo constant * fix: re-add deleted helpers * don't worry * compare raw bodies in matcher * fix linter issues * compare form bodies * fifo queue for nats * rename client * add custom endpoint attribute * forgot these * update documentation * fifo queue is sqs only (nats is always fifo) * document limitations * use classic list * fix logs * add descriptions * fix doc * use `logging.IsDebugOrHigher()` * add nats credentials in example queue
1 parent 931bfcd commit 60a69be

18 files changed

+4537
-63
lines changed

docs/resources/mnq_queue.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
subcategory: "Messaging and Queuing"
3+
page_title: "Scaleway: scaleway_mnq_queue"
4+
---
5+
6+
# scaleway_mnq_queue
7+
8+
Creates and manages Scaleway Messaging and Queuing queues.
9+
10+
For more information about MNQ, see [the documentation](https://www.scaleway.com/en/developers/api/messaging-and-queuing/).
11+
12+
## Examples
13+
14+
### NATS
15+
16+
```hcl
17+
resource "scaleway_mnq_namespace" "main" {
18+
protocol = "nats"
19+
}
20+
21+
resource "scaleway_mnq_credential" "main" {
22+
namespace_id = scaleway_mnq_namespace.main.id
23+
}
24+
25+
resource "scaleway_mnq_queue" "my_queue" {
26+
namespace_id = scaleway_mnq_namespace.main.id
27+
name = "my-queue"
28+
29+
nats {
30+
credentials = scaleway_mnq_credential.main.nats_credentials[0].content
31+
}
32+
}
33+
```
34+
35+
### SQS
36+
37+
```hcl
38+
resource "scaleway_mnq_namespace" "main" {
39+
protocol = "sqs_sns"
40+
}
41+
42+
resource "scaleway_mnq_credential" "main" {
43+
namespace_id = scaleway_mnq_namespace.main.id
44+
45+
sqs_sns_credentials {
46+
permissions {
47+
can_publish = true
48+
can_receive = true
49+
can_manage = true
50+
}
51+
}
52+
}
53+
54+
resource "scaleway_mnq_queue" "my_queue" {
55+
namespace_id = scaleway_mnq_namespace.main.id
56+
name = "my-queue"
57+
58+
sqs {
59+
access_key = scaleway_mnq_credential.main.sqs_sns_credentials.0.access_key
60+
secret_key = scaleway_mnq_credential.main.sqs_sns_credentials.0.secret_key
61+
}
62+
}
63+
```
64+
65+
### Argument Reference
66+
67+
The following arguments are supported:
68+
69+
* `namespace_id` - (Required) The ID of the Namespace associated to.
70+
71+
* `name` - (Optional) The name of the queue. Either `name` or `name_prefix` is required. Conflicts with `name_prefix`.
72+
73+
* `name_prefix` - (Optional) Creates a unique name beginning with the specified prefix. Conflicts with `name`.
74+
75+
* `message_max_age` - (Optional) The number of seconds the queue retains a message. Must be between 60 and 1_209_600. Defaults to 345_600.
76+
77+
* `message_max_size` - (Optional) The maximum size of a message. Should be in bytes. Must be between 1024 and 262_144. Defaults to 262_144.
78+
79+
* `sqs` - (Optional) The SQS attributes of the queue. Conflicts with `nats`.
80+
- `endpoint` - (Optional) The endpoint of the SQS queue. Can contain a {region} placeholder. Defaults to `http://sqs-sns.mnq.{region}.scw.cloud`.
81+
- `access_key` - (Required) The access key of the SQS queue.
82+
- `secret_key` - (Required) The secret key of the SQS queue.
83+
- `fifo_queue` - (Optional) Whether the queue is a FIFO queue. If true, the queue name must end with .fifo. Defaults to `false`.
84+
- `content_based_deduplication` - (Optional) Specifies whether to enable content-based deduplication. Defaults to `false`.
85+
- `receive_wait_time_seconds` - (Optional) The number of seconds to wait for a message to arrive in the queue before returning. Must be between 0 and 20. Defaults to 0.
86+
- `visibility_timeout_seconds` - (Optional) The number of seconds a message is hidden from other consumers. Must be between 0 and 43_200. Defaults to 30.
87+
- For more information about the SQS limitations, see [the documentation](https://www.scaleway.com/en/developers/api/messaging-and-queuing/#technical-limitations).
88+
89+
* `nats` - (Optional) The NATS attributes of the queue. Conflicts with `sqs`.
90+
- `endpoint` - (Optional) The endpoint of the NATS queue. Can contain a {region} placeholder. Defaults to `nats://nats.mnq.{region}.scw.cloud:4222`.
91+
- `credentials` - (Required) Line jump separated key and seed.
92+
- `retention_policy` - (Optional) The retention policy of the queue. See https://docs.nats.io/nats-concepts/jetstream/streams#retentionpolicy for more information. Defaults to `workqueue`.
93+
94+
95+
### Attribute Reference
96+
97+
In addition to all arguments above, the following attributes are exported:
98+
99+
* `sqs` - The SQS attributes of the queue.
100+
~ `url` - The URL of the queue.
101+
102+
### Import
103+
104+
Queues can be imported using the `{region}/{namespace-id}/{queue-name}` format:
105+
106+
```shell
107+
$ terraform import scaleway_mnq_queue.my_queue fr-par/11111111111111111111111111111111/my-queue
108+
```

go.mod

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/hashicorp/go-retryablehttp v0.7.2
1414
github.com/hashicorp/terraform-plugin-log v0.8.0
1515
github.com/hashicorp/terraform-plugin-sdk/v2 v2.26.1
16+
github.com/nats-io/nats.go v1.25.0
1617
github.com/robfig/cron/v3 v3.0.1
1718
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.16.0.20230425114903-9de7ce5b674f
1819
github.com/stretchr/testify v1.8.2
@@ -57,6 +58,9 @@ require (
5758
github.com/mitchellh/reflectwalk v1.0.2 // indirect
5859
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
5960
github.com/morikuni/aec v1.0.0 // indirect
61+
github.com/nats-io/nats-server/v2 v2.9.16 // indirect
62+
github.com/nats-io/nkeys v0.4.4 // indirect
63+
github.com/nats-io/nuid v1.0.1 // indirect
6064
github.com/oklog/run v1.1.0 // indirect
6165
github.com/opencontainers/go-digest v1.0.0 // indirect
6266
github.com/opencontainers/image-spec v1.0.2 // indirect
@@ -66,12 +70,11 @@ require (
6670
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
6771
github.com/vmihailenco/tagparser v0.1.2 // indirect
6872
github.com/zclconf/go-cty v1.13.1 // indirect
69-
golang.org/x/crypto v0.7.0 // indirect
73+
golang.org/x/crypto v0.8.0 // indirect
7074
golang.org/x/mod v0.8.0 // indirect
71-
golang.org/x/net v0.8.0 // indirect
72-
golang.org/x/sys v0.6.0 // indirect
73-
golang.org/x/text v0.8.0 // indirect
74-
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
75+
golang.org/x/net v0.9.0 // indirect
76+
golang.org/x/sys v0.7.0 // indirect
77+
golang.org/x/text v0.9.0 // indirect
7578
google.golang.org/appengine v1.6.7 // indirect
7679
google.golang.org/genproto v0.0.0-20220413183235-5e96e2839df9 // indirect
7780
google.golang.org/grpc v1.51.0 // indirect

go.sum

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 h1:DowS9hvgy
182182
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
183183
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
184184
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
185+
github.com/klauspost/compress v1.16.4 h1:91KN02FnsOYhuunwU4ssRe8lc2JosWmizWa91B5v1PU=
185186
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
186187
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
187188
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
@@ -203,6 +204,7 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
203204
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
204205
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
205206
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
207+
github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA/g=
206208
github.com/mitchellh/cli v1.1.5/go.mod h1:v8+iFts2sPIKUV1ltktPXMCC8fumSKFItNcD2cLtRR4=
207209
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
208210
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
@@ -223,6 +225,15 @@ github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6/go.mod h1:E2VnQOmVuvZB6U
223225
github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8=
224226
github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
225227
github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
228+
github.com/nats-io/jwt/v2 v2.4.1 h1:Y35W1dgbbz2SQUYDPCaclXcuqleVmpbRa7646Jf2EX4=
229+
github.com/nats-io/nats-server/v2 v2.9.16 h1:SuNe6AyCcVy0g5326wtyU8TdqYmcPqzTjhkHojAjprc=
230+
github.com/nats-io/nats-server/v2 v2.9.16/go.mod h1:z1cc5Q+kqJkz9mLUdlcSsdYnId4pyImHjNgoh6zxSC0=
231+
github.com/nats-io/nats.go v1.25.0 h1:t5/wCPGciR7X3Mu8QOi4jiJaXaWM8qtkLu4lzGZvYHE=
232+
github.com/nats-io/nats.go v1.25.0/go.mod h1:D2WALIhz7V8M0pH8Scx8JZXlg6Oqz5VG+nQkK8nJdvg=
233+
github.com/nats-io/nkeys v0.4.4 h1:xvBJ8d69TznjcQl9t6//Q5xXuVhyYiSos6RPtvQNTwA=
234+
github.com/nats-io/nkeys v0.4.4/go.mod h1:XUkxdLPTufzlihbamfzQ7mw/VGx6ObUs+0bN5sNvt64=
235+
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
236+
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
226237
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
227238
github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA=
228239
github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU=
@@ -290,8 +301,8 @@ golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm
290301
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
291302
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
292303
golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU=
293-
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
294-
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
304+
golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ=
305+
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
295306
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
296307
golang.org/x/exp v0.0.0-20230118134722-a68e582fa157 h1:fiNkyhJPUvxbRPbCqY/D9qdjmPzfHcpK3P4bM4gioSY=
297308
golang.org/x/exp v0.0.0-20230118134722-a68e582fa157/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
@@ -326,8 +337,8 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b
326337
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
327338
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
328339
golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws=
329-
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
330-
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
340+
golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM=
341+
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
331342
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
332343
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
333344
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -367,24 +378,23 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
367378
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
368379
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
369380
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
370-
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
371-
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
381+
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
382+
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
372383
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
373384
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
374385
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
375386
golang.org/x/term v0.4.0/go.mod h1:9P2UbLfCdcvo3p/nzKvsmas4TnlujnuoV9hGgYzW1lQ=
376-
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
387+
golang.org/x/term v0.7.0 h1:BEvjmm5fURWqcfbSKTdpkDXYBrUS1c0m8agp14W48vQ=
377388
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
378389
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
379390
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
380391
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
381392
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
382393
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
383394
golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
384-
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
385-
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
386-
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac h1:7zkz7BUtwNFFqcowJ+RIgu2MaV/MapERkDIy+mwPyjs=
387-
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
395+
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
396+
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
397+
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
388398
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
389399
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
390400
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=

scaleway/errors_object.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,32 @@ const (
2020
ErrCodeNoSuchWebsiteConfiguration = "NoSuchWebsiteConfiguration"
2121
// ErrCodeObjectLockConfigurationNotFoundError object lock configuration not found
2222
ErrCodeObjectLockConfigurationNotFoundError = "ObjectLockConfigurationNotFoundError"
23+
// ErrCodeAuthorizationError authorization error
24+
ErrCodeAuthorizationError = "AuthorizationError"
25+
// ErrCodeInternalException internal exception
26+
ErrCodeInternalException = "InternalException"
27+
// ErrCodeInternalServiceError internal exception error
28+
ErrCodeInternalServiceError = "InternalServiceError"
29+
// ErrCodeInvalidAction invalid action
30+
ErrCodeInvalidAction = "InvalidAction"
31+
// ErrCodeInvalidParameterException invalid parameter exception
32+
ErrCodeInvalidParameterException = "InvalidParameterException"
33+
// ErrCodeInvalidParameterValue invalid parameter value
34+
ErrCodeInvalidParameterValue = "InvalidParameterValue"
35+
// ErrCodeInvalidRequest invalid request
36+
ErrCodeInvalidRequest = "InvalidRequest"
37+
// ErrCodeOperationDisabledException operation disabled exception
38+
ErrCodeOperationDisabledException = "OperationDisabledException"
39+
// ErrCodeOperationNotPermitted operation not permitted
40+
ErrCodeOperationNotPermitted = "OperationNotPermitted"
41+
// ErrCodeUnknownOperationException unknown operation exception
42+
ErrCodeUnknownOperationException = "UnknownOperationException"
43+
// ErrCodeUnsupportedFeatureException = unsupported Feature exception
44+
ErrCodeUnsupportedFeatureException = "UnsupportedFeatureException"
45+
// ErrCodeUnsupportedOperation unsupported operation
46+
ErrCodeUnsupportedOperation = "UnsupportedOperation"
47+
// ErrCodeValidationError validation error
48+
ErrCodeValidationError = "ValidationError"
49+
// ErrCodeValidationException validation exception
50+
ErrCodeValidationException = "ValidationException"
2351
)

scaleway/helpers.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"testing"
1313
"time"
1414

15+
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
1516
"github.com/hashicorp/go-cty/cty"
1617
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1718
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -1006,3 +1007,57 @@ func NotFound(err error) bool {
10061007
var e *retry.NotFoundError // nosemgrep:ci.is-not-found-error
10071008
return errors.As(err, &e)
10081009
}
1010+
1011+
type RetryWhenConfig[T any] struct {
1012+
Timeout time.Duration
1013+
Interval time.Duration
1014+
Function func() (T, error)
1015+
}
1016+
1017+
var ErrRetryWhenTimeout = errors.New("timeout reached")
1018+
1019+
// retryWhen executes the function passed in the configuration object until the timeout is reached or the context is cancelled.
1020+
// It will retry if the shouldRetry function returns true. It will stop if the shouldRetry function returns false.
1021+
func retryWhen[T any](ctx context.Context, config *RetryWhenConfig[T], shouldRetry func(error) bool) (T, error) { //nolint: ireturn
1022+
retryInterval := config.Interval
1023+
if DefaultWaitRetryInterval != nil {
1024+
retryInterval = *DefaultWaitRetryInterval
1025+
}
1026+
1027+
timer := time.NewTimer(config.Timeout)
1028+
1029+
for {
1030+
result, err := config.Function()
1031+
if shouldRetry(err) {
1032+
select {
1033+
case <-timer.C:
1034+
return result, ErrRetryWhenTimeout
1035+
case <-ctx.Done():
1036+
return result, ctx.Err()
1037+
default:
1038+
time.Sleep(retryInterval) // lintignore:R018
1039+
continue
1040+
}
1041+
}
1042+
1043+
return result, err
1044+
}
1045+
}
1046+
1047+
// retryWhenAWSErrCodeEquals retries a function when it returns a specific AWS error
1048+
func retryWhenAWSErrCodeEquals[T any](ctx context.Context, codes []string, config *RetryWhenConfig[T]) (T, error) { //nolint: ireturn
1049+
return retryWhen(ctx, config, func(err error) bool {
1050+
return tfawserr.ErrCodeEquals(err, codes...)
1051+
})
1052+
}
1053+
1054+
// retryWhenAWSErrCodeNotEquals retries a function until it returns a specific AWS error
1055+
func retryWhenAWSErrCodeNotEquals[T any](ctx context.Context, codes []string, config *RetryWhenConfig[T]) (T, error) { //nolint: ireturn
1056+
return retryWhen(ctx, config, func(err error) bool {
1057+
if err == nil {
1058+
return true
1059+
}
1060+
1061+
return !tfawserr.ErrCodeEquals(err, codes...)
1062+
})
1063+
}

0 commit comments

Comments
 (0)