forked from scaleway/scaleway-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(domain): Add domain record data source (scaleway#874)
* 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
1 parent
bf4875c
commit a578f5f
Showing
7 changed files
with
3,857 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.