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