providers/cloudflare: use cloudflare-go

The most improtant change is using record IDs as identifiers.
This commit is contained in:
Mitchell Hashimoto 2016-05-03 09:43:08 -07:00
parent a7311fa68f
commit 109b3f5198
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
3 changed files with 53 additions and 134 deletions

View File

@ -3,7 +3,8 @@ package cloudflare
import ( import (
"log" "log"
"github.com/crackcomm/cloudflare" // NOTE: Temporary until they merge my PR:
"github.com/mitchellh/cloudflare-go"
) )
type Config struct { type Config struct {
@ -12,13 +13,8 @@ type Config struct {
} }
// Client() returns a new client for accessing cloudflare. // Client() returns a new client for accessing cloudflare.
func (c *Config) Client() (*cloudflare.Client, error) { func (c *Config) Client() (*cloudflare.API, error) {
client := cloudflare.New(&cloudflare.Options{ client := cloudflare.New(c.Token, c.Email)
Email: c.Email,
Key: c.Token,
})
log.Printf("[INFO] CloudFlare Client configured for user: %s", c.Email) log.Printf("[INFO] CloudFlare Client configured for user: %s", c.Email)
return client, nil return client, nil
} }

View File

@ -3,12 +3,11 @@ package cloudflare
import ( import (
"fmt" "fmt"
"log" "log"
"time"
"golang.org/x/net/context"
"github.com/crackcomm/cloudflare"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
// NOTE: Temporary until they merge my PR:
"github.com/mitchellh/cloudflare-go"
) )
func resourceCloudFlareRecord() *schema.Resource { func resourceCloudFlareRecord() *schema.Resource {
@ -71,13 +70,13 @@ func resourceCloudFlareRecord() *schema.Resource {
} }
func resourceCloudFlareRecordCreate(d *schema.ResourceData, meta interface{}) error { func resourceCloudFlareRecordCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.Client) client := meta.(*cloudflare.API)
newRecord := &cloudflare.Record{ newRecord := cloudflare.DNSRecord{
Content: d.Get("value").(string),
Name: d.Get("name").(string),
Proxied: d.Get("proxied").(bool),
Type: d.Get("type").(string), Type: d.Get("type").(string),
Name: d.Get("name").(string),
Content: d.Get("value").(string),
Proxied: d.Get("proxied").(bool),
ZoneName: d.Get("domain").(string), ZoneName: d.Get("domain").(string),
} }
@ -89,24 +88,22 @@ func resourceCloudFlareRecordCreate(d *schema.ResourceData, meta interface{}) er
newRecord.TTL = ttl.(int) newRecord.TTL = ttl.(int)
} }
zone, err := retrieveZone(client, newRecord.ZoneName) zoneId, err := client.ZoneIDByName(newRecord.ZoneName)
if err != nil { if err != nil {
return err return fmt.Errorf("Error finding zone %q: %s", newRecord.ZoneName, err)
} }
d.Set("zone_id", zone.ID) d.Set("zone_id", zoneId)
newRecord.ZoneID = zone.ID newRecord.ZoneID = zoneId
log.Printf("[DEBUG] CloudFlare Record create configuration: %#v", newRecord) log.Printf("[DEBUG] CloudFlare Record create configuration: %#v", newRecord)
ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(time.Second*30)) r, err := client.CreateDNSRecord(zoneId, newRecord)
err = client.Records.Create(ctx, newRecord)
if err != nil { if err != nil {
return fmt.Errorf("Failed to create record: %s", err) return fmt.Errorf("Failed to create record: %s", err)
} }
d.SetId(newRecord.ID) d.SetId(r.ID)
log.Printf("[INFO] CloudFlare Record ID: %s", d.Id()) log.Printf("[INFO] CloudFlare Record ID: %s", d.Id())
@ -114,19 +111,15 @@ func resourceCloudFlareRecordCreate(d *schema.ResourceData, meta interface{}) er
} }
func resourceCloudFlareRecordRead(d *schema.ResourceData, meta interface{}) error { func resourceCloudFlareRecordRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.Client) client := meta.(*cloudflare.API)
domain := d.Get("domain").(string) domain := d.Get("domain").(string)
rName := domain
if v := d.Get("name").(string); v != "@" {
rName = v + "." + rName
}
zone, err := retrieveZone(client, domain) zoneId, err := client.ZoneIDByName(domain)
if err != nil { if err != nil {
return err return fmt.Errorf("Error finding zone %q: %s", domain, err)
} }
record, err := retrieveRecord(client, zone, rName) record, err := client.DNSRecord(zoneId, d.Id())
if err != nil { if err != nil {
return err return err
} }
@ -138,21 +131,21 @@ func resourceCloudFlareRecordRead(d *schema.ResourceData, meta interface{}) erro
d.Set("ttl", record.TTL) d.Set("ttl", record.TTL)
d.Set("priority", record.Priority) d.Set("priority", record.Priority)
d.Set("proxied", record.Proxied) d.Set("proxied", record.Proxied)
d.Set("zone_id", zone.ID) d.Set("zone_id", zoneId)
return nil return nil
} }
func resourceCloudFlareRecordUpdate(d *schema.ResourceData, meta interface{}) error { func resourceCloudFlareRecordUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.Client) client := meta.(*cloudflare.API)
updateRecord := &cloudflare.Record{ updateRecord := cloudflare.DNSRecord{
Content: d.Get("value").(string),
ID: d.Id(), ID: d.Id(),
Name: d.Get("name").(string),
Proxied: false,
Type: d.Get("type").(string), Type: d.Get("type").(string),
Name: d.Get("name").(string),
Content: d.Get("value").(string),
ZoneName: d.Get("domain").(string), ZoneName: d.Get("domain").(string),
Proxied: false,
} }
if priority, ok := d.GetOk("priority"); ok { if priority, ok := d.GetOk("priority"); ok {
@ -167,18 +160,15 @@ func resourceCloudFlareRecordUpdate(d *schema.ResourceData, meta interface{}) er
updateRecord.TTL = ttl.(int) updateRecord.TTL = ttl.(int)
} }
zone, err := retrieveZone(client, updateRecord.ZoneName) zoneId, err := client.ZoneIDByName(updateRecord.ZoneName)
if err != nil { if err != nil {
return err return fmt.Errorf("Error finding zone %q: %s", updateRecord.ZoneName, err)
} }
updateRecord.ZoneID = zone.ID updateRecord.ZoneID = zoneId
log.Printf("[DEBUG] CloudFlare Record update configuration: %#v", updateRecord) log.Printf("[DEBUG] CloudFlare Record update configuration: %#v", updateRecord)
err = client.UpdateDNSRecord(zoneId, d.Id(), updateRecord)
ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(time.Second*30))
err = client.Records.Patch(ctx, updateRecord)
if err != nil { if err != nil {
return fmt.Errorf("Failed to update CloudFlare Record: %s", err) return fmt.Errorf("Failed to update CloudFlare Record: %s", err)
} }
@ -187,79 +177,20 @@ func resourceCloudFlareRecordUpdate(d *schema.ResourceData, meta interface{}) er
} }
func resourceCloudFlareRecordDelete(d *schema.ResourceData, meta interface{}) error { func resourceCloudFlareRecordDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.Client) client := meta.(*cloudflare.API)
domain := d.Get("domain").(string) domain := d.Get("domain").(string)
rName := domain
if v := d.Get("name").(string); v != "@" {
rName = v + "." + rName
}
zone, err := retrieveZone(client, domain) zoneId, err := client.ZoneIDByName(domain)
if err != nil { if err != nil {
return err return fmt.Errorf("Error finding zone %q: %s", domain, err)
}
record, err := retrieveRecord(client, zone, rName)
if err != nil {
return err
} }
log.Printf("[INFO] Deleting CloudFlare Record: %s, %s", domain, d.Id()) log.Printf("[INFO] Deleting CloudFlare Record: %s, %s", domain, d.Id())
ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(time.Second*30)) err = client.DeleteDNSRecord(zoneId, d.Id())
err = client.Records.Delete(ctx, zone.ID, record.ID)
if err != nil { if err != nil {
return fmt.Errorf("Error deleting CloudFlare Record: %s", err) return fmt.Errorf("Error deleting CloudFlare Record: %s", err)
} }
return nil return nil
} }
func retrieveRecord(
client *cloudflare.Client,
zone *cloudflare.Zone,
name string) (*cloudflare.Record, error) {
ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(time.Second*30))
rs, err := client.Records.List(ctx, zone.ID)
if err != nil {
return nil, fmt.Errorf("Unable to retrieve records for (%s): %s", zone.Name, err)
}
var record *cloudflare.Record
for _, r := range rs {
if r.Name == name {
record = r
}
}
if record == nil {
return nil, fmt.Errorf("Unable to find Cloudflare record %s", name)
}
return record, nil
}
func retrieveZone(client *cloudflare.Client, domain string) (*cloudflare.Zone, error) {
ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(time.Second*30))
zs, err := client.Zones.List(ctx)
if err != nil {
return nil, fmt.Errorf("Failed to fetch zone for %s: %s", domain, err)
}
var zone *cloudflare.Zone
for _, z := range zs {
if z.Name == domain {
zone = z
}
}
if zone == nil {
return nil, fmt.Errorf("Failed to find zone for: %s", domain)
}
return zone, nil
}

