github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/cloudflare/resource_cloudflare_record_test.go (about)

     1  package cloudflare
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/cloudflare/cloudflare-go"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccCloudFlareRecord_Basic(t *testing.T) {
    14  	var record cloudflare.DNSRecord
    15  	domain := os.Getenv("CLOUDFLARE_DOMAIN")
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckCloudFlareRecordDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: fmt.Sprintf(testAccCheckCloudFlareRecordConfigBasic, domain),
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckCloudFlareRecordExists("cloudflare_record.foobar", &record),
    26  					testAccCheckCloudFlareRecordAttributes(&record),
    27  					resource.TestCheckResourceAttr(
    28  						"cloudflare_record.foobar", "name", "terraform"),
    29  					resource.TestCheckResourceAttr(
    30  						"cloudflare_record.foobar", "domain", domain),
    31  					resource.TestCheckResourceAttr(
    32  						"cloudflare_record.foobar", "value", "192.168.0.10"),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func TestAccCloudFlareRecord_Apex(t *testing.T) {
    40  	var record cloudflare.DNSRecord
    41  	domain := os.Getenv("CLOUDFLARE_DOMAIN")
    42  
    43  	resource.Test(t, resource.TestCase{
    44  		PreCheck:     func() { testAccPreCheck(t) },
    45  		Providers:    testAccProviders,
    46  		CheckDestroy: testAccCheckCloudFlareRecordDestroy,
    47  		Steps: []resource.TestStep{
    48  			resource.TestStep{
    49  				Config: fmt.Sprintf(testAccCheckCloudFlareRecordConfigApex, domain),
    50  				Check: resource.ComposeTestCheckFunc(
    51  					testAccCheckCloudFlareRecordExists("cloudflare_record.foobar", &record),
    52  					testAccCheckCloudFlareRecordAttributes(&record),
    53  					resource.TestCheckResourceAttr(
    54  						"cloudflare_record.foobar", "name", "@"),
    55  					resource.TestCheckResourceAttr(
    56  						"cloudflare_record.foobar", "domain", domain),
    57  					resource.TestCheckResourceAttr(
    58  						"cloudflare_record.foobar", "value", "192.168.0.10"),
    59  				),
    60  			},
    61  		},
    62  	})
    63  }
    64  
    65  func TestAccCloudFlareRecord_Proxied(t *testing.T) {
    66  	var record cloudflare.DNSRecord
    67  	domain := os.Getenv("CLOUDFLARE_DOMAIN")
    68  
    69  	resource.Test(t, resource.TestCase{
    70  		PreCheck:     func() { testAccPreCheck(t) },
    71  		Providers:    testAccProviders,
    72  		CheckDestroy: testAccCheckCloudFlareRecordDestroy,
    73  		Steps: []resource.TestStep{
    74  			resource.TestStep{
    75  				Config: fmt.Sprintf(testAccCheckCloudFlareRecordConfigProxied, domain, domain),
    76  				Check: resource.ComposeTestCheckFunc(
    77  					testAccCheckCloudFlareRecordExists("cloudflare_record.foobar", &record),
    78  					resource.TestCheckResourceAttr(
    79  						"cloudflare_record.foobar", "domain", domain),
    80  					resource.TestCheckResourceAttr(
    81  						"cloudflare_record.foobar", "name", "terraform"),
    82  					resource.TestCheckResourceAttr(
    83  						"cloudflare_record.foobar", "proxied", "true"),
    84  					resource.TestCheckResourceAttr(
    85  						"cloudflare_record.foobar", "type", "CNAME"),
    86  					resource.TestCheckResourceAttr(
    87  						"cloudflare_record.foobar", "value", domain),
    88  				),
    89  			},
    90  		},
    91  	})
    92  }
    93  
    94  func TestAccCloudFlareRecord_Updated(t *testing.T) {
    95  	var record cloudflare.DNSRecord
    96  	domain := os.Getenv("CLOUDFLARE_DOMAIN")
    97  
    98  	resource.Test(t, resource.TestCase{
    99  		PreCheck:     func() { testAccPreCheck(t) },
   100  		Providers:    testAccProviders,
   101  		CheckDestroy: testAccCheckCloudFlareRecordDestroy,
   102  		Steps: []resource.TestStep{
   103  			resource.TestStep{
   104  				Config: fmt.Sprintf(testAccCheckCloudFlareRecordConfigBasic, domain),
   105  				Check: resource.ComposeTestCheckFunc(
   106  					testAccCheckCloudFlareRecordExists("cloudflare_record.foobar", &record),
   107  					testAccCheckCloudFlareRecordAttributes(&record),
   108  					resource.TestCheckResourceAttr(
   109  						"cloudflare_record.foobar", "name", "terraform"),
   110  					resource.TestCheckResourceAttr(
   111  						"cloudflare_record.foobar", "domain", domain),
   112  					resource.TestCheckResourceAttr(
   113  						"cloudflare_record.foobar", "value", "192.168.0.10"),
   114  				),
   115  			},
   116  			resource.TestStep{
   117  				Config: fmt.Sprintf(testAccCheckCloudFlareRecordConfigNewValue, domain),
   118  				Check: resource.ComposeTestCheckFunc(
   119  					testAccCheckCloudFlareRecordExists("cloudflare_record.foobar", &record),
   120  					testAccCheckCloudFlareRecordAttributesUpdated(&record),
   121  					resource.TestCheckResourceAttr(
   122  						"cloudflare_record.foobar", "name", "terraform"),
   123  					resource.TestCheckResourceAttr(
   124  						"cloudflare_record.foobar", "domain", domain),
   125  					resource.TestCheckResourceAttr(
   126  						"cloudflare_record.foobar", "value", "192.168.0.11"),
   127  				),
   128  			},
   129  		},
   130  	})
   131  }
   132  
   133  func TestAccCloudFlareRecord_forceNewRecord(t *testing.T) {
   134  	var afterCreate, afterUpdate cloudflare.DNSRecord
   135  	domain := os.Getenv("CLOUDFLARE_DOMAIN")
   136  
   137  	resource.Test(t, resource.TestCase{
   138  		PreCheck:     func() { testAccPreCheck(t) },
   139  		Providers:    testAccProviders,
   140  		CheckDestroy: testAccCheckCloudFlareRecordDestroy,
   141  		Steps: []resource.TestStep{
   142  			resource.TestStep{
   143  				Config: fmt.Sprintf(testAccCheckCloudFlareRecordConfigBasic, domain),
   144  				Check: resource.ComposeTestCheckFunc(
   145  					testAccCheckCloudFlareRecordExists("cloudflare_record.foobar", &afterCreate),
   146  				),
   147  			},
   148  			resource.TestStep{
   149  				Config: fmt.Sprintf(testAccCheckCloudFlareRecordConfigForceNew, domain, domain),
   150  				Check: resource.ComposeTestCheckFunc(
   151  					testAccCheckCloudFlareRecordExists("cloudflare_record.foobar", &afterUpdate),
   152  					testAccCheckCloudFlareRecordRecreated(t, &afterCreate, &afterUpdate),
   153  				),
   154  			},
   155  		},
   156  	})
   157  }
   158  
   159  func testAccCheckCloudFlareRecordRecreated(t *testing.T,
   160  	before, after *cloudflare.DNSRecord) resource.TestCheckFunc {
   161  	return func(s *terraform.State) error {
   162  		if before.ID == after.ID {
   163  			t.Fatalf("Expected change of Record Ids, but both were %v", before.ID)
   164  		}
   165  		return nil
   166  	}
   167  }
   168  
   169  func testAccCheckCloudFlareRecordDestroy(s *terraform.State) error {
   170  	client := testAccProvider.Meta().(*cloudflare.API)
   171  
   172  	for _, rs := range s.RootModule().Resources {
   173  		if rs.Type != "cloudflare_record" {
   174  			continue
   175  		}
   176  
   177  		_, err := client.DNSRecord(rs.Primary.Attributes["zone_id"], rs.Primary.ID)
   178  		if err == nil {
   179  			return fmt.Errorf("Record still exists")
   180  		}
   181  	}
   182  
   183  	return nil
   184  }
   185  
   186  func testAccCheckCloudFlareRecordAttributes(record *cloudflare.DNSRecord) resource.TestCheckFunc {
   187  	return func(s *terraform.State) error {
   188  
   189  		if record.Content != "192.168.0.10" {
   190  			return fmt.Errorf("Bad content: %s", record.Content)
   191  		}
   192  
   193  		return nil
   194  	}
   195  }
   196  
   197  func testAccCheckCloudFlareRecordAttributesUpdated(record *cloudflare.DNSRecord) resource.TestCheckFunc {
   198  	return func(s *terraform.State) error {
   199  
   200  		if record.Content != "192.168.0.11" {
   201  			return fmt.Errorf("Bad content: %s", record.Content)
   202  		}
   203  
   204  		return nil
   205  	}
   206  }
   207  
   208  func testAccCheckCloudFlareRecordExists(n string, record *cloudflare.DNSRecord) resource.TestCheckFunc {
   209  	return func(s *terraform.State) error {
   210  		rs, ok := s.RootModule().Resources[n]
   211  		if !ok {
   212  			return fmt.Errorf("Not found: %s", n)
   213  		}
   214  
   215  		if rs.Primary.ID == "" {
   216  			return fmt.Errorf("No Record ID is set")
   217  		}
   218  
   219  		client := testAccProvider.Meta().(*cloudflare.API)
   220  		foundRecord, err := client.DNSRecord(rs.Primary.Attributes["zone_id"], rs.Primary.ID)
   221  		if err != nil {
   222  			return err
   223  		}
   224  
   225  		if foundRecord.ID != rs.Primary.ID {
   226  			return fmt.Errorf("Record not found")
   227  		}
   228  
   229  		*record = foundRecord
   230  
   231  		return nil
   232  	}
   233  }
   234  
   235  const testAccCheckCloudFlareRecordConfigBasic = `
   236  resource "cloudflare_record" "foobar" {
   237  	domain = "%s"
   238  
   239  	name = "terraform"
   240  	value = "192.168.0.10"
   241  	type = "A"
   242  	ttl = 3600
   243  }`
   244  
   245  const testAccCheckCloudFlareRecordConfigApex = `
   246  resource "cloudflare_record" "foobar" {
   247  	domain = "%s"
   248  	name = "@"
   249  	value = "192.168.0.10"
   250  	type = "A"
   251  	ttl = 3600
   252  }`
   253  
   254  const testAccCheckCloudFlareRecordConfigProxied = `
   255  resource "cloudflare_record" "foobar" {
   256  	domain = "%s"
   257  
   258  	name = "terraform"
   259  	value = "%s"
   260  	type = "CNAME"
   261  	proxied = true
   262  }`
   263  
   264  const testAccCheckCloudFlareRecordConfigNewValue = `
   265  resource "cloudflare_record" "foobar" {
   266  	domain = "%s"
   267  
   268  	name = "terraform"
   269  	value = "192.168.0.11"
   270  	type = "A"
   271  	ttl = 3600
   272  }`
   273  
   274  const testAccCheckCloudFlareRecordConfigForceNew = `
   275  resource "cloudflare_record" "foobar" {
   276  	domain = "%s"
   277  
   278  	name = "terraform"
   279  	value = "%s"
   280  	type = "CNAME"
   281  	ttl = 3600
   282  }`