diff --git a/builtin/providers/digitalocean/resource_digitalocean_floating_ip.go b/builtin/providers/digitalocean/resource_digitalocean_floating_ip.go index 562e7e2b2..9a6f680ae 100644 --- a/builtin/providers/digitalocean/resource_digitalocean_floating_ip.go +++ b/builtin/providers/digitalocean/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/builtin/providers/digitalocean/resource_digitalocean_floating_ip_test.go b/builtin/providers/digitalocean/resource_digitalocean_floating_ip_test.go index f489aa412..2c4a438cf 100644 --- a/builtin/providers/digitalocean/resource_digitalocean_floating_ip_test.go +++ b/builtin/providers/digitalocean/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}" +}` diff --git a/website/source/docs/providers/do/r/floating_ip.html.markdown b/website/source/docs/providers/do/r/floating_ip.html.markdown new file mode 100644 index 000000000..df29a3cff --- /dev/null +++ b/website/source/docs/providers/do/r/floating_ip.html.markdown @@ -0,0 +1,43 @@ +--- +layout: "digitalocean" +page_title: "DigitalOcean: digitalocean_floating_ip" +sidebar_current: "docs-do-resource-floating-ip" +description: |- + Provides a DigitalOcean Floating IP resource. +--- + +# digitalocean\_floating_ip + +Provides a DigitalOcean Floating IP to represent a publicly-accessible static IP addresses that can be mapped to one of your Droplets. + +## Example Usage + +``` +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}" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `region` - (Optional) The region that the Floating IP is reserved to. +* `droplet_id` - (Optional) The ID of Droplet that the Floating IP will be assigned to. + +~> **NOTE:** A Floating IP can be assigned to a region OR a droplet_id. If both region AND droplet_id are specified, then the Floating IP will be assigned to the droplet and use that region + +## Attributes Reference + +The following attributes are exported: + +* `ip_address` - The IP Address of the resource diff --git a/website/source/layouts/digitalocean.erb b/website/source/layouts/digitalocean.erb index 42ede7db3..f3f4306df 100644 --- a/website/source/layouts/digitalocean.erb +++ b/website/source/layouts/digitalocean.erb @@ -21,6 +21,10 @@ digitalocean_droplet +