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_name to get one domain
- Fixed some comment in datasource_instance.go, datasource_instances.go

BREAKING CHANGE: No

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed May 18, 2020
1 parent 559594a commit 10629d6
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 4 deletions.
62 changes: 62 additions & 0 deletions civo/datasource_dns_domain_name.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions civo/datasource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions civo/datasource_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
1 change: 1 addition & 0 deletions civo/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
60 changes: 60 additions & 0 deletions website/docs/d/dns_domain_name.html.markdown
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 10629d6

Please sign in to comment.