Skip to content

Commit ebc5907

Browse files
Merge pull request #1 from commitconfirmed/dev
Push 0.2.0 changes into main
2 parents 2c38e0c + 22df8a8 commit ebc5907

19 files changed

+807
-371
lines changed

.coverage

0 Bytes
Binary file not shown.

README.md

+120-47
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ JSON Schema (for) Network as Code
88

99
The majority of Network and Infrastructure automation is done in YAML. Be it Ansible Host Variables, Network Device data to build a Jinja2 configuration with, or just a collection of data you want to run a script against to put into another product you have likely written a YAML file and have had to debug a typo or had to help another colleague build a new file for a new device.
1010

11-
In an ideal world you can (and should) put a lot of this data into a database or Network Source of Truth solution and pull from it so the validation is done for you. However, these solutions don't cover every case and generally don't play nice with a GIT CI/CD process so you still will likely end up creating some YAML files here and there.
11+
In an ideal world you can (and should) put a lot of this data into a database or Network Source of Truth solution and pull from it so the validation is done for you. However, these solutions don't cover every use case so you will likely end up creating some YAML files here and there.
1212

13-
Using a JSON schema for validating your YAML is a good practice in a CI/CD world but is very cumbersome to create from scratch.
13+
Using a JSON schema for validating & documenting your YAML is a good practice in a CI/CD world but is very cumbersome to create from scratch.
1414

15-
This project aims to simplify this whole process to help you build a decent JSON schema base from a YAML file with network and infrastructure data definitions in mind.
15+
This project aims to simplify this whole process by helping you build a JSON schema using YAML syntax that has network and infrastructure templates (or sub-schemas) in mind.
1616

1717
Now you can hopefully catch those rare mistakes before you run that Playbook, create that configuration with a Jinja2 template or run a REST query to that Source of Truth or Telemetry solution :)
1818

@@ -24,7 +24,7 @@ Take a basic Ansible host_vars YAML file for a host below:
2424
chassis:
2525
hostname: "ceos-spine1"
2626
model: "ceos"
27-
type: "router"
27+
device_type: "router"
2828

