Skip to content

Commit 9bb51ae

Browse files
feat(domain-zone): Add resource and datasource for domain zone (#1027)
1 parent 832f926 commit 9bb51ae

13 files changed

+981
-3
lines changed

.github/workflows/acceptance-tests.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
- Account
1313
- AppleSilicon
1414
- Baremetal
15-
- DomainRecord
15+
- Domain
1616
- Instance
1717
- Iot
1818
- K8S

.github/workflows/nightly.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
- Account
1515
- AppleSilicon
1616
- Baremetal
17-
- DomainRecord
17+
- Domain
1818
- Instance
1919
- Iot
2020
- K8S

docs/data-sources/domain_zone.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
page_title: "Scaleway: scaleway_domain_zone"
3+
description: |-
4+
Gets information about a domain zone.
5+
---
6+
7+
# scaleway_domain_zone
8+
9+
Gets information about a domain zone.
10+
11+
## Example Usage
12+
13+
```hcl
14+
# Get zone
15+
data "scaleway_domain_zone" "main" {
16+
domain = "scaleway-terraform.com"
17+
subdomain = "test"
18+
}
19+
```
20+
21+
## Argument Reference
22+
23+
The following arguments are supported:
24+
25+
- `domain` - (Required) The domain where the DNS zone will be created.
26+
27+
- `subdomain` - (Required) The subdomain(zone name) to create in the domain.
28+
29+
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the domain is associated with.
30+
31+
## Attributes Reference
32+
33+
In addition to all arguments above, the following attributes are exported:
34+
35+
- `ns` - NameServer list for zone.
36+
37+
- `ns_default` - NameServer default list for zone.
38+
39+
- `ns_master` - NameServer master list for zone.
40+
41+
- `status` - The domain zone status.
42+
43+
- `message` - Message
44+
45+
- `updated_at` - The date and time of the last update of the DNS zone.

docs/resources/domain_zone.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
page_title: "Scaleway: scaleway_domain_zone"
3+
description: |-
4+
Manages Scaleway Domain zones.
5+
---
6+
7+
# scaleway_domain_zone
8+
9+
Creates and manages Scaleway Domain zone.
10+
For more information, see [the documentation](https://www.scaleway.com/en/docs/scaleway-dns/).
11+
12+
## Examples
13+
14+
15+
```hcl
16+
resource "scaleway_domain_zone" "test" {
17+
domain = "scaleway-terraform.com"
18+
subdomain = "test"
19+
}
20+
```
21+
22+
## Arguments Reference
23+
24+
The following arguments are supported:
25+
26+
- `domain` - (Required) The domain where the DNS zone will be created.
27+
28+
- `subdomain` - (Required) The subdomain(zone name) to create in the domain.
29+
30+
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the domain is associated with.
31+
32+
33+
## Attributes Reference
34+
35+
In addition to all arguments above, the following attributes are exported:
36+
37+
- `ns` - NameServer list for zone.
38+
39+
- `ns_default` - NameServer default list for zone.
40+
41+
- `ns_master` - NameServer master list for zone.
42+
43+
- `status` - The domain zone status.
44+
45+
- `message` - Message
46+
47+
- `updated_at` - The date and time of the last update of the DNS zone.
48+
49+
## Import
50+
51+
Zone can be imported using the `{subdomain}.{domain}`, e.g.
52+
53+
```bash
54+
$ terraform import scaleway_domain_zone.test test.scaleway-terraform.com
55+
```

docs/resources/instance_private_nic.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ In addition to all above arguments, the following attributes are exported:
3737
Private NICs can be imported using the `{zone}/{server_id}/{private_nic_id}`, e.g.
3838

3939
```bash
40-
$ terraform import scaleway_instance_volume.server_volume fr-par-1/11111111-1111-1111-1111-111111111111/22222222-2222-2222-2222-222222222222
40+
$ terraform import scaleway_instance_private_nic.pnic01 fr-par-1/11111111-1111-1111-1111-111111111111/22222222-2222-2222-2222-222222222222
4141
```

scaleway/data_source_domain_zone.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
)
10+
11+
func dataSourceScalewayDomainZone() *schema.Resource {
12+
// Generate datasource schema from resource
13+
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayDomainZone().Schema)
14+
15+
addOptionalFieldsToSchema(dsSchema, "domain", "subdomain")
16+
17+
return &schema.Resource{
18+
ReadContext: dataSourceScalewayDomainZoneRead,
19+
Schema: dsSchema,
20+
}
21+
}
22+
23+
func dataSourceScalewayDomainZoneRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
24+
d.SetId(fmt.Sprintf("%s.%s", d.Get("subdomain").(string), d.Get("domain").(string)))
25+
26+
return resourceScalewayDomainZoneRead(ctx, d, meta)
27+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 TestAccScalewayDataSourceDomainZone_Basic(t *testing.T) {
11+
tt := NewTestTools(t)
12+
defer tt.Cleanup()
13+
14+
testDNSZone := "test-zone"
15+
16+
resource.ParallelTest(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
ProviderFactories: tt.ProviderFactories,
19+
CheckDestroy: testAccCheckScalewayDomainZoneDestroy(tt),
20+
Steps: []resource.TestStep{
21+
{
22+
Config: fmt.Sprintf(`
23+
resource scaleway_domain_zone main {
24+
domain = "%s"
25+
subdomain = "%s"
26+
}
27+
28+
data scaleway_domain_zone test {
29+
domain = scaleway_domain_zone.main.domain
30+
subdomain = scaleway_domain_zone.main.subdomain
31+
}
32+
`, testDomain, testDNSZone),
33+
Check: resource.ComposeTestCheckFunc(
34+
testAccCheckScalewayDomainZoneExists(tt, "data.scaleway_domain_zone.test"),
35+
resource.TestCheckResourceAttr("data.scaleway_domain_zone.test", "subdomain", testDNSZone),
36+
resource.TestCheckResourceAttr("data.scaleway_domain_zone.test", "domain", testDomain),
37+
resource.TestCheckResourceAttr("data.scaleway_domain_zone.test", "status", "active"),
38+
),
39+
},
40+
},
41+
})
42+
}

