-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresource_gd_record.go
142 lines (120 loc) · 2.99 KB
/
resource_gd_record.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"log"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceRecord() *schema.Resource {
return &schema.Resource{
Create: resourceRecordCreate,
Read: resourceRecordRead,
Update: resourceRecordCreate,
Delete: resourceRecordDelete,
Schema: map[string]*schema.Schema{
"domain": {
Type: schema.TypeString,
Required: true,
},
"type": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"data": {
Type: schema.TypeString,
Required: true,
},
"ttl": {
Type: schema.TypeInt,
Optional: true,
},
"priority": {
Type: schema.TypeInt,
Optional: true,
},
},
}
}
func resourceRecordCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*GoDaddyConfig)
//create our id as <name>.<domain>::<type>
id := d.Get("name").(string) + "." + d.Get("domain").(string) + "::" + d.Get("type").(string)
d.SetId(id)
//get the current GoDaddy state
state, err := fetchState(
config.key,
config.secret,
d.Get("domain").(string),
)
if err != nil {
log.Printf("Error: %s\n", err.Error())
return err
}
//create the new DnsRecord
record := DnsRecord {
Data: d.Get("data").(string),
Name: d.Get("name").(string),
Priority: d.Get("priority").(int),
Ttl: d.Get("ttl").(int),
Record_type: d.Get("type").(string),
}
//add it to our list
state = appendRecord(record, state)
//write it back
_, err = putState(
config.key,
config.secret,
d.Get("domain").(string),
state,
)
if err != nil {
log.Printf("Error: %s\n", err.Error())
return err
}
return nil
}
func resourceRecordRead(d *schema.ResourceData, m interface{}) error {
//Note: Does this function actually have a purpose? It only seems to read from the state file, and returning
// nil seems to be OK....
return nil
}
func resourceRecordDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*GoDaddyConfig)
//create our id as <name>.<domain>::<type>
id := d.Get("name").(string) + "." + d.Get("domain").(string) + "::" + d.Get("type").(string)
d.SetId(id)
//get the current GoDaddy state
state, err := fetchState(
config.key,
config.secret,
d.Get("domain").(string),
)
if err != nil {
log.Printf("Error: %s\n", err.Error())
return err
}
//create the new DnsRecord
record := DnsRecord {
Data: d.Get("data").(string),
Name: d.Get("name").(string),
Priority: d.Get("priority").(int),
Ttl: d.Get("ttl").(int),
Record_type: d.Get("type").(string),
}
//add it to our list
state = removeRecord(record, state)
//write it back
_, err = putState(
config.key,
config.secret,
d.Get("domain").(string),
state,
)
if err != nil {
log.Printf("Error: %s\n", err.Error())
return err
}
return nil
}