github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/cloudflare/resource_cloudflare_record.go (about) 1 package cloudflare 2 3 import ( 4 "fmt" 5 "log" 6 "strings" 7 8 "github.com/hashicorp/terraform/helper/schema" 9 "github.com/pearkes/cloudflare" 10 ) 11 12 func resourceCloudFlareRecord() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceCloudFlareRecordCreate, 15 Read: resourceCloudFlareRecordRead, 16 Update: resourceCloudFlareRecordUpdate, 17 Delete: resourceCloudFlareRecordDelete, 18 19 Schema: map[string]*schema.Schema{ 20 "domain": &schema.Schema{ 21 Type: schema.TypeString, 22 Required: true, 23 }, 24 25 "name": &schema.Schema{ 26 Type: schema.TypeString, 27 Required: true, 28 }, 29 30 "hostname": &schema.Schema{ 31 Type: schema.TypeString, 32 Computed: true, 33 }, 34 35 "type": &schema.Schema{ 36 Type: schema.TypeString, 37 Required: true, 38 }, 39 40 "value": &schema.Schema{ 41 Type: schema.TypeString, 42 Required: true, 43 }, 44 45 "ttl": &schema.Schema{ 46 Type: schema.TypeString, 47 Optional: true, 48 }, 49 50 "priority": &schema.Schema{ 51 Type: schema.TypeString, 52 Optional: true, 53 }, 54 }, 55 } 56 } 57 58 func resourceCloudFlareRecordCreate(d *schema.ResourceData, meta interface{}) error { 59 client := meta.(*cloudflare.Client) 60 61 // Create the new record 62 newRecord := &cloudflare.CreateRecord{ 63 Name: d.Get("name").(string), 64 Type: d.Get("type").(string), 65 Content: d.Get("value").(string), 66 } 67 68 if ttl, ok := d.GetOk("ttl"); ok { 69 newRecord.Ttl = ttl.(string) 70 } 71 72 if priority, ok := d.GetOk("priority"); ok { 73 newRecord.Priority = priority.(string) 74 } 75 76 log.Printf("[DEBUG] CloudFlare Record create configuration: %#v", newRecord) 77 78 rec, err := client.CreateRecord(d.Get("domain").(string), newRecord) 79 80 if err != nil { 81 return fmt.Errorf("Failed to create CloudFlare Record: %s", err) 82 } 83 84 d.SetId(rec.Id) 85 log.Printf("[INFO] CloudFlare Record ID: %s", d.Id()) 86 87 return resourceCloudFlareRecordRead(d, meta) 88 } 89 90 func resourceCloudFlareRecordRead(d *schema.ResourceData, meta interface{}) error { 91 client := meta.(*cloudflare.Client) 92 93 rec, err := client.RetrieveRecord(d.Get("domain").(string), d.Id()) 94 if err != nil { 95 if strings.Contains(err.Error(), "not found") { 96 d.SetId("") 97 return nil 98 } 99 100 return fmt.Errorf( 101 "Couldn't find CloudFlare Record ID (%s) for domain (%s): %s", 102 d.Id(), d.Get("domain").(string), err) 103 } 104 105 d.Set("name", rec.Name) 106 d.Set("hostname", rec.FullName) 107 d.Set("type", rec.Type) 108 d.Set("value", rec.Value) 109 d.Set("ttl", rec.Ttl) 110 d.Set("priority", rec.Priority) 111 112 return nil 113 } 114 115 func resourceCloudFlareRecordUpdate(d *schema.ResourceData, meta interface{}) error { 116 client := meta.(*cloudflare.Client) 117 118 // CloudFlare requires we send all values for an update request 119 updateRecord := &cloudflare.UpdateRecord{ 120 Name: d.Get("name").(string), 121 Type: d.Get("type").(string), 122 Content: d.Get("value").(string), 123 } 124 125 if ttl, ok := d.GetOk("ttl"); ok { 126 updateRecord.Ttl = ttl.(string) 127 } 128 129 if priority, ok := d.GetOk("priority"); ok { 130 updateRecord.Priority = priority.(string) 131 } 132 133 log.Printf("[DEBUG] CloudFlare Record update configuration: %#v", updateRecord) 134 135 err := client.UpdateRecord(d.Get("domain").(string), d.Id(), updateRecord) 136 if err != nil { 137 return fmt.Errorf("Failed to update CloudFlare Record: %s", err) 138 } 139 140 return resourceCloudFlareRecordRead(d, meta) 141 } 142 143 func resourceCloudFlareRecordDelete(d *schema.ResourceData, meta interface{}) error { 144 client := meta.(*cloudflare.Client) 145 146 log.Printf("[INFO] Deleting CloudFlare Record: %s, %s", d.Get("domain").(string), d.Id()) 147 148 err := client.DestroyRecord(d.Get("domain").(string), d.Id()) 149 150 if err != nil { 151 return fmt.Errorf("Error deleting CloudFlare Record: %s", err) 152 } 153 154 return nil 155 }