aboutsummaryrefslogtreecommitdiff
path: root/clients/netbox/config_file_client.go
blob: c692b9f9339672ff877cc811fa5416dcbfc8b760 (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package netbox

import (
	"context"
	"encoding/json"
	"fmt"
	"io/fs"
	"net"
	"path/filepath"

	"github.com/mitchellh/mapstructure"
	"gopkg.in/yaml.v2"
)

type ConfigFileNetboxClient struct {
	cfg map[string]any
}

var _ NetboxClient = (*ConfigFileNetboxClient)(nil)

// NewConfigFileClient creates a new ConfigFileNetboxClient by loading a
// named config file from a filesystem and unmarshalling it. The config
// file can be in JSON or YAML format, determined by a .json, .yaml, or
// .yml extension. The configuration must be nested within a key in that
// file to support sharing the file with other subsystems.
//
// See method docs for the extected format of this file.
func NewConfigFileClient(filesystem fs.FS, name, key string) (NetboxClient, error) {
	fp, err := filesystem.Open(name)
	if err != nil {
		return nil, err
	}
	defer fp.Close()

	fc := map[string]any{}

	switch e := filepath.Ext(name); e {
	case ".yaml", ".yml":
		if err := yaml.NewDecoder(fp).Decode(&fc); err != nil {
			return nil, err
		}
	case ".json":
		if err := json.NewDecoder(fp).Decode(&fc); err != nil {
			return nil, err
		}
	default:
		return nil, fmt.Errorf("Config files with extension %s are not supported", e)
	}

	ck, ok := fc[key]
	if !ok {
		return nil, fmt.Errorf("Key %s does not exist in config file", key)
	}

	cfg := map[string]any{}
	if err := mapstructure.Decode(ck, &cfg); err != nil {
		return nil, fmt.Errorf("Config file key was not of correct type: %w", err)
	}

	return &ConfigFileNetboxClient{
		cfg: cfg,
	}, nil
}

// GetSitePrefixesWithTag gets a list of IP prefixes for a site based on
// some tag.
//
// The data format of the config file should be, under the key "sites":
//
//	map[string]map[string][]string{
//		"site_name": map[string][]string{
//			"tag_name": []string{
//				"127.0.0.1/24",
//			},
//		},
//	}
//
// In JSON format:
//
//	{ "sites": {
//	    "site": {
//	      "tag_name": [ "127.0.0.1/24" ]
//	    }
//	}
func (c *ConfigFileNetboxClient) GetSitePrefixesWithTag(ctx context.Context, site string, tag string) ([]*net.IPNet, error) {
	s, ok := c.cfg["sites"]
	if !ok {
		return nil, fmt.Errorf("No key 'sites' in config file")
	}

	sites := map[string]map[string][]string{}
	if err := mapstructure.Decode(s, &sites); err != nil {
		return nil, fmt.Errorf("Key 'sites' not in correct format: %w", err)
	}

	tags, ok := sites[site]
	if !ok {
		return nil, fmt.Errorf("Site %s not in sites", site)
	}

	values, ok := tags[tag]
	if !ok {
		return nil, fmt.Errorf("Tag %s not in tags", tag)
	}

	return parseValues(values)
}

// GetPrefixesWithTag gets a list fo IP prefixes based on some tag.
//
// The data format of the config file should be, under the key "tags":
//
//	map[string][]string{
//		"tag_name": []string{
//		    "127.0.0.1/24",
//		},
//	}
//
// In JSON format:
//
// { "tags": { "tag_name": [ "127.0.0.1/24" ] } }
func (c *ConfigFileNetboxClient) GetPrefixesWithTag(ctx context.Context, tag string) ([]*net.IPNet, error) {
	t, ok := c.cfg["tags"]
	if !ok {
		return nil, fmt.Errorf("No key 'tags' in config file")
	}

	tags := map[string][]string{}
	if err := mapstructure.Decode(t, &tags); err != nil {
		return nil, fmt.Errorf("Key 'tags' not in correct format: %w", err)
	}

	values, ok := tags[tag]
	if !ok {
		return nil, fmt.Errorf("Tag %s not in tags", tag)
	}

	return parseValues(values)
}

func parseValues(v []string) ([]*net.IPNet, error) {
	out := make([]*net.IPNet, len(v))
	for i, n := range v {
		_, in, err := net.ParseCIDR(n)
		if err != nil {
			return nil, err
		}
		out[i] = in
	}
	return out, nil
}

func parseIps(v []string) ([]net.IP, error) {
	out := make([]net.IP, len(v))
	for i, n := range v {
		ip := net.ParseIP(n)
		if ip == nil {
			return nil, fmt.Errorf("Invalid IP '%s'", n)
		}
		out[i] = ip
	}
	return out, nil
}

// GetServicesForVm returns a list of services for a named VM
//
// The data format of the config file should be, under the key "services":
//
//	map[string]map[string][]string{
//		"vm_name": []*Service{},
//	}
//
// In JSON format:
//
//	{ "services": {
//	    "myVm": [
//	      { "id": "myid }
//	    ]
//	}
func (c *ConfigFileNetboxClient) GetServicesForVm(ctx context.Context, vmName string) ([]*Service, error) {
	s, ok := c.cfg["services"]
	if !ok {
		return nil, fmt.Errorf("No key 'services' in config file")
	}

	services := map[string][]*Service{}
	if err := mapstructure.Decode(s, &services); err != nil {
		return nil, fmt.Errorf("Key 'services' not in correct format: %w", err)
	}

	vm, ok := services[vmName]
	if !ok {
		return nil, fmt.Errorf("VM named %s not in config", vmName)
	}

	return vm, nil
}

// GetDnsServersForSite gets a list of IP address for a site that are
// assigned to DNS servers.
//
// The data format of the config file should be, under the key "sites":
//
//	map[string]map[string][]string{
//		"site_name": map[string][]string{
//			"dns-servers": []string{
//				"127.0.0.1",
//			},
//		},
//	}
//
// In JSON format:
//
//	{ "sites": {
//	    "site": {
//	      "dns-servers": [ "127.0.0.1" ]
//	    }
//	}
func (c *ConfigFileNetboxClient) GetDnsServersForSite(ctx context.Context, site string) ([]net.IP, error) {
	s, ok := c.cfg["sites"]
	if !ok {
		return nil, fmt.Errorf("No key 'sites' in config file")
	}

	sites := map[string]map[string][]string{}
	if err := mapstructure.Decode(s, &sites); err != nil {
		return nil, fmt.Errorf("Key 'sites' not in correct format: %w", err)
	}

	tags, ok := sites[site]
	if !ok {
		return nil, fmt.Errorf("Site %s not in sites", site)
	}

	values, ok := tags["dns-servers"]
	if !ok {
		return nil, fmt.Errorf("Site %s not in sites", site)
	}

	return parseIps(values)
}