Skip to content

Commit

Permalink
feat(domain): Add domain record data source (scaleway#874)
Browse files Browse the repository at this point in the history
* feat(domain): Add domain record data source

* fix tests

Co-authored-by: Jeremy JACQUEMIN <jjacquemin@scaleway.com>
Co-authored-by: jaime Bernabe <6184069+Monitob@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 23, 2021
1 parent bf4875c commit a578f5f
Show file tree
Hide file tree
Showing 7 changed files with 3,857 additions and 6 deletions.
56 changes: 56 additions & 0 deletions docs/data-sources/domain_record.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
page_title: "Scaleway: scaleway_domain_record"
description: |-
Gets information about a domain record.
---

# scaleway_domain_record

Gets information about a domain record.

## Example Usage

```hcl
# Get record by name, type and data
data "scaleway_domain_record" "by_content" {
dns_zone = "domain.tld"
name = "www"
type = "A"
data = "1.2.3.4"
}
# Get info by ID
data "scaleway_domain_record" "by_id" {
dns_zone = "domain.tld"
record_id = "11111111-1111-1111-1111-111111111111"
}
```

## Argument Reference

- `dns_zone` - (Optional) The IP address.

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

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

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

- `record_id` - (Optional) The record ID.
Cannot be used with `name`, `type` and `data`.

- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the domain is associated with.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

- `ttl` - Time To Tive of the record in seconds.
- `priority` - The priority of the record (mostly used with an `MX` record)
- `geo_ip` - Dynamic record base on user geolocalisation ([More information about dynamic records](../resources/domain_record.md#dynamic-records))
- `http_service` - Dynamic record base on URL resolve ([More information about dynamic records](../resources/domain_record.md#dynamic-records))
- `weighted` - Dynamic record base on IP weights ([More information about dynamic records](../resources/domain_record.md#dynamic-records))
- `view` - Dynamic record based on the client’s (resolver) subnet ([More information about dynamic records](../resources/domain_record.md#dynamic-records))
2 changes: 1 addition & 1 deletion docs/resources/domain_record.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ The following arguments are supported:

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

**Dynamic records:**
### 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)*
Expand Down
71 changes: 71 additions & 0 deletions scaleway/data_source_domain_record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package scaleway

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
domain "github.com/scaleway/scaleway-sdk-go/api/domain/v2beta1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func dataSourceScalewayDomainRecord() *schema.Resource {
// Generate datasource schema from resource
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayDomainRecord().Schema)

// Set 'Optional' schema elements
addOptionalFieldsToSchema(dsSchema, "dns_zone", "name", "type", "data")

dsSchema["name"].ConflictsWith = []string{"record_id"}
dsSchema["type"].ConflictsWith = []string{"record_id"}
dsSchema["data"].ConflictsWith = []string{"record_id"}
dsSchema["record_id"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The ID of the record",
ValidateFunc: validationUUID(),
ConflictsWith: []string{"name", "type", "data"},
}

return &schema.Resource{
ReadContext: dataSourceScalewayDomainRecordRead,
Schema: dsSchema,
}
}

func dataSourceScalewayDomainRecordRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
domainAPI := newDomainAPI(meta)

recordID, ok := d.GetOk("record_id")
if !ok { // Get Record by dns_zone, name, type and data.
res, err := domainAPI.ListDNSZoneRecords(&domain.ListDNSZoneRecordsRequest{
DNSZone: d.Get("dns_zone").(string),
Name: d.Get("name").(string),
Type: domain.RecordType(d.Get("type").(string)),
ProjectID: expandStringPtr(d.Get("project_id")),
}, scw.WithContext(ctx), scw.WithAllPages())
if err != nil {
return diag.FromErr(err)
}
if len(res.Records) == 0 {
return diag.FromErr(fmt.Errorf("no record found with the type %s", d.Get("type")))
}
var record *domain.Record
for i := range res.Records {
if res.Records[i].Data == d.Get("data").(string) {
if record != nil {
return diag.FromErr(fmt.Errorf("more than one record found with this name: %s, type: %s and data: %s", d.Get("name"), d.Get("type"), d.Get("data")))
}
record = res.Records[i]
}
}
if record == nil {
return diag.FromErr(fmt.Errorf("no record found with the type this name: %s, type: %s and data: %s", d.Get("name"), d.Get("type"), d.Get("data")))
}
recordID = record.ID
}

d.SetId(fmt.Sprintf("%s/%s", d.Get("dns_zone"), recordID.(string)))
return resourceScalewayDomainRecordRead(ctx, d, meta)
}
Loading

0 comments on commit a578f5f

Please sign in to comment.