github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/cloudflare/resource_cloudflare_record.go (about) 1 package cloudflare 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/cloudflare/cloudflare-go" 8 "github.com/hashicorp/terraform/helper/schema" 9 ) 10 11 func resourceCloudFlareRecord() *schema.Resource { 12 return &schema.Resource{ 13 Create: resourceCloudFlareRecordCreate, 14 Read: resourceCloudFlareRecordRead, 15 Update: resourceCloudFlareRecordUpdate, 16 Delete: resourceCloudFlareRecordDelete, 17 18 SchemaVersion: 1, 19 MigrateState: resourceCloudFlareRecordMigrateState, 20 Schema: map[string]*schema.Schema{ 21 "domain": &schema.Schema{ 22 Type: schema.TypeString, 23 Required: true, 24 }, 25 26 "name": &schema.Schema{ 27 Type: schema.TypeString, 28 Required: true, 29 }, 30 31 "hostname": &schema.Schema{ 32 Type: schema.TypeString, 33 Computed: true, 34 }, 35 36 "type": &schema.Schema{ 37 Type: schema.TypeString, 38 Required: true, 39 ForceNew: true, 40 ValidateFunc: validateRecordType, 41 }, 42 43 "value": &schema.Schema{ 44 Type: schema.TypeString, 45 Required: true, 46 }, 47 48 "ttl": &schema.Schema{ 49 Type: schema.TypeInt, 50 Optional: true, 51 Computed: true, 52 }, 53 54 "priority": &schema.Schema{ 55 Type: schema.TypeInt, 56 Optional: true, 57 }, 58 59 "proxied": &schema.Schema{ 60 Default: false, 61 Optional: true, 62 Type: schema.TypeBool, 63 }, 64 65 "zone_id": &schema.Schema{ 66 Type: schema.TypeString, 67 Computed: true, 68 }, 69 }, 70 } 71 } 72 73 func resourceCloudFlareRecordCreate(d *schema.ResourceData, meta interface{}) error { 74 client := meta.(*cloudflare.API) 75 76 newRecord := cloudflare.DNSRecord{ 77 Type: d.Get("type").(string), 78 Name: d.Get("name").(string), 79 Content: d.Get("value").(string), 80 Proxied: d.Get("proxied").(bool), 81 ZoneName: d.Get("domain").(string), 82 } 83 84 if priority, ok := d.GetOk("priority"); ok { 85 newRecord.Priority = priority.(int) 86 } 87 88 if ttl, ok := d.GetOk("ttl"); ok { 89 newRecord.TTL = ttl.(int) 90 } 91 92 // Validate value based on type 93 if err := validateRecordName(newRecord.Type, newRecord.Content); err != nil { 94 return fmt.Errorf("Error validating record name %q: %s", newRecord.Name, err) 95 } 96 97 zoneId, err := client.ZoneIDByName(newRecord.ZoneName) 98 if err != nil { 99 return fmt.Errorf("Error finding zone %q: %s", newRecord.ZoneName, err) 100 } 101 102 d.Set("zone_id", zoneId) 103 newRecord.ZoneID = zoneId 104 105 log.Printf("[DEBUG] CloudFlare Record create configuration: %#v", newRecord) 106 107 r, err := client.CreateDNSRecord(zoneId, newRecord) 108 if err != nil { 109 return fmt.Errorf("Failed to create record: %s", err) 110 } 111 112 // In the Event that the API returns an empty DNS Record, we verify that the 113 // ID returned is not the default "" 114 if r.Result.ID == "" { 115 return fmt.Errorf("Failed to find record in Creat response; Record was empty") 116 } 117 118 d.SetId(r.Result.ID) 119 120 log.Printf("[INFO] CloudFlare Record ID: %s", d.Id()) 121 122 return resourceCloudFlareRecordRead(d, meta) 123 } 124 125 func resourceCloudFlareRecordRead(d *schema.ResourceData, meta interface{}) error { 126 client := meta.(*cloudflare.API) 127 domain := d.Get("domain").(string) 128 129 zoneId, err := client.ZoneIDByName(domain) 130 if err != nil { 131 return fmt.Errorf("Error finding zone %q: %s", domain, err) 132 } 133 134 record, err := client.DNSRecord(zoneId, d.Id()) 135 if err != nil { 136 return err 137 } 138 139 d.SetId(record.ID) 140 d.Set("hostname", record.Name) 141 d.Set("type", record.Type) 142 d.Set("value", record.Content) 143 d.Set("ttl", record.TTL) 144 d.Set("priority", record.Priority) 145 d.Set("proxied", record.Proxied) 146 d.Set("zone_id", zoneId) 147 148 return nil 149 } 150 151 func resourceCloudFlareRecordUpdate(d *schema.ResourceData, meta interface{}) error { 152 client := meta.(*cloudflare.API) 153 154 updateRecord := cloudflare.DNSRecord{ 155 ID: d.Id(), 156 Type: d.Get("type").(string), 157 Name: d.Get("name").(string), 158 Content: d.Get("value").(string), 159 ZoneName: d.Get("domain").(string), 160 Proxied: false, 161 } 162 163 if priority, ok := d.GetOk("priority"); ok { 164 updateRecord.Priority = priority.(int) 165 } 166 167 if proxied, ok := d.GetOk("proxied"); ok { 168 updateRecord.Proxied = proxied.(bool) 169 } 170 171 if ttl, ok := d.GetOk("ttl"); ok { 172 updateRecord.TTL = ttl.(int) 173 } 174 175 zoneId, err := client.ZoneIDByName(updateRecord.ZoneName) 176 if err != nil { 177 return fmt.Errorf("Error finding zone %q: %s", updateRecord.ZoneName, err) 178 } 179 180 updateRecord.ZoneID = zoneId 181 182 log.Printf("[DEBUG] CloudFlare Record update configuration: %#v", updateRecord) 183 err = client.UpdateDNSRecord(zoneId, d.Id(), updateRecord) 184 if err != nil { 185 return fmt.Errorf("Failed to update CloudFlare Record: %s", err) 186 } 187 188 return resourceCloudFlareRecordRead(d, meta) 189 } 190 191 func resourceCloudFlareRecordDelete(d *schema.ResourceData, meta interface{}) error { 192 client := meta.(*cloudflare.API) 193 domain := d.Get("domain").(string) 194 195 zoneId, err := client.ZoneIDByName(domain) 196 if err != nil { 197 return fmt.Errorf("Error finding zone %q: %s", domain, err) 198 } 199 200 log.Printf("[INFO] Deleting CloudFlare Record: %s, %s", domain, d.Id()) 201 202 err = client.DeleteDNSRecord(zoneId, d.Id()) 203 if err != nil { 204 return fmt.Errorf("Error deleting CloudFlare Record: %s", err) 205 } 206 207 return nil 208 }