Skip to content

Commit 6354fdd

Browse files
authored
feat(tem): add tem_domain resource and data-source (#1557)
1 parent 79576bc commit 6354fdd

14 files changed

+2083
-3
lines changed

.github/workflows/acceptance-tests.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ jobs:
2525
- Rdb
2626
- Redis
2727
- Registry
28+
- Tem
2829
- VPC
2930
runs-on: ubuntu-latest
3031
steps:

.github/workflows/nightly.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ jobs:
2626
- Rdb
2727
- Redis
2828
- Registry
29+
- Tem
2930
- VPC
3031
runs-on: ubuntu-latest
3132
steps:

docs/data-sources/tem_domain.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
page_title: "Scaleway: scaleway_tem_domain"
3+
description: |-
4+
Gets information about a transactional email domain.
5+
---
6+
7+
# scaleway_tem_domain
8+
9+
Gets information about a transactional email domain.
10+
11+
## Example Usage
12+
13+
```hcl
14+
// Get info by domain name
15+
data "scaleway_tem_domain" "my_domain" {
16+
name = "example.com"
17+
}
18+
19+
// Get info by domain ID
20+
data "scaleway_tem_domain" "my_domain" {
21+
id = "11111111-1111-1111-1111-111111111111"
22+
}
23+
```
24+
25+
## Argument Reference
26+
27+
- `name` - (Optional) The domain name.
28+
Only one of `name` and `id` should be specified.
29+
30+
- `id` - (Optional) The domain id.
31+
Only one of `name` and `id` should be specified.
32+
33+
- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions) in which the domain exists.
34+
35+
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the domain is associated with.
36+
37+
## Attributes Reference
38+
39+
In addition to all above arguments, the following attributes are exported:
40+
41+
- `id` - The ID of the Transaction Email Domain.
42+
43+
- `status` - The status of the Transaction Email Domain.
44+
45+
- `created_at` - The date and time of the Transaction Email Domain's creation (RFC 3339 format).
46+
47+
- `next_check_at` - The date and time of the next scheduled check (RFC 3339 format).
48+
49+
- `last_valid_at` - The date and time the domain was last found to be valid (RFC 3339 format).
50+
51+
- `revoked_at` - The date and time of the revocation of the domain (RFC 3339 format).
52+
53+
- `last_error` - The error message if the last check failed.
54+
55+
- `spf_config` - The snippet of the SPF record that should be registered in the DNS zone.
56+
57+
- `dkim_config` - The DKIM public key, as should be recorded in the DNS zone.

