github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/ultradns/resource_ultradns_record.go (about) 1 package ultradns 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "log" 7 "strconv" 8 "strings" 9 10 "github.com/Ensighten/udnssdk" 11 "github.com/hashicorp/terraform/helper/schema" 12 ) 13 14 func newRRSetResource(d *schema.ResourceData) (rRSetResource, error) { 15 r := rRSetResource{} 16 17 // TODO: return error if required attributes aren't ok 18 19 if attr, ok := d.GetOk("name"); ok { 20 r.OwnerName = attr.(string) 21 } 22 23 if attr, ok := d.GetOk("type"); ok { 24 r.RRType = attr.(string) 25 } 26 27 if attr, ok := d.GetOk("zone"); ok { 28 r.Zone = attr.(string) 29 } 30 31 if attr, ok := d.GetOk("rdata"); ok { 32 rdata := attr.(*schema.Set).List() 33 r.RData = make([]string, len(rdata)) 34 for i, j := range rdata { 35 r.RData[i] = j.(string) 36 } 37 } 38 39 if attr, ok := d.GetOk("ttl"); ok { 40 r.TTL, _ = strconv.Atoi(attr.(string)) 41 } 42 43 return r, nil 44 } 45 46 func populateResourceDataFromRRSet(r udnssdk.RRSet, d *schema.ResourceData) error { 47 zone := d.Get("zone") 48 typ := d.Get("type") 49 // ttl 50 d.Set("ttl", r.TTL) 51 // rdata 52 rdata := r.RData 53 54 // UltraDNS API returns answers double-encoded like JSON, so we must decode. This is their bug. 55 if typ == "TXT" { 56 rdata = make([]string, len(r.RData)) 57 for i := range r.RData { 58 var s string 59 err := json.Unmarshal([]byte(r.RData[i]), &s) 60 if err != nil { 61 log.Printf("[INFO] TXT answer parse error: %+v", err) 62 s = r.RData[i] 63 } 64 rdata[i] = s 65 66 } 67 } 68 69 err := d.Set("rdata", makeSetFromStrings(rdata)) 70 if err != nil { 71 return fmt.Errorf("ultradns_record.rdata set failed: %#v", err) 72 } 73 // hostname 74 if r.OwnerName == "" { 75 d.Set("hostname", zone) 76 } else { 77 if strings.HasSuffix(r.OwnerName, ".") { 78 d.Set("hostname", r.OwnerName) 79 } else { 80 d.Set("hostname", fmt.Sprintf("%s.%s", r.OwnerName, zone)) 81 } 82 } 83 return nil 84 } 85 86 func resourceUltradnsRecord() *schema.Resource { 87 return &schema.Resource{ 88 Create: resourceUltraDNSRecordCreate, 89 Read: resourceUltraDNSRecordRead, 90 Update: resourceUltraDNSRecordUpdate, 91 Delete: resourceUltraDNSRecordDelete, 92 93 Schema: map[string]*schema.Schema{ 94 // Required 95 "zone": &schema.Schema{ 96 Type: schema.TypeString, 97 Required: true, 98 ForceNew: true, 99 }, 100 "name": &schema.Schema{ 101 Type: schema.TypeString, 102 Required: true, 103 ForceNew: true, 104 }, 105 "type": &schema.Schema{ 106 Type: schema.TypeString, 107 Required: true, 108 ForceNew: true, 109 }, 110 "rdata": &schema.Schema{ 111 Type: schema.TypeSet, 112 Set: schema.HashString, 113 Required: true, 114 Elem: &schema.Schema{Type: schema.TypeString}, 115 }, 116 // Optional 117 "ttl": &schema.Schema{ 118 Type: schema.TypeString, 119 Optional: true, 120 Default: "3600", 121 }, 122 // Computed 123 "hostname": &schema.Schema{ 124 Type: schema.TypeString, 125 Computed: true, 126 }, 127 }, 128 } 129 } 130 131 // CRUD Operations 132 133 func resourceUltraDNSRecordCreate(d *schema.ResourceData, meta interface{}) error { 134 client := meta.(*udnssdk.Client) 135 136 r, err := newRRSetResource(d) 137 if err != nil { 138 return err 139 } 140 141 log.Printf("[INFO] ultradns_record create: %+v", r) 142 _, err = client.RRSets.Create(r.RRSetKey(), r.RRSet()) 143 if err != nil { 144 return fmt.Errorf("create failed: %v", err) 145 } 146 147 d.SetId(r.ID()) 148 log.Printf("[INFO] ultradns_record.id: %v", d.Id()) 149 150 return resourceUltraDNSRecordRead(d, meta) 151 } 152 153 func resourceUltraDNSRecordRead(d *schema.ResourceData, meta interface{}) error { 154 client := meta.(*udnssdk.Client) 155 156 r, err := newRRSetResource(d) 157 if err != nil { 158 return err 159 } 160 161 rrsets, err := client.RRSets.Select(r.RRSetKey()) 162 if err != nil { 163 uderr, ok := err.(*udnssdk.ErrorResponseList) 164 if ok { 165 for _, r := range uderr.Responses { 166 // 70002 means Records Not Found 167 if r.ErrorCode == 70002 { 168 d.SetId("") 169 return nil 170 } 171 return fmt.Errorf("not found: %v", err) 172 } 173 } 174 return fmt.Errorf("not found: %v", err) 175 } 176 rec := rrsets[0] 177 return populateResourceDataFromRRSet(rec, d) 178 } 179 180 func resourceUltraDNSRecordUpdate(d *schema.ResourceData, meta interface{}) error { 181 client := meta.(*udnssdk.Client) 182 183 r, err := newRRSetResource(d) 184 if err != nil { 185 return err 186 } 187 188 log.Printf("[INFO] ultradns_record update: %+v", r) 189 _, err = client.RRSets.Update(r.RRSetKey(), r.RRSet()) 190 if err != nil { 191 return fmt.Errorf("update failed: %v", err) 192 } 193 194 return resourceUltraDNSRecordRead(d, meta) 195 } 196 197 func resourceUltraDNSRecordDelete(d *schema.ResourceData, meta interface{}) error { 198 client := meta.(*udnssdk.Client) 199 200 r, err := newRRSetResource(d) 201 if err != nil { 202 return err 203 } 204 205 log.Printf("[INFO] ultradns_record delete: %+v", r) 206 _, err = client.RRSets.Delete(r.RRSetKey()) 207 if err != nil { 208 return fmt.Errorf("delete failed: %v", err) 209 } 210 211 return nil 212 } 213 214 // Conversion helper functions