github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/digitalocean/resource_digitalocean_record.go (about) 1 package digitalocean 2 3 import ( 4 "fmt" 5 "log" 6 "strconv" 7 "strings" 8 9 "github.com/digitalocean/godo" 10 "github.com/hashicorp/terraform/helper/schema" 11 ) 12 13 func resourceDigitalOceanRecord() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceDigitalOceanRecordCreate, 16 Read: resourceDigitalOceanRecordRead, 17 Update: resourceDigitalOceanRecordUpdate, 18 Delete: resourceDigitalOceanRecordDelete, 19 20 Schema: map[string]*schema.Schema{ 21 "type": &schema.Schema{ 22 Type: schema.TypeString, 23 Required: true, 24 ForceNew: true, 25 }, 26 27 "domain": &schema.Schema{ 28 Type: schema.TypeString, 29 Required: true, 30 ForceNew: true, 31 }, 32 33 "name": &schema.Schema{ 34 Type: schema.TypeString, 35 Optional: true, 36 }, 37 38 "port": &schema.Schema{ 39 Type: schema.TypeString, 40 Optional: true, 41 Computed: true, 42 ForceNew: true, 43 }, 44 45 "priority": &schema.Schema{ 46 Type: schema.TypeString, 47 Optional: true, 48 Computed: true, 49 ForceNew: true, 50 }, 51 52 "weight": &schema.Schema{ 53 Type: schema.TypeString, 54 Optional: true, 55 Computed: true, 56 ForceNew: true, 57 }, 58 59 "value": &schema.Schema{ 60 Type: schema.TypeString, 61 Optional: true, 62 Computed: true, 63 ForceNew: true, 64 }, 65 }, 66 } 67 } 68 69 func resourceDigitalOceanRecordCreate(d *schema.ResourceData, meta interface{}) error { 70 client := meta.(*godo.Client) 71 72 newRecord := godo.DomainRecordEditRequest{ 73 Type: d.Get("type").(string), 74 Name: d.Get("name").(string), 75 Data: d.Get("value").(string), 76 } 77 78 var err error 79 if priority := d.Get("priority").(string); priority != "" { 80 newRecord.Priority, err = strconv.Atoi(priority) 81 if err != nil { 82 return fmt.Errorf("Failed to parse priority as an integer: %v", err) 83 } 84 } 85 if port := d.Get("port").(string); port != "" { 86 newRecord.Port, err = strconv.Atoi(port) 87 if err != nil { 88 return fmt.Errorf("Failed to parse port as an integer: %v", err) 89 } 90 } 91 if weight := d.Get("weight").(string); weight != "" { 92 newRecord.Weight, err = strconv.Atoi(weight) 93 if err != nil { 94 return fmt.Errorf("Failed to parse weight as an integer: %v", err) 95 } 96 } 97 98 log.Printf("[DEBUG] record create configuration: %#v", newRecord) 99 rec, _, err := client.Domains.CreateRecord(d.Get("domain").(string), &newRecord) 100 if err != nil { 101 return fmt.Errorf("Failed to create record: %s", err) 102 } 103 104 d.SetId(strconv.Itoa(rec.ID)) 105 log.Printf("[INFO] Record ID: %s", d.Id()) 106 107 return resourceDigitalOceanRecordRead(d, meta) 108 } 109 110 func resourceDigitalOceanRecordRead(d *schema.ResourceData, meta interface{}) error { 111 client := meta.(*godo.Client) 112 domain := d.Get("domain").(string) 113 id, err := strconv.Atoi(d.Id()) 114 if err != nil { 115 return fmt.Errorf("invalid record ID: %v", err) 116 } 117 118 rec, _, err := client.Domains.Record(domain, id) 119 if err != nil { 120 // If the record is somehow already destroyed, mark as 121 // successfully gone 122 if strings.Contains(err.Error(), "404 Not Found") { 123 d.SetId("") 124 return nil 125 } 126 127 return err 128 } 129 130 // Update response data for records with domain value 131 if t := rec.Type; t == "CNAME" || t == "MX" || t == "NS" || t == "SRV" { 132 // Append dot to response if resource value is absolute 133 if value := d.Get("value").(string); strings.HasSuffix(value, ".") { 134 rec.Data += "." 135 // If resource value ends with current domain, make response data absolute 136 if strings.HasSuffix(value, domain+".") { 137 rec.Data += domain + "." 138 } 139 } 140 } 141 142 d.Set("name", rec.Name) 143 d.Set("type", rec.Type) 144 d.Set("value", rec.Data) 145 d.Set("weight", strconv.Itoa(rec.Weight)) 146 d.Set("priority", strconv.Itoa(rec.Priority)) 147 d.Set("port", strconv.Itoa(rec.Port)) 148 149 return nil 150 } 151 152 func resourceDigitalOceanRecordUpdate(d *schema.ResourceData, meta interface{}) error { 153 client := meta.(*godo.Client) 154 155 domain := d.Get("domain").(string) 156 id, err := strconv.Atoi(d.Id()) 157 if err != nil { 158 return fmt.Errorf("invalid record ID: %v", err) 159 } 160 161 var editRecord godo.DomainRecordEditRequest 162 if v, ok := d.GetOk("name"); ok { 163 editRecord.Name = v.(string) 164 } 165 166 log.Printf("[DEBUG] record update configuration: %#v", editRecord) 167 _, _, err = client.Domains.EditRecord(domain, id, &editRecord) 168 if err != nil { 169 return fmt.Errorf("Failed to update record: %s", err) 170 } 171 172 return resourceDigitalOceanRecordRead(d, meta) 173 } 174 175 func resourceDigitalOceanRecordDelete(d *schema.ResourceData, meta interface{}) error { 176 client := meta.(*godo.Client) 177 178 domain := d.Get("domain").(string) 179 id, err := strconv.Atoi(d.Id()) 180 if err != nil { 181 return fmt.Errorf("invalid record ID: %v", err) 182 } 183 184 log.Printf("[INFO] Deleting record: %s, %d", domain, id) 185 186 _, err = client.Domains.DeleteRecord(domain, id) 187 if err != nil { 188 // If the record is somehow already destroyed, mark as 189 // successfully gone 190 if strings.Contains(err.Error(), "404 Not Found") { 191 return nil 192 } 193 194 return fmt.Errorf("Error deleting record: %s", err) 195 } 196 197 return nil 198 }