-
Notifications
You must be signed in to change notification settings - Fork 29
/
resource_dns_record.go
139 lines (109 loc) · 2.99 KB
/
resource_dns_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
package mikrotik
import (
"context"
"log"
"github.com/ddelnano/terraform-provider-mikrotik/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceRecord() *schema.Resource {
return &schema.Resource{
CreateContext: resourceServerCreate,
ReadContext: resourceServerRead,
UpdateContext: resourceServerUpdate,
DeleteContext: resourceServerDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"address": {
Type: schema.TypeString,
Required: true,
},
"comment": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
"ttl": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
},
}
}
func resourceServerCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
record := prepareDnsRecord(d)
c := m.(*client.Mikrotik)
dnsRecord, err := c.AddDnsRecord(record)
if err != nil {
return diag.FromErr(err)
}
return recordToData(dnsRecord, d)
}
func resourceServerRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*client.Mikrotik)
record, err := c.FindDnsRecord(d.Id())
if _, ok := err.(*client.NotFound); ok {
d.SetId("")
return nil
}
return recordToData(record, d)
}
func resourceServerUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*client.Mikrotik)
currentRecord, err := c.FindDnsRecord(d.Id())
record := prepareDnsRecord(d)
record.Id = currentRecord.Id
if err != nil {
return diag.FromErr(err)
}
log.Printf("[DEBUG] About to update dns record with %v", record)
dnsRecord, err := c.UpdateDnsRecord(record)
if err != nil {
return diag.FromErr(err)
}
return recordToData(dnsRecord, d)
}
func resourceServerDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
name := d.Id()
c := m.(*client.Mikrotik)
record, err := c.FindDnsRecord(name)
if err != nil {
return diag.FromErr(err)
}
err = c.DeleteDnsRecord(record.Id)
if err != nil {
return diag.FromErr(err)
}
d.SetId("")
return nil
}
func recordToData(record *client.DnsRecord, d *schema.ResourceData) diag.Diagnostics {
values := map[string]interface{}{
"name": record.Name,
"address": record.Address,
"ttl": record.Ttl,
}
d.SetId(record.Name)
var diags diag.Diagnostics
for key, value := range values {
if err := d.Set(key, value); err != nil {
diags = append(diags, diag.Errorf("failed to set %s: %v", key, err)...)
}
}
return diags
}
func prepareDnsRecord(d *schema.ResourceData) *client.DnsRecord {
dnsRecord := new(client.DnsRecord)
dnsRecord.Name = d.Get("name").(string)
dnsRecord.Ttl = d.Get("ttl").(int)
dnsRecord.Address = d.Get("address").(string)
dnsRecord.Comment = d.Get("comment").(string)
return dnsRecord
}