diff --git a/civo/datasource_dns_domain_name.go b/civo/datasource_dns_domain_name.go new file mode 100644 index 00000000..0f2a61a8 --- /dev/null +++ b/civo/datasource_dns_domain_name.go @@ -0,0 +1,62 @@ +package civo + +import ( + "fmt" + "github.com/civo/civogo" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" + "log" +) + +// Data source to get from the api a specific domain +// using the id or the name of the domain +func dataSourceDnsDomainName() *schema.Resource { + return &schema.Resource{ + Read: dataSourceDnsDomainNameRead, + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.NoZeroValues, + ExactlyOneOf: []string{"id", "name"}, + }, + "name": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.NoZeroValues, + ExactlyOneOf: []string{"id", "name"}, + }, + }, + } +} + +func dataSourceDnsDomainNameRead(d *schema.ResourceData, m interface{}) error { + apiClient := m.(*civogo.Client) + + var foundDomain *civogo.DNSDomain + + if id, ok := d.GetOk("id"); ok { + log.Printf("[INFO] Getting the domain by id") + domain, err := apiClient.FindDNSDomain(id.(string)) + if err != nil { + fmt.Errorf("[ERR] failed to retrive domain: %s", err) + return err + } + + foundDomain = domain + } else if name, ok := d.GetOk("name"); ok { + log.Printf("[INFO] Getting the domain by name") + image, err := apiClient.FindDNSDomain(name.(string)) + if err != nil { + fmt.Errorf("[ERR] failed to retrive domain: %s", err) + return err + } + + foundDomain = image + } + + d.SetId(foundDomain.ID) + d.Set("name", foundDomain.Name) + + return nil +} diff --git a/civo/datasource_instance.go b/civo/datasource_instance.go index 544a981b..b49c8b98 100644 --- a/civo/datasource_instance.go +++ b/civo/datasource_instance.go @@ -8,8 +8,8 @@ import ( "log" ) -// Data source to get from the api a specific template -// using the code of the image +// Data source to get from the api a specific instance +// using the id or the hostname func dataSourceInstance() *schema.Resource { return &schema.Resource{ Read: dataSourceInstanceRead, diff --git a/civo/datasource_instances.go b/civo/datasource_instances.go index f3c1ad3a..2e20c510 100644 --- a/civo/datasource_instances.go +++ b/civo/datasource_instances.go @@ -7,8 +7,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/schema" ) -// Data source to get and filter all instances size -// use to define the size in resourceInstance +// Data source to get and filter all instances with filter func dataSourceInstances() *schema.Resource { dataListConfig := &datalist.ResourceConfig{ RecordSchema: map[string]*schema.Schema{ diff --git a/civo/provider.go b/civo/provider.go index e38c62b6..97783c3d 100644 --- a/civo/provider.go +++ b/civo/provider.go @@ -22,6 +22,7 @@ func Provider() terraform.ResourceProvider { "civo_instances_size": dataSourceInstancesSize(), "civo_instances": dataSourceInstances(), "civo_instance": dataSourceInstance(), + "civo_dns_domain_name": dataSourceDnsDomainName(), }, ResourcesMap: map[string]*schema.Resource{ "civo_instance": resourceInstance(), diff --git a/website/docs/d/dns_domain_name.html.markdown b/website/docs/d/dns_domain_name.html.markdown new file mode 100644 index 00000000..e1a3eb97 --- /dev/null +++ b/website/docs/d/dns_domain_name.html.markdown @@ -0,0 +1,60 @@ +--- +layout: "civo" +page_title: "Civo: civo_dns_domain_name" +sidebar_current: "docs-civo-datasource-domain" +description: |- + Get information on a domain. +--- + +# civo_dns_domain_name + +Get information on a domain. This data source provides the name and the id, this is useful if the domain +name in question is not managed by Terraform. + +An error is triggered if the provided domain name is not managed with your +Civo account. + +## Example Usage + +Get the name and the id file for a domain: + +```hcl +data "civo_dns_domain_name" "domain" { + name = "domain.com" +} + +output "domain_output" { + value = data.civo_dns_domain_name.domain.name +} +output "domain_id_output" { + value = data.civo_dns_domain_name.domain.id +} + +``` + +``` + $ terraform apply + +data.civo_dns_domain_name.domain: Refreshing state... + +Apply complete! Resources: 0 added, 0 changed, 0 destroyed. + +Outputs: + +domain_output = domain.com. +domain_id_output = 6ea98024-c6d7-4d0c-bd01-8ee0cab5224e +``` + +## Argument Reference + +The following arguments are supported: + +* `id` - (Optional) The id of the domain. +* `name` - (Optional) The name of the domain. + +## Attributes Reference + +The following attributes are exported: + +* `id` - A unique ID that can be used to identify and reference a domain. +* `name` - The name of the domain.