github.com/adamar/terraform@v0.2.2-0.20141016210445-2e703afdad0e/builtin/providers/dnsimple/resource_dnsimple_record.go (about) 1 package dnsimple 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/hashicorp/terraform/helper/config" 8 "github.com/hashicorp/terraform/helper/diff" 9 "github.com/hashicorp/terraform/terraform" 10 "github.com/pearkes/dnsimple" 11 ) 12 13 func resource_dnsimple_record_create( 14 s *terraform.InstanceState, 15 d *terraform.InstanceDiff, 16 meta interface{}) (*terraform.InstanceState, error) { 17 p := meta.(*ResourceProvider) 18 client := p.client 19 20 // Merge the diff into the state so that we have all the attributes 21 // properly. 22 rs := s.MergeDiff(d) 23 24 var err error 25 26 newRecord := dnsimple.ChangeRecord{ 27 Name: rs.Attributes["name"], 28 Value: rs.Attributes["value"], 29 Type: rs.Attributes["type"], 30 } 31 32 if attr, ok := rs.Attributes["ttl"]; ok { 33 newRecord.Ttl = attr 34 } 35 36 log.Printf("[DEBUG] record create configuration: %#v", newRecord) 37 38 recId, err := client.CreateRecord(rs.Attributes["domain"], &newRecord) 39 40 if err != nil { 41 return nil, fmt.Errorf("Failed to create record: %s", err) 42 } 43 44 rs.ID = recId 45 log.Printf("[INFO] record ID: %s", rs.ID) 46 47 record, err := resource_dnsimple_record_retrieve(rs.Attributes["domain"], rs.ID, client) 48 if err != nil { 49 return nil, fmt.Errorf("Couldn't find record: %s", err) 50 } 51 52 return resource_dnsimple_record_update_state(rs, record) 53 } 54 55 func resource_dnsimple_record_update( 56 s *terraform.InstanceState, 57 d *terraform.InstanceDiff, 58 meta interface{}) (*terraform.InstanceState, error) { 59 p := meta.(*ResourceProvider) 60 client := p.client 61 rs := s.MergeDiff(d) 62 63 updateRecord := dnsimple.ChangeRecord{} 64 65 if attr, ok := d.Attributes["name"]; ok { 66 updateRecord.Name = attr.New 67 } 68 69 if attr, ok := d.Attributes["value"]; ok { 70 updateRecord.Value = attr.New 71 } 72 73 if attr, ok := d.Attributes["type"]; ok { 74 updateRecord.Type = attr.New 75 } 76 77 if attr, ok := d.Attributes["ttl"]; ok { 78 updateRecord.Ttl = attr.New 79 } 80 81 log.Printf("[DEBUG] record update configuration: %#v", updateRecord) 82 83 _, err := client.UpdateRecord(rs.Attributes["domain"], rs.ID, &updateRecord) 84 if err != nil { 85 return rs, fmt.Errorf("Failed to update record: %s", err) 86 } 87 88 record, err := resource_dnsimple_record_retrieve(rs.Attributes["domain"], rs.ID, client) 89 if err != nil { 90 return rs, fmt.Errorf("Couldn't find record: %s", err) 91 } 92 93 return resource_dnsimple_record_update_state(rs, record) 94 } 95 96 func resource_dnsimple_record_destroy( 97 s *terraform.InstanceState, 98 meta interface{}) error { 99 p := meta.(*ResourceProvider) 100 client := p.client 101 102 log.Printf("[INFO] Deleting record: %s, %s", s.Attributes["domain"], s.ID) 103 104 err := client.DestroyRecord(s.Attributes["domain"], s.ID) 105 106 if err != nil { 107 return fmt.Errorf("Error deleting record: %s", err) 108 } 109 110 return nil 111 } 112 113 func resource_dnsimple_record_refresh( 114 s *terraform.InstanceState, 115 meta interface{}) (*terraform.InstanceState, error) { 116 p := meta.(*ResourceProvider) 117 client := p.client 118 119 rec, err := resource_dnsimple_record_retrieve(s.Attributes["domain"], s.ID, client) 120 if err != nil { 121 return nil, err 122 } 123 124 return resource_dnsimple_record_update_state(s, rec) 125 } 126 127 func resource_dnsimple_record_diff( 128 s *terraform.InstanceState, 129 c *terraform.ResourceConfig, 130 meta interface{}) (*terraform.InstanceDiff, error) { 131 132 b := &diff.ResourceBuilder{ 133 Attrs: map[string]diff.AttrType{ 134 "domain": diff.AttrTypeCreate, 135 "name": diff.AttrTypeUpdate, 136 "value": diff.AttrTypeUpdate, 137 "ttl": diff.AttrTypeUpdate, 138 "type": diff.AttrTypeUpdate, 139 }, 140 141 ComputedAttrs: []string{ 142 "priority", 143 "domain_id", 144 "ttl", 145 }, 146 147 ComputedAttrsUpdate: []string{ 148 "hostname", 149 }, 150 } 151 152 return b.Diff(s, c) 153 } 154 155 func resource_dnsimple_record_update_state( 156 s *terraform.InstanceState, 157 rec *dnsimple.Record) (*terraform.InstanceState, error) { 158 159 s.Attributes["name"] = rec.Name 160 s.Attributes["value"] = rec.Content 161 s.Attributes["type"] = rec.RecordType 162 s.Attributes["ttl"] = rec.StringTtl() 163 s.Attributes["priority"] = rec.StringPrio() 164 s.Attributes["domain_id"] = rec.StringDomainId() 165 166 if rec.Name == "" { 167 s.Attributes["hostname"] = s.Attributes["domain"] 168 } else { 169 s.Attributes["hostname"] = fmt.Sprintf("%s.%s", rec.Name, s.Attributes["domain"]) 170 } 171 172 return s, nil 173 } 174 175 func resource_dnsimple_record_retrieve(domain string, id string, client *dnsimple.Client) (*dnsimple.Record, error) { 176 record, err := client.RetrieveRecord(domain, id) 177 if err != nil { 178 return nil, err 179 } 180 181 return record, nil 182 } 183 184 func resource_dnsimple_record_validation() *config.Validator { 185 return &config.Validator{ 186 Required: []string{ 187 "domain", 188 "name", 189 "value", 190 "type", 191 }, 192 Optional: []string{ 193 "ttl", 194 }, 195 } 196 }