docs/resources/tem_domain.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
page_title: "Scaleway: scaleway_tem_domain"
3+
description: |-
4+
Manages Scaleway Transactional Email Domains.
5+
---
6+
7+
# scaleway_tem_domain
8+
9+
Creates and manages Scaleway Transactional Email Domains.
10+
For more information see [the documentation](https://developers.scaleway.com/en/products/registry/api/).
11+
12+
## Examples
13+
14+
### Basic
15+
16+
```hcl
17+
resource "scaleway_tem_domain" "main" {
18+
name = "example.com"
19+
}
20+
```
21+
22+
## Arguments Reference
23+
24+
The following arguments are supported:
25+
26+
- `name` - (Required) The domain name, must not be used in another Transactional Email Domain.
27+
~> **Important** Updates to `name` will recreate the domain.
28+
29+
- `region` - (Defaults to [provider](../index.md#region) `region`). The [region](../guides/regions_and_zones.md#regions) in which the domain should be created.
30+
31+
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the domain is associated with.
32+
33+
## Attributes Reference
34+
35+
In addition to all above arguments, the following attributes are exported:
36+
37+
- `id` - The ID of the Transaction Email Domain.
38+
39+
- `status` - The status of the Transaction Email Domain.
40+
41+
- `created_at` - The date and time of the Transaction Email Domain's creation (RFC 3339 format).
42+
43+
- `next_check_at` - The date and time of the next scheduled check (RFC 3339 format).
44+
45+
- `last_valid_at` - The date and time the domain was last found to be valid (RFC 3339 format).
46+
47+
- `revoked_at` - The date and time of the revocation of the domain (RFC 3339 format).
48+
49+
- `last_error` - The error message if the last check failed.
50+
51+
- `spf_config` - The snippet of the SPF record that should be registered in the DNS zone.
52+
53+
- `dkim_config` - The DKIM public key, as should be recorded in the DNS zone.
54+
55+
## Import
56+
57+
Domains can be imported using the `{region}/{id}`, e.g.
58+
59+
```bash
60+
$ terraform import scaleway_tem_domain.main fr-par/11111111-1111-1111-1111-111111111111
61+
```

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313
github.com/hashicorp/terraform-plugin-log v0.7.0
1414
github.com/hashicorp/terraform-plugin-sdk/v2 v2.23.0
1515
github.com/robfig/cron/v3 v3.0.1
16-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9.0.20221004145052-e9075dc74aef
16+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9.0.20221028091551-ad6d8eb6e1ee
1717
github.com/stretchr/testify v1.8.0
1818
)
1919

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:
230230
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
231231
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
232232
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
233-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9.0.20221004145052-e9075dc74aef h1:3/QVdaqb5VivKvo3L1KmdFiqoQkvGMnvgmDjHzLTXo8=
234-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9.0.20221004145052-e9075dc74aef/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
233+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9.0.20221028091551-ad6d8eb6e1ee h1:pyJiFC0/YpS0EFVKF3LnYwDWbYh5n5480bDQ8QBnmds=
234+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9.0.20221028091551-ad6d8eb6e1ee/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
235235
github.com/sebdah/goldie v1.0.0/go.mod h1:jXP4hmWywNEwZzhMuv2ccnqTSFpuq8iyQhtQdkkZBH4=
236236
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
237237
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=

scaleway/data_source_tem_domain.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package scaleway
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
tem "github.com/scaleway/scaleway-sdk-go/api/tem/v1alpha1"
10+
"github.com/scaleway/scaleway-sdk-go/scw"
11+
)
12+
13+
func dataSourceScalewayTemDomain() *schema.Resource {
14+
// Generate datasource schema from resource
15+
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayTemDomain().Schema)
16+
17+
// Set 'Optional' schema elements
18+
addOptionalFieldsToSchema(dsSchema, "name", "region")
19+
20+
dsSchema["name"].ConflictsWith = []string{"domain_id"}
21+
dsSchema["domain_id"] = &schema.Schema{
22+
Type: schema.TypeString,
23+
Optional: true,
24+
Description: "The ID of the tem domain",
25+
ValidateFunc: validationUUIDorUUIDWithLocality(),
26+
ConflictsWith: []string{"name"},
27+
}
28+
29+
return &schema.Resource{
30+
ReadContext: dataSourceScalewayTemDomainRead,
31+
32+
Schema: dsSchema,
33+
}
34+
}
35+
36+
func dataSourceScalewayTemDomainRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
37+
api, region, err := temAPIWithRegion(d, meta)
38+
if err != nil {
39+
return diag.FromErr(err)
40+
}
41+
42+
domainID, ok := d.GetOk("domain_id")
43+
if !ok {
44+
res, err := api.ListDomains(&tem.ListDomainsRequest{
45+
Region: region,
46+
Name: expandStringPtr(d.Get("name")),
47+
ProjectID: expandStringPtr(d.Get("project_id")),
48+
}, scw.WithContext(ctx))
49+
if err != nil {
50+
return diag.FromErr(err)
51+
}
52+
53+
for _, domain := range res.Domains {
54+
if domain.Status == tem.DomainStatusRevoked {
55+
continue
56+
}
57+
58+
if domain.Name == d.Get("name").(string) {
59+
if domainID != "" {
60+
return diag.FromErr(fmt.Errorf("more than 1 server found with the same name %s", d.Get("name")))
61+
}
62+
63+
domainID = domain.ID
64+
}
65+
}
66+
67+
if domainID == "" {
68+
return diag.FromErr(fmt.Errorf("no domain found with the name %s", d.Get("name")))
69+
}
70+
}
71+
72+
regionalID := datasourceNewRegionalizedID(domainID, region)
73+
d.SetId(regionalID)
74+
err = d.Set("domain_id", regionalID)
75+
if err != nil {
76+
return diag.FromErr(err)
77+
}
78+
79+
diags := resourceScalewayTemDomainRead(ctx, d, meta)
80+
if diags != nil {
81+
return append(diags, diag.Errorf("failed to read tem domain state")...)
82+
}
83+
84+
if d.Id() == "" {
85+
return diag.Errorf("tem domain (%s) not found", regionalID)
86+
}
87+
88+
return nil
89+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package scaleway
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
)
9+
10+
func TestAccScalewayDataSourceTemDomain_Basic(t *testing.T) {
11+
tt := NewTestTools(t)
12+
defer tt.Cleanup()
13+
14+
domainName := "terraform-ds.test.local"
15+
16+
resource.ParallelTest(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
ProviderFactories: tt.ProviderFactories,
19+
CheckDestroy: testAccCheckScalewayTemDomainDestroy(tt),
20+
Steps: []resource.TestStep{
21+
{
22+
Config: fmt.Sprintf(`
23+
resource "scaleway_tem_domain" "main" {
24+
name = "%s"
25+
}
26+
27+
data "scaleway_tem_domain" "prod" {
28+
name = "${scaleway_tem_domain.main.name}"
29+
}
30+
31+
data "scaleway_tem_domain" "stg" {
32+
domain_id = "${scaleway_tem_domain.main.id}"
33+
}
34+
`, domainName),
35+
Check: resource.ComposeTestCheckFunc(
36+
testAccCheckScalewayTemDomainExists(tt, "data.scaleway_tem_domain.prod"),
37+
resource.TestCheckResourceAttr("data.scaleway_tem_domain.prod", "name", domainName),
38+
39+
testAccCheckScalewayTemDomainExists(tt, "data.scaleway_tem_domain.stg"),
40+
resource.TestCheckResourceAttr("data.scaleway_tem_domain.stg", "name", domainName),
41+
),
42+
},
43+
},
44+
})
45+
}

