terraform/builtin/providers/digitalocean/resource_digitalocean_domai...

90 lines
1.8 KiB
Go
Raw Normal View History

2014-07-24 19:24:02 +02:00
package digitalocean
import (
"fmt"
"log"
"strings"
2014-07-24 19:24:02 +02:00
"github.com/hashicorp/terraform/helper/schema"
2014-07-24 19:24:02 +02:00
"github.com/pearkes/digitalocean"
)
func resourceDomain() *schema.Resource {
return &schema.Resource{
Create: resourceDomainCreate,
Read: resourceDomainRead,
Delete: resourceDomainDelete,
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,
},
},
}
}
func resourceDomainCreate(d *schema.ResourceData, meta interface{}) error {
2014-07-24 19:24:02 +02:00
p := meta.(*ResourceProvider)
client := p.client
// Build up our creation options
opts := digitalocean.CreateDomain{
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)
name, err := client.CreateDomain(&opts)
if err != nil {
return fmt.Errorf("Error creating Domain: %s", err)
2014-07-24 19:24:02 +02:00
}
d.SetId(name)
2014-07-24 19:24:02 +02:00
log.Printf("[INFO] Domain Name: %s", name)
return nil
2014-07-24 19:24:02 +02:00
}
func resourceDomainDelete(d *schema.ResourceData, meta interface{}) error {
2014-07-24 19:24:02 +02:00
p := meta.(*ResourceProvider)
client := p.client
log.Printf("[INFO] Deleting Domain: %s", d.Id())
err := client.DestroyDomain(d.Id())
2014-07-24 19:24:02 +02:00
if err != nil {
return fmt.Errorf("Error deleting Domain: %s", err)
}
d.SetId("")
2014-07-24 19:24:02 +02:00
return nil
}
func resourceDomainRead(d *schema.ResourceData, meta interface{}) error {
2014-07-24 19:24:02 +02:00
p := meta.(*ResourceProvider)
client := p.client
domain, err := client.RetrieveDomain(d.Id())
2014-07-24 19:24:02 +02:00
if err != nil {
// If the domain is somehow already destroyed, mark as
// succesfully gone
if strings.Contains(err.Error(), "404 Not Found") {
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving domain: %s", err)
2014-07-24 19:24:02 +02:00
}
d.Set("name", domain.Name)
2014-07-24 19:24:02 +02:00
return nil
2014-07-24 19:24:02 +02:00
}