2929
system:
3030
domain_name: "example.com"
@@ -40,55 +40,128 @@ interfaces:
4040
ipv4: "10.1.0.20/24"
4141
```
4242
43-
You can simply write out how you would like to validate this data, and this program will write out a JSON schema you can use. You can just also keep your existing data if you just want some basic type validation (string, integer, float, array, etc.).
43+
You can simply write out how you would like to document & validate this data in YAML using kinds, and this program will write out a JSON schema you can use.
4444
4545
```yaml
46-
chassis:
47-
hostname:
48-
jsnac_type: pattern
49-
jsnac_pattern: "^ceos-[a-zA-Z]{1,16}[0-9]$"
50-
model: "ceos"
51-
type:
52-
jsnac_type: choice
53-
jsnac_choices: ["router", "switch", "spine", "leaf"]
54-
55-
system:
56-
domain_name:
57-
jsnac_type: domain
58-
ntp_servers:
59-
jsnac_type: ipv4
60-
61-
interfaces:
62-
- if:
63-
jsnac_type: string
64-
desc:
65-
jsnac_type: string
66-
ipv4:
67-
jsnac_type: ipv4_cidr
68-
ipv6:
69-
jsnac_type: ipv6_cidr
46+
header:
47+
title: "Ansible host vars"
48+
49+
schema:
50+
chassis:
51+
title: "Chassis"
52+
type: "object"
53+
properties:
54+
hostname:
55+
kind: { name: "string" }
56+
model:
57+
kind: { name: "string" }
58+
device_type:
59+
kind: { name: "choice", choices: [ "router", "switch", "firewall", "load-balancer" ] }
60+
system:
61+
type: "object"
62+
properties:
63+
domain_name:
64+
kind: { name: "string" }
65+
ntp_servers:
66+
type: "array"
67+
items:
68+
kind: { name: "ipv4" }
69+
interfaces:
70+
type: "array"
71+
items:
72+
type: "object"
73+
properties:
74+
if:
75+
kind: { name: "string" }
76+
desc:
77+
kind: { name: "string" }
78+
ipv4:
79+
kind: { name: "ipv4_cidr" }
80+
ipv6:
81+
kind: { name: "ipv6_cidr" }
7082
```
7183
72-
Alternatively, you can also just use dictionaries inplace:
84+
We also have full support for writing your own titles, descriptions, kinds (sub-schemas), objects that are required, etc. A more fleshed out example of the same schema is below:
7385
7486
```yaml
75-
chassis:
76-
hostname: { jsnac_type: pattern, jsnac_pattern: "^ceos-[a-zA-Z]{1,16}[0-9]$" }
77-
model: "ceos"
78-
type: { jsnac_type: choice, jsnac_choices: ["router", "switch", "spine", "leaf"] }
79-
80-
system:
81-
domain_name: { jsnac_type: domain }
82-
ntp_servers: { jsnac_type: ipv4 }
83-
84-
interfaces:
85-
- if: { jsnac_type: string }
86-
desc: { jsnac_type: string }
87-
ipv4: { jsnac_type: ipv4_cidr }
88-
ipv6: { jsnac_type: ipv6_cidr }
87+
header:
88+
id: "example-schema.json"
89+
title: "Ansible host vars"
90+
description: |
91+
Ansible host vars for my networking device. Requires the below objects:
92+
- chassis
93+
- system
94+
- interfaces
95+
96+
kinds:
97+
hostname:
98+
title: "Hostname"
99+
description: "Hostname of the device"
100+
type: "pattern"
101+
regex: "^[a-zA-Z0-9-]{1,63}$"
102+
103+
schema:
104+
chassis:
105+
title: "Chassis"
106+
description: |
107+
Object containing Chassis information. Has the below properties:
108+
hostname [required]: hostname
109+
model [required]: string
110+
device_type [required]: choice (router, switch, firewall, load-balancer)
111+
type: "object"
112+
properties:
113+
hostname:
114+
kind: { name: "hostname" }
115+
model:
116+
kind: { name: "string" }
117+
device_type:
118+
title: "Device Type"
119+
description: |
120+
Device Type options are:
121+
router, switch, firewall, load-balancer
122+
kind: { name: "choice", choices: [ "router", "switch", "firewall", "load-balancer" ] }
123+
required: [ "hostname", "model", "device_type" ]
124+
system:
125+
title: "System"
126+
description: |
127+
Object containing System information. Has the below properties:
128+
domain_name [required]: string
129+
ntp_servers [required]: list of ipv4 addresses
130+
type: "object"
131+
properties:
132+
domain_name:
133+
kind: { name: "string" }
134+
ntp_servers:
135+
title: "NTP Servers"
136+
description: "List of NTP servers"
137+
type: "array"
138+
items:
139+
kind: { name: "ipv4" }
140+
required: [ "domain_name", "ntp_servers" ]
141+
interfaces:
142+
title: "Device Interfaces"
143+
description: |
144+
List of device interfaces. Each interface has the below properties:
145+
if [required]: string
146+
desc: string
147+
ipv4: ipv4_cidr
148+
ipv6: ipv6_cidr
149+
type: "array"
150+
items:
151+
type: "object"
152+
properties:
153+
if:
154+
kind: { name: "string" }
155+
desc:
156+
kind: { name: "string" }
157+
ipv4:
158+
kind: { name: "ipv4_cidr" }
159+
ipv6:
160+
kind: { name: "ipv6_cidr" }
161+
required: [ "if" ]
89162
```
90163
91-
A full list of jsnac_types is available in the documentation (readthedocs coming soon)
164+
A full list of kinds are available in the ![documentation](https://jsnac.readthedocs.io/en/latest/)
92165
93166
## Usage
94167
@@ -104,7 +177,7 @@ jsnac -f data/example-jsnac.yml
104177
# Build a JSON schema from a YAML file and save it to a custom file
105178
jsnac -f data/example-jsnac.yml -o my.schema.json
106179

107-
# Increase the verbosity of the output
180+
# Increase the verbosity of the output (this generates alot of messages as I use it for debugging)
108181
jsnac -f data/example-jsnac.yml -v
109182
```
110183

@@ -129,7 +202,7 @@ def main():
129202
# jsnac.add_json(json_data)
130203

131204
# Build the JSON schema
132-
schema = jsnac.build()
205+
schema = jsnac.build_schema()
133206
print(schema)
134207

