github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/dnsimple/resource_dnsimple_record.go (about) 1 package dnsimple 2 3 import ( 4 "fmt" 5 "log" 6 "strconv" 7 "strings" 8 9 "github.com/dnsimple/dnsimple-go/dnsimple" 10 "github.com/hashicorp/terraform/helper/schema" 11 ) 12 13 func resourceDNSimpleRecord() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceDNSimpleRecordCreate, 16 Read: resourceDNSimpleRecordRead, 17 Update: resourceDNSimpleRecordUpdate, 18 Delete: resourceDNSimpleRecordDelete, 19 Importer: &schema.ResourceImporter{ 20 State: resourceDNSimpleRecordImport, 21 }, 22 23 Schema: map[string]*schema.Schema{ 24 "domain": { 25 Type: schema.TypeString, 26 Required: true, 27 ForceNew: true, 28 }, 29 30 "domain_id": { 31 Type: schema.TypeString, 32 Computed: true, 33 }, 34 35 "name": { 36 Type: schema.TypeString, 37 Required: true, 38 }, 39 40 "hostname": { 41 Type: schema.TypeString, 42 Computed: true, 43 }, 44 45 "type": { 46 Type: schema.TypeString, 47 Required: true, 48 ForceNew: true, 49 }, 50 51 "value": { 52 Type: schema.TypeString, 53 Required: true, 54 }, 55 56 "ttl": { 57 Type: schema.TypeString, 58 Optional: true, 59 Default: "3600", 60 }, 61 62 "priority": { 63 Type: schema.TypeString, 64 Computed: true, 65 Optional: true, 66 }, 67 }, 68 } 69 } 70 71 func resourceDNSimpleRecordCreate(d *schema.ResourceData, meta interface{}) error { 72 provider := meta.(*Client) 73 74 // Create the new record 75 newRecord := dnsimple.ZoneRecord{ 76 Name: d.Get("name").(string), 77 Type: d.Get("type").(string), 78 Content: d.Get("value").(string), 79 } 80 if attr, ok := d.GetOk("ttl"); ok { 81 newRecord.TTL, _ = strconv.Atoi(attr.(string)) 82 } 83 84 if attr, ok := d.GetOk("priority"); ok { 85 newRecord.Priority, _ = strconv.Atoi(attr.(string)) 86 } 87 88 log.Printf("[DEBUG] DNSimple Record create configuration: %#v", newRecord) 89 90 resp, err := provider.client.Zones.CreateRecord(provider.config.Account, d.Get("domain").(string), newRecord) 91 if err != nil { 92 return fmt.Errorf("Failed to create DNSimple Record: %s", err) 93 } 94 95 d.SetId(strconv.Itoa(resp.Data.ID)) 96 log.Printf("[INFO] DNSimple Record ID: %s", d.Id()) 97 98 return resourceDNSimpleRecordRead(d, meta) 99 } 100 101 func resourceDNSimpleRecordRead(d *schema.ResourceData, meta interface{}) error { 102 provider := meta.(*Client) 103 104 recordID, err := strconv.Atoi(d.Id()) 105 if err != nil { 106 return fmt.Errorf("Error converting Record ID: %s", err) 107 } 108 109 resp, err := provider.client.Zones.GetRecord(provider.config.Account, d.Get("domain").(string), recordID) 110 if err != nil { 111 if err != nil && strings.Contains(err.Error(), "404") { 112 log.Printf("DNSimple Record Not Found - Refreshing from State") 113 d.SetId("") 114 return nil 115 } 116 return fmt.Errorf("Couldn't find DNSimple Record: %s", err) 117 } 118 119 record := resp.Data 120 d.Set("domain_id", record.ZoneID) 121 d.Set("name", record.Name) 122 d.Set("type", record.Type) 123 d.Set("value", record.Content) 124 d.Set("ttl", strconv.Itoa(record.TTL)) 125 d.Set("priority", strconv.Itoa(record.Priority)) 126 127 if record.Name == "" { 128 d.Set("hostname", d.Get("domain").(string)) 129 } else { 130 d.Set("hostname", fmt.Sprintf("%s.%s", record.Name, d.Get("domain").(string))) 131 } 132 133 return nil 134 } 135 136 func resourceDNSimpleRecordUpdate(d *schema.ResourceData, meta interface{}) error { 137 provider := meta.(*Client) 138 139 recordID, err := strconv.Atoi(d.Id()) 140 if err != nil { 141 return fmt.Errorf("Error converting Record ID: %s", err) 142 } 143 144 updateRecord := dnsimple.ZoneRecord{} 145 146 if attr, ok := d.GetOk("name"); ok { 147 updateRecord.Name = attr.(string) 148 } 149 if attr, ok := d.GetOk("type"); ok { 150 updateRecord.Type = attr.(string) 151 } 152 if attr, ok := d.GetOk("value"); ok { 153 updateRecord.Content = attr.(string) 154 } 155 if attr, ok := d.GetOk("ttl"); ok { 156 updateRecord.TTL, _ = strconv.Atoi(attr.(string)) 157 } 158 159 if attr, ok := d.GetOk("priority"); ok { 160 updateRecord.Priority, _ = strconv.Atoi(attr.(string)) 161 } 162 163 log.Printf("[DEBUG] DNSimple Record update configuration: %#v", updateRecord) 164 165 _, err = provider.client.Zones.UpdateRecord(provider.config.Account, d.Get("domain").(string), recordID, updateRecord) 166 if err != nil { 167 return fmt.Errorf("Failed to update DNSimple Record: %s", err) 168 } 169 170 return resourceDNSimpleRecordRead(d, meta) 171 } 172 173 func resourceDNSimpleRecordDelete(d *schema.ResourceData, meta interface{}) error { 174 provider := meta.(*Client) 175 176 log.Printf("[INFO] Deleting DNSimple Record: %s, %s", d.Get("domain").(string), d.Id()) 177 178 recordID, err := strconv.Atoi(d.Id()) 179 if err != nil { 180 return fmt.Errorf("Error converting Record ID: %s", err) 181 } 182 183 _, err = provider.client.Zones.DeleteRecord(provider.config.Account, d.Get("domain").(string), recordID) 184 if err != nil { 185 return fmt.Errorf("Error deleting DNSimple Record: %s", err) 186 } 187 188 return nil 189 } 190 191 func resourceDNSimpleRecordImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { 192 parts := strings.Split(d.Id(), "_") 193 194 if len(parts) != 2 { 195 return nil, fmt.Errorf("Error Importing dnsimple_record. Please make sure the record ID is in the form DOMAIN_RECORDID (i.e. example.com_1234") 196 } 197 198 d.SetId(parts[1]) 199 d.Set("domain", parts[0]) 200 201 if err := resourceDNSimpleRecordRead(d, meta); err != nil { 202 return nil, err 203 } 204 return []*schema.ResourceData{d}, nil 205 }