github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/google/resource_dns_record_set.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/hashicorp/terraform/helper/schema" 8 "google.golang.org/api/dns/v1" 9 "google.golang.org/api/googleapi" 10 ) 11 12 func resourceDnsRecordSet() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceDnsRecordSetCreate, 15 Read: resourceDnsRecordSetRead, 16 Delete: resourceDnsRecordSetDelete, 17 Update: resourceDnsRecordSetUpdate, 18 19 Schema: map[string]*schema.Schema{ 20 "managed_zone": &schema.Schema{ 21 Type: schema.TypeString, 22 Required: true, 23 ForceNew: true, 24 }, 25 26 "name": &schema.Schema{ 27 Type: schema.TypeString, 28 Required: true, 29 ForceNew: true, 30 }, 31 32 "rrdatas": &schema.Schema{ 33 Type: schema.TypeList, 34 Required: true, 35 Elem: &schema.Schema{ 36 Type: schema.TypeString, 37 }, 38 }, 39 40 "ttl": &schema.Schema{ 41 Type: schema.TypeInt, 42 Required: true, 43 }, 44 45 "type": &schema.Schema{ 46 Type: schema.TypeString, 47 Required: true, 48 }, 49 50 "project": &schema.Schema{ 51 Type: schema.TypeString, 52 Optional: true, 53 ForceNew: true, 54 }, 55 }, 56 } 57 } 58 59 func resourceDnsRecordSetCreate(d *schema.ResourceData, meta interface{}) error { 60 config := meta.(*Config) 61 62 project, err := getProject(d, config) 63 if err != nil { 64 return err 65 } 66 67 zone := d.Get("managed_zone").(string) 68 69 // Build the change 70 chg := &dns.Change{ 71 Additions: []*dns.ResourceRecordSet{ 72 &dns.ResourceRecordSet{ 73 Name: d.Get("name").(string), 74 Type: d.Get("type").(string), 75 Ttl: int64(d.Get("ttl").(int)), 76 Rrdatas: rrdata(d), 77 }, 78 }, 79 } 80 81 log.Printf("[DEBUG] DNS Record create request: %#v", chg) 82 chg, err = config.clientDns.Changes.Create(project, zone, chg).Do() 83 if err != nil { 84 return fmt.Errorf("Error creating DNS RecordSet: %s", err) 85 } 86 87 d.SetId(chg.Id) 88 89 w := &DnsChangeWaiter{ 90 Service: config.clientDns, 91 Change: chg, 92 Project: project, 93 ManagedZone: zone, 94 } 95 _, err = w.Conf().WaitForState() 96 if err != nil { 97 return fmt.Errorf("Error waiting for Google DNS change: %s", err) 98 } 99 100 return resourceDnsRecordSetRead(d, meta) 101 } 102 103 func resourceDnsRecordSetRead(d *schema.ResourceData, meta interface{}) error { 104 config := meta.(*Config) 105 106 project, err := getProject(d, config) 107 if err != nil { 108 return err 109 } 110 111 zone := d.Get("managed_zone").(string) 112 113 // name and type are effectively the 'key' 114 name := d.Get("name").(string) 115 dnsType := d.Get("type").(string) 116 117 resp, err := config.clientDns.ResourceRecordSets.List( 118 project, zone).Name(name).Type(dnsType).Do() 119 if err != nil { 120 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 121 log.Printf("[WARN] Removing DNS Record Set %q because it's gone", d.Get("name").(string)) 122 // The resource doesn't exist anymore 123 d.SetId("") 124 125 return nil 126 } 127 128 return fmt.Errorf("Error reading DNS RecordSet: %#v", err) 129 } 130 if len(resp.Rrsets) == 0 { 131 // The resource doesn't exist anymore 132 d.SetId("") 133 return nil 134 } 135 136 if len(resp.Rrsets) > 1 { 137 return fmt.Errorf("Only expected 1 record set, got %d", len(resp.Rrsets)) 138 } 139 140 d.Set("ttl", resp.Rrsets[0].Ttl) 141 d.Set("rrdatas", resp.Rrsets[0].Rrdatas) 142 143 return nil 144 } 145 146 func resourceDnsRecordSetDelete(d *schema.ResourceData, meta interface{}) error { 147 config := meta.(*Config) 148 149 project, err := getProject(d, config) 150 if err != nil { 151 return err 152 } 153 154 zone := d.Get("managed_zone").(string) 155 156 // Build the change 157 chg := &dns.Change{ 158 Deletions: []*dns.ResourceRecordSet{ 159 &dns.ResourceRecordSet{ 160 Name: d.Get("name").(string), 161 Type: d.Get("type").(string), 162 Ttl: int64(d.Get("ttl").(int)), 163 Rrdatas: rrdata(d), 164 }, 165 }, 166 } 167 168 log.Printf("[DEBUG] DNS Record delete request: %#v", chg) 169 chg, err = config.clientDns.Changes.Create(project, zone, chg).Do() 170 if err != nil { 171 return fmt.Errorf("Error deleting DNS RecordSet: %s", err) 172 } 173 174 w := &DnsChangeWaiter{ 175 Service: config.clientDns, 176 Change: chg, 177 Project: project, 178 ManagedZone: zone, 179 } 180 _, err = w.Conf().WaitForState() 181 if err != nil { 182 return fmt.Errorf("Error waiting for Google DNS change: %s", err) 183 } 184 185 d.SetId("") 186 return nil 187 } 188 189 func resourceDnsRecordSetUpdate(d *schema.ResourceData, meta interface{}) error { 190 config := meta.(*Config) 191 192 project, err := getProject(d, config) 193 if err != nil { 194 return err 195 } 196 197 zone := d.Get("managed_zone").(string) 198 recordName := d.Get("name").(string) 199 200 oldTtl, newTtl := d.GetChange("ttl") 201 oldType, newType := d.GetChange("type") 202 203 oldCountRaw, _ := d.GetChange("rrdatas.#") 204 oldCount := oldCountRaw.(int) 205 206 chg := &dns.Change{ 207 Deletions: []*dns.ResourceRecordSet{ 208 &dns.ResourceRecordSet{ 209 Name: recordName, 210 Type: oldType.(string), 211 Ttl: int64(oldTtl.(int)), 212 Rrdatas: make([]string, oldCount), 213 }, 214 }, 215 Additions: []*dns.ResourceRecordSet{ 216 &dns.ResourceRecordSet{ 217 Name: recordName, 218 Type: newType.(string), 219 Ttl: int64(newTtl.(int)), 220 Rrdatas: rrdata(d), 221 }, 222 }, 223 } 224 225 for i := 0; i < oldCount; i++ { 226 rrKey := fmt.Sprintf("rrdatas.%d", i) 227 oldRR, _ := d.GetChange(rrKey) 228 chg.Deletions[0].Rrdatas[i] = oldRR.(string) 229 } 230 log.Printf("[DEBUG] DNS Record change request: %#v old: %#v new: %#v", chg, chg.Deletions[0], chg.Additions[0]) 231 chg, err = config.clientDns.Changes.Create(project, zone, chg).Do() 232 if err != nil { 233 return fmt.Errorf("Error changing DNS RecordSet: %s", err) 234 } 235 236 w := &DnsChangeWaiter{ 237 Service: config.clientDns, 238 Change: chg, 239 Project: project, 240 ManagedZone: zone, 241 } 242 if _, err = w.Conf().WaitForState(); err != nil { 243 return fmt.Errorf("Error waiting for Google DNS change: %s", err) 244 } 245 246 return resourceDnsRecordSetRead(d, meta) 247 } 248 249 func rrdata( 250 d *schema.ResourceData, 251 ) []string { 252 rrdatasCount := d.Get("rrdatas.#").(int) 253 data := make([]string, rrdatasCount) 254 for i := 0; i < rrdatasCount; i++ { 255 data[i] = d.Get(fmt.Sprintf("rrdatas.%d", i)).(string) 256 } 257 return data 258 }