Skip to content

Commit 2cd313f

Browse files
authored
feat(serverless): support nats triggers (#2174)
* feat(serverless): support nats triggers * add function triggers * fix conflictsWith * empty * lint doc * add missing cassette * remove check for missing project_id
1 parent 4084a99 commit 2cd313f

10 files changed

+3035
-27
lines changed

docs/resources/container_trigger.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ For more information see [the documentation](https://www.scaleway.com/en/develop
1010

1111
## Examples
1212

13-
### Basic
13+
### SQS
1414

1515
```hcl
1616
resource scaleway_container_trigger main {
@@ -25,6 +25,21 @@ resource scaleway_container_trigger main {
2525
}
2626
```
2727

28+
### Nats
29+
30+
```hcl
31+
resource scaleway_container_trigger main {
32+
container_id = scaleway_container.main.id
33+
name = "my-trigger"
34+
nats {
35+
account_id = scaleway_mnq_nats_account.main.id
36+
subject = "MySubject"
37+
# If region is different
38+
region = scaleway_mnq_nats_account.main.region
39+
}
40+
}
41+
```
42+
2843
## Arguments Reference
2944

3045
The following arguments are supported:
@@ -38,8 +53,14 @@ The following arguments are supported:
3853
- `sqs` The configuration of the Scaleway's SQS used by the trigger
3954
- `namespace_id` (Optional) ID of the mnq namespace. Deprecated.
4055
- `queue` (Required) Name of the queue
41-
- `project_id` (Optional) ID of the project that contain the mnq namespace, defaults to provider's project
42-
- `region` (Optional) Region where the mnq namespace is, defaults to provider's region
56+
- `project_id` (Optional) ID of the project where sqs is enabled, defaults to provider's project
57+
- `region` (Optional) Region where sqs is enabled, defaults to provider's region
58+
59+
- `nats` The configuration for the Scaleway's Nats used by the trigger
60+
- `account_id` (Required) ID of the mnq nats account.
61+
- `subject` (Required) The subject to listen to
62+
- `project_id` (Optional) ID of the project that contain the mnq nats account, defaults to provider's project
63+
- `region` (Optional) Region where the mnq nats account is, defaults to provider's region
4364

4465
- `region` - (Defaults to [provider](../index.md#region) `region`). The [region](../guides/regions_and_zones.md#regions) in which the namespace should be created.
4566

docs/resources/function_trigger.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ For more information see [the documentation](https://www.scaleway.com/en/develop
1010

1111
## Examples
1212

13-
### Basic
13+
### SQS
1414

1515
```hcl
1616
resource scaleway_function_trigger main {
@@ -25,6 +25,21 @@ resource scaleway_function_trigger main {
2525
}
2626
```
2727

28+
### Nats
29+
30+
```hcl
31+
resource scaleway_function_trigger main {
32+
container_id = scaleway_container.main.id
33+
name = "my-trigger"
34+
nats {
35+
account_id = scaleway_mnq_nats_account.main.id
36+
subject = "MySubject"
37+
# If region is different
38+
region = scaleway_mnq_nats_account.main.region
39+
}
40+
}
41+
```
42+
2843
## Arguments Reference
2944

3045
The following arguments are supported:
@@ -41,6 +56,13 @@ The following arguments are supported:
4156
- `project_id` (Optional) ID of the project that contain the mnq namespace, defaults to provider's project
4257
- `region` (Optional) Region where the mnq namespace is, defaults to provider's region
4358

59+
- `nats` The configuration for the Scaleway's Nats used by the trigger
60+
- `account_id` (Required) ID of the mnq nats account.
61+
- `subject` (Required) The subject to listen to
62+
- `project_id` (Optional) ID of the project that contain the mnq nats account, defaults to provider's project
63+
- `region` (Optional) Region where the mnq nats account is, defaults to provider's region
64+
65+
4466
- `region` - (Defaults to [provider](../index.md#region) `region`). The [region](../guides/regions_and_zones.md#regions) in which the namespace should be created.
4567

4668

scaleway/helpers_container_triggers.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package scaleway
22

33
import (
44
"context"
5-
"fmt"
65
"time"
76

87
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -44,7 +43,18 @@ func expandContainerTriggerMnqSqsCreationConfig(i interface{}) *container.Create
4443
return req
4544
}
4645

47-
func completeContainerTriggerMnqSqsCreationConfig(i interface{}, d *schema.ResourceData, meta interface{}, region scw.Region) error {
46+
func expandContainerTriggerMnqNatsCreationConfig(i interface{}) *container.CreateTriggerRequestMnqNatsClientConfig {
47+
m := i.(map[string]interface{})
48+
49+
return &container.CreateTriggerRequestMnqNatsClientConfig{
50+
Subject: m["subject"].(string),
51+
MnqProjectID: m["project_id"].(string),
52+
MnqRegion: m["region"].(string),
53+
MnqNatsAccountID: expandID(m["account_id"]),
54+
}
55+
}
56+
57+
func completeContainerTriggerMnqCreationConfig(i interface{}, d *schema.ResourceData, meta interface{}, region scw.Region) error {
4858
m := i.(map[string]interface{})
4959

5060
if sqsRegion, exists := m["region"]; !exists || sqsRegion == "" {
@@ -53,10 +63,9 @@ func completeContainerTriggerMnqSqsCreationConfig(i interface{}, d *schema.Resou
5363

5464
if projectID, exists := m["project_id"]; !exists || projectID == "" {
5565
projectID, _, err := extractProjectID(d, meta.(*Meta))
56-
if err != nil {
57-
return fmt.Errorf("failed to find a valid project_id")
66+
if err == nil {
67+
m["project_id"] = projectID
5868
}
59-
m["project_id"] = projectID
6069
}
6170

6271
return nil

scaleway/helpers_function_triggers.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package scaleway
22

33
import (
4-
"fmt"
5-
64
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
75
function "github.com/scaleway/scaleway-sdk-go/api/function/v1beta1"
86
"github.com/scaleway/scaleway-sdk-go/scw"
@@ -26,7 +24,18 @@ func expandFunctionTriggerMnqSqsCreationConfig(i interface{}) *function.CreateTr
2624
return req
2725
}
2826

29-
func completeFunctionTriggerMnqSqsCreationConfig(i interface{}, d *schema.ResourceData, meta interface{}, region scw.Region) error {
27+
func expandFunctionTriggerMnqNatsCreationConfig(i interface{}) *function.CreateTriggerRequestMnqNatsClientConfig {
28+
m := i.(map[string]interface{})
29+
30+
return &function.CreateTriggerRequestMnqNatsClientConfig{
31+
Subject: expandID(m["subject"]),
32+
MnqProjectID: m["project_id"].(string),
33+
MnqRegion: m["region"].(string),
34+
MnqNatsAccountID: expandID(m["account_id"]),
35+
}
36+
}
37+
38+
func completeFunctionTriggerMnqCreationConfig(i interface{}, d *schema.ResourceData, meta interface{}, region scw.Region) error {
3039
m := i.(map[string]interface{})
3140

3241
if sqsRegion, exists := m["region"]; !exists || sqsRegion == "" {
@@ -35,10 +44,9 @@ func completeFunctionTriggerMnqSqsCreationConfig(i interface{}, d *schema.Resour
3544

3645
if projectID, exists := m["project_id"]; !exists || projectID == "" {
3746
projectID, _, err := extractProjectID(d, meta.(*Meta))
38-
if err != nil {
39-
return fmt.Errorf("failed to find a valid project_id")
47+
if err == nil {
48+
m["project_id"] = projectID
4049
}
41-
m["project_id"] = projectID
4250
}
4351

4452
return nil

scaleway/resource_container_trigger.go

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ func resourceScalewayContainerTrigger() *schema.Resource {
4646
Description: "The trigger description",
4747
},
4848
"sqs": {
49-
Type: schema.TypeList,
50-
MaxItems: 1,
51-
Description: "Config for sqs based trigger using scaleway mnq",
52-
Optional: true,
53-
ForceNew: true,
49+
Type: schema.TypeList,
50+
MaxItems: 1,
51+
Description: "Config for sqs based trigger using scaleway mnq",
52+
Optional: true,
53+
ForceNew: true,
54+
ConflictsWith: []string{"nats"},
5455
Elem: &schema.Resource{
5556
Schema: map[string]*schema.Schema{
5657
"namespace_id": {
@@ -79,6 +80,41 @@ func resourceScalewayContainerTrigger() *schema.Resource {
7980
},
8081
},
8182
},
83+
"nats": {
84+
Type: schema.TypeList,
85+
MaxItems: 1,
86+
Description: "Config for nats based trigger using scaleway mnq",
87+
Optional: true,
88+
ForceNew: true,
89+
ConflictsWith: []string{"sqs"},
90+
Elem: &schema.Resource{
91+
Schema: map[string]*schema.Schema{
92+
"account_id": {
93+
Optional: true,
94+
Type: schema.TypeString,
95+
Description: "ID of the mnq nats account",
96+
DiffSuppressFunc: diffSuppressFuncLocality,
97+
},
98+
"subject": {
99+
Required: true,
100+
Type: schema.TypeString,
101+
Description: "Subject to listen to",
102+
},
103+
"project_id": {
104+
Computed: true,
105+
Optional: true,
106+
Type: schema.TypeString,
107+
Description: "Project ID of the project where the mnq sqs exists, defaults to provider project_id",
108+
},
109+
"region": {
110+
Computed: true,
111+
Optional: true,
112+
Type: schema.TypeString,
113+
Description: "Region where the mnq sqs exists, defaults to function's region",
114+
},
115+
},
116+
},
117+
},
82118
"region": regionSchema(),
83119
},
84120
CustomizeDiff: customizeDiffLocalityCheck("container_id"),
@@ -99,7 +135,7 @@ func resourceScalewayContainerTriggerCreate(ctx context.Context, d *schema.Resou
99135
}
100136

101137
if scwSqs, isScwSqs := d.GetOk("sqs.0"); isScwSqs {
102-
err := completeContainerTriggerMnqSqsCreationConfig(scwSqs, d, meta, region)
138+
err := completeContainerTriggerMnqCreationConfig(scwSqs, d, meta, region)
103139
if err != nil {
104140
return diag.FromErr(fmt.Errorf("failed to complete sqs config: %w", err))
105141
}
@@ -108,6 +144,16 @@ func resourceScalewayContainerTriggerCreate(ctx context.Context, d *schema.Resou
108144
req.ScwSqsConfig = expandContainerTriggerMnqSqsCreationConfig(scwSqs)
109145
}
110146

147+
if scwNats, isScwNats := d.GetOk("nats.0"); isScwNats {
148+
err := completeContainerTriggerMnqCreationConfig(scwNats, d, meta, region)
149+
if err != nil {
150+
return diag.FromErr(fmt.Errorf("failed to complete nats config: %w", err))
151+
}
152+
153+
_ = d.Set("nats", []any{scwNats})
154+
req.ScwNatsConfig = expandContainerTriggerMnqNatsCreationConfig(scwNats)
155+
}
156+
111157
trigger, err := api.CreateTrigger(req, scw.WithContext(ctx))
112158
if err != nil {
113159
return diag.FromErr(err)

scaleway/resource_container_trigger_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,53 @@ func TestAccScalewayContainerTrigger_SQS_MNQv1beta1(t *testing.T) {
195195
})
196196
}
197197

198+
func TestAccScalewayContainerTrigger_Nats(t *testing.T) {
199+
tt := NewTestTools(t)
200+
defer tt.Cleanup()
201+
202+
basicConfig := `
203+
resource scaleway_container_namespace main {
204+
}
205+
206+
resource scaleway_container main {
207+
namespace_id = scaleway_container_namespace.main.id
208+
}
209+
210+
resource "scaleway_mnq_nats_account" "main" {}
211+
212+
resource scaleway_container_trigger main {
213+
container_id = scaleway_container.main.id
214+
name = "test-container-trigger-nats"
215+
nats {
216+
subject = "TestSubject"
217+
account_id = scaleway_mnq_nats_account.main.id
218+
region = scaleway_mnq_nats_account.main.region
219+
}
220+
}
221+
`
222+
223+
resource.ParallelTest(t, resource.TestCase{
224+
PreCheck: func() { testAccPreCheck(t) },
225+
ProviderFactories: tt.ProviderFactories,
226+
CheckDestroy: testAccCheckScalewayContainerTriggerDestroy(tt),
227+
Steps: []resource.TestStep{
228+
{
229+
Config: basicConfig,
230+
Check: resource.ComposeTestCheckFunc(
231+
testAccCheckScalewayContainerTriggerExists(tt, "scaleway_container_trigger.main"),
232+
testCheckResourceAttrUUID("scaleway_container_trigger.main", "id"),
233+
resource.TestCheckResourceAttr("scaleway_container_trigger.main", "name", "test-container-trigger-nats"),
234+
testAccCheckScalewayContainerTriggerStatusReady(tt, "scaleway_container_trigger.main"),
235+
),
236+
},
237+
{
238+
Config: basicConfig,
239+
PlanOnly: true,
240+
},
241+
},
242+
})
243+
}
244+
198245
func testAccCheckScalewayContainerTriggerExists(tt *TestTools, n string) resource.TestCheckFunc {
199246
return func(state *terraform.State) error {
200247
rs, ok := state.RootModule().Resources[n]

0 commit comments

Comments
 (0)