135208
if __name__ == '__main__':

data/example-jsnac.json

+103-35
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,112 @@
11
{
2-
"chassis": {
2+
"header": {
3+
"id": "example-schema.json",
4+
"schema": "http://json-schema.org/draft-07/schema",
5+
"title": "Example Schema",
6+
"description": "Ansible host vars for my networking device. Requires the below objects:\n- chassis\n- system\n- interfaces\n"
7+
},
8+
"kinds": {
39
"hostname": {
4-
"jsnac_type": "pattern",
5-
"jsnac_pattern": "^ceos-[a-zA-Z]{1,16}[0-9]$"
6-
},
7-
"model": "ceos",
8-
"type": {
9-
"jsnac_type": "choice",
10-
"jsnac_choices": [
11-
"router",
12-
"switch",
13-
"spine",
14-
"leaf"
15-
]
10+
"title": "Hostname",
11+
"description": "Hostname of the device",
12+
"type": "pattern",
13+
"regex": "^[a-zA-Z0-9-]{1,63}$"
1614
}
1715
},
18-
"system": {
19-
"domain_name": {
20-
"jsnac_type": "domain"
21-
},
22-
"ntp_servers": [
23-
{
24-
"jsnac_type": "ipv4"
25-
}
26-
]
27-
},
28-
"interfaces": [
29-
{
30-
"if": {
31-
"jsnac_type": "string"
16+
"schema": {
17+
"chassis": {
18+
"title": "Chassis",
19+
"description": "Object containing Chassis information. Has the below properties: \nhostname [required]: hostname\nmodel [required]: string\ndevice_type [required]: choice (router, switch, firewall, load-balancer)\n",
20+
"type": "object",
21+
"properties": {
22+
"hostname": {
23+
"kind": {
24+
"name": "hostname"
25+
}
26+
},
27+
"model": {
28+
"kind": {
29+
"name": "string"
30+
}
31+
},
32+
"device_type": {
33+
"title": "Device Type",
34+
"description": "Device Type options are:\nrouter, switch, firewall, load-balancer\n",
35+
"kind": {
36+
"name": "choice",
37+
"choices": [
38+
"router",
39+
"switch",
40+
"firewall",
41+
"load-balancer"
42+
]
43+
}
44+
}
3245
},
33-
"desc": {
34-
"jsnac_type": "string"
35-
},
36-
"ipv4": {
37-
"jsnac_type": "ipv4_cidr"
46+
"required": [
47+
"hostname",
48+
"model",
49+
"device_type"
50+
]
51+
},
52+
"system": {
53+
"title": "System",
54+
"description": "Object containing System information. Has the below properties:\ndomain_name [required]: string\nntp_servers [required]: list of ipv4 addresses\n",
55+
"type": "object",
56+
"properties": {
57+
"domain_name": {
58+
"kind": {
59+
"name": "string"
60+
}
61+
},
62+
"ntp_servers": {
63+
"title": "NTP Servers",
64+
"description": "List of NTP servers",
65+
"type": "array",
66+
"items": {
67+
"kind": {
68+
"name": "ipv4"
69+
}
70+
}
71+
}
3872
},
39-
"ipv6": {
40-
"jsnac_type": "ipv6_cidr"
73+
"required": [
74+
"domain_name",
75+
"ntp_servers"
76+
]
77+
},
78+
"interfaces": {
79+
"title": "Device Interfaces",
80+
"description": "List of device interfaces. Each interface has the below properties:\nif [required]: string\ndesc: string\nipv4: ipv4_cidr\nipv6: ipv6_cidr\n",
81+
"type": "array",
82+
"items": {
83+
"type": "object",
84+
"properties": {
85+
"if": {
86+
"kind": {
87+
"name": "string"
88+
}
89+
},
90+
"desc": {
91+
"kind": {
92+
"name": "string"
93+
}
94+
},
95+
"ipv4": {
96+
"kind": {
97+
"name": "ipv4_cidr"
98+
}
99+
},
100+
"ipv6": {
101+
"kind": {
102+
"name": "ipv6_cidr"
103+
}
104+
}
105+
},
106+
"required": [
107+
"if"
108+
]
41109
}
42110
}
43-
]
111+
}
44112
}

0 commit comments

Comments
 (0)