diff options
Diffstat (limited to 'clients/netbox/config_file_client.go')
-rw-r--r-- | clients/netbox/config_file_client.go | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/clients/netbox/config_file_client.go b/clients/netbox/config_file_client.go index d84dd80..c692b9f 100644 --- a/clients/netbox/config_file_client.go +++ b/clients/netbox/config_file_client.go | |||
@@ -150,6 +150,18 @@ func parseValues(v []string) ([]*net.IPNet, error) { | |||
150 | return out, nil | 150 | return out, nil |
151 | } | 151 | } |
152 | 152 | ||
153 | func parseIps(v []string) ([]net.IP, error) { | ||
154 | out := make([]net.IP, len(v)) | ||
155 | for i, n := range v { | ||
156 | ip := net.ParseIP(n) | ||
157 | if ip == nil { | ||
158 | return nil, fmt.Errorf("Invalid IP '%s'", n) | ||
159 | } | ||
160 | out[i] = ip | ||
161 | } | ||
162 | return out, nil | ||
163 | } | ||
164 | |||
153 | // GetServicesForVm returns a list of services for a named VM | 165 | // GetServicesForVm returns a list of services for a named VM |
154 | // | 166 | // |
155 | // The data format of the config file should be, under the key "services": | 167 | // The data format of the config file should be, under the key "services": |
@@ -183,3 +195,47 @@ func (c *ConfigFileNetboxClient) GetServicesForVm(ctx context.Context, vmName st | |||
183 | 195 | ||
184 | return vm, nil | 196 | return vm, nil |
185 | } | 197 | } |
198 | |||
199 | // GetDnsServersForSite gets a list of IP address for a site that are | ||
200 | // assigned to DNS servers. | ||
201 | // | ||
202 | // The data format of the config file should be, under the key "sites": | ||
203 | // | ||
204 | // map[string]map[string][]string{ | ||
205 | // "site_name": map[string][]string{ | ||
206 | // "dns-servers": []string{ | ||
207 | // "127.0.0.1", | ||
208 | // }, | ||
209 | // }, | ||
210 | // } | ||
211 | // | ||
212 | // In JSON format: | ||
213 | // | ||
214 | // { "sites": { | ||
215 | // "site": { | ||
216 | // "dns-servers": [ "127.0.0.1" ] | ||
217 | // } | ||
218 | // } | ||
219 | func (c *ConfigFileNetboxClient) GetDnsServersForSite(ctx context.Context, site string) ([]net.IP, error) { | ||
220 | s, ok := c.cfg["sites"] | ||
221 | if !ok { | ||
222 | return nil, fmt.Errorf("No key 'sites' in config file") | ||
223 | } | ||
224 | |||
225 | sites := map[string]map[string][]string{} | ||
226 | if err := mapstructure.Decode(s, &sites); err != nil { | ||
227 | return nil, fmt.Errorf("Key 'sites' not in correct format: %w", err) | ||
228 | } | ||
229 | |||
230 | tags, ok := sites[site] | ||
231 | if !ok { | ||
232 | return nil, fmt.Errorf("Site %s not in sites", site) | ||
233 | } | ||
234 | |||
235 | values, ok := tags["dns-servers"] | ||
236 | if !ok { | ||
237 | return nil, fmt.Errorf("Site %s not in sites", site) | ||
238 | } | ||
239 | |||
240 | return parseIps(values) | ||
241 | } | ||