-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathresource_transip_domain_dnssec.go
138 lines (121 loc) · 3.88 KB
/
resource_transip_domain_dnssec.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
package main
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/transip/gotransip/v6/domain"
"github.com/transip/gotransip/v6/repository"
)
func resourceDomainDNSSec() *schema.Resource {
return &schema.Resource{
Create: resourceDomainDNSSecUpdate,
Read: resourceDomainDNSSecRead,
Update: resourceDomainDNSSecUpdate,
Delete: resourceDomainDNSSecDelete,
Importer: &schema.ResourceImporter{
State: resourceDomainDNSSecImport,
},
Schema: map[string]*schema.Schema{
"domain": {
Type: schema.TypeString,
Description: "The domain, including the tld",
Required: true,
},
"dnssec": {
Type: schema.TypeList,
Description: "List of dnssec entries associated with domain",
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key_tag": {
Type: schema.TypeInt,
Description: "A 5-digit key of the Zonesigner",
Required: true,
},
"flags": {
Type: schema.TypeInt,
Description: "The signing key number, either 256 (Zone Signing Key) or 257 (Key Signing Key)",
Required: true,
},
"algorithm": {
Type: schema.TypeInt,
Description: "The algorithm type that is used, see: https://www.transip.nl/vragen/461-domeinnaam-nameservers-gebruikt-beveiligen-dnssec/ for the possible options.",
Required: true,
},
"public_key": {
Type: schema.TypeString,
Description: "The public key",
Required: true,
},
},
},
},
},
}
}
func resourceDomainDNSSecImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
d.Set("domain", d.Id())
return []*schema.ResourceData{d}, nil
}
func resourceDomainDNSSecRead(d *schema.ResourceData, m interface{}) error {
client := m.(repository.Client)
repository := domain.Repository{Client: client}
domain := d.Get("domain").(string)
entries, err := repository.GetDNSSecEntries(domain)
if err != nil {
return fmt.Errorf("failed to get dnssec entries of domain %q: %s", domain, err)
}
err = d.Set("dnssec", entriesToMaps(entries))
if err != nil {
return fmt.Errorf("failed to parse dnssec entries of domain %q: %s", domain, err)
}
d.SetId(domain)
return nil
}
func resourceDomainDNSSecUpdate(d *schema.ResourceData, m interface{}) error {
client := m.(repository.Client)
repository := domain.Repository{Client: client}
domain := d.Get("domain").(string)
entries := interfacesToDNSSec(d.Get("dnssec").([]interface{}))
err := repository.ReplaceDNSSecEntries(domain, entries)
if err != nil {
return fmt.Errorf("failed to update dnssec entries of domain %q: %s", domain, err)
}
d.SetId(domain)
return nil
}
func resourceDomainDNSSecDelete(d *schema.ResourceData, m interface{}) error {
client := m.(repository.Client)
repository := domain.Repository{Client: client}
entries := make([]domain.DNSSecEntry, 0)
domain := d.Get("domain").(string)
err := repository.ReplaceDNSSecEntries(domain, entries)
if err != nil {
return fmt.Errorf("failed to reset dnssec of domain %q: %s", domain, err)
}
d.SetId("")
return nil
}
func entriesToMaps(entries []domain.DNSSecEntry) []map[string]interface{} {
maps := make([]map[string]interface{}, len(entries))
for i, v := range entries {
maps[i] = make(map[string]interface{})
maps[i]["key_tag"] = v.KeyTag
maps[i]["flags"] = v.Flags
maps[i]["algorithm"] = v.Algorithm
maps[i]["public_key"] = v.PublicKey
}
return maps
}
func interfacesToDNSSec(interfaces []interface{}) []domain.DNSSecEntry {
entries := make([]domain.DNSSecEntry, len(interfaces))
for i, v := range interfaces {
map_ := v.(map[string]interface{})
entries[i] = domain.DNSSecEntry{
KeyTag: map_["key_tag"].(int),
Flags: map_["flags"].(int),
Algorithm: map_["algorithm"].(int),
PublicKey: map_["public_key"].(string),
}
}
return entries
}