From f29196ec725a41901ba89ab7c9ed353114f051a1 Mon Sep 17 00:00:00 2001 From: stack72 Date: Wed, 4 Nov 2015 10:38:26 +0000 Subject: [PATCH] Adding the first pass of the work to get a floatingip assigned to a region --- provider.go | 9 +-- resource_digitalocean_floating_ip.go | 78 +++++++++++++++++++++ resource_digitalocean_floating_ip_test.go | 85 +++++++++++++++++++++++ 3 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 resource_digitalocean_floating_ip.go create mode 100644 resource_digitalocean_floating_ip_test.go diff --git a/provider.go b/provider.go index 080716e2..be197a32 100644 --- a/provider.go +++ b/provider.go @@ -18,10 +18,11 @@ func Provider() terraform.ResourceProvider { }, ResourcesMap: map[string]*schema.Resource{ - "digitalocean_domain": resourceDigitalOceanDomain(), - "digitalocean_droplet": resourceDigitalOceanDroplet(), - "digitalocean_record": resourceDigitalOceanRecord(), - "digitalocean_ssh_key": resourceDigitalOceanSSHKey(), + "digitalocean_domain": resourceDigitalOceanDomain(), + "digitalocean_droplet": resourceDigitalOceanDroplet(), + "digitalocean_floating_ip": resourceDigitalOceanFloatingIp(), + "digitalocean_record": resourceDigitalOceanRecord(), + "digitalocean_ssh_key": resourceDigitalOceanSSHKey(), }, ConfigureFunc: providerConfigure, diff --git a/resource_digitalocean_floating_ip.go b/resource_digitalocean_floating_ip.go new file mode 100644 index 00000000..562e7e2b --- /dev/null +++ b/resource_digitalocean_floating_ip.go @@ -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 +} diff --git a/resource_digitalocean_floating_ip_test.go b/resource_digitalocean_floating_ip_test.go new file mode 100644 index 00000000..f489aa41 --- /dev/null +++ b/resource_digitalocean_floating_ip_test.go @@ -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" +}`