7b39308e35db2845d1461e41bd81541c161940e1
[plstackapi.git] / planetstack / core / static / shell / opencloud.js
1 var opencloud_data = [];
2 var opencloud_data_received = false;
3
4 function updateOpenCloud(onLoaded) {
5     $.ajax({url: "/admin/shelldata",
6         dataType: "json",
7         type: "GET",
8         success: function(data) {
9             opencloud_data = data;
10             if (!opencloud_data_received) {
11                 opencloud_data_received = true;
12                 if (onLoaded!=null) {
13                     onLoaded();
14                 }
15             }
16             // do this again in 30 seconds
17             setTimeout(function() {updateOpenCloud(onLoaded)}, 10000);
18         },
19         error: function() {
20             console.log("something went wrong. trying again");
21             // do this again in 30 seconds
22             setTimeout(function() {updateOpenCloud(onLoaded)}, 10000);
23         }
24     });
25 }
26
27 function OpenCloudModel(name) {
28     this.all = function() { return opencloud_data[name]; };
29
30     this.match = function(filterDict,obj) {
31                    for (var k in filterDict) {
32                        if (obj[k] == filterDict[k]) {
33                            return true;
34                        }
35                    }
36                    return false;
37                  };
38
39     this.filter = function(filterDict) {
40                    result = []
41                    all_objs = this.all()
42                    for (var k in all_objs) {
43                         obj = all_objs[k];
44                         if (this.match(filterDict, obj)) {
45                             result.push(obj);
46                         }
47                    }
48                    return result;
49                  };
50
51     this.get = function(filterDict) {
52                    return this.filter(filterDict)[0];
53                  };
54
55     this.__str__ = function() { return '["all", "filter", "get"]' };
56 }
57
58 function OpenCloud() {
59     this.slices = new OpenCloudModel("slices");
60     this.slivers = new OpenCloudModel("slivers");
61     this.nodes = new OpenCloudModel("nodes");
62     this.sites = new OpenCloudModel("sites");
63     this.__str__ = function() { return '["slices", "slivers", "nodes", "sites"]'; }
64 };