View File

@ -4,17 +4,16 @@ import (
"fmt" "fmt"
"os" "os"
"testing" "testing"
"time"
"golang.org/x/net/context"
"github.com/crackcomm/cloudflare"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
// NOTE: Temporary until they merge my PR:
"github.com/mitchellh/cloudflare-go"
) )
func TestAccCloudFlareRecord_Basic(t *testing.T) { func TestAccCloudFlareRecord_Basic(t *testing.T) {
var record cloudflare.Record var record cloudflare.DNSRecord
domain := os.Getenv("CLOUDFLARE_DOMAIN") domain := os.Getenv("CLOUDFLARE_DOMAIN")
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
@ -40,7 +39,7 @@ func TestAccCloudFlareRecord_Basic(t *testing.T) {
} }
func TestAccCloudFlareRecord_Apex(t *testing.T) { func TestAccCloudFlareRecord_Apex(t *testing.T) {
var record cloudflare.Record var record cloudflare.DNSRecord
domain := os.Getenv("CLOUDFLARE_DOMAIN") domain := os.Getenv("CLOUDFLARE_DOMAIN")
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
@ -66,7 +65,7 @@ func TestAccCloudFlareRecord_Apex(t *testing.T) {
} }
func TestAccCloudFlareRecord_Proxied(t *testing.T) { func TestAccCloudFlareRecord_Proxied(t *testing.T) {
var record cloudflare.Record var record cloudflare.DNSRecord
domain := os.Getenv("CLOUDFLARE_DOMAIN") domain := os.Getenv("CLOUDFLARE_DOMAIN")
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
@ -95,7 +94,7 @@ func TestAccCloudFlareRecord_Proxied(t *testing.T) {
} }
func TestAccCloudFlareRecord_Updated(t *testing.T) { func TestAccCloudFlareRecord_Updated(t *testing.T) {
var record cloudflare.Record var record cloudflare.DNSRecord
domain := os.Getenv("CLOUDFLARE_DOMAIN") domain := os.Getenv("CLOUDFLARE_DOMAIN")
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
@ -134,7 +133,7 @@ func TestAccCloudFlareRecord_Updated(t *testing.T) {
} }
func TestAccCloudFlareRecord_forceNewRecord(t *testing.T) { func TestAccCloudFlareRecord_forceNewRecord(t *testing.T) {
var afterCreate, afterUpdate cloudflare.Record var afterCreate, afterUpdate cloudflare.DNSRecord
domain := os.Getenv("CLOUDFLARE_DOMAIN") domain := os.Getenv("CLOUDFLARE_DOMAIN")
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
@ -160,7 +159,7 @@ func TestAccCloudFlareRecord_forceNewRecord(t *testing.T) {
} }
func testAccCheckCloudFlareRecordRecreated(t *testing.T, func testAccCheckCloudFlareRecordRecreated(t *testing.T,
before, after *cloudflare.Record) resource.TestCheckFunc { before, after *cloudflare.DNSRecord) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
if before.ID == after.ID { if before.ID == after.ID {
t.Fatalf("Expected change of Record Ids, but both were %v", before.ID) t.Fatalf("Expected change of Record Ids, but both were %v", before.ID)
@ -170,17 +169,14 @@ func testAccCheckCloudFlareRecordRecreated(t *testing.T,
} }
func testAccCheckCloudFlareRecordDestroy(s *terraform.State) error { func testAccCheckCloudFlareRecordDestroy(s *terraform.State) error {
var ( client := testAccProvider.Meta().(*cloudflare.API)
client = testAccProvider.Meta().(*cloudflare.Client)
ctx, _ = context.WithDeadline(context.Background(), time.Now().Add(time.Second*30))
)
for _, rs := range s.RootModule().Resources { for _, rs := range s.RootModule().Resources {
if rs.Type != "cloudflare_record" { if rs.Type != "cloudflare_record" {
continue continue
} }
_, err := client.Records.Details(ctx, rs.Primary.Attributes["zone_id"], rs.Primary.ID) _, err := client.DNSRecord(rs.Primary.Attributes["zone_id"], rs.Primary.ID)
if err == nil { if err == nil {
return fmt.Errorf("Record still exists") return fmt.Errorf("Record still exists")
} }
@ -189,7 +185,7 @@ func testAccCheckCloudFlareRecordDestroy(s *terraform.State) error {
return nil return nil
} }
func testAccCheckCloudFlareRecordAttributes(record *cloudflare.Record) resource.TestCheckFunc { func testAccCheckCloudFlareRecordAttributes(record *cloudflare.DNSRecord) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
if record.Content != "192.168.0.10" { if record.Content != "192.168.0.10" {
@ -200,7 +196,7 @@ func testAccCheckCloudFlareRecordAttributes(record *cloudflare.Record) resource.
} }
} }
func testAccCheckCloudFlareRecordAttributesUpdated(record *cloudflare.Record) resource.TestCheckFunc { func testAccCheckCloudFlareRecordAttributesUpdated(record *cloudflare.DNSRecord) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
if record.Content != "192.168.0.11" { if record.Content != "192.168.0.11" {
@ -211,7 +207,7 @@ func testAccCheckCloudFlareRecordAttributesUpdated(record *cloudflare.Record) re
} }
} }
func testAccCheckCloudFlareRecordExists(n string, record *cloudflare.Record) resource.TestCheckFunc { func testAccCheckCloudFlareRecordExists(n string, record *cloudflare.DNSRecord) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n] rs, ok := s.RootModule().Resources[n]
if !ok { if !ok {
@ -222,12 +218,8 @@ func testAccCheckCloudFlareRecordExists(n string, record *cloudflare.Record) res
return fmt.Errorf("No Record ID is set") return fmt.Errorf("No Record ID is set")
} }
var ( client := testAccProvider.Meta().(*cloudflare.API)
client = testAccProvider.Meta().(*cloudflare.Client) foundRecord, err := client.DNSRecord(rs.Primary.Attributes["zone_id"], rs.Primary.ID)
ctx, _ = context.WithDeadline(context.Background(), time.Now().Add(time.Second*30))
)
foundRecord, err := client.Records.Details(ctx, rs.Primary.Attributes["zone_id"], rs.Primary.ID)
if err != nil { if err != nil {
return err return err
} }
@ -236,7 +228,7 @@ func testAccCheckCloudFlareRecordExists(n string, record *cloudflare.Record) res
return fmt.Errorf("Record not found") return fmt.Errorf("Record not found")
} }
*record = *foundRecord *record = foundRecord
return nil return nil
} }