Skip to content

Commit

Permalink
feat(serverless): support nats triggers (#2174)
Browse files Browse the repository at this point in the history
* feat(serverless): support nats triggers

* add function triggers

* fix conflictsWith

* empty

* lint doc

* add missing cassette

* remove check for missing project_id
  • Loading branch information
Codelax authored Oct 19, 2023
1 parent 4084a99 commit 2cd313f
Show file tree
Hide file tree
Showing 10 changed files with 3,035 additions and 27 deletions.
27 changes: 24 additions & 3 deletions docs/resources/container_trigger.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ For more information see [the documentation](https://www.scaleway.com/en/develop

## Examples

### Basic
### SQS

```hcl
resource scaleway_container_trigger main {
Expand All @@ -25,6 +25,21 @@ resource scaleway_container_trigger main {
}
```

### Nats

```hcl
resource scaleway_container_trigger main {
container_id = scaleway_container.main.id
name = "my-trigger"
nats {
account_id = scaleway_mnq_nats_account.main.id
subject = "MySubject"
# If region is different
region = scaleway_mnq_nats_account.main.region
}
}
```

## Arguments Reference

The following arguments are supported:
Expand All @@ -38,8 +53,14 @@ The following arguments are supported:
- `sqs` The configuration of the Scaleway's SQS used by the trigger
- `namespace_id` (Optional) ID of the mnq namespace. Deprecated.
- `queue` (Required) Name of the queue
- `project_id` (Optional) ID of the project that contain the mnq namespace, defaults to provider's project
- `region` (Optional) Region where the mnq namespace is, defaults to provider's region
- `project_id` (Optional) ID of the project where sqs is enabled, defaults to provider's project
- `region` (Optional) Region where sqs is enabled, defaults to provider's region

- `nats` The configuration for the Scaleway's Nats used by the trigger
- `account_id` (Required) ID of the mnq nats account.
- `subject` (Required) The subject to listen to
- `project_id` (Optional) ID of the project that contain the mnq nats account, defaults to provider's project
- `region` (Optional) Region where the mnq nats account is, defaults to provider's region

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

Expand Down
24 changes: 23 additions & 1 deletion docs/resources/function_trigger.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ For more information see [the documentation](https://www.scaleway.com/en/develop

## Examples

### Basic
### SQS

```hcl
resource scaleway_function_trigger main {
Expand All @@ -25,6 +25,21 @@ resource scaleway_function_trigger main {
}
```

### Nats

```hcl
resource scaleway_function_trigger main {
container_id = scaleway_container.main.id
name = "my-trigger"
nats {
account_id = scaleway_mnq_nats_account.main.id
subject = "MySubject"
# If region is different
region = scaleway_mnq_nats_account.main.region
}
}
```

## Arguments Reference

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

- `nats` The configuration for the Scaleway's Nats used by the trigger
- `account_id` (Required) ID of the mnq nats account.
- `subject` (Required) The subject to listen to
- `project_id` (Optional) ID of the project that contain the mnq nats account, defaults to provider's project
- `region` (Optional) Region where the mnq nats account is, defaults to provider's region


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


Expand Down
19 changes: 14 additions & 5 deletions scaleway/helpers_container_triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package scaleway

import (
"context"
"fmt"
"time"

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

func completeContainerTriggerMnqSqsCreationConfig(i interface{}, d *schema.ResourceData, meta interface{}, region scw.Region) error {
func expandContainerTriggerMnqNatsCreationConfig(i interface{}) *container.CreateTriggerRequestMnqNatsClientConfig {
m := i.(map[string]interface{})

return &container.CreateTriggerRequestMnqNatsClientConfig{
Subject: m["subject"].(string),
MnqProjectID: m["project_id"].(string),
MnqRegion: m["region"].(string),
MnqNatsAccountID: expandID(m["account_id"]),
}
}

func completeContainerTriggerMnqCreationConfig(i interface{}, d *schema.ResourceData, meta interface{}, region scw.Region) error {
m := i.(map[string]interface{})

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

if projectID, exists := m["project_id"]; !exists || projectID == "" {
projectID, _, err := extractProjectID(d, meta.(*Meta))
if err != nil {
return fmt.Errorf("failed to find a valid project_id")
if err == nil {
m["project_id"] = projectID
}
m["project_id"] = projectID
}

return nil
Expand Down
20 changes: 14 additions & 6 deletions scaleway/helpers_function_triggers.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package scaleway

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
function "github.com/scaleway/scaleway-sdk-go/api/function/v1beta1"
"github.com/scaleway/scaleway-sdk-go/scw"
Expand All @@ -26,7 +24,18 @@ func expandFunctionTriggerMnqSqsCreationConfig(i interface{}) *function.CreateTr
return req
}

func completeFunctionTriggerMnqSqsCreationConfig(i interface{}, d *schema.ResourceData, meta interface{}, region scw.Region) error {
func expandFunctionTriggerMnqNatsCreationConfig(i interface{}) *function.CreateTriggerRequestMnqNatsClientConfig {
m := i.(map[string]interface{})

return &function.CreateTriggerRequestMnqNatsClientConfig{
Subject: expandID(m["subject"]),
MnqProjectID: m["project_id"].(string),
MnqRegion: m["region"].(string),
MnqNatsAccountID: expandID(m["account_id"]),
}
}

func completeFunctionTriggerMnqCreationConfig(i interface{}, d *schema.ResourceData, meta interface{}, region scw.Region) error {
m := i.(map[string]interface{})

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

if projectID, exists := m["project_id"]; !exists || projectID == "" {
projectID, _, err := extractProjectID(d, meta.(*Meta))
if err != nil {
return fmt.Errorf("failed to find a valid project_id")
if err == nil {
m["project_id"] = projectID
}
m["project_id"] = projectID
}

return nil
Expand Down
58 changes: 52 additions & 6 deletions scaleway/resource_container_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ func resourceScalewayContainerTrigger() *schema.Resource {
Description: "The trigger description",
},
"sqs": {
Type: schema.TypeList,
MaxItems: 1,
Description: "Config for sqs based trigger using scaleway mnq",
Optional: true,
ForceNew: true,
Type: schema.TypeList,
MaxItems: 1,
Description: "Config for sqs based trigger using scaleway mnq",
Optional: true,
ForceNew: true,
ConflictsWith: []string{"nats"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"namespace_id": {
Expand Down Expand Up @@ -79,6 +80,41 @@ func resourceScalewayContainerTrigger() *schema.Resource {
},
},
},
"nats": {
Type: schema.TypeList,
MaxItems: 1,
Description: "Config for nats based trigger using scaleway mnq",
Optional: true,
ForceNew: true,
ConflictsWith: []string{"sqs"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"account_id": {
Optional: true,
Type: schema.TypeString,
Description: "ID of the mnq nats account",
DiffSuppressFunc: diffSuppressFuncLocality,
},
"subject": {
Required: true,
Type: schema.TypeString,
Description: "Subject to listen to",
},
"project_id": {
Computed: true,
Optional: true,
Type: schema.TypeString,
Description: "Project ID of the project where the mnq sqs exists, defaults to provider project_id",
},
"region": {
Computed: true,
Optional: true,
Type: schema.TypeString,
Description: "Region where the mnq sqs exists, defaults to function's region",
},
},
},
},
"region": regionSchema(),
},
CustomizeDiff: customizeDiffLocalityCheck("container_id"),
Expand All @@ -99,7 +135,7 @@ func resourceScalewayContainerTriggerCreate(ctx context.Context, d *schema.Resou
}

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

if scwNats, isScwNats := d.GetOk("nats.0"); isScwNats {
err := completeContainerTriggerMnqCreationConfig(scwNats, d, meta, region)
if err != nil {
return diag.FromErr(fmt.Errorf("failed to complete nats config: %w", err))
}

_ = d.Set("nats", []any{scwNats})
req.ScwNatsConfig = expandContainerTriggerMnqNatsCreationConfig(scwNats)
}

trigger, err := api.CreateTrigger(req, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
Expand Down
47 changes: 47 additions & 0 deletions scaleway/resource_container_trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,53 @@ func TestAccScalewayContainerTrigger_SQS_MNQv1beta1(t *testing.T) {
})
}

func TestAccScalewayContainerTrigger_Nats(t *testing.T) {
tt := NewTestTools(t)
defer tt.Cleanup()

basicConfig := `
resource scaleway_container_namespace main {
}
resource scaleway_container main {
namespace_id = scaleway_container_namespace.main.id
}
resource "scaleway_mnq_nats_account" "main" {}
resource scaleway_container_trigger main {
container_id = scaleway_container.main.id
name = "test-container-trigger-nats"
nats {
subject = "TestSubject"
account_id = scaleway_mnq_nats_account.main.id
region = scaleway_mnq_nats_account.main.region
}
}
`

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: tt.ProviderFactories,
CheckDestroy: testAccCheckScalewayContainerTriggerDestroy(tt),
Steps: []resource.TestStep{
{
Config: basicConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayContainerTriggerExists(tt, "scaleway_container_trigger.main"),
testCheckResourceAttrUUID("scaleway_container_trigger.main", "id"),
resource.TestCheckResourceAttr("scaleway_container_trigger.main", "name", "test-container-trigger-nats"),
testAccCheckScalewayContainerTriggerStatusReady(tt, "scaleway_container_trigger.main"),
),
},
{
Config: basicConfig,
PlanOnly: true,
},
},
})
}

func testAccCheckScalewayContainerTriggerExists(tt *TestTools, n string) resource.TestCheckFunc {
return func(state *terraform.State) error {
rs, ok := state.RootModule().Resources[n]
Expand Down
Loading

0 comments on commit 2cd313f

Please sign in to comment.