github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/digitalocean/resource_digitalocean_domain.go (about) 1 package digitalocean 2 3 import ( 4 "fmt" 5 "log" 6 "strings" 7 8 "github.com/digitalocean/godo" 9 "github.com/hashicorp/terraform/helper/schema" 10 ) 11 12 func resourceDigitalOceanDomain() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceDigitalOceanDomainCreate, 15 Read: resourceDigitalOceanDomainRead, 16 Delete: resourceDigitalOceanDomainDelete, 17 18 Schema: map[string]*schema.Schema{ 19 "name": &schema.Schema{ 20 Type: schema.TypeString, 21 Required: true, 22 ForceNew: true, 23 }, 24 25 "ip_address": &schema.Schema{ 26 Type: schema.TypeString, 27 Required: true, 28 ForceNew: true, 29 }, 30 }, 31 } 32 } 33 34 func resourceDigitalOceanDomainCreate(d *schema.ResourceData, meta interface{}) error { 35 client := meta.(*godo.Client) 36 37 // Build up our creation options 38 39 opts := &godo.DomainCreateRequest{ 40 Name: d.Get("name").(string), 41 IPAddress: d.Get("ip_address").(string), 42 } 43 44 log.Printf("[DEBUG] Domain create configuration: %#v", opts) 45 domain, _, err := client.Domains.Create(opts) 46 if err != nil { 47 return fmt.Errorf("Error creating Domain: %s", err) 48 } 49 50 d.SetId(domain.Name) 51 log.Printf("[INFO] Domain Name: %s", domain.Name) 52 53 return resourceDigitalOceanDomainRead(d, meta) 54 } 55 56 func resourceDigitalOceanDomainRead(d *schema.ResourceData, meta interface{}) error { 57 client := meta.(*godo.Client) 58 59 domain, _, err := client.Domains.Get(d.Id()) 60 if err != nil { 61 // If the domain is somehow already destroyed, mark as 62 // successfully gone 63 if strings.Contains(err.Error(), "404 Not Found") { 64 d.SetId("") 65 return nil 66 } 67 68 return fmt.Errorf("Error retrieving domain: %s", err) 69 } 70 71 d.Set("name", domain.Name) 72 73 return nil 74 } 75 76 func resourceDigitalOceanDomainDelete(d *schema.ResourceData, meta interface{}) error { 77 client := meta.(*godo.Client) 78 79 log.Printf("[INFO] Deleting Domain: %s", d.Id()) 80 _, err := client.Domains.Delete(d.Id()) 81 if err != nil { 82 return fmt.Errorf("Error deleting Domain: %s", err) 83 } 84 85 d.SetId("") 86 return nil 87 }