2014-07-24 19:24:02 +02:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2014-09-09 22:36:47 +02:00
|
|
|
"strings"
|
2014-07-24 19:24:02 +02:00
|
|
|
|
2014-09-09 22:42:06 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-07-24 19:24:02 +02:00
|
|
|
"github.com/pearkes/digitalocean"
|
|
|
|
)
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
func resourceDigitalOceanDomain() *schema.Resource {
|
2014-09-09 22:42:06 +02:00
|
|
|
return &schema.Resource{
|
2014-11-17 18:55:45 +01:00
|
|
|
Create: resourceDigitalOceanDomainCreate,
|
|
|
|
Read: resourceDigitalOceanDomainRead,
|
|
|
|
Delete: resourceDigitalOceanDomainDelete,
|
2014-09-09 22:42:06 +02:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"ip_address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
func resourceDigitalOceanDomainCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*digitalocean.Client)
|
2014-07-24 19:24:02 +02:00
|
|
|
|
|
|
|
// Build up our creation options
|
2014-11-17 18:55:45 +01:00
|
|
|
opts := &digitalocean.CreateDomain{
|
2014-09-09 22:42:06 +02:00
|
|
|
Name: d.Get("name").(string),
|
|
|
|
IPAddress: d.Get("ip_address").(string),
|
2014-07-24 19:24:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Domain create configuration: %#v", opts)
|
2014-11-17 18:55:45 +01:00
|
|
|
name, err := client.CreateDomain(opts)
|
2014-07-24 19:24:02 +02:00
|
|
|
if err != nil {
|
2014-09-09 22:42:06 +02:00
|
|
|
return fmt.Errorf("Error creating Domain: %s", err)
|
2014-07-24 19:24:02 +02:00
|
|
|
}
|
|
|
|
|
2014-09-09 22:42:06 +02:00
|
|
|
d.SetId(name)
|
2014-07-24 19:24:02 +02:00
|
|
|
log.Printf("[INFO] Domain Name: %s", name)
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
return resourceDigitalOceanDomainRead(d, meta)
|
2014-07-24 19:24:02 +02:00
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
func resourceDigitalOceanDomainRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*digitalocean.Client)
|
2014-07-24 19:24:02 +02:00
|
|
|
|
2014-09-09 22:42:06 +02:00
|
|
|
domain, err := client.RetrieveDomain(d.Id())
|
2014-07-24 19:24:02 +02:00
|
|
|
if err != nil {
|
2014-09-09 22:36:47 +02:00
|
|
|
// If the domain is somehow already destroyed, mark as
|
|
|
|
// succesfully gone
|
|
|
|
if strings.Contains(err.Error(), "404 Not Found") {
|
2014-09-09 22:42:06 +02:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
2014-09-09 22:36:47 +02:00
|
|
|
}
|
|
|
|
|
2014-09-09 22:42:06 +02:00
|
|
|
return fmt.Errorf("Error retrieving domain: %s", err)
|
2014-07-24 19:24:02 +02:00
|
|
|
}
|
|
|
|
|
2014-09-09 22:42:06 +02:00
|
|
|
d.Set("name", domain.Name)
|
2014-07-24 19:24:02 +02:00
|
|
|
|
2014-09-09 22:42:06 +02:00
|
|
|
return nil
|
2014-07-24 19:24:02 +02:00
|
|
|
}
|
2014-11-17 18:55:45 +01:00
|
|
|
|
|
|
|
func resourceDigitalOceanDomainDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*digitalocean.Client)
|
|
|
|
|
|
|
|
log.Printf("[INFO] Deleting Domain: %s", d.Id())
|
|
|
|
err := client.DestroyDomain(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting Domain: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|