Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adddatasource for groups and record sets #115

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/data-sources/group.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# vinyldns_group

Use this data source to retrieve the `name`, `email`, and `description` for a group.

## Example Usage

```hcl
data "vinyldns_group" "test" {
id = "foo"
}
```

## Arguments Reference

* `id` - (Required) The id of the group.

## Attributes Reference

* `name` - The name of the group

* `desription` - The description of the group

* `email` - The email associated with the group
20 changes: 20 additions & 0 deletions docs/data-sources/recordset.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# vinyldns_record_set

Use this data source to retrieve the list of record sets present in a zone

## Example Usage

```hcl
data "vinyldns_record_set" "test" {
zone_id = "fbf7a440-891c-441a-ad09-e1cbc861sda2q"
}
```

## Arguments Reference

* `zone_id` - (Required) The id of the zone.

## Attributes Reference

* `recordset` - List of records present for the particular zone

25 changes: 25 additions & 0 deletions example.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


resource "vinyldns_group" "test_group" {
name = "terraform-provider-test-group"
member_ids = ["123"]
Expand Down Expand Up @@ -32,3 +34,26 @@ resource "vinyldns_record_set" "another_test_record_set" {
ttl = 6000
record_cname = "foo-bar.com."
}

data "vinyldns_group" "test_group_datasource" {
id = vinyldns_group.test_group.id
}

output "name" {
value = data.vinyldns_group.test_group_datasource.name
}

output "email" {
value = data.vinyldns_group.test_group_datasource.email
}

output "description" {
value = data.vinyldns_group.test_group_datasource.description
}

data "vinyldns_record_set" "test_recordset_datasource" {
zoneid = "zoneid"
}
output "recordset"{
value = data.vinyldns_record_set.test_recordset_datasource.recordset
}
8 changes: 8 additions & 0 deletions provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
required_providers {
vinyldns = {
source = "local/vinyldns-provider/vinyldns"
version = "0.0.1"
}
}
}
53 changes: 53 additions & 0 deletions vinyldns/data_source_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package vinyldns

import (
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/vinyldns/go-vinyldns/vinyldns"
)

func dataSourceVinylDNSGroup() *schema.Resource {
return &schema.Resource{
Read: dataSourceVinylDNSGroupRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"email": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceVinylDNSGroupRead(d *schema.ResourceData, meta interface{}) error {
var id string
if n, ok := d.GetOk("id"); ok {
id = n.(string)
}
if id == "" {
return fmt.Errorf("%s must be provided", "id")
}
log.Printf("[INFO] Reading VinylDNS Group %s", id)
z, err := meta.(*vinyldns.Client).Group(id)
if err != nil {
return err
}
d.SetId(z.ID)
d.Set("name", z.Name)
d.Set("email", z.Email)
d.Set("description", z.Description)
return nil
}
63 changes: 63 additions & 0 deletions vinyldns/data_source_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package vinyldns

import (
"fmt"
"log"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/vinyldns/go-vinyldns/vinyldns"
)

func TestAccVinylDNSGroupDataSource_basic(t *testing.T) {
id := "fdc7c6ed-44dc-4c75-9ec1-bb96e73489d5"
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccVinylDNSGroupDataSourcePreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckVinylDNSGroupDataSourceConfig(id),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.vinyldns_group.test", "name"),
resource.TestCheckResourceAttrSet("data.vinyldns_group.test", "email"),
resource.TestCheckResourceAttrSet("data.vinyldns_group.test", "description"),
resource.TestCheckResourceAttr("data.vinyldns_group.test", "id", id),
),
},
},
})
}

// create a group and zone such that the zone can be imported
func testAccVinylDNSGroupDataSourcePreCheck(t *testing.T) error {
client := vinyldns.NewClientFromEnv()
g, err := client.GroupCreate(&vinyldns.Group{
Name: "terraform-provider-group_datasource_test",
Email: "[email protected]",
Members: []vinyldns.User{
vinyldns.User{
ID: "6e07183f-a68e-42cf-acce-044296ede753",
}},
Admins: []vinyldns.User{
vinyldns.User{
ID: "6e07183f-a68e-42cf-acce-044296ede753",
}},
})
if err != nil {
log.Printf("[INFO] Error creating VinylDNS group %s", err)
return err
}
log.Printf("[INFO] Created VinylDNS group %s", g.Name)

return nil
}

