github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/digitalocean/resource_digitalocean_floating_ip_test.go (about)

     1  package digitalocean
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/digitalocean/godo"
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccDigitalOceanFloatingIP_Region(t *testing.T) {
    14  	var floatingIP godo.FloatingIP
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckDigitalOceanFloatingIPDestroy,
    20  		Steps: []resource.TestStep{
    21  			{
    22  				Config: testAccCheckDigitalOceanFloatingIPConfig_region,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckDigitalOceanFloatingIPExists("digitalocean_floating_ip.foobar", &floatingIP),
    25  					resource.TestCheckResourceAttr(
    26  						"digitalocean_floating_ip.foobar", "region", "nyc3"),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func TestAccDigitalOceanFloatingIP_Droplet(t *testing.T) {
    34  	var floatingIP godo.FloatingIP
    35  	rInt := acctest.RandInt()
    36  
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck:     func() { testAccPreCheck(t) },
    39  		Providers:    testAccProviders,
    40  		CheckDestroy: testAccCheckDigitalOceanFloatingIPDestroy,
    41  		Steps: []resource.TestStep{
    42  			{
    43  				Config: testAccCheckDigitalOceanFloatingIPConfig_droplet(rInt),
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testAccCheckDigitalOceanFloatingIPExists("digitalocean_floating_ip.foobar", &floatingIP),
    46  					resource.TestCheckResourceAttr(
    47  						"digitalocean_floating_ip.foobar", "region", "nyc3"),
    48  				),
    49  			},
    50  		},
    51  	})
    52  }
    53  
    54  func testAccCheckDigitalOceanFloatingIPDestroy(s *terraform.State) error {
    55  	client := testAccProvider.Meta().(*godo.Client)
    56  
    57  	for _, rs := range s.RootModule().Resources {
    58  		if rs.Type != "digitalocean_floating_ip" {
    59  			continue
    60  		}
    61  
    62  		// Try to find the key
    63  		_, _, err := client.FloatingIPs.Get(rs.Primary.ID)
    64  
    65  		if err == nil {
    66  			return fmt.Errorf("Floating IP still exists")
    67  		}
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  func testAccCheckDigitalOceanFloatingIPExists(n string, floatingIP *godo.FloatingIP) resource.TestCheckFunc {
    74  	return func(s *terraform.State) error {
    75  		rs, ok := s.RootModule().Resources[n]
    76  
    77  		if !ok {
    78  			return fmt.Errorf("Not found: %s", n)
    79  		}
    80  
    81  		if rs.Primary.ID == "" {
    82  			return fmt.Errorf("No Record ID is set")
    83  		}
    84  
    85  		client := testAccProvider.Meta().(*godo.Client)
    86  
    87  		// Try to find the FloatingIP
    88  		foundFloatingIP, _, err := client.FloatingIPs.Get(rs.Primary.ID)
    89  
    90  		if err != nil {
    91  			return err
    92  		}
    93  
    94  		if foundFloatingIP.IP != rs.Primary.ID {
    95  			return fmt.Errorf("Record not found")
    96  		}
    97  
    98  		*floatingIP = *foundFloatingIP
    99  
   100  		return nil
   101  	}
   102  }
   103  
   104  var testAccCheckDigitalOceanFloatingIPConfig_region = `
   105  resource "digitalocean_floating_ip" "foobar" {
   106    region = "nyc3"
   107  }`
   108  
   109  func testAccCheckDigitalOceanFloatingIPConfig_droplet(rInt int) string {
   110  	return fmt.Sprintf(`
   111  resource "digitalocean_droplet" "foobar" {
   112    name               = "baz-%d"
   113    size               = "1gb"
   114    image              = "centos-7-x64"
   115    region             = "nyc3"
   116    ipv6               = true
   117    private_networking = true
   118  }
   119  
   120  resource "digitalocean_floating_ip" "foobar" {
   121    droplet_id = "${digitalocean_droplet.foobar.id}"
   122    region     = "${digitalocean_droplet.foobar.region}"
   123  }`, rInt)
   124  }