Adding the work to assign a Floating IP to a Droplet

This commit is contained in:
stack72 2015-11-04 11:37:33 +00:00
parent 9cf1c2943c
commit 7bda855590
4 changed files with 106 additions and 11 deletions

View File

@ -15,17 +15,23 @@ func resourceDigitalOceanFloatingIp() *schema.Resource {
Delete: resourceDigitalOceanFloatingIpDelete, Delete: resourceDigitalOceanFloatingIpDelete,
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"region": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"ip_address": &schema.Schema{ "ip_address": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
Computed: 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) client := meta.(*godo.Client)
// Build up our creation options // Build up our creation options
opts := &godo.FloatingIPCreateRequest{}
opts := &godo.FloatingIPCreateRequest{ if v, ok := d.GetOk("droplet_id"); ok {
Region: d.Get("region").(string), 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) 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("region", floatingIp.Region)
d.Set("ip_address", floatingIp.IP)
return nil return nil
} }

View File

@ -9,7 +9,7 @@ import (
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
) )
func TestAccDigitalOceanFloatingIP_Basic(t *testing.T) { func TestAccDigitalOceanFloatingIP_Region(t *testing.T) {
var floatingIP godo.FloatingIP var floatingIP godo.FloatingIP
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
@ -18,7 +18,7 @@ func TestAccDigitalOceanFloatingIP_Basic(t *testing.T) {
CheckDestroy: testAccCheckDigitalOceanFloatingIPDestroy, CheckDestroy: testAccCheckDigitalOceanFloatingIPDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAccCheckDigitalOceanFloatingIPConfig_basic, Config: testAccCheckDigitalOceanFloatingIPConfig_region,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanFloatingIPExists("digitalocean_floating_ip.foobar", &floatingIP), testAccCheckDigitalOceanFloatingIPExists("digitalocean_floating_ip.foobar", &floatingIP),
resource.TestCheckResourceAttr( 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 { func testAccCheckDigitalOceanFloatingIPDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*godo.Client) 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" { resource "digitalocean_floating_ip" "foobar" {
region = "nyc3" 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}"
}`

View File

@ -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

View File

@ -21,6 +21,10 @@
<a href="/docs/providers/do/r/droplet.html">digitalocean_droplet</a> <a href="/docs/providers/do/r/droplet.html">digitalocean_droplet</a>
</li> </li>
<li<%= sidebar_current("docs-do-resource-floating-ip") %>>
<a href="/docs/providers/do/r/floating_ip.html">digitalocean_floating_ip</a>
</li>
<li<%= sidebar_current("docs-do-resource-record") %>> <li<%= sidebar_current("docs-do-resource-record") %>>
<a href="/docs/providers/do/r/record.html">digitalocean_record</a> <a href="/docs/providers/do/r/record.html">digitalocean_record</a>
</li> </li>