/
resource_infoblox_host_record.go
164 lines (151 loc) · 4.52 KB
/
resource_infoblox_host_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"log"
"strings"
"fmt"
"github.com/go-resty/resty"
"github.com/hashicorp/terraform/helper/schema"
)
type Object struct {
Ref string `json:"_ref"`
}
type Host struct {
Object
Name, View string
Ttl int
Use_Ttl bool
Ipv4addrs []Ipv4
}
type Ipv4 struct {
Object
Host, Ipv4addr string
Configure_for_dhcp bool
}
type WapiError struct {
Error, Code, Text string
}
func resourceInfobloxHostRecord() *schema.Resource {
return &schema.Resource{
Create: resourceInfobloxHostRecordCreate,
Read: resourceInfobloxHostRecordRead,
Update: resourceInfobloxHostRecordUpdate,
Delete: resourceInfobloxHostRecordDelete,
Schema: map[string]*schema.Schema{
"domain": &schema.Schema{
Description: "The domain name to create these records in",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": &schema.Schema{
Description: "The subdomain of the record",
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"ipv4": &schema.Schema{
Description: "The ip-address or function used to generate one",
Type: schema.TypeString,
Required: true,
},
"ttl": &schema.Schema{
Description: "The TTL of the DNS record in seconds, used for client-cache invalidation",
Type: schema.TypeInt,
Optional: true,
Default: 600,
},
},
}
}
func resourceInfobloxHostRecordCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("\n[infoblox-provider] %s", "----------------- host record create")
name := d.Get("name").(string) + "." + d.Get("domain").(string)
ipv4 := d.Get("ipv4").(string)
ttl := d.Get("ttl").(int)
wapiErr := WapiError{}
resp, err := resty.R().
SetError(&wapiErr).
SetBody(map[string]interface{}{
"name": name,
"ipv4addrs": []map[string]interface{}{
map[string]interface{}{
"ipv4addr": ipv4,
},
},
"ttl": ttl,
"use_ttl": true,
}).
Post("/record:host")
if handler := handleError(err, resp, wapiErr); handler != nil {
return handler
}
d.SetId(strings.Replace(resp.String(), "\"", "", 2))
return resourceInfobloxHostRecordRead(d, meta)
}
func resourceInfobloxHostRecordRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("\n[infoblox-provider] %s", "----------------- host record read")
host := Host{}
wapiErr := WapiError{}
resp, err := resty.R().
SetResult(&host).
SetError(&wapiErr).
SetQueryParams(map[string]string{
"_return_fields+": "ttl,use_ttl",
}).
Get("/" + d.Id())
log.Printf("\n[infoblox-provider] Wapi Object: %+v", host)
if handler := handleError(err, resp, wapiErr); handler != nil {
return handler
}
splitFqdn := strings.Split(host.Name, ".")
d.Set("fqdn", host.Name)
d.Set("name", splitFqdn[0])
d.Set("domain", strings.Join(splitFqdn[1:], "."))
d.Set("ipv4", host.Ipv4addrs[0].Ipv4addr)
d.Set("ttl", host.Ttl)
d.Set("view", host.View)
return nil
}
func resourceInfobloxHostRecordUpdate(d *schema.ResourceData, meta interface{}) error {
log.Printf("\n[infoblox-provider] %s", "----------------- host record update")
ipv4 := d.Get("ipv4").(string)
wapiErr := WapiError{}
resp, err := resty.R().
SetError(&wapiErr).
SetBody(map[string]interface{}{
"ipv4addrs": []map[string]interface{}{
map[string]interface{}{
"ipv4addr": ipv4,
},
},
}).
Put("/" + d.Id())
if handler := handleError(err, resp, wapiErr); handler != nil {
return handler
}
return resourceInfobloxHostRecordRead(d, meta)
}
func resourceInfobloxHostRecordDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("\n[infoblox-provider] %s", "----------------- host record delete")
wapiErr := WapiError{}
resp, err := resty.R().
SetError(&wapiErr).
Delete("/" + d.Id())
if handler := handleError(err, resp, wapiErr); handler != nil {
return handler
}
return nil
}
func handleError(err error, resp *resty.Response, wapiErr WapiError) error {
log.Printf("\n[infoblox-provider] HTTP Code: (%v) Response Body: %v", resp.StatusCode(), resp)
if err != nil {
return fmt.Errorf("[infoblox-provider] Resty Error: %+v", err)
} else if resp.StatusCode() >= 300 && resp.StatusCode() < 400 {
return fmt.Errorf("[infoblox-provider] HTTP Redirect: (%v)", resp.StatusCode())
} else if resp.StatusCode() >= 400 && wapiErr.Error != "" {
return fmt.Errorf("[infoblox-provider] WAPI Error: (%v) %+v", resp.StatusCode(), wapiErr)
} else if resp.StatusCode() >= 400 {
return fmt.Errorf("[infoblox-provider] Unknown HTTP Error: (%v) %+v", resp.StatusCode(), resp.String())
}
return nil
}