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