From f08f01626f173251f4a276f2edc69e07cd8fe50d Mon Sep 17 00:00:00 2001 From: stack72 Date: Wed, 4 Nov 2015 11:37:33 +0000 Subject: [PATCH] Adding the work to assign a Floating IP to a Droplet --- resource_digitalocean_floating_ip.go | 29 +++++++++++----- resource_digitalocean_floating_ip_test.go | 41 +++++++++++++++++++++-- 2 files changed, 59 insertions(+), 11 deletions(-) diff --git a/resource_digitalocean_floating_ip.go b/resource_digitalocean_floating_ip.go index 562e7e2b..9a6f680a 100644 --- a/resource_digitalocean_floating_ip.go +++ b/resource_digitalocean_floating_ip.go @@ -15,17 +15,23 @@ func resourceDigitalOceanFloatingIp() *schema.Resource { Delete: resourceDigitalOceanFloatingIpDelete, Schema: map[string]*schema.Schema{ - "region": &schema.Schema{ - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, - "ip_address": &schema.Schema{ Type: schema.TypeString, Optional: true, Computed: true, }, + + "region": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + + "droplet_id": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + }, }, } } @@ -34,9 +40,15 @@ func resourceDigitalOceanFloatingIpCreate(d *schema.ResourceData, meta interface client := meta.(*godo.Client) // Build up our creation options + opts := &godo.FloatingIPCreateRequest{} - opts := &godo.FloatingIPCreateRequest{ - Region: d.Get("region").(string), + if v, ok := d.GetOk("droplet_id"); ok { + log.Printf("[INFO] Found a droplet_id to try and attach to the FloatingIP") + opts.DropletID = v.(int) + } else if d.Get("region").(string) != "" { + opts.Region = d.Get("region").(string) + } else { + return fmt.Errorf("You must specify either a Droplet ID or a Region for a FloatingIP") } log.Printf("[DEBUG] FloatingIP Create: %#v", opts) @@ -60,6 +72,7 @@ func resourceDigitalOceanFloatingIpRead(d *schema.ResourceData, meta interface{} } d.Set("region", floatingIp.Region) + d.Set("ip_address", floatingIp.IP) return nil } diff --git a/resource_digitalocean_floating_ip_test.go b/resource_digitalocean_floating_ip_test.go index f489aa41..2c4a438c 100644 --- a/resource_digitalocean_floating_ip_test.go +++ b/resource_digitalocean_floating_ip_test.go @@ -9,7 +9,7 @@ import ( "github.com/hashicorp/terraform/terraform" ) -func TestAccDigitalOceanFloatingIP_Basic(t *testing.T) { +func TestAccDigitalOceanFloatingIP_Region(t *testing.T) { var floatingIP godo.FloatingIP resource.Test(t, resource.TestCase{ @@ -18,7 +18,7 @@ func TestAccDigitalOceanFloatingIP_Basic(t *testing.T) { CheckDestroy: testAccCheckDigitalOceanFloatingIPDestroy, Steps: []resource.TestStep{ resource.TestStep{ - Config: testAccCheckDigitalOceanFloatingIPConfig_basic, + Config: testAccCheckDigitalOceanFloatingIPConfig_region, Check: resource.ComposeTestCheckFunc( testAccCheckDigitalOceanFloatingIPExists("digitalocean_floating_ip.foobar", &floatingIP), resource.TestCheckResourceAttr( @@ -29,6 +29,26 @@ func TestAccDigitalOceanFloatingIP_Basic(t *testing.T) { }) } +func TestAccDigitalOceanFloatingIP_Droplet(t *testing.T) { + var floatingIP godo.FloatingIP + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckDigitalOceanFloatingIPDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckDigitalOceanFloatingIPConfig_droplet, + Check: resource.ComposeTestCheckFunc( + testAccCheckDigitalOceanFloatingIPExists("digitalocean_floating_ip.foobar", &floatingIP), + resource.TestCheckResourceAttr( + "digitalocean_floating_ip.foobar", "region", "sgp1"), + ), + }, + }, + }) +} + func testAccCheckDigitalOceanFloatingIPDestroy(s *terraform.State) error { client := testAccProvider.Meta().(*godo.Client) @@ -79,7 +99,22 @@ func testAccCheckDigitalOceanFloatingIPExists(n string, floatingIP *godo.Floatin } } -var testAccCheckDigitalOceanFloatingIPConfig_basic = ` +var testAccCheckDigitalOceanFloatingIPConfig_region = ` resource "digitalocean_floating_ip" "foobar" { region = "nyc3" }` + +var testAccCheckDigitalOceanFloatingIPConfig_droplet = ` + +resource "digitalocean_droplet" "foobar" { + name = "baz" + size = "1gb" + image = "centos-5-8-x32" + region = "sgp1" + ipv6 = true + private_networking = true +} + +resource "digitalocean_floating_ip" "foobar" { + droplet_id = "${digitalocean_droplet.foobar.id}" +}`