add Mutex so that we only do one Dyn operation at a time (extra thanks to @daveadams)

This commit is contained in:
David Radcliffe 2015-09-02 13:51:36 -04:00
parent 76dcc66597
commit 83e6d8b60c
2 changed files with 82 additions and 0 deletions

View File

@ -3,11 +3,14 @@ package dyn
import (
"fmt"
"log"
"sync"
"github.com/hashicorp/terraform/helper/schema"
"github.com/nesv/go-dynect/dynect"
)
var mutex = &sync.Mutex{}
func resourceDynRecord() *schema.Resource {
return &schema.Resource{
Create: resourceDynRecordCreate,
@ -54,6 +57,8 @@ func resourceDynRecord() *schema.Resource {
}
func resourceDynRecordCreate(d *schema.ResourceData, meta interface{}) error {
mutex.Lock()
client := meta.(*dynect.ConvenientClient)
record := &dynect.Record{
@ -68,26 +73,33 @@ func resourceDynRecordCreate(d *schema.ResourceData, meta interface{}) error {
// create the record
err := client.CreateRecord(record)
if err != nil {
mutex.Unlock()
return fmt.Errorf("Failed to create Dyn record: %s", err)
}
// publish the zone
err = client.PublishZone(record.Zone)
if err != nil {
mutex.Unlock()
return fmt.Errorf("Failed to publish Dyn zone: %s", err)
}
// get the record ID
err = client.GetRecordID(record)
if err != nil {
mutex.Unlock()
return fmt.Errorf("%s", err)
}
d.SetId(record.ID)
mutex.Unlock()
return resourceDynRecordRead(d, meta)
}
func resourceDynRecordRead(d *schema.ResourceData, meta interface{}) error {
mutex.Lock()
defer mutex.Unlock()
client := meta.(*dynect.ConvenientClient)
record := &dynect.Record{
@ -115,6 +127,8 @@ func resourceDynRecordRead(d *schema.ResourceData, meta interface{}) error {
}
func resourceDynRecordUpdate(d *schema.ResourceData, meta interface{}) error {
mutex.Lock()
client := meta.(*dynect.ConvenientClient)
record := &dynect.Record{
@ -129,26 +143,33 @@ func resourceDynRecordUpdate(d *schema.ResourceData, meta interface{}) error {
// update the record
err := client.UpdateRecord(record)
if err != nil {
mutex.Unlock()
return fmt.Errorf("Failed to update Dyn record: %s", err)
}
// publish the zone
err = client.PublishZone(record.Zone)
if err != nil {
mutex.Unlock()
return fmt.Errorf("Failed to publish Dyn zone: %s", err)
}
// get the record ID
err = client.GetRecordID(record)
if err != nil {
mutex.Unlock()
return fmt.Errorf("%s", err)
}
d.SetId(record.ID)
mutex.Unlock()
return resourceDynRecordRead(d, meta)
}
func resourceDynRecordDelete(d *schema.ResourceData, meta interface{}) error {
mutex.Lock()
defer mutex.Unlock()
client := meta.(*dynect.ConvenientClient)
record := &dynect.Record{

View File

@ -75,6 +75,44 @@ func TestAccDynRecord_Updated(t *testing.T) {
})
}
func TestAccDynRecord_Multiple(t *testing.T) {
var record dynect.Record
zone := os.Getenv("DYN_ZONE")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDynRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(testAccCheckDynRecordConfig_multiple, zone, zone, zone),
Check: resource.ComposeTestCheckFunc(
testAccCheckDynRecordExists("dyn_record.foobar1", &record),
testAccCheckDynRecordAttributes(&record),
resource.TestCheckResourceAttr(
"dyn_record.foobar1", "name", "terraform1"),
resource.TestCheckResourceAttr(
"dyn_record.foobar1", "zone", zone),
resource.TestCheckResourceAttr(
"dyn_record.foobar1", "value", "192.168.0.10"),
resource.TestCheckResourceAttr(
"dyn_record.foobar2", "name", "terraform2"),
resource.TestCheckResourceAttr(
"dyn_record.foobar2", "zone", zone),
resource.TestCheckResourceAttr(
"dyn_record.foobar2", "value", "192.168.1.10"),
resource.TestCheckResourceAttr(
"dyn_record.foobar3", "name", "terraform3"),
resource.TestCheckResourceAttr(
"dyn_record.foobar3", "zone", zone),
resource.TestCheckResourceAttr(
"dyn_record.foobar3", "value", "192.168.2.10"),
),
},
},
})
}
func testAccCheckDynRecordDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*dynect.ConvenientClient)
@ -176,3 +214,26 @@ resource "dyn_record" "foobar" {
type = "A"
ttl = 3600
}`
const testAccCheckDynRecordConfig_multiple = `
resource "dyn_record" "foobar1" {
zone = "%s"
name = "terraform1"
value = "192.168.0.10"
type = "A"
ttl = 3600
}
resource "dyn_record" "foobar2" {
zone = "%s"
name = "terraform2"
value = "192.168.1.10"
type = "A"
ttl = 3600
}
resource "dyn_record" "foobar3" {
zone = "%s"
name = "terraform3"
value = "192.168.2.10"
type = "A"
ttl = 3600
}`