diff options
author | Daniel Isaksen <d@duniel.no> | 2018-12-06 14:59:57 +0100 |
---|---|---|
committer | Daniel Isaksen <d@duniel.no> | 2018-12-06 14:59:57 +0100 |
commit | 509a2b817a939ad3f29831bc6b7db5d736f0f147 (patch) | |
tree | 0b4680fc320d9e531fff33b1be51344166d3e2fe /alpine-netviz.js | |
download | alpine-netviz-509a2b817a939ad3f29831bc6b7db5d736f0f147.tar.bz2 alpine-netviz-509a2b817a939ad3f29831bc6b7db5d736f0f147.tar.xz |
first commit
Diffstat (limited to 'alpine-netviz.js')
-rw-r--r-- | alpine-netviz.js | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/alpine-netviz.js b/alpine-netviz.js new file mode 100644 index 0000000..0357fba --- /dev/null +++ b/alpine-netviz.js @@ -0,0 +1,154 @@ +jQuery(document).ready(function($) { + "use strict"; + + function NetBox() { + this.url = "http://netbox.alpin.pw/api"; + this.endpoints = { + circuits: [ + "providers", "circuit-types", "circuits", + "circuits-termination" + ], + dcim: [ + "regions", "sites", "rack-groups", "rack-roles", "racks", + "rack-reservations", "manufacturers", "device-types", + "console-port-templates", "console-server-port-templates", + "power-port-templates", "power-outlet-templates", + "interface-templates", "device-bay-templates", "device-roles", + "platforms", "devices", "console-ports", + "console-server-ports", "power-ports", "power-outlets", + "interfaces", "device-bays", "inventory-items", + "console-connections", "power-connections", + "interface-connections", "virtual-chassis", "connected-device" + ], + extras: [ + "graphs", "export-termplates", "topology-maps", "tags", + "image-attachments", "config-contexts", "reports", + "object-changes", "recent-activity" + ], + ipam: [ + "vrfs", "rirs", "aggregates", "roles", "prefixes", + "ip-addresses", "vlan-groups", "vlans", "services" + ], + secrets: [ + "secret-roles", "secrets", "get-session-key", + "generate-rsa-key-pair" + ], + tenancy: [ + "tenant-groups", "tenants" + ], + virtualization: [ + "cluster-types", "cluster-groups", "clusters", + "virtual-machines", "interfaces" + ] + }; + } + + NetBox.prototype.visjs_data = function(cb) { + this.pull("virtualization", "virtual-machines", function(r_vms) { + let vms = r_vms.results, + clusters = Array.from(new Set(vms.map(x => x.cluster.name))), + nodes = [], + edges = []; + + // add virtual machines to the list of nodes + vms.forEach(function(el, idx) { + nodes.push({ + id: el.name, + label: el.name, + group: "virtual_machines", + _cluster: el.cluster.name + }); + }); + + // add clusters to the list of nodes + clusters.forEach(function(el, idx) { + nodes.push({ + id: el, + label: el, + group: "clusters" + }); + }); + + // add edges between all non-cluster nodes + nodes + .filter(x => x.group !== "clusters") + .forEach(function(node) { + if (node._cluster) { + edges.push({ + from: node.id, + to: node._cluster + }); + } + }); + + // filter out all netbox clusters from the list of nodes + let t_nodes = nodes.filter(x => x.group === "clusters"); + + // make edges between all but the first and last nodes + t_nodes.forEach(function(node, idx) { + if (idx !== t_nodes.length - 1) { + edges.push({ + from: node.id, + to: t_nodes[idx + 1].id + }); + } + }); + + // last link + edges.push({ + from: t_nodes[0].id, + to: t_nodes[t_nodes.length - 1].id + }); + + cb({ + nodes: nodes, + edges: edges + }); + }); + } + + NetBox.prototype.pull = function(category, item, cb) { + if (!Object.keys(this.endpoints).includes(category)) { + throw new Error(`the category '${category}' is not in the list ` + + "of allowed categories"); + } + + if (!Object.values(this.endpoints[category]).includes(item)) { + throw new Error(`the item '${item}' is not in the list of ` + + "allowed endpoints"); + } + + $.ajax({ + url: `${this.url}/${category}/${item}/`, + data: { + format: "json", + limit: 1000 + }, + success: function(data) { + cb(data); + } + }); + } + + let nb = new NetBox(); + + nb.visjs_data(function(data) { + let container = document.getElementById("network"), + options = { + nodes: { + shape: 'dot', + size: 20, + font: { + size: 15, + color: '#ffffff' + }, + borderWidth: 2 + }, + edges: { + width: 2 + }, + groups: {} + }, + network = new vis.Network(container, data, options); + }); +}); |