scaleway/helpers_tem.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package scaleway
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
tem "github.com/scaleway/scaleway-sdk-go/api/tem/v1alpha1"
9+
"github.com/scaleway/scaleway-sdk-go/scw"
10+
)
11+
12+
const (
13+
defaultTemDomainTimeout = 5 * time.Minute
14+
defaultTemDomainRetryInterval = 15 * time.Second
15+
)
16+
17+
// temAPIWithRegion returns a new Tem API and the region for a Create request
18+
func temAPIWithRegion(d *schema.ResourceData, m interface{}) (*tem.API, scw.Region, error) {
19+
meta := m.(*Meta)
20+
api := tem.NewAPI(meta.scwClient)
21+
22+
region, err := extractRegion(d, meta)
23+
if err != nil {
24+
return nil, "", err
25+
}
26+
return api, region, nil
27+
}
28+
29+
// temAPIWithRegionAndID returns a Tem API with zone and ID extracted from the state
30+
func temAPIWithRegionAndID(m interface{}, id string) (*tem.API, scw.Region, string, error) {
31+
meta := m.(*Meta)
32+
api := tem.NewAPI(meta.scwClient)
33+
34+
region, id, err := parseRegionalID(id)
35+
if err != nil {
36+
return nil, "", "", err
37+
}
38+
return api, region, id, nil
39+
}
40+
41+
func waitForTemDomain(ctx context.Context, api *tem.API, region scw.Region, id string, timeout time.Duration) (*tem.Domain, error) {
42+
retryInterval := defaultTemDomainRetryInterval
43+
if DefaultWaitRetryInterval != nil {
44+
retryInterval = *DefaultWaitRetryInterval
45+
}
46+
47+
domain, err := api.WaitForDomain(&tem.WaitForDomainRequest{
48+
Region: region,
49+
DomainID: id,
50+
RetryInterval: &retryInterval,
51+
Timeout: scw.TimeDurationPtr(timeout),
52+
}, scw.WithContext(ctx))
53+
54+
return domain, err
55+
}

scaleway/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ func Provider(config *ProviderConfig) plugin.ProviderFunc {
133133
"scaleway_lb_frontend": resourceScalewayLbFrontend(),
134134
"scaleway_lb_route": resourceScalewayLbRoute(),
135135
"scaleway_registry_namespace": resourceScalewayRegistryNamespace(),
136+
"scaleway_tem_domain": resourceScalewayTemDomain(),
136137
"scaleway_container": resourceScalewayContainer(),
137138
"scaleway_container_token": resourceScalewayContainerToken(),
138139
"scaleway_rdb_acl": resourceScalewayRdbACL(),
@@ -191,6 +192,7 @@ func Provider(config *ProviderConfig) plugin.ProviderFunc {
191192
"scaleway_rdb_privilege": dataSourceScalewayRDBPrivilege(),
192193
"scaleway_redis_cluster": dataSourceScalewayRedisCluster(),
193194
"scaleway_registry_namespace": dataSourceScalewayRegistryNamespace(),
195+
"scaleway_tem_domain": dataSourceScalewayTemDomain(),
194196
"scaleway_registry_image": dataSourceScalewayRegistryImage(),
195197
"scaleway_vpc_public_gateway": dataSourceScalewayVPCPublicGateway(),
196198
"scaleway_vpc_gateway_network": dataSourceScalewayVPCGatewayNetwork(),

0 commit comments

Comments
 (0)