Adding the first pass of the work to get a floatingip assigned to a region
This commit is contained in:
parent
3416178b5b
commit
f29196ec72
|
@ -18,10 +18,11 @@ func Provider() terraform.ResourceProvider {
|
||||||
},
|
},
|
||||||
|
|
||||||
ResourcesMap: map[string]*schema.Resource{
|
ResourcesMap: map[string]*schema.Resource{
|
||||||
"digitalocean_domain": resourceDigitalOceanDomain(),
|
"digitalocean_domain": resourceDigitalOceanDomain(),
|
||||||
"digitalocean_droplet": resourceDigitalOceanDroplet(),
|
"digitalocean_droplet": resourceDigitalOceanDroplet(),
|
||||||
"digitalocean_record": resourceDigitalOceanRecord(),
|
"digitalocean_floating_ip": resourceDigitalOceanFloatingIp(),
|
||||||
"digitalocean_ssh_key": resourceDigitalOceanSSHKey(),
|
"digitalocean_record": resourceDigitalOceanRecord(),
|
||||||
|
"digitalocean_ssh_key": resourceDigitalOceanSSHKey(),
|
||||||
},
|
},
|
||||||
|
|
||||||
ConfigureFunc: providerConfigure,
|
ConfigureFunc: providerConfigure,
|
||||||
|
|
|
@ -0,0 +1,78 @@
|
||||||
|
package digitalocean
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/digitalocean/godo"
|
||||||
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
func resourceDigitalOceanFloatingIp() *schema.Resource {
|
||||||
|
return &schema.Resource{
|
||||||
|
Create: resourceDigitalOceanFloatingIpCreate,
|
||||||
|
Read: resourceDigitalOceanFloatingIpRead,
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func resourceDigitalOceanFloatingIpCreate(d *schema.ResourceData, meta interface{}) error {
|
||||||
|
client := meta.(*godo.Client)
|
||||||
|
|
||||||
|
// Build up our creation options
|
||||||
|
|
||||||
|
opts := &godo.FloatingIPCreateRequest{
|
||||||
|
Region: d.Get("region").(string),
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("[DEBUG] FloatingIP Create: %#v", opts)
|
||||||
|
floatingIp, _, err := client.FloatingIPs.Create(opts)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Error creating FloatingIP: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
d.SetId(floatingIp.IP)
|
||||||
|
log.Printf("[INFO] Floating IP: %s", floatingIp.IP)
|
||||||
|
|
||||||
|
return resourceDigitalOceanFloatingIpRead(d, meta)
|
||||||
|
}
|
||||||
|
|
||||||
|
func resourceDigitalOceanFloatingIpRead(d *schema.ResourceData, meta interface{}) error {
|
||||||
|
client := meta.(*godo.Client)
|
||||||
|
|
||||||
|
floatingIp, _, err := client.FloatingIPs.Get(d.Id())
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Error retrieving FloatingIP: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
d.Set("region", floatingIp.Region)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func resourceDigitalOceanFloatingIpDelete(d *schema.ResourceData, meta interface{}) error {
|
||||||
|
client := meta.(*godo.Client)
|
||||||
|
|
||||||
|
log.Printf("[INFO] Deleting FloatingIP: %s", d.Id())
|
||||||
|
_, err := client.FloatingIPs.Delete(d.Id())
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Error deleting FloatingIP: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
d.SetId("")
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -0,0 +1,85 @@
|
||||||
|
package digitalocean
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/digitalocean/godo"
|
||||||
|
"github.com/hashicorp/terraform/helper/resource"
|
||||||
|
"github.com/hashicorp/terraform/terraform"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAccDigitalOceanFloatingIP_Basic(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_basic,
|
||||||
|
Check: resource.ComposeTestCheckFunc(
|
||||||
|
testAccCheckDigitalOceanFloatingIPExists("digitalocean_floating_ip.foobar", &floatingIP),
|
||||||
|
resource.TestCheckResourceAttr(
|
||||||
|
"digitalocean_floating_ip.foobar", "region", "nyc3"),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func testAccCheckDigitalOceanFloatingIPDestroy(s *terraform.State) error {
|
||||||
|
client := testAccProvider.Meta().(*godo.Client)
|
||||||
|
|
||||||
|
for _, rs := range s.RootModule().Resources {
|
||||||
|
if rs.Type != "digitalocean_floating_ip" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to find the key
|
||||||
|
_, _, err := client.FloatingIPs.Get(rs.Primary.ID)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
fmt.Errorf("Floating IP still exists")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func testAccCheckDigitalOceanFloatingIPExists(n string, floatingIP *godo.FloatingIP) resource.TestCheckFunc {
|
||||||
|
return func(s *terraform.State) error {
|
||||||
|
rs, ok := s.RootModule().Resources[n]
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("Not found: %s", n)
|
||||||
|
}
|
||||||
|
|
||||||
|
if rs.Primary.ID == "" {
|
||||||
|
return fmt.Errorf("No Record ID is set")
|
||||||
|
}
|
||||||
|
|
||||||
|
client := testAccProvider.Meta().(*godo.Client)
|
||||||
|
|
||||||
|
// Try to find the FloatingIP
|
||||||
|
foundFloatingIP, _, err := client.FloatingIPs.Get(rs.Primary.ID)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if foundFloatingIP.IP != rs.Primary.ID {
|
||||||
|
return fmt.Errorf("Record not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
*floatingIP = *foundFloatingIP
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var testAccCheckDigitalOceanFloatingIPConfig_basic = `
|
||||||
|
resource "digitalocean_floating_ip" "foobar" {
|
||||||
|
region = "nyc3"
|
||||||
|
}`
|
Loading…
Reference in New Issue