2014-07-24 19:24:02 +02:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2017-05-15 15:54:16 +02:00
|
|
|
"context"
|
2014-07-24 19:24:02 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
"github.com/digitalocean/godo"
|
2014-09-09 22:42:06 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-07-24 19:24:02 +02:00
|
|
|
)
|
|
|
|
|
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,
|
2016-06-29 17:10:56 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2014-09-09 22:42:06 +02:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-01-20 18:47:54 +01:00
|
|
|
"name": {
|
2014-09-09 22:42:06 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2017-01-20 18:47:54 +01:00
|
|
|
"ip_address": {
|
2014-09-09 22:42:06 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
func resourceDigitalOceanDomainCreate(d *schema.ResourceData, meta interface{}) error {
|
2015-09-27 06:06:51 +02:00
|
|
|
client := meta.(*godo.Client)
|
2014-07-24 19:24:02 +02:00
|
|
|
|
|
|
|
// Build up our creation options
|
2015-09-27 06:06:51 +02:00
|
|
|
|
|
|
|
opts := &godo.DomainCreateRequest{
|
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)
|
2017-05-15 15:54:16 +02:00
|
|
|
domain, _, err := client.Domains.Create(context.Background(), 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
|
|
|
}
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
d.SetId(domain.Name)
|
|
|
|
log.Printf("[INFO] Domain Name: %s", domain.Name)
|
2014-07-24 19:24:02 +02:00
|
|
|
|
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 {
|
2015-09-27 06:06:51 +02:00
|
|
|
client := meta.(*godo.Client)
|
2014-07-24 19:24:02 +02:00
|
|
|
|
2017-05-15 15:54:16 +02:00
|
|
|
domain, resp, err := client.Domains.Get(context.Background(), 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
|
2015-09-11 20:56:20 +02:00
|
|
|
// successfully gone
|
2015-12-06 12:52:35 +01:00
|
|
|
if resp.StatusCode == 404 {
|
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 {
|
2015-09-27 06:06:51 +02:00
|
|
|
client := meta.(*godo.Client)
|
2014-11-17 18:55:45 +01:00
|
|
|
|
|
|
|
log.Printf("[INFO] Deleting Domain: %s", d.Id())
|
2017-05-15 15:54:16 +02:00
|
|
|
_, err := client.Domains.Delete(context.Background(), d.Id())
|
2014-11-17 18:55:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting Domain: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|