scaleway/helpers_domain.go

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
const (
1212
defaultDomainRecordTimeout = 30 * time.Second
13+
defaultDomainZoneTimeout = 30 * time.Second
1314
)
1415

1516
// domainAPI returns a new domain API.

scaleway/provider.go

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func Provider(config *ProviderConfig) plugin.ProviderFunc {
6464
"scaleway_apple_silicon_server": resourceScalewayAppleSiliconServer(),
6565
"scaleway_baremetal_server": resourceScalewayBaremetalServer(),
6666
"scaleway_domain_record": resourceScalewayDomainRecord(),
67+
"scaleway_domain_zone": resourceScalewayDomainZone(),
6768
"scaleway_instance_ip": resourceScalewayInstanceIP(),
6869
"scaleway_instance_ip_reverse_dns": resourceScalewayInstanceIPReverseDNS(),
6970
"scaleway_instance_volume": resourceScalewayInstanceVolume(),
@@ -104,6 +105,7 @@ func Provider(config *ProviderConfig) plugin.ProviderFunc {
104105
"scaleway_account_ssh_key": dataSourceScalewayAccountSSHKey(),
105106
"scaleway_baremetal_offer": dataSourceScalewayBaremetalOffer(),
106107
"scaleway_domain_record": dataSourceScalewayDomainRecord(),
108+
"scaleway_domain_zone": dataSourceScalewayDomainZone(),
107109
"scaleway_instance_ip": dataSourceScalewayInstanceIP(),
108110
"scaleway_instance_security_group": dataSourceScalewayInstanceSecurityGroup(),
109111
"scaleway_instance_server": dataSourceScalewayInstanceServer(),

scaleway/resource_domain_zone.go

+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
domain "github.com/scaleway/scaleway-sdk-go/api/domain/v2beta1"
10+
"github.com/scaleway/scaleway-sdk-go/scw"
11+
)
12+
13+
func resourceScalewayDomainZone() *schema.Resource {
14+
return &schema.Resource{
15+
CreateContext: resourceScalewayDomainZoneCreate,
16+
ReadContext: resourceScalewayDomainZoneRead,
17+
UpdateContext: resourceScalewayDomainZoneUpdate,
18+
DeleteContext: resourceScalewayDomainZoneDelete,
19+
Timeouts: &schema.ResourceTimeout{
20+
Default: schema.DefaultTimeout(defaultDomainZoneTimeout),
21+
},
22+
Importer: &schema.ResourceImporter{
23+
StateContext: schema.ImportStatePassthroughContext,
24+
},
25+
SchemaVersion: 0,
26+
Schema: map[string]*schema.Schema{
27+
"domain": {
28+
Type: schema.TypeString,
29+
Description: "The domain where the DNS zone will be created.",
30+
Required: true,
31+
ForceNew: true,
32+
},
33+
"subdomain": {
34+
Type: schema.TypeString,
35+
Description: "The subdomain of the DNS zone to create.",
36+
Required: true,
37+
},
38+
"ns": {
39+
Type: schema.TypeList,
40+
Description: "NameServer list for zone.",
41+
Computed: true,
42+
Elem: &schema.Schema{
43+
Type: schema.TypeString,
44+
},
45+
},
46+
"ns_default": {
47+
Type: schema.TypeList,
48+
Description: "NameServer default list for zone.",
49+
Computed: true,
50+
Elem: &schema.Schema{
51+
Type: schema.TypeString,
52+
},
53+
},
54+
"ns_master": {
55+
Type: schema.TypeList,
56+
Description: "NameServer master list for zone.",
57+
Computed: true,
58+
Elem: &schema.Schema{
59+
Type: schema.TypeString,
60+
},
61+
},
62+
"status": {
63+
Type: schema.TypeString,
64+
Description: "The domain zone status.",
65+
Computed: true,
66+
},
67+
"message": {
68+
Type: schema.TypeString,
69+
Description: "Message",
70+
Computed: true,
71+
},
72+
"updated_at": {
73+
Type: schema.TypeString,
74+
Description: "The date and time of the last update of the DNS zone.",
75+
Computed: true,
76+
},
77+
"project_id": projectIDSchema(),
78+
},
79+
}
80+
}
81+
82+
func resourceScalewayDomainZoneCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
83+
domainAPI := newDomainAPI(meta)
84+
85+
var dnsZone *domain.DNSZone
86+
87+
dnsZone, err := domainAPI.CreateDNSZone(&domain.CreateDNSZoneRequest{
88+
ProjectID: d.Get("project_id").(string),
89+
Domain: d.Get("domain").(string),
90+
Subdomain: d.Get("subdomain").(string),
91+
}, scw.WithContext(ctx))
92+
93+
if err != nil {
94+
return diag.FromErr(err)
95+
}
96+
d.SetId(fmt.Sprintf("%s.%s", dnsZone.Subdomain, dnsZone.Domain))
97+
98+
return resourceScalewayDomainZoneRead(ctx, d, meta)
99+
}
100+
101+
func resourceScalewayDomainZoneRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
102+
domainAPI := newDomainAPI(meta)
103+
104+
var zone *domain.DNSZone
105+
106+
zones, err := domainAPI.ListDNSZones(&domain.ListDNSZonesRequest{
107+
ProjectID: expandStringPtr(d.Get("project_id")),
108+
DNSZone: d.Id(),
109+
}, scw.WithContext(ctx))
110+
111+
if err != nil {
112+
return diag.FromErr(err)
113+
}
114+
115+
if len(zones.DNSZones) == 0 {
116+
return diag.FromErr(fmt.Errorf("no zone found with the name %s", d.Id()))
117+
}
118+
119+
if len(zones.DNSZones) > 1 {
120+
return diag.FromErr(fmt.Errorf("%d zone found with the same name %s", len(zones.DNSZones), d.Id()))
121+
}
122+
123+
zone = zones.DNSZones[0]
124+
125+
_ = d.Set("subdomain", zone.Subdomain)
126+
_ = d.Set("domain", zone.Domain)
127+
_ = d.Set("ns", zone.Ns)
128+
_ = d.Set("ns_default", zone.NsDefault)
129+
_ = d.Set("ns_master", zone.NsMaster)
130+
_ = d.Set("status", zone.Status.String())
131+
_ = d.Set("message", zone.Message)
132+
_ = d.Set("updated_at", zone.UpdatedAt.String())
133+
_ = d.Set("project_id", zone.ProjectID)
134+
135+
return nil
136+
}
137+
138+
func resourceScalewayDomainZoneUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
139+
domainAPI := newDomainAPI(meta)
140+
141+
if d.HasChangesExcept("subdomain") {
142+
_, err := domainAPI.UpdateDNSZone(&domain.UpdateDNSZoneRequest{
143+
ProjectID: d.Get("project_id").(string),
144+
DNSZone: d.Id(),
145+
NewDNSZone: scw.StringPtr(d.Get("subdomain").(string)),
146+
}, scw.WithContext(ctx))
147+
148+
if err != nil {
149+
return diag.FromErr(err)
150+
}
151+
}
152+
return resourceScalewayDomainZoneRead(ctx, d, meta)
153+
}
154+
155+
func resourceScalewayDomainZoneDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
156+
domainAPI := newDomainAPI(meta)
157+
158+
_, err := domainAPI.DeleteDNSZone(&domain.DeleteDNSZoneRequest{
159+
ProjectID: d.Get("project_id").(string),
160+
DNSZone: d.Id(),
161+
}, scw.WithContext(ctx))
162+
163+
if err != nil && !is404Error(err) {
164+
return diag.FromErr(err)
165+
}
166+
167+
return nil
168+
}

0 commit comments

Comments
 (0)