Skip to content

Commit 7bda855

Browse files
committed
Adding the work to assign a Floating IP to a Droplet
1 parent 9cf1c29 commit 7bda855

4 files changed

Lines changed: 105 additions & 10 deletions

File tree

builtin/providers/digitalocean/resource_digitalocean_floating_ip.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,22 @@ func resourceDigitalOceanFloatingIp() *schema.Resource {
1515
Delete: resourceDigitalOceanFloatingIpDelete,
1616

1717
Schema: map[string]*schema.Schema{
18+
"ip_address": &schema.Schema{
19+
Type: schema.TypeString,
20+
Optional: true,
21+
Computed: true,
22+
},
23+
1824
"region": &schema.Schema{
1925
Type: schema.TypeString,
20-
Required: true,
26+
Optional: true,
2127
ForceNew: true,
2228
},
2329

24-
"ip_address": &schema.Schema{
25-
Type: schema.TypeString,
30+
"droplet_id": &schema.Schema{
31+
Type: schema.TypeInt,
2632
Optional: true,
27-
Computed: true,
33+
ForceNew: true,
2834
},
2935
},
3036
}
@@ -34,9 +40,15 @@ func resourceDigitalOceanFloatingIpCreate(d *schema.ResourceData, meta interface
3440
client := meta.(*godo.Client)
3541

3642
// Build up our creation options
37-
38-
opts := &godo.FloatingIPCreateRequest{
39-
Region: d.Get("region").(string),
43+
opts := &godo.FloatingIPCreateRequest{}
44+
45+
if v, ok := d.GetOk("droplet_id"); ok {
46+
log.Printf("[INFO] Found a droplet_id to try and attach to the FloatingIP")
47+
opts.DropletID = v.(int)
48+
} else if d.Get("region").(string) != "" {
49+
opts.Region = d.Get("region").(string)
50+
} else {
51+
return fmt.Errorf("You must specify either a Droplet ID or a Region for a FloatingIP")
4052
}
4153

4254
log.Printf("[DEBUG] FloatingIP Create: %#v", opts)
@@ -60,6 +72,7 @@ func resourceDigitalOceanFloatingIpRead(d *schema.ResourceData, meta interface{}
6072
}
6173

6274
d.Set("region", floatingIp.Region)
75+
d.Set("ip_address", floatingIp.IP)
6376

6477
return nil
6578
}

builtin/providers/digitalocean/resource_digitalocean_floating_ip_test.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/hashicorp/terraform/terraform"
1010
)
1111

12-
func TestAccDigitalOceanFloatingIP_Basic(t *testing.T) {
12+
func TestAccDigitalOceanFloatingIP_Region(t *testing.T) {
1313
var floatingIP godo.FloatingIP
1414

1515
resource.Test(t, resource.TestCase{
@@ -18,7 +18,7 @@ func TestAccDigitalOceanFloatingIP_Basic(t *testing.T) {
1818
CheckDestroy: testAccCheckDigitalOceanFloatingIPDestroy,
1919
Steps: []resource.TestStep{
2020
resource.TestStep{
21-
Config: testAccCheckDigitalOceanFloatingIPConfig_basic,
21+
Config: testAccCheckDigitalOceanFloatingIPConfig_region,
2222
Check: resource.ComposeTestCheckFunc(
2323
testAccCheckDigitalOceanFloatingIPExists("digitalocean_floating_ip.foobar", &floatingIP),
2424
resource.TestCheckResourceAttr(
@@ -29,6 +29,26 @@ func TestAccDigitalOceanFloatingIP_Basic(t *testing.T) {
2929
})
3030
}
3131

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", "sgp1"),
46+
),
47+
},
48+
},
49+
})
50+
}
51+
3252
func testAccCheckDigitalOceanFloatingIPDestroy(s *terraform.State) error {
3353
client := testAccProvider.Meta().(*godo.Client)
3454

@@ -79,7 +99,22 @@ func testAccCheckDigitalOceanFloatingIPExists(n string, floatingIP *godo.Floatin
7999
}
80100
}
81101

82-
var testAccCheckDigitalOceanFloatingIPConfig_basic = `
102+
var testAccCheckDigitalOceanFloatingIPConfig_region = `
83103
resource "digitalocean_floating_ip" "foobar" {
84104
region = "nyc3"
85105
}`
106+
107+
var testAccCheckDigitalOceanFloatingIPConfig_droplet = `
108+
109+
resource "digitalocean_droplet" "foobar" {
110+
name = "baz"
111+
size = "1gb"
112+
image = "centos-5-8-x32"
113+
region = "sgp1"
114+
ipv6 = true
115+
private_networking = true
116+
}
117+
118+
resource "digitalocean_floating_ip" "foobar" {
119+
droplet_id = "${digitalocean_droplet.foobar.id}"
120+
}`
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
layout: "digitalocean"
3+
page_title: "DigitalOcean: digitalocean_floating_ip"
4+
sidebar_current: "docs-do-resource-floating-ip"
5+
description: |-
6+
Provides a DigitalOcean Floating IP resource.
7+
---
8+
9+
# digitalocean\_floating_ip
10+
11+
Provides a DigitalOcean Floating IP to represent a publicly-accessible static IP addresses that can be mapped to one of your Droplets.
12+
13+
## Example Usage
14+
15+
```
16+
resource "digitalocean_droplet" "foobar" {
17+
name = "baz"
18+
size = "1gb"
19+
image = "centos-5-8-x32"
20+
region = "sgp1"
21+
ipv6 = true
22+
private_networking = true
23+
}
24+
25+
resource "digitalocean_floating_ip" "foobar" {
26+
droplet_id = "${digitalocean_droplet.foobar.id}"
27+
}
28+
```
29+
30+
## Argument Reference
31+
32+
The following arguments are supported:
33+
34+
* `region` - (Optional) The region that the Floating IP is reserved to.
35+
* `droplet_id` - (Optional) The ID of Droplet that the Floating IP will be assigned to.
36+
37+
~> **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
38+
39+
## Attributes Reference
40+
41+
The following attributes are exported:
42+
43+
* `ip_address` - The IP Address of the resource

website/source/layouts/digitalocean.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
<a href="/docs/providers/do/r/droplet.html">digitalocean_droplet</a>
2222
</li>
2323

24+
<li<%= sidebar_current("docs-do-resource-floating-ip") %>>
25+
<a href="/docs/providers/do/r/floating_ip.html">digitalocean_floating_ip</a>
26+
</li>
27+
2428
<li<%= sidebar_current("docs-do-resource-record") %>>
2529
<a href="/docs/providers/do/r/record.html">digitalocean_record</a>
2630
</li>

0 commit comments

Comments
 (0)