Skip to content

Commit

Permalink
feat(DataSource): Added new data source
Browse files Browse the repository at this point in the history
- Added the new data source dns_domain_record to get one record
- Fixed some text in the help of resource dns_domain_record

BREAKING CHANGE: No

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed May 18, 2020
1 parent 10629d6 commit aeb3932
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 2 deletions.
101 changes: 101 additions & 0 deletions civo/datasource_dns_domain_record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package civo

import (
"fmt"
"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)

// Data source to get from the api a specific domain record
// using the id or the name of the domain
func dataSourceDnsDomainRecord() *schema.Resource {
return &schema.Resource{
Read: dataSourceDnsDomainRecordRead,
Schema: map[string]*schema.Schema{
"domain_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
},
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
},
// Computed resource
"type": {
Type: schema.TypeString,
Computed: true,
},
"value": {
Type: schema.TypeString,
Computed: true,
},
"priority": {
Type: schema.TypeInt,
Computed: true,
},
"ttl": {
Type: schema.TypeInt,
Computed: true,
},
"account_id": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceDnsDomainRecordRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)
domain := d.Get("domain_id").(string)
name := d.Get("name").(string)

allRecords, err := apiClient.ListDNSRecords(domain)
if err != nil {
return fmt.Errorf("error retrieving all domain records: %s", err)
}

record, err := getRecordByName(allRecords, name)
if err != nil {
return err
}

d.SetId(record.ID)
d.Set("name", record.Name)
d.Set("type", record.Type)
d.Set("value", record.Value)
d.Set("priority", record.Priority)
d.Set("ttl", record.TTL)
d.Set("account_id", record.AccountID)
d.Set("created_at", record.CreatedAt.UTC().String())
d.Set("updated_at", record.UpdatedAt.UTC().String())

return nil
}

func getRecordByName(allRecord []civogo.DNSRecord, name string) (*civogo.DNSRecord, error) {
results := make([]civogo.DNSRecord, 0)
for _, v := range allRecord {
if v.Name == name {
results = append(results, v)
}
}
if len(results) == 1 {
return &results[0], nil
}
if len(results) == 0 {
return nil, fmt.Errorf("no records found with name %s", name)
}
return nil, fmt.Errorf("too many records found (found %d, expected 1)", len(results))
}
1 change: 1 addition & 0 deletions civo/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func Provider() terraform.ResourceProvider {
"civo_instances": dataSourceInstances(),
"civo_instance": dataSourceInstance(),
"civo_dns_domain_name": dataSourceDnsDomainName(),
"civo_dns_domain_record": dataSourceDnsDomainRecord(),
},
ResourcesMap: map[string]*schema.Resource{
"civo_instance": resourceInstance(),
Expand Down
74 changes: 74 additions & 0 deletions website/docs/d/dns_domain_record.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
layout: "civo"
page_title: "Civo: civo_dns_domain_record"
sidebar_current: "docs-civo-datasource-record"
description: |-
Get information on a DNS record.
---

# civo_dns_domain_record

Get information on a DNS record. This data source provides the name, TTL, and zone
file as configured on your Civo account. This is useful if the record
in question is not managed by Terraform.

An error is triggered if the provided domain name or record are not managed with
your Civo account.

## Example Usage

Get data from a DNS record:

```hcl
data "civo_dns_domain_name" "domain" {
name = "domain.com"
}
data "civo_dns_domain_record" "www" {
domain_id = data.civo_dns_domain_name.domain.id
name = "www"
}
output "record_type" {
value = data.civo_dns_domain_record.www.type
}
output "record_ttl" {
value = data.civo_dns_domain_record.www.ttl
}
```

```
$ terraform apply
data.civo_dns_domain_record.www: Refreshing state...
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
record_ttl = 3600
record_type = A
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The name of the record.
* `domain_id` - (Required) The domain id of the record.

## Attributes Reference

The following attributes are exported:

* `id` - A unique ID that can be used to identify and reference a Record.
* `domain_id` - The id of the domain
* `type` - The choice of record type from a, cname, mx or txt
* `name` - The portion before the domain name (e.g. www) or an @ for the apex/root domain (you cannot use an A record with an amex/root domain)
* `value` - The IP address (A or MX), hostname (CNAME or MX) or text value (TXT) to serve for this record
* `priority` - The priority of the record.
* `ttl` - How long caching DNS servers should cache this record.
* `account_id` - The id account of the domain.
* `created_at` - The date when it was created in UTC format
* `updated_at` - The date when it was updated in UTC format
4 changes: 2 additions & 2 deletions website/docs/r/dns_domain_record.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ resource "civo_dns_domain_record" "www" {
The following arguments are supported:

* `domain_id` - (Required) The id of the domain
* `type` - (Required) The choice of RR type from a, cname, mx or txt
* `type` - (Required) The choice of record type from a, cname, mx or txt
* `name` - (Required) The portion before the domain name (e.g. www) or an @ for the apex/root domain (you cannot use an A record with an amex/root domain)
* `value` - (Required) The IP address (A or MX), hostname (CNAME or MX) or text value (TXT) to serve for this record
* `priority` - (Optional) Useful for MX records only, the priority mail should be attempted it (defaults to 10)
Expand All @@ -42,7 +42,7 @@ The following attributes are exported including the arguments:
* `id` - A unique ID that can be used to identify and reference a Record.
* `account_id` - The id account of the domain
* `created_at` - The date when it was created in UTC format
* `updated_at` - Tthe date when it was updated in UTC format
* `updated_at` - The date when it was updated in UTC format

## Import

Expand Down

0 comments on commit aeb3932

Please sign in to comment.