github.com/ricardclau/terraform@v0.6.17-0.20160519222547-283e3ae6b5a9/builtin/providers/cloudflare/resource_cloudflare_record.go (about) 1 package cloudflare 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/hashicorp/terraform/helper/schema" 8 9 // NOTE: Temporary until they merge my PR: 10 "github.com/mitchellh/cloudflare-go" 11 ) 12 13 func resourceCloudFlareRecord() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceCloudFlareRecordCreate, 16 Read: resourceCloudFlareRecordRead, 17 Update: resourceCloudFlareRecordUpdate, 18 Delete: resourceCloudFlareRecordDelete, 19 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 }, 41 42 "value": &schema.Schema{ 43 Type: schema.TypeString, 44 Required: true, 45 }, 46 47 "ttl": &schema.Schema{ 48 Type: schema.TypeInt, 49 Optional: true, 50 Computed: true, 51 }, 52 53 "priority": &schema.Schema{ 54 Type: schema.TypeInt, 55 Optional: true, 56 }, 57 58 "proxied": &schema.Schema{ 59 Default: false, 60 Optional: true, 61 Type: schema.TypeBool, 62 }, 63 64 "zone_id": &schema.Schema{ 65 Type: schema.TypeString, 66 Computed: true, 67 }, 68 }, 69 } 70 } 71 72 func resourceCloudFlareRecordCreate(d *schema.ResourceData, meta interface{}) error { 73 client := meta.(*cloudflare.API) 74 75 newRecord := cloudflare.DNSRecord{ 76 Type: d.Get("type").(string), 77 Name: d.Get("name").(string), 78 Content: d.Get("value").(string), 79 Proxied: d.Get("proxied").(bool), 80 ZoneName: d.Get("domain").(string), 81 } 82 83 if priority, ok := d.GetOk("priority"); ok { 84 newRecord.Priority = priority.(int) 85 } 86 87 if ttl, ok := d.GetOk("ttl"); ok { 88 newRecord.TTL = ttl.(int) 89 } 90 91 zoneId, err := client.ZoneIDByName(newRecord.ZoneName) 92 if err != nil { 93 return fmt.Errorf("Error finding zone %q: %s", newRecord.ZoneName, err) 94 } 95 96 d.Set("zone_id", zoneId) 97 newRecord.ZoneID = zoneId 98 99 log.Printf("[DEBUG] CloudFlare Record create configuration: %#v", newRecord) 100 101 r, err := client.CreateDNSRecord(zoneId, newRecord) 102 if err != nil { 103 return fmt.Errorf("Failed to create record: %s", err) 104 } 105 106 d.SetId(r.ID) 107 108 log.Printf("[INFO] CloudFlare Record ID: %s", d.Id()) 109 110 return resourceCloudFlareRecordRead(d, meta) 111 } 112 113 func resourceCloudFlareRecordRead(d *schema.ResourceData, meta interface{}) error { 114 client := meta.(*cloudflare.API) 115 domain := d.Get("domain").(string) 116 117 zoneId, err := client.ZoneIDByName(domain) 118 if err != nil { 119 return fmt.Errorf("Error finding zone %q: %s", domain, err) 120 } 121 122 record, err := client.DNSRecord(zoneId, d.Id()) 123 if err != nil { 124 return err 125 } 126 127 d.SetId(record.ID) 128 d.Set("hostname", record.Name) 129 d.Set("type", record.Type) 130 d.Set("value", record.Content) 131 d.Set("ttl", record.TTL) 132 d.Set("priority", record.Priority) 133 d.Set("proxied", record.Proxied) 134 d.Set("zone_id", zoneId) 135 136 return nil 137 } 138 139 func resourceCloudFlareRecordUpdate(d *schema.ResourceData, meta interface{}) error { 140 client := meta.(*cloudflare.API) 141 142 updateRecord := cloudflare.DNSRecord{ 143 ID: d.Id(), 144 Type: d.Get("type").(string), 145 Name: d.Get("name").(string), 146 Content: d.Get("value").(string), 147 ZoneName: d.Get("domain").(string), 148 Proxied: false, 149 } 150 151 if priority, ok := d.GetOk("priority"); ok { 152 updateRecord.Priority = priority.(int) 153 } 154 155 if proxied, ok := d.GetOk("proxied"); ok { 156 updateRecord.Proxied = proxied.(bool) 157 } 158 159 if ttl, ok := d.GetOk("ttl"); ok { 160 updateRecord.TTL = ttl.(int) 161 } 162 163 zoneId, err := client.ZoneIDByName(updateRecord.ZoneName) 164 if err != nil { 165 return fmt.Errorf("Error finding zone %q: %s", updateRecord.ZoneName, err) 166 } 167 168 updateRecord.ZoneID = zoneId 169 170 log.Printf("[DEBUG] CloudFlare Record update configuration: %#v", updateRecord) 171 err = client.UpdateDNSRecord(zoneId, d.Id(), updateRecord) 172 if err != nil { 173 return fmt.Errorf("Failed to update CloudFlare Record: %s", err) 174 } 175 176 return resourceCloudFlareRecordRead(d, meta) 177 } 178 179 func resourceCloudFlareRecordDelete(d *schema.ResourceData, meta interface{}) error { 180 client := meta.(*cloudflare.API) 181 domain := d.Get("domain").(string) 182 183 zoneId, err := client.ZoneIDByName(domain) 184 if err != nil { 185 return fmt.Errorf("Error finding zone %q: %s", domain, err) 186 } 187 188 log.Printf("[INFO] Deleting CloudFlare Record: %s, %s", domain, d.Id()) 189 190 err = client.DeleteDNSRecord(zoneId, d.Id()) 191 if err != nil { 192 return fmt.Errorf("Error deleting CloudFlare Record: %s", err) 193 } 194 195 return nil 196 }