github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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", "nyc3"),
    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  			return 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 = fmt.Sprintf(`
   108  resource "digitalocean_ssh_key" "foobar" {
   109    name       = "foobar"
   110    public_key = "%s"
   111  }
   112  
   113  resource "digitalocean_droplet" "foobar" {
   114    name               = "baz"
   115    size               = "1gb"
   116    image              = "centos-7-x64"
   117    region             = "nyc3"
   118    ipv6               = true
   119    private_networking = true
   120    ssh_keys           = ["${digitalocean_ssh_key.foobar.id}"]
   121  }
   122  
   123  resource "digitalocean_floating_ip" "foobar" {
   124    droplet_id = "${digitalocean_droplet.foobar.id}"
   125    region     = "${digitalocean_droplet.foobar.region}"
   126  }`, testAccValidPublicKey)