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