github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/ultradns/resource_ultradns_rdpool.go (about) 1 package ultradns 2 3 import ( 4 "fmt" 5 "log" 6 "strings" 7 8 "github.com/Ensighten/udnssdk" 9 "github.com/hashicorp/terraform/helper/schema" 10 "github.com/hashicorp/terraform/helper/validation" 11 ) 12 13 func resourceUltradnsRdpool() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceUltradnsRdpoolCreate, 16 Read: resourceUltradnsRdpoolRead, 17 Update: resourceUltradnsRdpoolUpdate, 18 Delete: resourceUltradnsRdpoolDelete, 19 20 Schema: map[string]*schema.Schema{ 21 // Required 22 "zone": &schema.Schema{ 23 Type: schema.TypeString, 24 Required: true, 25 ForceNew: true, 26 }, 27 "name": &schema.Schema{ 28 Type: schema.TypeString, 29 Required: true, 30 ForceNew: true, 31 }, 32 "rdata": &schema.Schema{ 33 Type: schema.TypeSet, 34 Set: schema.HashString, 35 Required: true, 36 Elem: &schema.Schema{Type: schema.TypeString}, 37 }, 38 // Optional 39 "order": &schema.Schema{ 40 Type: schema.TypeString, 41 Optional: true, 42 Default: "ROUND_ROBIN", 43 ValidateFunc: validation.StringInSlice([]string{ 44 "ROUND_ROBIN", 45 "FIXED", 46 "RANDOM", 47 }, false), 48 }, 49 "description": &schema.Schema{ 50 Type: schema.TypeString, 51 Optional: true, 52 ValidateFunc: validation.StringLenBetween(0, 255), 53 }, 54 "ttl": &schema.Schema{ 55 Type: schema.TypeInt, 56 Optional: true, 57 Default: 3600, 58 }, 59 // Computed 60 "hostname": &schema.Schema{ 61 Type: schema.TypeString, 62 Computed: true, 63 }, 64 }, 65 } 66 } 67 68 // CRUD Operations 69 70 func resourceUltradnsRdpoolCreate(d *schema.ResourceData, meta interface{}) error { 71 log.Printf("[INFO] ultradns_rdpool create") 72 client := meta.(*udnssdk.Client) 73 74 r, err := newRRSetResourceFromRdpool(d) 75 if err != nil { 76 return err 77 } 78 79 log.Printf("[INFO] ultradns_rdpool create: %#v", r) 80 _, err = client.RRSets.Create(r.RRSetKey(), r.RRSet()) 81 if err != nil { 82 return fmt.Errorf("create failed: %#v -> %v", r, err) 83 } 84 85 d.SetId(r.ID()) 86 log.Printf("[INFO] ultradns_rdpool.id: %v", d.Id()) 87 88 return resourceUltradnsRdpoolRead(d, meta) 89 } 90 91 func resourceUltradnsRdpoolRead(d *schema.ResourceData, meta interface{}) error { 92 log.Printf("[INFO] ultradns_rdpool read") 93 client := meta.(*udnssdk.Client) 94 95 rr, err := newRRSetResourceFromRdpool(d) 96 if err != nil { 97 return err 98 } 99 100 rrsets, err := client.RRSets.Select(rr.RRSetKey()) 101 if err != nil { 102 uderr, ok := err.(*udnssdk.ErrorResponseList) 103 if ok { 104 for _, resps := range uderr.Responses { 105 // 70002 means Records Not Found 106 if resps.ErrorCode == 70002 { 107 d.SetId("") 108 return nil 109 } 110 return fmt.Errorf("resource not found: %v", err) 111 } 112 } 113 return fmt.Errorf("resource not found: %v", err) 114 } 115 116 r := rrsets[0] 117 118 zone := d.Get("zone") 119 120 // hostname 121 if r.OwnerName == "" { 122 d.Set("hostname", zone) 123 } else { 124 if strings.HasSuffix(r.OwnerName, ".") { 125 d.Set("hostname", r.OwnerName) 126 } else { 127 d.Set("hostname", fmt.Sprintf("%s.%s", r.OwnerName, zone)) 128 } 129 } 130 131 // And now... the Profile! 132 if r.Profile == nil { 133 return fmt.Errorf("RRSet.profile missing: invalid RDPool schema in: %#v", r) 134 } 135 p, err := r.Profile.RDPoolProfile() 136 if err != nil { 137 return fmt.Errorf("RRSet.profile could not be unmarshalled: %v\n", err) 138 } 139 140 // Set simple values 141 d.Set("ttl", r.TTL) 142 d.Set("description", p.Description) 143 d.Set("order", p.Order) 144 145 err = d.Set("rdata", makeSetFromStrings(r.RData)) 146 if err != nil { 147 return fmt.Errorf("rdata set failed: %#v", err) 148 } 149 return nil 150 } 151 152 func resourceUltradnsRdpoolUpdate(d *schema.ResourceData, meta interface{}) error { 153 log.Printf("[INFO] ultradns_rdpool update") 154 client := meta.(*udnssdk.Client) 155 156 r, err := newRRSetResourceFromRdpool(d) 157 if err != nil { 158 return err 159 } 160 161 log.Printf("[INFO] ultradns_rdpool update: %+v", r) 162 _, err = client.RRSets.Update(r.RRSetKey(), r.RRSet()) 163 if err != nil { 164 return fmt.Errorf("resource update failed: %v", err) 165 } 166 167 return resourceUltradnsRdpoolRead(d, meta) 168 } 169 170 func resourceUltradnsRdpoolDelete(d *schema.ResourceData, meta interface{}) error { 171 log.Printf("[INFO] ultradns_rdpool delete") 172 client := meta.(*udnssdk.Client) 173 174 r, err := newRRSetResourceFromRdpool(d) 175 if err != nil { 176 return err 177 } 178 179 log.Printf("[INFO] ultradns_rdpool delete: %+v", r) 180 _, err = client.RRSets.Delete(r.RRSetKey()) 181 if err != nil { 182 return fmt.Errorf("resource delete failed: %v", err) 183 } 184 185 return nil 186 } 187 188 // Resource Helpers 189 190 func newRRSetResourceFromRdpool(d *schema.ResourceData) (rRSetResource, error) { 191 //rDataRaw := d.Get("rdata").(*schema.Set).List() 192 r := rRSetResource{ 193 // "The only valid rrtype value for RDpools is A" 194 // per https://portal.ultradns.com/static/docs/REST-API_User_Guide.pdf 195 RRType: "A", 196 Zone: d.Get("zone").(string), 197 OwnerName: d.Get("name").(string), 198 TTL: d.Get("ttl").(int), 199 } 200 if attr, ok := d.GetOk("rdata"); ok { 201 rdata := attr.(*schema.Set).List() 202 r.RData = make([]string, len(rdata)) 203 for i, j := range rdata { 204 r.RData[i] = j.(string) 205 } 206 } 207 208 profile := udnssdk.RDPoolProfile{ 209 Context: udnssdk.RDPoolSchema, 210 Order: d.Get("order").(string), 211 Description: d.Get("description").(string), 212 } 213 214 rp := profile.RawProfile() 215 r.Profile = rp 216 217 return r, nil 218 }