-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(DataSource): Added new data source
- 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 <[email protected]>
- Loading branch information
1 parent
10629d6
commit aeb3932
Showing
4 changed files
with
178 additions
and
2 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,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)) | ||
} |
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,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 |
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