summaryrefslogtreecommitdiffstats
path: root/alpine-netviz.js
blob: 7820ef2bf856c416d51dc1a41a573485ce0711ba (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
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,
                        color: { color: "#FF851B" }
                    });
                }
            });

            // last link
            edges.push({
                from:  t_nodes[0].id,
                to:    t_nodes[t_nodes.length - 1].id,
                color: { color: "#FF851B" }
            });

            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: {
                    virtual_machines: {
                        shape: "icon",
                        icon: {
                            face:  "FontAwesome",
                            code:  "\uF233",
                            color: "#0074D9"
                        }
                    },
                    clusters: {
                        shape: "icon",
                        icon: {
                            face:  "FontAwesome",
                            code:  "\uF0C2",
                            color: "#FF851B"
                        }
                    }
                }
            },
            network = new vis.Network(container, data, options);
    });
});