-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinfoblox.go
182 lines (150 loc) · 4.08 KB
/
infoblox.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2018 Josh Lindsey <joshua.s.lindsey@gmail.com>
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
const (
doViewSep = `<<<<++++>>>>`
)
type ibTXTRecord struct {
Ref string `json:"_ref"`
DNSName string `json:"dns_name"`
Data string `json:"text"`
}
func (r *ibTXTRecord) ID() string {
return r.Ref
}
func (r *ibTXTRecord) Name() string {
return r.DNSName
}
func (r *ibTXTRecord) Text() string {
return r.Data
}
// IBConfig encodes the configuration for the Infoblox DNS Provider.
type IBConfig struct {
BaseURL string `json:"url"`
Username string `json:"username"`
Password string `json:"password"`
Views []string `json:"views"`
}
// IBProvider is the Infoblox DNS Provider.
type IBProvider struct {
client *http.Client
config *IBConfig
}
// NewIBProvider creates a new Infoblox DNS Provider from the provided config.
func NewIBProvider(config *IBConfig) *IBProvider {
return &IBProvider{
config: config,
client: &http.Client{},
}
}
// Get implements the DNSProvider interface Get method.
func (d *IBProvider) Get(id string) (TXTRecord, error) {
split := strings.Split(id, doViewSep)
uri := fmt.Sprintf("%s/%s", d.config.BaseURL, split[0])
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, err
}
req.SetBasicAuth(d.config.Username, d.config.Password)
q := req.URL.Query()
q.Set("_return_fields+", "dns_name")
req.URL.RawQuery = q.Encode()
resp, err := d.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("bad response from infoblox: %s", respBody)
}
out := new(ibTXTRecord)
err = json.Unmarshal(respBody, out)
if err != nil {
return nil, err
}
return out, nil
}
// Create implements the DNSProvider interface Create method.
func (d *IBProvider) Create(proto TXTRecord) (string, error) {
out := make([]string, len(d.config.Views))
for i, view := range d.config.Views {
uri := fmt.Sprintf("%s/record:txt", d.config.BaseURL)
reqBody := map[string]interface{}{
"name": proto.Name(),
"text": proto.Text(),
"view": view,
"ttl": acmeChallengeTTL,
}
body, err := json.Marshal(reqBody)
if err != nil {
return "", err
}
req, err := http.NewRequest("POST", uri, bytes.NewBuffer(body))
if err != nil {
return "", err
}
req.SetBasicAuth(d.config.Username, d.config.Password)
resp, err := d.client.Do(req)
if err != nil {
return "", err
}
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusCreated {
return "", fmt.Errorf("bad response from infoblox: %s", respBody)
}
out[i] = strings.Trim(string(respBody), `"`)
}
return strings.Join(out, doViewSep), nil
}
// Delete implements the DNSProvider interface Delete method.
func (d *IBProvider) Delete(joinedIDs string) error {
ids := strings.Split(joinedIDs, doViewSep)
for _, id := range ids {
uri := fmt.Sprintf("%s/%s", d.config.BaseURL, id)
req, err := http.NewRequest("DELETE", uri, nil)
if err != nil {
return err
}
req.SetBasicAuth(d.config.Username, d.config.Password)
resp, err := d.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad response from infoblox: %s", respBody)
}
}
return nil
}