This repository has been archived by the owner on Jul 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
resource_vultr_dns_domain.go
109 lines (86 loc) · 2.31 KB
/
resource_vultr_dns_domain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// This code was originally based on the Digital Ocean provider from
// https://github.com/terraform-providers/terraform-provider-digitalocean.
package main
import (
"fmt"
"github.com/JamesClonk/vultr/lib"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceVultrDNSDomain() *schema.Resource {
return &schema.Resource{
Create: resourceVultrDNSDomainCreate,
Read: resourceVultrDNSDomainRead,
Delete: resourceVultrDNSDomainDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"ipv4_address": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceVultrDNSDomainCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*lib.Client)
name := d.Get("name").(string)
ipv4Address := d.Get("ipv4_address").(string)
err := client.CreateDNSDomain(name, ipv4Address)
if err != nil {
return fmt.Errorf("Error creating domain: %s", err)
}
d.SetId(name)
return resourceVultrDNSDomainRead(d, meta)
}
func resourceVultrDNSDomainRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*lib.Client)
domains, err := client.GetDNSDomains()
if err != nil {
return fmt.Errorf("Error retrieving domain: %s", err)
}
var domain *lib.DNSDomain
for _, c := range domains {
if c.Domain == d.Id() {
domain = &c
break
}
}
// if the domain is somehow already destroyed mark as succesfully gone.
if domain == nil {
d.SetId("")
return nil
}
// find the ipv4 address record associated with the domain.
records, err := client.GetDNSRecords(domain.Domain)
if err != nil {
return fmt.Errorf("Error retrieving domain records: %s", err)
}
var record *lib.DNSRecord
for _, r := range records {
if r.Type == "A" && r.Name == "" {
record = &r
break
}
}
// if we cannot find the default ipv4 record for the domain, mark the entire domain as succesfully gone.
if record == nil {
d.SetId("")
return nil
}
d.Set("name", domain.Domain)
d.Set("ipv4_address", record.Data)
return nil
}
func resourceVultrDNSDomainDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*lib.Client)
err := client.DeleteDNSDomain(d.Id())
if err != nil {
return fmt.Errorf("Error deleting domain: %s", err)
}
d.SetId("")
return nil
}