-SLIVER_API = "/plstackapi/slivers/";
-SLICE_API = "/plstackapi/slices/";
-NODE_API = "/plstackapi/nodes/";
-SITE_API = "/plstackapi/sites/";
-USER_API = "/plstackapi/users/";
-DEPLOYMENT_API = "/plstackapi/deployments";
-
-SLICEPLUS_API = "/xoslib/slicesplus/";
-
-XOSModel = Backbone.Model.extend({
- /* from backbone-tastypie.js */
- //idAttribute: 'resource_uri',
-
- /* from backbone-tastypie.js */
- url: function() {
- var url = this.attributes.resource_uri;
-
- if (!url) {
- url = this.urlRoot + this.id;
- }
-
- if (!url) {
- // XXX I'm not sure this does anything useful
- url = ( _.isFunction( this.collection.url ) ? this.collection.url() : this.collection.url );
- url = url || this.urlRoot;
- }
-
- // remove any existing query parameters
- url && ( url.indexOf("?") > -1 ) && ( url = url.split("?")[0] );
-
- url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
-
- url && ( url += "?no_hyperlinks=1" );
-
- return url;
- },
-
- listMethods: function() {
- var res = [];\r
- for(var m in this) {\r
- if(typeof this[m] == "function") {\r
- res.push(m)\r
+if (! window.XOSLIB_LOADED ) {
+ window.XOSLIB_LOADED=true;
+
+ SLIVER_API = "/plstackapi/slivers/";
+ SLICE_API = "/plstackapi/slices/";
+ SLICEROLE_API = "/plstackapi/slice_roles/";
+ NODE_API = "/plstackapi/nodes/";
+ SITE_API = "/plstackapi/sites/";
+ USER_API = "/plstackapi/users/";
+ USERDEPLOYMENT_API = "/plstackapi/user_deployments/";
+ DEPLOYMENT_API = "/plstackapi/deployments/";
+ IMAGE_API = "/plstackapi/images/";
+ NETWORKTEMPLATE_API = "/plstackapi/networktemplates/";
+ NETWORK_API = "/plstackapi/networks/";
+ NETWORKSLIVER_API = "/plstackapi/networkslivers/";
+ SERVICE_API = "/plstackapi/services/";
+ SLICEPRIVILEGE_API = "/plstackapi/slice_privileges/";
+ NETWORKDEPLOYMENT_API = "/plstackapi/networkdeployments/";
+
+ /* changed as a side effect of the big rename
+ SLICEDEPLOYMENT_API = "/plstackapi/slice_deployments/";
+ USERDEPLOYMENT_API = "/plstackapi/user_deployments/";
+ */
+
+ SLICEDEPLOYMENT_API = "/plstackapi/slicedeployments/";
+ USERDEPLOYMENT_API = "/plstackapi/userdeployments/";
+
+ SLICEPLUS_API = "/xoslib/slicesplus/";
+
+ XOSModel = Backbone.Model.extend({
+ /* from backbone-tastypie.js */
+ //idAttribute: 'resource_uri',
+
+ /* from backbone-tastypie.js */
+ url: function() {
+ var url = this.attributes.resource_uri;
+
+ if (!url) {
+ if (this.id) {
+ url = this.urlRoot + this.id;
+ } else {
+ // this happens when creating a new model.
+ url = this.urlRoot;
+ }
+ }
+
+ if (!url) {
+ // XXX I'm not sure this does anything useful
+ url = ( _.isFunction( this.collection.url ) ? this.collection.url() : this.collection.url );
+ url = url || this.urlRoot;
+ }
+
+ // remove any existing query parameters
+ url && ( url.indexOf("?") > -1 ) && ( url = url.split("?")[0] );
+
+ url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
+
+ url && ( url += "?no_hyperlinks=1" );
+
+ return url;
+ },
+
+ listMethods: function() {
+ var res = [];\r
+ for(var m in this) {\r
+ if(typeof this[m] == "function") {\r
+ res.push(m)\r
+ }\r
}\r
- }\r
- return res;\r
- }
-});
-
-XOSCollection = Backbone.Collection.extend({
- objects: function() {
- return this.models.map(function(element) { return element.attributes; });
- },
-
- initialize: function(){
- this.sortVar = 'name';\r
- this.sortOrder = 'asc';\r
- },\r
+ return res;\r
+ },
+
+ xosValidate: function(attrs, options) {
+ errors = {};
+ foundErrors = false;
+ _.each(this.validators, function(validatorList, fieldName) {
+ _.each(validatorList, function(validator) {
+ if (fieldName in attrs) {
+ validatorResult = validateField(validator, attrs[fieldName], this)
+ if (validatorResult != true) {
+ errors[fieldName] = validatorResult;
+ foundErrors = true;
+ }
+ }
+ });
+ });
+ if (foundErrors) {
+ return errors;
+ }
+ // backbone.js semantics -- on successful validate, return nothing
+ }
+ });
+
+ XOSCollection = Backbone.Collection.extend({
+ objects: function() {
+ return this.models.map(function(element) { return element.attributes; });
+ },
+
+ initialize: function(){
+ this.isLoaded = false;
+ this.failedLoad = false;
+ this.startedLoad = false;
+ this.sortVar = 'name';\r
+ this.sortOrder = 'asc';\r
+ this.on( "sort", this.sorted );\r
+ },\r
\r
- simpleComparator: function( model ){\r
- parts=this.sortVar.split(".");\r
- result = model.get(parts[0]);\r
- for (index=1; index<parts.length; ++index) {\r
- result=result[parts[index]];\r
- }\r
- return result;\r
- },\r
+ relatedCollections: [],\r
+ foreignCollections: [],\r
\r
- comparator: function (left, right) {\r
- var l = this.simpleComparator(left);\r
- var r = this.simpleComparator(right);\r
+ sorted: function() {\r
+ //console.log("sorted " + this.modelName);\r
+ },\r
\r
- if (l === void 0) return -1;\r
- if (r === void 0) return 1;\r
+ simpleComparator: function( model ){\r
+ parts=this.sortVar.split(".");\r
+ result = model.get(parts[0]);\r
+ for (index=1; index<parts.length; ++index) {\r
+ result=result[parts[index]];\r
+ }\r
+ return result;\r
+ },\r
\r
- if (this.sortOrder=="desc") {\r
- return l < r ? 1 : l > r ? -1 : 0;\r
- } else {\r
- return l < r ? -1 : l > r ? 1 : 0;\r
- }\r
- },\r
+ comparator: function (left, right) {\r
+ var l = this.simpleComparator(left);\r
+ var r = this.simpleComparator(right);\r
\r
- startPolling: function() {\r
- if (!this._polling) {
- collection=this;
- setInterval(function() { console.log(collection); collection.fetch(); }, 10000);
- this._polling=true;
- this.fetch();
- }
- },
-
- maybeFetch: function(options){
- // Helper function to fetch only if this collection has not been fetched before.
- if(this._fetched){
- // If this has already been fetched, call the success, if it exists
- options.success && options.success();
- console.log("alreadyFetched");
- return;
- }
-
- // when the original success function completes mark this collection as fetched
- var self = this,
- successWrapper = function(success){
- return function(){
- self._fetched = true;
- success && success.apply(this, arguments);
+ if (l === void 0) return -1;\r
+ if (r === void 0) return 1;\r
+\r
+ if (this.sortOrder=="desc") {\r
+ return l < r ? 1 : l > r ? -1 : 0;\r
+ } else {\r
+ return l < r ? -1 : l > r ? 1 : 0;\r
+ }\r
+ },\r
+\r
+ fetchSuccess: function(collection, response, options) {\r
+ //console.log("fetch succeeded " + collection.modelName);\r
+ this.failedLoad = false;\r
+ this.fetching = false;\r
+ if (!this.isLoaded) {\r
+ this.isLoaded = true;\r
+ Backbone.trigger("xoslib:collectionLoadChange", this);\r
+ }\r
+ this.trigger("fetchStateChange");\r
+ if (options["orig_success"]) {\r
+ options["orig_success"](collection, response, options);\r
+ }\r
+ },\r
+\r
+ fetchFailure: function(collection, response, options) {\r
+ //console.log("fetch failed " + collection.modelName);\r
+ this.fetching = false;\r
+ if ((!this.isLoaded) && (!this.failedLoad)) {\r
+ this.failedLoad=true;\r
+ Backbone.trigger("xoslib:collectionLoadChange", this);\r
+ }\r
+ this.trigger("fetchStateChange");\r
+ if (options["orig_failure"]) {\r
+ options["orig_failure"](collection, response, options);\r
+ }\r
+ },\r
+\r
+ fetch: function(options) {\r
+ var self=this;\r
+ this.fetching=true;\r
+ //console.log("fetch " + this.modelName);\r
+ if (!this.startedLoad) {\r
+ this.startedLoad=true;\r
+ Backbone.trigger("xoslib:collectionLoadChange", this);\r
+ }\r
+ this.trigger("fetchStateChange");\r
+ if (options == undefined) {\r
+ options = {};\r
+ }\r
+ options["orig_success"] = options["success"];\r
+ options["orig_failure"] = options["failure"];\r
+ options["success"] = function(collection, response, options) { self.fetchSuccess.call(self, collection, response, options); };\r
+ options["failure"] = this.fetchFailure;\r
+ Backbone.Collection.prototype.fetch.call(this, options);\r
+ },\r
+\r
+ startPolling: function() {\r
+ if (!this._polling) {\r
+ var collection=this;
+ setInterval(function() { collection.fetch(); }, 10000);
+ this._polling=true;
+ this.fetch();
+ }
+ },
+
+ refresh: function(refreshRelated) {
+ if (!this.fetching) {
+ this.fetch();
+ }
+ if (refreshRelated) {
+ for (related in this.relatedCollections) {
+ related = xos[related];
+ if (!related.fetching) {
+ related.fetch();
+ }
+ }
+ }
+ },
+
+ maybeFetch: function(options){
+ // Helper function to fetch only if this collection has not been fetched before.
+ if(this._fetched){
+ // If this has already been fetched, call the success, if it exists
+ options.success && options.success();
+ console.log("alreadyFetched");
+ return;
+ }
+
+ // when the original success function completes mark this collection as fetched
+ var self = this,
+ successWrapper = function(success){
+ return function(){
+ self._fetched = true;
+ success && success.apply(this, arguments);
+ };
};
- };
- options.success = successWrapper(options.success);
- console.log("call fetch");
- this.fetch(options);
- },
-
- getOrFetch: function(id, options){
- // Helper function to use this collection as a cache for models on the server
- var model = this.get(id);
-
- if(model){
- options.success && options.success(model);
- return;
- }
+ options.success = successWrapper(options.success);
+ console.log("call fetch");
+ this.fetch(options);
+ },
+
+ getOrFetch: function(id, options){
+ // Helper function to use this collection as a cache for models on the server
+ var model = this.get(id);
+
+ if(model){
+ options.success && options.success(model);
+ return;
+ }
+
+ model = new this.model({
+ resource_uri: id
+ });
+
+ model.fetch(options);
+ },
+
+ filterBy: function(fieldName, value) {
+ filtered = this.filter(function(obj) {
+ return obj.get(fieldName) == value;
+ });
+ return new this.constructor(filtered);
+ },
+
+ /* from backbone-tastypie.js */
+ url: function( models ) {
+ var url = this.urlRoot || ( models && models.length && models[0].urlRoot );
+ url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
+
+ // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute
+ // (set to 'resource_uri') contains the model's id.
+ if ( models && models.length ) {
+ var ids = _.map( models, function( model ) {
+ var parts = _.compact( model.id.split('/') );
+ return parts[ parts.length - 1 ];
+ });
+ url += 'set/' + ids.join(';') + '/';
+ }
+
+ url && ( url += "?no_hyperlinks=1" );
+
+ return url;
+ },
- model = new this.model({
- resource_uri: id
- });
-
- model.fetch(options);
- },
+ listMethods: function() {
+ var res = [];\r
+ for(var m in this) {\r
+ if(typeof this[m] == "function") {\r
+ res.push(m)\r
+ }\r
+ }\r
+ return res;\r
+ },
+ });
- /* from backbone-tastypie.js */
- url: function( models ) {
- var url = this.urlRoot || ( models && models.length && models[0].urlRoot );
- url && ( url += ( url.length > 0 && url.charAt( url.length - 1 ) === '/' ) ? '' : '/' );
+ function define_model(lib, attrs) {
+ modelName = attrs.modelName;
+ modelClassName = modelName;
+ collectionClassName = modelName + "Collection";
- // Build a url to retrieve a set of models. This assume the last part of each model's idAttribute
- // (set to 'resource_uri') contains the model's id.
- if ( models && models.length ) {
- var ids = _.map( models, function( model ) {
- var parts = _.compact( model.id.split('/') );
- return parts[ parts.length - 1 ];
- });
- url += 'set/' + ids.join(';') + '/';
- }
+ if (!attrs.collectionName) {
+ attrs.collectionName = modelName + "s";
+ }
+ collectionName = attrs.collectionName;
+
+ modelAttrs = {}
+ collectionAttrs = {}
+
+ for (key in attrs) {
+ value = attrs[key];
+ if ($.inArray(key, ["urlRoot", "modelName", "validate"])>=0) {
+ modelAttrs[key] = value;
+ }
+ if ($.inArray(key, ["urlRoot", "modelName", "relatedCollections", "foreignCollections"])>=0) {
+ collectionAttrs[key] = value;
+ }
+ }
- url && ( url += "?no_hyperlinks=1" );
+ if (xosdefaults && xosdefaults[modelName]) {
+ modelAttrs["defaults"] = xosdefaults[modelName];
+ }
- return url;
- },
+ if (xosvalidators && xosvalidators[modelName]) {
+ modelAttrs["validators"] = xosvalidators[modelName];
+ }
- listMethods: function() {
- var res = [];\r
- for(var m in this) {\r
- if(typeof this[m] == "function") {\r
- res.push(m)\r
+ lib[modelName] = XOSModel.extend(modelAttrs);
+
+ collectionAttrs["model"] = lib[modelName];
+
+ lib[collectionClassName] = XOSCollection.extend(collectionAttrs);
+ lib[collectionName] = new lib[collectionClassName]();
+
+ lib.allCollectionNames.push(collectionName);
+ lib.allCollections.push(lib[collectionName]);
+ };
+
+ function xoslib() {
+ this.allCollectionNames = [];
+ this.allCollections = [];
+
+ define_model(this, {urlRoot: SLIVER_API,
+ relatedCollections: {"networkSlivers": "sliver"},
+ foreignCollections: ["slices", "deployments", "images", "nodes", "users"],
+ modelName: "sliver"});
+
+ define_model(this, {urlRoot: SLICE_API,
+ relatedCollections: {"slivers": "slice", "sliceDeployments": "slice", "slicePrivileges": "slice", "networks": "owner"},
+ foreignCollections: ["services", "sites"],
+ modelName: "slice",
+ validate: function(attrs, options) {
+ errors = XOSModel.prototype.validate(this, attrs, options);
+ // validate that slice.name starts with site.login_base
+ site = attrs.site || this.site;
+ if ((site!=undefined) && (attrs.name!=undefined)) {
+ site = xos.sites.get(site);
+ if (attrs.name.indexOf(site.attributes.login_base+"_") != 0) {
+ errors = errors || {};
+ errors["name"] = "must start with " + site.attributes.login_base + "_";
+ }
+ }
+ return errors;
+ },
+ });
+
+ define_model(this, {urlRoot: SLICEDEPLOYMENT_API,
+ foreignCollections: ["slices", "deployments"],
+ modelName: "sliceDeployment"});
+
+ define_model(this, {urlRoot: SLICEPRIVILEGE_API,
+ foreignCollections: ["slices", "users", "sliceRoles"],
+ modelName: "slicePrivilege"});
+
+ define_model(this, {urlRoot: SLICEROLE_API,
+ modelName: "sliceRole"});
+
+ define_model(this, {urlRoot: NODE_API,
+ foreignCollections: ["sites", "deployments"],
+ modelName: "node"});
+
+ define_model(this, {urlRoot: SITE_API,
+ relatedCollections: {"users": "site", "slices": "site", "nodes": "site"},
+ modelName: "site"});
+
+ define_model(this, {urlRoot: USER_API,
+ relatedCollections: {"slicePrivileges": "user", "slices": "owner", "userDeployments": "user"},
+ foreignCollections: ["sites"],
+ modelName: "user"});
+
+ define_model(this, {urlRoot: USERDEPLOYMENT_API,
+ foreignCollections: ["users","deployments"],
+ modelName: "userDeployment"});
+
+ define_model(this, { urlRoot: DEPLOYMENT_API,
+ relatedCollections: {"nodes": "deployment", "slivers": "deploymentNetwork", "networkDeployments": "deployment", "userDeployments": "deployment"},
+ modelName: "deployment"});
+
+ define_model(this, {urlRoot: IMAGE_API,
+ model: this.image,
+ modelName: "image"});
+
+ define_model(this, {urlRoot: NETWORKTEMPLATE_API,
+ modelName: "networkTemplate"});
+
+ define_model(this, {urlRoot: NETWORK_API,
+ relatedCollections: {"networkDeployments": "network", "networkSlivers": "network"},
+ foreignCollections: ["slices", "networkTemplates"],
+ modelName: "network"});
+
+ define_model(this, {urlRoot: NETWORKSLIVER_API,
+ modelName: "networkSliver"});
+
+ define_model(this, {urlRoot: NETWORKDEPLOYMENT_API,
+ modelName: "networkDeployment"});
+
+ define_model(this, {urlRoot: SERVICE_API,
+ modelName: "service"});
+
+ // enhanced REST
+ define_model(this, {urlRoot: SLICEPLUS_API,
+ relatedCollections: {'slivers': "slice"},
+ modelName: "slicePlus",
+ collectionName: "slicesPlus"});
+
+ this.listObjects = function() { return this.allCollectionNames; };
+
+ this.getCollectionStatus = function() {
+ stats = {isLoaded: 0, failedLoad: 0, startedLoad: 0};
+ for (index in this.allCollections) {
+ collection = this.allCollections[index];
+ if (collection.isLoaded) {
+ stats["isLoaded"] = stats["isLoaded"] + 1;
+ }
+ if (collection.failedLoad) {
+ stats["failedLoad"] = stats["failedLoad"] + 1;
+ }
+ if (collection.startedLoad) {
+ stats["startedLoad"] = stats["startedLoad"] + 1;
+ }
+ }
+ stats["completedLoad"] = stats["failedLoad"] + stats["isLoaded"];
+ return stats;
+ };
+ };
+
+ xos = new xoslib();
+
+ function getCookie(name) {
+ var cookieValue = null;\r
+ if (document.cookie && document.cookie != '') {\r
+ var cookies = document.cookie.split(';');\r
+ for (var i = 0; i < cookies.length; i++) {\r
+ var cookie = jQuery.trim(cookies[i]);\r
+ // Does this cookie string begin with the name we want?\r
+ if (cookie.substring(0, name.length + 1) == (name + '=')) {\r
+ cookieValue = decodeURIComponent(cookie.substring(name.length + 1));\r
+ break;\r
}\r
}\r
- return res;\r
- }
-});
-
-function xoslib() {
- // basic REST
- this.sliver = XOSModel.extend({ urlRoot: SLIVER_API });
- this.sliverCollection = XOSCollection.extend({ urlRoot: SLIVER_API,
- model: this.sliver});
- this.slivers = new this.sliverCollection();
-
- this.slice = XOSModel.extend({ urlRoot: SLICE_API });
- this.sliceCollection = XOSCollection.extend({ urlRoot: SLICE_API,
- model: this.slice});
- this.slices = new this.sliceCollection();
-
- this.node = XOSModel.extend({ urlRoot: NODE_API });
- this.nodeCollection = XOSCollection.extend({ urlRoot: NODE_API,
- model: this.node});
- this.nodes = new this.nodeCollection();
-
- this.site = XOSModel.extend({ urlRoot: SITE_API });
- this.siteCollection = XOSCollection.extend({ urlRoot: SITE_API,
- model: this.site});
- this.sites = new this.siteCollection();
-
- this.user = XOSModel.extend({ urlRoot: USER_API });
- this.userCollection = XOSCollection.extend({ urlRoot: USER_API,
- model: this.user});
- this.users = new this.userCollection();
-
- this.deployment = XOSModel.extend({ urlRoot: DEPLOYMENT_API });
- this.deploymentCollection = XOSCollection.extend({ urlRoot: DEPLOYMENT_API,
- model: this.deployment});
- this.deployments = new this.deploymentCollection();
-
- // enhanced REST
- this.slicePlus = XOSModel.extend({ urlRoot: SLICEPLUS_API });
- this.slicePlusCollection = XOSCollection.extend({ urlRoot: SLICEPLUS_API,
- model: this.slicePlus});
- this.slicesPlus = new this.slicePlusCollection();
-
- this.listObjects = function() { return ["slivers", "slices", "nodes", "sites", "users", "deployments"]; };
-};
-
-xos = new xoslib();
-
+ }\r
+ return cookieValue;\r
+ }
+
+ (function() {
+ var _sync = Backbone.sync;\r
+ Backbone.sync = function(method, model, options){\r
+ options.beforeSend = function(xhr){\r
+ var token = getCookie("csrftoken");\r
+ xhr.setRequestHeader('X-CSRFToken', token);\r
+ };\r
+ return _sync(method, model, options);\r
+ };\r
+ })();
+}