Skip to content

Commit

Permalink
feat(domain): add record resource (scaleway#854)
Browse files Browse the repository at this point in the history
Co-authored-by: Jeremy JACQUEMIN <[email protected]>
  • Loading branch information
jerjako and jerjako authored Sep 14, 2021
1 parent aa12d5d commit 63bc17f
Show file tree
Hide file tree
Showing 15 changed files with 4,722 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/acceptance-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
- Account
- AppleSilicon
- Baremetal
- DomainRecord
- Instance
- Iot
- K8S
Expand All @@ -35,6 +36,7 @@ jobs:
TF_LOG: DEBUG
TF_ACC: 1
TF_UPDATE_CASSETTES: false
TF_TEST_DOMAIN: scaleway-terraform.com
SCW_DEBUG: 0
SCW_ACCESS_KEY: "SCWXXXXXXXXXXXXXFAKE"
SCW_SECRET_KEY: "11111111-1111-1111-1111-111111111111"
4 changes: 3 additions & 1 deletion .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Nightly Acceptance Tests
on:
schedule:
# Will run at 00:00 every day
- cron: '0 0 * * *'
- cron: "0 0 * * *"

jobs:
nightly:
Expand All @@ -14,6 +14,7 @@ jobs:
- Account
- AppleSilicon
- Baremetal
- DomainRecord
- Instance
- Iot
- K8S
Expand All @@ -39,6 +40,7 @@ jobs:
TF_ACC: 1
# Enable recording with the cassette system. By doing so, we ensure that real HTTPS requests are made.
TF_UPDATE_CASSETTES: true
TF_TEST_DOMAIN: scaleway-terraform.com
SCW_DEBUG: 1
SCW_ACCESS_KEY: ${{ secrets.SCW_ACCESS_KEY }}
SCW_SECRET_KEY: ${{ secrets.SCW_SECRET_KEY }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ website/node_modules
*.backup
./*.tfstate
.terraform/
test.tf
*.log
*.bak
*~
Expand Down
12 changes: 10 additions & 2 deletions TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,22 @@ export TF_UPDATE_CASSETTES=true
make testacc
```

It's also required to have Scaleway environment vars available :
It's also required to have Scaleway environment vars available:

```sh
export SCW_ACCESS_KEY=SCWXXXXXXXXXXXXXXXXX
export SCW_SECRET_KEY=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
export SCW_DEFAULT_PROJECT_ID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
```

For testing the domain API, it will use the first available domain in your domains list. You need to have a valid domain.

You can force the test domain with an environment var:

```sh
export TF_TEST_DOMAIN=your-domain.tld
```

To ease debugging you can also set:
```sh
export TF_LOG=DEBUG
Expand All @@ -46,5 +54,5 @@ export SCW_DEBUG=1

Running a single test:
```sh
TF_UPDATE_CASSETTES=true;TF_LOG=DEBUG;SCW_DEBUG=1;TF_ACC=1 go test ./scaleway -v -run=TestAccScalewayDataSourceRDBInstance_Basic -timeout=120m -parallel=10
TF_UPDATE_CASSETTES=true TF_LOG=DEBUG SCW_DEBUG=1 TF_ACC=1 go test ./scaleway -v -run=TestAccScalewayDataSourceRDBInstance_Basic -timeout=120m -parallel=10
```
35 changes: 27 additions & 8 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ You can test this config by creating a `test.tf` and run terraform commands from
- Build the infrastructure: `terraform apply`

```hcl
variable "project_id" {
type = string
description = "Your project ID."
}
terraform {
required_providers {
scaleway = {
Expand All @@ -50,18 +55,31 @@ terraform {
}
provider "scaleway" {
zone = "fr-par-1"
region = "fr-par"
zone = "fr-par-1"
region = "fr-par"
}
resource "scaleway_instance_ip" "public_ip" {}
resource "scaleway_instance_ip" "public_ip" {
project_id = var.project_id
}
resource "scaleway_instance_ip" "public_ip_backup" {
project_id = var.project_id
}
resource "scaleway_instance_volume" "data" {
project_id = var.project_id
size_in_gb = 30
type = "l_ssd"
type = "l_ssd"
}
resource "scaleway_instance_volume" "data_backup" {
project_id = var.project_id
size_in_gb = 10
type = "l_ssd"
}
resource "scaleway_instance_security_group" "www" {
project_id = var.project_id
inbound_default_policy = "drop"
outbound_default_policy = "accept"
Expand All @@ -83,14 +101,15 @@ resource "scaleway_instance_security_group" "www" {
}
resource "scaleway_instance_server" "web" {
type = "DEV1-L"
image = "ubuntu_focal"
project_id = var.project_id
type = "DEV1-L"
image = "ubuntu_focal"
tags = [ "front", "web" ]
tags = ["front", "web"]
ip_id = scaleway_instance_ip.public_ip.id
additional_volume_ids = [ scaleway_instance_volume.data.id ]
additional_volume_ids = [scaleway_instance_volume.data.id]
root_volume {
# The local storage of a DEV1-L instance is 80 GB, subtract 30 GB from the additional l_ssd volume, then the root volume needs to be 50 GB.
Expand Down
240 changes: 240 additions & 0 deletions docs/resources/domain_record.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
---
page_title: "Scaleway: scaleway_domain_record"
description: |-
Manages Scaleway Domain records.
---

# scaleway_domain_record

Creates and manages Scaleway Domain record.
For more information, see [the documentation](https://www.scaleway.com/en/docs/scaleway-dns/).

## Examples

### Basic

```hcl
resource "scaleway_domain_record" "www" {
dns_zone = "domain.tld"
name = "www"
type = "A"
data = "1.2.3.4"
ttl = 3600
}
resource "scaleway_domain_record" "www2" {
dns_zone = "domain.tld"
name = "www"
type = "A"
data = "1.2.3.5"
ttl = 3600
}
resource "scaleway_domain_record" "mx" {
dns_zone = "domain.tld"
name = ""
type = "MX"
data = "mx.online.net."
ttl = 3600
priority = 10
}
resource "scaleway_domain_record" "mx2" {
dns_zone = "domain.tld"
name = ""
type = "MX"
data = "mx-cache.online.net."
ttl = 3600
priority = 20
}
```

### With dynamic records

```hcl
resource "scaleway_domain_record" "geo_ip" {
dns_zone = "domain.tld"
name = "images"
type = "A"
data = "1.2.3.4"
ttl = 3600
geo_ip {
matches {
continents = ["EU"]
countries = ["FR"]
data = "1.2.3.5"
}
matches {
continents = ["NA"]
data = "4.3.2.1"
}
}
}
resource "scaleway_domain_record" "http_service" {
dns_zone = "domain.tld"
name = "app"
type = "A"
data = "1.2.3.4"
ttl = 3600
http_service {
ips = ["1.2.3.5", "1.2.3.6"]
must_contain = "up"
url = "http://mywebsite.com/health"
user_agent = "scw_service_up"
strategy = "hashed"
}
}
resource "scaleway_domain_record" "view" {
dns_zone = "domain.tld"
name = "db"
type = "A"
data = "1.2.3.4"
ttl = 3600
view {
subnet = "100.0.0.0/16"
data = "1.2.3.5"
}
view {
subnet = "100.1.0.0/16"
data = "1.2.3.6"
}
}
resource "scaleway_domain_record" "weighted" {
dns_zone = "domain.tld"
name = "web"
type = "A"
data = "1.2.3.4"
ttl = 3600
weighted {
ip = "1.2.3.5"
weight = 1
}
weighted {
ip = "1.2.3.6"
weight = 2
}
}
```

### Create an instance and add records with the new instance IP

```hcl
variable "project_id" {
type = string
description = "Your project ID."
}
variable "dns_zone" {
type = string
description = "The DNS Zone used for testing records."
}
resource "scaleway_instance_ip" "public_ip" {
project_id = var.project_id
}
resource "scaleway_instance_server" "web" {
project_id = var.project_id
type = "DEV1-S"
image = "ubuntu_focal"
tags = ["front", "web"]
ip_id = scaleway_instance_ip.public_ip.id
root_volume {
size_in_gb = 20
}
}
resource "scaleway_domain_record" "web_A" {
dns_zone = var.dns_zone
name = "web"
type = "A"
data = scaleway_instance_server.web.public_ip
ttl = 3600
}
resource "scaleway_domain_record" "web_cname" {
dns_zone = var.dns_zone
name = "www"
type = "CNAME"
data = "web.${var.dns_zone}."
ttl = 3600
}
resource "scaleway_domain_record" "web_alias" {
dns_zone = var.dns_zone
name = ""
type = "ALIAS"
data = "web.${var.dns_zone}."
ttl = 3600
}
```

## Arguments Reference

The following arguments are supported:

- `dns_zone` - (Required) The DNS Zone of the domain. If the DNS zone doesn't exist, it will be automatically created.

- `keep_empty_zone` - (Optional, default: `false`) When destroying a resource, if only NS records remain and this is set to `false`, the zone will be deleted. Please note, each zone not deleted will [cost you money](https://www.scaleway.com/en/dns/)

- `name` - (Required) The name of the record (can be an empty string for a root record).

- `type` - (Required) The type of the record (`A`, `AAAA`, `MX`, `CNAME`, `ALIAS`, `NS`, `PTR`, `SRV`, `TXT`, `TLSA`, or `CAA`).

- `data` - (Required) The content of the record (an IPv4 for an `A`, a string for a `TXT`...).

- `ttl` - (Optional, default: `3600`) Time To Tive of the record in seconds.

- `priority` - (Optional, default: `0`) The priority of the record (mostly used with an `MX` record)

**Dynamic records:**

- `geo_ip` - (Optional) The Geo IP feature provides DNS resolution, based on the user’s geographical location. You can define a default IP that resolves if no Geo IP rule matches, and specify IPs for each geographical zone. [Documentation and usage example](https://www.scaleway.com/en/docs/scaleway-dns/#-Geo-IP-Records)
- `matches` - (Required) The list of matches. *(Can be more than 1)*
- `countries` - (Optional) List of countries (eg: `FR` for France, `US` for the United States, `GB` for Great Britain...). [List of all countries code](https://api.scaleway.com/domain-private/v2beta1/countries)
- `continents` - (Optional) List of continents (eg: `EU` for Europe, `NA` for North America, `AS` for Asia...). [List of all continents code](https://api.scaleway.com/domain-private/v2beta1/continents)
- `data` (Required) The data of the match result


- `http_service` - (Optional) The DNS service checks the provided URL on the configured IPs and resolves the request to one of the IPs by excluding the ones not responding to the given string to check. [Documentation and usage example](https://www.scaleway.com/en/docs/scaleway-dns/#-Healthcheck-records)
- `ips` - (Required) List of IPs to check
- `must_contain` - (Required) Text to search
- `url` - (Required) URL to match the `must_contain` text to validate an IP
- `user_agent` - (Optional) User-agent used when checking the URL
- `strategy` - (Required) Strategy to return an IP from the IPs list. Can be `random` or `hashed`


- `view` - (Optional) The answer to a DNS request is based on the client’s (resolver) subnet. *(Can be more than 1)* [Documentation and usage example](https://www.scaleway.com/en/docs/scaleway-dns/#-Views-records)
- `subnet` - (Required) The subnet of the view
- `data` - (Required) The data of the view record


- `weighted` - (Optional) You provide a list of IPs with their corresponding weights. These weights are used to proportionally direct requests to each IP. Depending on the weight of a record more or fewer requests are answered with its related IP compared to the others in the list. *(Can be more than 1)* [Documentation and usage example](https://www.scaleway.com/en/docs/scaleway-dns/#-Weight-Records)
- `ip` - (Required) The weighted IP
- `weight` - (Required) The weight of the IP as an integer UInt32.

## Multiple records

Some record types can have multiple `data` with the same `name` (eg: `A`, `AAAA`, `MX`, `NS`...).
You can duplicate a resource `scaleway_domain_record` with the same `name`, the records will be added.

Please note, some record (eg: `CNAME`, Multiple dynamic records of different types...) has to be unique.

## Import

Record can be imported using the `{dns_zone}/{id}`, e.g.

```bash
$ terraform import scaleway_domain_record.www subdomain.domain.tld/11111111-1111-1111-1111-111111111111
```
Loading

0 comments on commit 63bc17f

Please sign in to comment.