github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/dme/resource_dme_record.go (about) 1 package dme 2 3 import ( 4 "fmt" 5 "log" 6 "strings" 7 8 "github.com/hashicorp/terraform/helper/schema" 9 "github.com/soniah/dnsmadeeasy" 10 ) 11 12 func resourceDMERecord() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceDMERecordCreate, 15 Read: resourceDMERecordRead, 16 Update: resourceDMERecordUpdate, 17 Delete: resourceDMERecordDelete, 18 19 Schema: map[string]*schema.Schema{ 20 // Use recordid for TF ID. 21 "domainid": &schema.Schema{ 22 Type: schema.TypeString, 23 Required: true, 24 }, 25 "name": &schema.Schema{ 26 Type: schema.TypeString, 27 Required: true, 28 }, 29 "type": &schema.Schema{ 30 Type: schema.TypeString, 31 Required: true, 32 }, 33 "value": &schema.Schema{ 34 Type: schema.TypeString, 35 Required: true, 36 StateFunc: func(value interface{}) string { 37 return strings.ToLower(value.(string)) 38 }, 39 }, 40 "ttl": &schema.Schema{ 41 Type: schema.TypeInt, 42 Optional: true, 43 }, 44 "mxLevel": &schema.Schema{ 45 Type: schema.TypeInt, 46 Optional: true, 47 }, 48 "weight": &schema.Schema{ 49 Type: schema.TypeInt, 50 Optional: true, 51 }, 52 "priority": &schema.Schema{ 53 Type: schema.TypeInt, 54 Optional: true, 55 }, 56 "port": &schema.Schema{ 57 Type: schema.TypeInt, 58 Optional: true, 59 }, 60 "keywords": &schema.Schema{ 61 Type: schema.TypeString, 62 Optional: true, 63 }, 64 "title": &schema.Schema{ 65 Type: schema.TypeString, 66 Optional: true, 67 }, 68 "hardLink": &schema.Schema{ 69 Type: schema.TypeBool, 70 Optional: true, 71 }, 72 "redirectType": &schema.Schema{ 73 Type: schema.TypeString, 74 Optional: true, 75 }, 76 "description": &schema.Schema{ 77 Type: schema.TypeString, 78 Optional: true, 79 }, 80 "gtdLocation": &schema.Schema{ 81 Type: schema.TypeString, 82 Optional: true, 83 }, 84 }, 85 } 86 } 87 88 func resourceDMERecordCreate(d *schema.ResourceData, meta interface{}) error { 89 client := meta.(*dnsmadeeasy.Client) 90 91 domainid := d.Get("domainid").(string) 92 log.Printf("[INFO] Creating record for domainid: %s", domainid) 93 94 cr := make(map[string]interface{}) 95 if err := getAll(d, cr); err != nil { 96 return err 97 } 98 log.Printf("[DEBUG] record create configuration: %#v", cr) 99 100 result, err := client.CreateRecord(domainid, cr) 101 if err != nil { 102 return fmt.Errorf("Failed to create record: %s", err) 103 } 104 105 d.SetId(result) 106 log.Printf("[INFO] record ID: %s", d.Id()) 107 108 return resourceDMERecordRead(d, meta) 109 } 110 111 func resourceDMERecordRead(d *schema.ResourceData, meta interface{}) error { 112 client := meta.(*dnsmadeeasy.Client) 113 114 domainid := d.Get("domainid").(string) 115 recordid := d.Id() 116 log.Printf("[INFO] Reading record for domainid: %s recordid: %s", domainid, recordid) 117 118 rec, err := client.ReadRecord(domainid, recordid) 119 if err != nil { 120 if strings.Contains(err.Error(), "Unable to find") { 121 d.SetId("") 122 return nil 123 } 124 125 return fmt.Errorf("Couldn't find record: %s", err) 126 } 127 128 return setAll(d, rec) 129 } 130 131 func resourceDMERecordUpdate(d *schema.ResourceData, meta interface{}) error { 132 client := meta.(*dnsmadeeasy.Client) 133 134 domainid := d.Get("domainid").(string) 135 recordid := d.Id() 136 137 cr := make(map[string]interface{}) 138 if err := getAll(d, cr); err != nil { 139 return err 140 } 141 log.Printf("[DEBUG] record update configuration: %+#v", cr) 142 143 if _, err := client.UpdateRecord(domainid, recordid, cr); err != nil { 144 return fmt.Errorf("Error updating record: %s", err) 145 } 146 147 return resourceDMERecordRead(d, meta) 148 } 149 150 func resourceDMERecordDelete(d *schema.ResourceData, meta interface{}) error { 151 client := meta.(*dnsmadeeasy.Client) 152 153 domainid := d.Get("domainid").(string) 154 recordid := d.Id() 155 log.Printf("[INFO] Deleting record for domainid: %s recordid: %s", domainid, recordid) 156 157 if err := client.DeleteRecord(domainid, recordid); err != nil { 158 return fmt.Errorf("Error deleting record: %s", err) 159 } 160 161 return nil 162 } 163 164 func getAll(d *schema.ResourceData, cr map[string]interface{}) error { 165 166 if attr, ok := d.GetOk("name"); ok { 167 cr["name"] = attr.(string) 168 } 169 if attr, ok := d.GetOk("type"); ok { 170 cr["type"] = attr.(string) 171 } 172 if attr, ok := d.GetOk("ttl"); ok { 173 cr["ttl"] = int64(attr.(int)) 174 } 175 if attr, ok := d.GetOk("value"); ok { 176 cr["value"] = attr.(string) 177 } 178 if attr, ok := d.GetOk("gtdLocation"); ok { 179 cr["gtdLocation"] = attr.(string) 180 } 181 182 switch strings.ToUpper(d.Get("type").(string)) { 183 case "A", "CNAME", "ANAME", "TXT", "SPF", "NS", "PTR", "AAAA": 184 // all done 185 case "MX": 186 if attr, ok := d.GetOk("mxLevel"); ok { 187 cr["mxLevel"] = int64(attr.(int)) 188 } 189 case "SRV": 190 if attr, ok := d.GetOk("priority"); ok { 191 cr["priority"] = int64(attr.(int)) 192 } 193 if attr, ok := d.GetOk("weight"); ok { 194 cr["weight"] = int64(attr.(int)) 195 } 196 if attr, ok := d.GetOk("port"); ok { 197 cr["port"] = int64(attr.(int)) 198 } 199 case "HTTPRED": 200 if attr, ok := d.GetOk("hardLink"); ok && attr.(bool) { 201 cr["hardLink"] = "true" 202 } 203 if attr, ok := d.GetOk("redirectType"); ok { 204 cr["redirectType"] = attr.(string) 205 } 206 if attr, ok := d.GetOk("title"); ok { 207 cr["title"] = attr.(string) 208 } 209 if attr, ok := d.GetOk("keywords"); ok { 210 cr["keywords"] = attr.(string) 211 } 212 if attr, ok := d.GetOk("description"); ok { 213 cr["description"] = attr.(string) 214 } 215 default: 216 return fmt.Errorf("getAll: type not found") 217 } 218 return nil 219 } 220 221 func setAll(d *schema.ResourceData, rec *dnsmadeeasy.Record) error { 222 d.Set("type", rec.Type) 223 d.Set("name", rec.Name) 224 d.Set("ttl", rec.TTL) 225 d.Set("value", rec.Value) 226 // only set gtdLocation if it is given as this is optional. 227 if rec.GtdLocation != "" { 228 d.Set("gtdLocation", rec.GtdLocation) 229 } 230 231 switch rec.Type { 232 case "A", "CNAME", "ANAME", "TXT", "SPF", "NS", "PTR": 233 // all done 234 case "AAAA": 235 // overwrite value set above - DME ipv6 is lower case 236 d.Set("value", strings.ToLower(rec.Value)) 237 case "MX": 238 d.Set("mxLevel", rec.MXLevel) 239 case "SRV": 240 d.Set("priority", rec.Priority) 241 d.Set("weight", rec.Weight) 242 d.Set("port", rec.Port) 243 case "HTTPRED": 244 d.Set("hardLink", rec.HardLink) 245 d.Set("redirectType", rec.RedirectType) 246 d.Set("title", rec.Title) 247 d.Set("keywords", rec.Keywords) 248 d.Set("description", rec.Description) 249 default: 250 return fmt.Errorf("setAll: type not found") 251 } 252 return nil 253 }