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