Adding the work to assign a Floating IP to a Droplet
This commit is contained in:
parent
f29196ec72
commit
f08f01626f
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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}"
|
||||
}`
|
||||
|
|
Loading…
Reference in New Issue