Created
July 10, 2018 16:49
-
-
Save tomislacker/a5295c1eca4f53d662e8ff209813b007 to your computer and use it in GitHub Desktop.
VPC Network Designer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from netaddr import *\n", | |
"vpc = IPNetwork(\"10.255.140.0/22\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"subnets = {\n", | |
" 'prodPublicA': ((23, 0), (24, 0), (25, 0), (26, 0)),\n", | |
" 'prodPublicB': ((23, 0), (24, 0), (25, 0), (26, 1)),\n", | |
" 'prodPublicC': ((23, 0), (24, 0), (25, 1), (26, 0)),\n", | |
" 'qaPublicA': ((23, 0), (24, 0), (25, 1), (26, 1)),\n", | |
" 'qaPublicB': ((23, 0), (24, 1), (25, 0), (26, 0)),\n", | |
" 'qaPublicC': ((23, 0), (24, 1), (25, 0), (26, 1)),\n", | |
" 'prodPrivateA': ((23, 0), (24, 1), (25, 1), (26, 0)),\n", | |
" 'prodPrivateB': ((23, 0), (24, 1), (25, 1), (26, 1)),\n", | |
" 'prodPrivateC': ((23, 1), (24, 0), (25, 0), (26, 0)),\n", | |
" 'unalloc01': ((23, 1), (24, 0), (25, 0), (26, 1)),\n", | |
" 'qaPrivateA': ((23, 1), (24, 0), (25, 1)),\n", | |
" 'qaPrivateB': ((23, 1), (24, 1), (25, 0)),\n", | |
" 'qaPrivateC': ((23, 1), (24, 1), (25, 1)),\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def spec_to_network(spec, vpc_cidr=None):\n", | |
" vpc_cidr = vpc_cidr or vpc\n", | |
" net = vpc_cidr\n", | |
" specs = []\n", | |
" for size, idx in spec:\n", | |
" try:\n", | |
" net = list(net.subnet(size))[idx]\n", | |
" specs.append((size, idx))\n", | |
" except IndexError:\n", | |
" print(\"IndexError\")\n", | |
" print(f\"Specs sliced so far: {specs}\")\n", | |
" print(f\"Current net {net}\")\n", | |
" raise\n", | |
" return net\n", | |
"\n", | |
"def subnets_to_networks(subnets, *args, **kwargs):\n", | |
" nethash = {}\n", | |
" for name, spec in subnets.items():\n", | |
" nethash.update({\n", | |
" name: spec_to_network(spec, *args, **kwargs)\n", | |
" })\n", | |
" return nethash\n", | |
"\n", | |
"def networks_are_valid(networks):\n", | |
" found_errors = 0\n", | |
" checked_names = []\n", | |
" for name1, net1 in networks.items():\n", | |
" checked_names.append(name1)\n", | |
" for name2, net2 in networks.items():\n", | |
" if name2 in checked_names:\n", | |
" # Ignore already checked\n", | |
" continue\n", | |
" if net1 in net2:\n", | |
" print(f\"{name1} ({net1}) is in {name2} ({net2})\")\n", | |
" found_errors += 1\n", | |
" elif net2 in net1:\n", | |
" print(f\"{name2} ({net2}) is in {name1} ({net1})\")\n", | |
" found_errors += 1\n", | |
"\n", | |
" return found_errors == 0" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Valid\n" | |
] | |
} | |
], | |
"source": [ | |
"networks = subnets_to_networks(subnets)\n", | |
"if networks_are_valid(networks):\n", | |
" print(\"Valid\")\n", | |
"else:\n", | |
" print(\"Invalid!\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"+EVPC (10.255.140.0/22, 100.0% Usage)-----+\n", | |
"| Name | Size | Subnet |\n", | |
"+--------------+------+-------------------+\n", | |
"| prodPublicA | 64 | 10.255.140.0/26 |\n", | |
"| prodPublicB | 64 | 10.255.140.64/26 |\n", | |
"| prodPublicC | 64 | 10.255.140.128/26 |\n", | |
"| qaPublicA | 64 | 10.255.140.192/26 |\n", | |
"| qaPublicB | 64 | 10.255.141.0/26 |\n", | |
"| qaPublicC | 64 | 10.255.141.64/26 |\n", | |
"| prodPrivateA | 64 | 10.255.141.128/26 |\n", | |
"| prodPrivateB | 64 | 10.255.141.192/26 |\n", | |
"| prodPrivateC | 64 | 10.255.142.0/26 |\n", | |
"| unalloc01 | 64 | 10.255.142.64/26 |\n", | |
"| qaPrivateA | 128 | 10.255.142.128/25 |\n", | |
"| qaPrivateB | 128 | 10.255.143.0/25 |\n", | |
"| qaPrivateC | 128 | 10.255.143.128/25 |\n", | |
"+--------------+------+-------------------+\n" | |
] | |
} | |
], | |
"source": [ | |
"from terminaltables import AsciiTable\n", | |
"from operator import itemgetter\n", | |
"\n", | |
"table_headers = [\n", | |
" 'Name',\n", | |
" 'Size',\n", | |
" 'Subnet',\n", | |
"]\n", | |
"table_data = sorted(\n", | |
" [\n", | |
" [name, len(net), str(net)]\n", | |
" for name, net in networks.items()\n", | |
" ],\n", | |
" key=lambda x: int(x[2][0]))\n", | |
"\n", | |
"usage = round(\n", | |
" sum([len(n) for n in networks.values()]) / float(len(vpc)), 3) * 100.0\n", | |
"title = f\"EVPC ({vpc}, {usage}% Usage)\"\n", | |
"print(AsciiTable([table_headers] + table_data, title).table)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment