-
Notifications
You must be signed in to change notification settings - Fork 3
/
resource_dns_a.go
111 lines (88 loc) · 2.16 KB
/
resource_dns_a.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
110
111
package main
import (
"errors"
"log"
"net"
"strings"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceRecordA() *schema.Resource {
return &schema.Resource{
Create: createRecordA,
Delete: deleteRecordA,
Read: readRecordA,
Update: updateRecordA,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"ip": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"zone": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"ptr": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
}
}
func createRecordA(d *schema.ResourceData, m interface{}) error {
log.Printf("[DEBUG] validate a ip address")
ip := net.ParseIP(d.Get("ip").(string))
if ip == nil {
return errors.New("Invalid IP Address")
}
c := m.(*Communicator)
c.Connect()
zone := d.Get("zone").(string)
name := d.Get("name").(string)
err := c.AddDNSRecordA(zone, ip, name)
if err != nil {
return err
}
d.SetId("A_z:" + zone + "_n:" + name + "_ip:" + ip.String())
ptr := d.Get("ptr").(string)
if ptr != "" {
ptrArr, lastByteArr := computePtrAndLastByte(ptr, ip)
err = c.AddDNSRecordPTR(zone, ip, name, ptrArr, lastByteArr)
}
return err
}
func computePtrAndLastByte(ptr string, ip net.IP) ([]string, []string) {
ptrArr := strings.Split(ptr, ".")
ipArr := strings.Split(ip.String(), ".")
lastByteArr := make([]string, 1)
if len(ptrArr) == 3 {
lastByteArr[0] = ipArr[3]
}
if len(ptrArr) == 2 {
lastByteArr = ipArr[2:4]
}
return ptrArr, lastByteArr
}
func deleteRecordA(d *schema.ResourceData, m interface{}) error {
c := m.(*Communicator)
c.Connect()
ip := net.ParseIP(d.Get("ip").(string))
ptr := d.Get("ptr").(string)
if ptr != "" {
ptrArr, lastByteArr := computePtrAndLastByte(ptr, ip)
err := c.RemoveDNSRecordPTR(ptrArr, lastByteArr)
if err != nil {
return err
}
}
return c.RemoveDNSRecordA(d.Get("zone").(string), ip, d.Get("name").(string))
}
func readRecordA(d *schema.ResourceData, m interface{}) error {
return nil
}
func updateRecordA(d *schema.ResourceData, m interface{}) error {
return nil
}