diff options
author | Mike Crute <mike@crute.us> | 2023-09-27 16:56:29 -0700 |
---|---|---|
committer | Mike Crute <mike@crute.us> | 2023-09-27 16:56:29 -0700 |
commit | 4a4105de661955cf84b62253c4038201a7526e44 (patch) | |
tree | 401fdd6ba7d7f89fc11dc52895dc51680b575664 /clients | |
parent | 02f5fbcdd06c50d13c4f4df09fc29ca479dd492b (diff) | |
download | golib-4a4105de661955cf84b62253c4038201a7526e44.tar.bz2 golib-4a4105de661955cf84b62253c4038201a7526e44.tar.xz golib-4a4105de661955cf84b62253c4038201a7526e44.zip |
netbox: add GetDnsServersForSiteclients/netbox/v4.1.0
Diffstat (limited to 'clients')
-rw-r--r-- | clients/netbox/client.go | 23 | ||||
-rw-r--r-- | clients/netbox/config_file_client.go | 56 | ||||
-rw-r--r-- | clients/netbox/config_file_client_test.go | 12 |
3 files changed, 91 insertions, 0 deletions
diff --git a/clients/netbox/client.go b/clients/netbox/client.go index 3b7fbaf..aceebed 100644 --- a/clients/netbox/client.go +++ b/clients/netbox/client.go | |||
@@ -11,6 +11,7 @@ type NetboxClient interface { | |||
11 | GetSitePrefixesWithTag(ctx context.Context, site string, tag string) ([]*net.IPNet, error) | 11 | GetSitePrefixesWithTag(ctx context.Context, site string, tag string) ([]*net.IPNet, error) |
12 | GetPrefixesWithTag(ctx context.Context, tag string) ([]*net.IPNet, error) | 12 | GetPrefixesWithTag(ctx context.Context, tag string) ([]*net.IPNet, error) |
13 | GetServicesForVm(ctx context.Context, vmName string) ([]*Service, error) | 13 | GetServicesForVm(ctx context.Context, vmName string) ([]*Service, error) |
14 | GetDnsServersForSite(ctx context.Context, site string) ([]net.IP, error) | ||
14 | } | 15 | } |
15 | 16 | ||
16 | type BasicNetboxClient struct { | 17 | type BasicNetboxClient struct { |
@@ -145,3 +146,25 @@ func (c *BasicNetboxClient) GetServicesForVm(ctx context.Context, vmName string) | |||
145 | 146 | ||
146 | return out, nil | 147 | return out, nil |
147 | } | 148 | } |
149 | |||
150 | func (c *BasicNetboxClient) GetDnsServersForSite(ctx context.Context, site string) ([]net.IP, error) { | ||
151 | var m dnsServerGQLResponse | ||
152 | if err := c.Do(ctx, &NetboxGraphQLRequest{dnsServerGQLQuery}, &m); err != nil { | ||
153 | return nil, err | ||
154 | } | ||
155 | |||
156 | out := []net.IP{} | ||
157 | for _, a := range m.Data.AddressList { | ||
158 | if a.AssignedObject.VirtualMachine.Site.Name == site || | ||
159 | a.AssignedObject.Device.Site.Name == site { | ||
160 | |||
161 | ip, _, err := net.ParseCIDR(a.Address) | ||
162 | if err != nil { | ||
163 | return nil, err | ||
164 | } | ||
165 | |||
166 | out = append(out, ip) | ||
167 | } | ||
168 | } | ||
169 | return out, nil | ||
170 | } | ||
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 | } | ||
diff --git a/clients/netbox/config_file_client_test.go b/clients/netbox/config_file_client_test.go index 117db00..7af4647 100644 --- a/clients/netbox/config_file_client_test.go +++ b/clients/netbox/config_file_client_test.go | |||
@@ -13,11 +13,15 @@ import ( | |||
13 | var ( | 13 | var ( |
14 | ipNet1 *net.IPNet | 14 | ipNet1 *net.IPNet |
15 | ipNet2 *net.IPNet | 15 | ipNet2 *net.IPNet |
16 | ip1 net.IP | ||
17 | ip2 net.IP | ||
16 | ) | 18 | ) |
17 | 19 | ||
18 | func init() { | 20 | func init() { |
19 | _, ipNet1, _ = net.ParseCIDR("127.0.0.1/8") | 21 | _, ipNet1, _ = net.ParseCIDR("127.0.0.1/8") |
20 | _, ipNet2, _ = net.ParseCIDR("10.0.10.0/24") | 22 | _, ipNet2, _ = net.ParseCIDR("10.0.10.0/24") |
23 | ip1 = net.ParseIP("127.0.0.1") | ||
24 | ip2 = net.ParseIP("127.0.0.2") | ||
21 | } | 25 | } |
22 | 26 | ||
23 | var testFs fstest.MapFS | 27 | var testFs fstest.MapFS |
@@ -57,6 +61,7 @@ netbox: | |||
57 | sites: | 61 | sites: |
58 | site1: | 62 | site1: |
59 | tag1: [ "127.0.0.1/8", "10.0.10.0/24" ] | 63 | tag1: [ "127.0.0.1/8", "10.0.10.0/24" ] |
64 | dns-servers: [ "127.0.0.1", "127.0.0.2" ] | ||
60 | tags: | 65 | tags: |
61 | tag1: [ "127.0.0.1/8", "10.0.10.0/24" ] | 66 | tag1: [ "127.0.0.1/8", "10.0.10.0/24" ] |
62 | services: | 67 | services: |
@@ -186,6 +191,13 @@ func (s *ConfigFileNetboxClientSuite) TestGetServicesForVMMissingVM() { | |||
186 | assert.ErrorContains(s.T(), err, "VM named foo not in config") | 191 | assert.ErrorContains(s.T(), err, "VM named foo not in config") |
187 | } | 192 | } |
188 | 193 | ||
194 | func (s *ConfigFileNetboxClientSuite) TestGetDnsServersForSite() { | ||
195 | ips, err := s.c.GetDnsServersForSite(context.TODO(), "site1") | ||
196 | assert.NoError(s.T(), err) | ||
197 | assert.True(s.T(), ip1.Equal(ips[0])) | ||
198 | assert.True(s.T(), ip2.Equal(ips[1])) | ||
199 | } | ||
200 | |||
189 | func TestConfigFileNetboxClientSuite(t *testing.T) { | 201 | func TestConfigFileNetboxClientSuite(t *testing.T) { |
190 | suite.Run(t, &ConfigFileNetboxClientSuite{}) | 202 | suite.Run(t, &ConfigFileNetboxClientSuite{}) |
191 | } | 203 | } |