func testAccCheckVinylDNSGroupDataSourceConfig(id string) string {
return fmt.Sprintf(`
data "vinyldns_group" "test" {
id = "%s"
}
`, id)
}
53 changes: 53 additions & 0 deletions vinyldns/data_source_recordset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package vinyldns

import (
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/vinyldns/go-vinyldns/vinyldns"
)

func dataSourceVinylDNSRecordSet() *schema.Resource {
return &schema.Resource{
Read: dataSourceVinylDNSRecordSetRead,
Schema: map[string]*schema.Schema{
"zoneid": {
Type: schema.TypeString,
Optional: true,
},
"recordset": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceVinylDNSRecordSetRead(d *schema.ResourceData, meta interface{}) error {
var zoneid string

if n, ok := d.GetOk("zoneid"); ok {
zoneid = n.(string)
}

if zoneid == "" {
return fmt.Errorf("%s must be provided", "zoneid")
}

log.Printf("[INFO] Reading VinylDNS Recordset %s", zoneid)
z, err := meta.(*vinyldns.Client).RecordSets(zoneid)
if err != nil {
return err
}
elementMap := make(map[int]string)
for i, num := range z {
elementMap[i] = fmt.Sprintf("%+v", num)
}
recordset := fmt.Sprintf("%+v", elementMap)

d.SetId(zoneid)
d.Set("zoneid", zoneid)
d.Set("recordset", recordset)
return nil
}
100 changes: 100 additions & 0 deletions vinyldns/data_source_recordset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package vinyldns

import (
"fmt"
"log"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/vinyldns/go-vinyldns/vinyldns"
)

func TestAccVinylDNSRecordSetDataSource_basic(t *testing.T) {
zoneid := "fbf7a440-891c-441a-ad09-e1cbc861sda2q"
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccVinylDNSZoneDataSourcePreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckVinylDNSRecordSetDataSourceConfig(zoneid),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.vinyldns_record_set.test", "recordset"),
resource.TestCheckResourceAttr("data.vinyldns_record_set.test", "zoneid", zoneid),
),
},
},
})
}

// create a group and zone such that the zone can be imported
func testAccVinylDNSRecordSetDataSourcePreCheck(t *testing.T) error {
client := vinyldns.NewClientFromEnv()
g, err := client.GroupCreate(&vinyldns.Group{
Name: "terraformdatasourcetestgroup",
Email: "[email protected]",
Members: []vinyldns.User{
vinyldns.User{
ID: "ok",
}},
Admins: []vinyldns.User{
vinyldns.User{
ID: "ok",
}},
})
if err != nil {
log.Printf("[INFO] Error creating VinylDNS group %s", err)
return err
}
log.Printf("[INFO] Created VinylDNS group %s", g.Name)

z, err := client.ZoneCreate(&vinyldns.Zone{
Name: "ok.",
Email: "[email protected]",
AdminGroupID: g.ID,
Connection: &vinyldns.ZoneConnection{
Name: "vinyldns.",
Key: "nzisn+4G2ldMn0q1CV3vsg==",
KeyName: "vinyldns.",
PrimaryServer: "localhost:19001",
},
})
if err != nil {
log.Printf("[INFO] Error creating VinylDNS zone %s", err)
return err
}

createdZoneID := z.Zone.ID
limit := 10
for i := 0; i < limit; time.Sleep(10 * time.Second) {
i++

zg, err := client.Zone(createdZoneID)
if err == nil && zg.ID != createdZoneID {
log.Printf("[INFO] unable to get VinylDNS zone %s", createdZoneID)
return err
}
if err == nil && zg.ID == createdZoneID {
break
}

if i == (limit - 1) {
log.Printf("[INFO] %d retries reached in polling VinylDNS zone %s", limit, createdZoneID)
return err
}
}

log.Printf("[INFO] Created VinylDNS zone %s", z.Zone.Name)

return nil
}

func testAccCheckVinylDNSRecordSetDataSourceConfig(zoneid string) string {
return fmt.Sprintf(`
data "vinyldns_record_set" "test" {
zoneid = "%s"
}
`, zoneid)
}
4 changes: 3 additions & 1 deletion vinyldns/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ func Provider() terraform.ResourceProvider {
},

DataSourcesMap: map[string]*schema.Resource{
"vinyldns_zone": dataSourceVinylDNSZone(),
"vinyldns_zone": dataSourceVinylDNSZone(),
"vinyldns_group": dataSourceVinylDNSGroup(),
"vinyldns_record_set": dataSourceVinylDNSRecordSet(),
},

ConfigureFunc: providerConfigure,
Expand Down
Loading