diff --git a/views/ngXosLib/.eslintrc b/views/ngXosLib/.eslintrc
new file mode 100644
index 0000000..cf02168
--- /dev/null
+++ b/views/ngXosLib/.eslintrc
@@ -0,0 +1,41 @@
+{
+    "ecmaFeatures": {
+        "blockBindings": true,
+        "forOf": true,
+        "destructuring": true,
+        "arrowFunctions": true,
+        "templateStrings": true
+    },
+    "env": { 
+        "browser": true,
+        "node": true,
+        "es6": true
+    },
+    "plugins": [
+        //"angular"
+    ],
+    "rules": {
+        "quotes": [2, "single"],
+        "camelcase": [0, {"properties": "always"}],
+        "no-underscore-dangle": 1,
+        "eqeqeq": [2, "smart"],
+        "no-alert": 1,
+        "key-spacing": [1, { "beforeColon": false, "afterColon": true }],
+        "indent": [2, 2],
+        "no-irregular-whitespace": 1,
+        "eol-last": 0,
+        "comma-spacing": [1, {"before": false, "after": true}],
+        "no-trailing-spaces": [1, { skipBlankLines: true }],
+        "no-unused-vars": [1, {"vars": "all", "args": "after-used"}],
+        "new-cap": 0,
+
+        //"angular/ng_module_name": [2, '/^xos\.*[a-z]*$/'],
+        //"angular/ng_controller_name": [2, '/^[a-z].*Ctrl$/'],
+        //"angular/ng_service_name": [2, '/^[A-Z].*Service$/'],
+        //"angular/ng_directive_name": [2, '/^[a-z]+[[A-Z].*]*$/'],
+        //"angular/ng_di": [0, "function or array"]
+    },
+    "globals" :{
+        "angular": true
+    } 
+}
\ No newline at end of file
diff --git a/views/ngXosLib/.gitignore b/views/ngXosLib/.gitignore
index bb93d68..5df6f2d 100644
--- a/views/ngXosLib/.gitignore
+++ b/views/ngXosLib/.gitignore
@@ -1,2 +1,5 @@
 node_modules
-bower_components
\ No newline at end of file
+bower_components
+docs
+xosHelpers/.tmp
+xos
\ No newline at end of file
diff --git a/views/ngXosLib/README.md b/views/ngXosLib/README.md
index cfce430..2eb73cb 100644
--- a/views/ngXosLib/README.md
+++ b/views/ngXosLib/README.md
@@ -10,9 +10,9 @@
 
 Usage: `npm run apigen`
 
-This tool will automatically generate an angular resource file for each endpoint available in Swagger.
+This tool will automatically generate an angular resource file for each endpoint available in Apiary.
 
->You can generate api related documentation with: `npm run apidoc`. The output is locate in `api/docs`. You can have a list of available method also trough Swagger at `http://localhost:9999/docs/`
+>You can generate api related documentation with: `npm run doc`. The output is locate in `api/docs`. You can have a list of available method also trough Apiary at `http://docs.xos.apiary.io/#`
 
 ### Vendors
 
@@ -21,9 +21,10 @@
 - angular-route
 - angular-resource
 - angular-cookie
+- angular-animate
 - ng-lodash
 
-This libraries are server through Django, so they will not be included in your minified vendor file. To add a library and generate a new file (that will override the old one), you should:
+This libraries are served through Django, so they will not be included in your minified vendor file. To add a library and generate a new file (that will override the old one), you should:
 - enter `ngXosLib` folder
 - run `bower install [myPackage] --save`
 - rebuild the file with `gulp vendor`
@@ -45,12 +46,11 @@
 It will automatically ad a `token` to all your request, eventually you can take advantage of some other services:
 
 - **NoHyperlinks Interceptor**: will add a `?no_hyperlinks=1` to your request, to tell Django to return ids instead of links.
-- **XosApi** wrapper for `/xos` endpoints.
-- **XoslibApi** wrapper for `/xoslib` endpoints.
-- **HpcApi** wrapper for `/hpcapi` endpoints.
 
 >_NOTE: for the API related service, check documentation in [Apigen](#apigen) section._
 
+To develop components inside this folder there is a particular command: `npm run dev`, this will watch the helpers file and rebuild them with sourcemaps. For this reason remember to build them when done developing.
+
 When some changes are applied to this common library it should be rebuilt with: `npm run build`
 
 ### Yo Xos
@@ -85,7 +85,7 @@
 
 To install a local dependency use bower with `--save`. Common modules are saved in `devDependencies` as they already loaded in the Django template.
 
-The `npm start` command is watching your dependencies and will automatically inject it in your `index.html`.
+The `npm start` command is watching your dependencies and will automatically inject them in your `index.html`.
 
 #### Linting
 
@@ -98,6 +98,4 @@
 
 ## TODO
 
-- Use Angular $resource instead of $http
-- Use ngDoc instead of jsDoc
 - Define styleguide (both visual and js) and if needed define some UI components
\ No newline at end of file
diff --git a/views/ngXosLib/api/ng-hpcapi.js b/views/ngXosLib/api/ng-hpcapi.js
deleted file mode 100755
index 762b304..0000000
--- a/views/ngXosLib/api/ng-hpcapi.js
+++ /dev/null
@@ -1,5440 +0,0 @@
-/*jshint -W069 */
-/*global angular:false */
-angular.module('xos.hpcapi', [])
-    .factory('hpcapi', ['$q', '$http', '$rootScope', function($q, $http, $rootScope) {
-        'use strict';
-
-        /**
-         * 
-         * @class hpcapi
-         * @param {(string|object)} [domainOrOptions] - The project domain or options object. If object, see the object's optional properties.
-         * @param {string} [domainOrOptions.domain] - The project domain
-         * @param {string} [domainOrOptions.cache] - An angularjs cache implementation
-         * @param {object} [domainOrOptions.token] - auth token - object with value property and optional headerOrQueryName and isQuery properties
-         * @param {string} [cache] - An angularjs cache implementation
-         */
-        var hpcapi = (function() {
-            function hpcapi(options, cache) {
-                var domain = (typeof options === 'object') ? options.domain : options;
-                this.domain = typeof(domain) === 'string' ? domain : 'http://localhost:9999';
-                cache = cache || ((typeof options === 'object') ? options.cache : cache);
-                this.cache = cache;
-            }
-
-            hpcapi.prototype.$on = function($scope, path, handler) {
-                var url = domain + path;
-                $scope.$on(url, function() {
-                    handler();
-                });
-                return this;
-            };
-
-            hpcapi.prototype.$broadcast = function(path) {
-                var url = domain + path;
-                //cache.remove(url);
-                $rootScope.$broadcast(url);
-                return this;
-            };
-
-            hpcapi.transformRequest = function(obj) {
-                var str = [];
-                for (var p in obj) {
-                    var val = obj[p];
-                    if (angular.isArray(val)) {
-                        val.forEach(function(val) {
-                            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(val));
-                        });
-                    } else {
-                        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(val));
-                    }
-                }
-                return str.join("&");
-            };
-
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Api_Root_GET
-             * 
-             */
-            hpcapi.prototype.Hpc_Api_Root_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Health_Check_List_GET
-             * 
-             */
-            hpcapi.prototype.Hpc_Health_Check_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/hpchealthchecks/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Health_Check_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} hpcService - 
-             * @param {choice} kind - 
-             * @param {string} resource_name - 
-             * @param {string} result_contains - 
-             * @param {integer} result_min_size - 
-             * @param {integer} result_max_size - 
-             * 
-             */
-            hpcapi.prototype.Hpc_Health_Check_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/hpchealthchecks/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['hpcService'] !== undefined) {
-                    form['hpcService'] = parameters['hpcService'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['resource_name'] !== undefined) {
-                    form['resource_name'] = parameters['resource_name'];
-                }
-
-                if (parameters['resource_name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: resource_name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['result_contains'] !== undefined) {
-                    form['result_contains'] = parameters['result_contains'];
-                }
-
-                if (parameters['result_min_size'] !== undefined) {
-                    form['result_min_size'] = parameters['result_min_size'];
-                }
-
-                if (parameters['result_max_size'] !== undefined) {
-                    form['result_max_size'] = parameters['result_max_size'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Health_Check_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Hpc_Health_Check_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/hpchealthchecks/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Health_Check_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} hpcService - 
-             * @param {choice} kind - 
-             * @param {string} resource_name - 
-             * @param {string} result_contains - 
-             * @param {integer} result_min_size - 
-             * @param {integer} result_max_size - 
-             * 
-             */
-            hpcapi.prototype.Hpc_Health_Check_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/hpchealthchecks/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['hpcService'] !== undefined) {
-                    form['hpcService'] = parameters['hpcService'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['resource_name'] !== undefined) {
-                    form['resource_name'] = parameters['resource_name'];
-                }
-
-                if (parameters['resource_name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: resource_name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['result_contains'] !== undefined) {
-                    form['result_contains'] = parameters['result_contains'];
-                }
-
-                if (parameters['result_min_size'] !== undefined) {
-                    form['result_min_size'] = parameters['result_min_size'];
-                }
-
-                if (parameters['result_max_size'] !== undefined) {
-                    form['result_max_size'] = parameters['result_max_size'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Health_Check_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} hpcService - 
-             * @param {choice} kind - 
-             * @param {string} resource_name - 
-             * @param {string} result_contains - 
-             * @param {integer} result_min_size - 
-             * @param {integer} result_max_size - 
-             * 
-             */
-            hpcapi.prototype.Hpc_Health_Check_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/hpchealthchecks/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['hpcService'] !== undefined) {
-                    form['hpcService'] = parameters['hpcService'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['resource_name'] !== undefined) {
-                    form['resource_name'] = parameters['resource_name'];
-                }
-
-                if (parameters['result_contains'] !== undefined) {
-                    form['result_contains'] = parameters['result_contains'];
-                }
-
-                if (parameters['result_min_size'] !== undefined) {
-                    form['result_min_size'] = parameters['result_min_size'];
-                }
-
-                if (parameters['result_max_size'] !== undefined) {
-                    form['result_max_size'] = parameters['result_max_size'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Health_Check_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Hpc_Health_Check_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/hpchealthchecks/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Service_List_GET
-             * 
-             */
-            hpcapi.prototype.Hpc_Service_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/hpcservices/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Service_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} description - Description of Service
-             * @param {boolean} enabled - 
-             * @param {string} kind - Kind of service
-             * @param {string} name - Service Name
-             * @param {string} versionNumber - Version of Service Definition
-             * @param {boolean} published - 
-             * @param {string} view_url - 
-             * @param {string} icon_url - 
-             * @param {string} public_key - Public key string
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * @param {string} cmi_hostname - 
-             * @param {boolean} hpc_port80 - Enable port 80 for HPC
-             * @param {string} watcher_hpc_network - Network for hpc_watcher to contact hpc instance
-             * @param {string} watcher_dnsdemux_network - Network for hpc_watcher to contact dnsdemux instance
-             * @param {string} watcher_dnsredir_network - Network for hpc_watcher to contact dnsredir instance
-             * 
-             */
-            hpcapi.prototype.Hpc_Service_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/hpcservices/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['versionNumber'] !== undefined) {
-                    form['versionNumber'] = parameters['versionNumber'];
-                }
-
-                if (parameters['versionNumber'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: versionNumber'));
-                    return deferred.promise;
-                }
-
-                if (parameters['published'] !== undefined) {
-                    form['published'] = parameters['published'];
-                }
-
-                if (parameters['view_url'] !== undefined) {
-                    form['view_url'] = parameters['view_url'];
-                }
-
-                if (parameters['icon_url'] !== undefined) {
-                    form['icon_url'] = parameters['icon_url'];
-                }
-
-                if (parameters['public_key'] !== undefined) {
-                    form['public_key'] = parameters['public_key'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['cmi_hostname'] !== undefined) {
-                    form['cmi_hostname'] = parameters['cmi_hostname'];
-                }
-
-                if (parameters['hpc_port80'] !== undefined) {
-                    form['hpc_port80'] = parameters['hpc_port80'];
-                }
-
-                if (parameters['watcher_hpc_network'] !== undefined) {
-                    form['watcher_hpc_network'] = parameters['watcher_hpc_network'];
-                }
-
-                if (parameters['watcher_dnsdemux_network'] !== undefined) {
-                    form['watcher_dnsdemux_network'] = parameters['watcher_dnsdemux_network'];
-                }
-
-                if (parameters['watcher_dnsredir_network'] !== undefined) {
-                    form['watcher_dnsredir_network'] = parameters['watcher_dnsredir_network'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Service_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Hpc_Service_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/hpcservices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Service_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} description - Description of Service
-             * @param {boolean} enabled - 
-             * @param {string} kind - Kind of service
-             * @param {string} name - Service Name
-             * @param {string} versionNumber - Version of Service Definition
-             * @param {boolean} published - 
-             * @param {string} view_url - 
-             * @param {string} icon_url - 
-             * @param {string} public_key - Public key string
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * @param {string} cmi_hostname - 
-             * @param {boolean} hpc_port80 - Enable port 80 for HPC
-             * @param {string} watcher_hpc_network - Network for hpc_watcher to contact hpc instance
-             * @param {string} watcher_dnsdemux_network - Network for hpc_watcher to contact dnsdemux instance
-             * @param {string} watcher_dnsredir_network - Network for hpc_watcher to contact dnsredir instance
-             * 
-             */
-            hpcapi.prototype.Hpc_Service_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/hpcservices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['versionNumber'] !== undefined) {
-                    form['versionNumber'] = parameters['versionNumber'];
-                }
-
-                if (parameters['versionNumber'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: versionNumber'));
-                    return deferred.promise;
-                }
-
-                if (parameters['published'] !== undefined) {
-                    form['published'] = parameters['published'];
-                }
-
-                if (parameters['view_url'] !== undefined) {
-                    form['view_url'] = parameters['view_url'];
-                }
-
-                if (parameters['icon_url'] !== undefined) {
-                    form['icon_url'] = parameters['icon_url'];
-                }
-
-                if (parameters['public_key'] !== undefined) {
-                    form['public_key'] = parameters['public_key'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['cmi_hostname'] !== undefined) {
-                    form['cmi_hostname'] = parameters['cmi_hostname'];
-                }
-
-                if (parameters['hpc_port80'] !== undefined) {
-                    form['hpc_port80'] = parameters['hpc_port80'];
-                }
-
-                if (parameters['watcher_hpc_network'] !== undefined) {
-                    form['watcher_hpc_network'] = parameters['watcher_hpc_network'];
-                }
-
-                if (parameters['watcher_dnsdemux_network'] !== undefined) {
-                    form['watcher_dnsdemux_network'] = parameters['watcher_dnsdemux_network'];
-                }
-
-                if (parameters['watcher_dnsredir_network'] !== undefined) {
-                    form['watcher_dnsredir_network'] = parameters['watcher_dnsredir_network'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Service_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} description - Description of Service
-             * @param {boolean} enabled - 
-             * @param {string} kind - Kind of service
-             * @param {string} name - Service Name
-             * @param {string} versionNumber - Version of Service Definition
-             * @param {boolean} published - 
-             * @param {string} view_url - 
-             * @param {string} icon_url - 
-             * @param {string} public_key - Public key string
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * @param {string} cmi_hostname - 
-             * @param {boolean} hpc_port80 - Enable port 80 for HPC
-             * @param {string} watcher_hpc_network - Network for hpc_watcher to contact hpc instance
-             * @param {string} watcher_dnsdemux_network - Network for hpc_watcher to contact dnsdemux instance
-             * @param {string} watcher_dnsredir_network - Network for hpc_watcher to contact dnsredir instance
-             * 
-             */
-            hpcapi.prototype.Hpc_Service_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/hpcservices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['versionNumber'] !== undefined) {
-                    form['versionNumber'] = parameters['versionNumber'];
-                }
-
-                if (parameters['published'] !== undefined) {
-                    form['published'] = parameters['published'];
-                }
-
-                if (parameters['view_url'] !== undefined) {
-                    form['view_url'] = parameters['view_url'];
-                }
-
-                if (parameters['icon_url'] !== undefined) {
-                    form['icon_url'] = parameters['icon_url'];
-                }
-
-                if (parameters['public_key'] !== undefined) {
-                    form['public_key'] = parameters['public_key'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['cmi_hostname'] !== undefined) {
-                    form['cmi_hostname'] = parameters['cmi_hostname'];
-                }
-
-                if (parameters['hpc_port80'] !== undefined) {
-                    form['hpc_port80'] = parameters['hpc_port80'];
-                }
-
-                if (parameters['watcher_hpc_network'] !== undefined) {
-                    form['watcher_hpc_network'] = parameters['watcher_hpc_network'];
-                }
-
-                if (parameters['watcher_dnsdemux_network'] !== undefined) {
-                    form['watcher_dnsdemux_network'] = parameters['watcher_dnsdemux_network'];
-                }
-
-                if (parameters['watcher_dnsredir_network'] !== undefined) {
-                    form['watcher_dnsredir_network'] = parameters['watcher_dnsredir_network'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Service_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Hpc_Service_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/hpcservices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Origin_Server_List_GET
-             * 
-             */
-            hpcapi.prototype.Origin_Server_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/originservers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Origin_Server_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {integer} origin_server_id - 
-             * @param {string} url - 
-             * @param {string} contentProvider - 
-             * @param {boolean} authenticated - Status for this Site
-             * @param {boolean} enabled - Status for this Site
-             * @param {choice} protocol - 
-             * @param {boolean} redirects - Indicates whether Origin Server redirects should be used for this Origin Server
-             * @param {string} description - 
-             * 
-             */
-            hpcapi.prototype.Origin_Server_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/originservers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['origin_server_id'] !== undefined) {
-                    form['origin_server_id'] = parameters['origin_server_id'];
-                }
-
-                if (parameters['url'] !== undefined) {
-                    form['url'] = parameters['url'];
-                }
-
-                if (parameters['url'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url'));
-                    return deferred.promise;
-                }
-
-                if (parameters['contentProvider'] !== undefined) {
-                    form['contentProvider'] = parameters['contentProvider'];
-                }
-
-                if (parameters['contentProvider'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: contentProvider'));
-                    return deferred.promise;
-                }
-
-                if (parameters['authenticated'] !== undefined) {
-                    form['authenticated'] = parameters['authenticated'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['protocol'] !== undefined) {
-                    form['protocol'] = parameters['protocol'];
-                }
-
-                if (parameters['protocol'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: protocol'));
-                    return deferred.promise;
-                }
-
-                if (parameters['redirects'] !== undefined) {
-                    form['redirects'] = parameters['redirects'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Origin_Server_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Origin_Server_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/originservers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Origin_Server_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {integer} origin_server_id - 
-             * @param {string} url - 
-             * @param {string} contentProvider - 
-             * @param {boolean} authenticated - Status for this Site
-             * @param {boolean} enabled - Status for this Site
-             * @param {choice} protocol - 
-             * @param {boolean} redirects - Indicates whether Origin Server redirects should be used for this Origin Server
-             * @param {string} description - 
-             * 
-             */
-            hpcapi.prototype.Origin_Server_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/originservers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['origin_server_id'] !== undefined) {
-                    form['origin_server_id'] = parameters['origin_server_id'];
-                }
-
-                if (parameters['url'] !== undefined) {
-                    form['url'] = parameters['url'];
-                }
-
-                if (parameters['url'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url'));
-                    return deferred.promise;
-                }
-
-                if (parameters['contentProvider'] !== undefined) {
-                    form['contentProvider'] = parameters['contentProvider'];
-                }
-
-                if (parameters['contentProvider'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: contentProvider'));
-                    return deferred.promise;
-                }
-
-                if (parameters['authenticated'] !== undefined) {
-                    form['authenticated'] = parameters['authenticated'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['protocol'] !== undefined) {
-                    form['protocol'] = parameters['protocol'];
-                }
-
-                if (parameters['protocol'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: protocol'));
-                    return deferred.promise;
-                }
-
-                if (parameters['redirects'] !== undefined) {
-                    form['redirects'] = parameters['redirects'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Origin_Server_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {integer} origin_server_id - 
-             * @param {string} url - 
-             * @param {string} contentProvider - 
-             * @param {boolean} authenticated - Status for this Site
-             * @param {boolean} enabled - Status for this Site
-             * @param {choice} protocol - 
-             * @param {boolean} redirects - Indicates whether Origin Server redirects should be used for this Origin Server
-             * @param {string} description - 
-             * 
-             */
-            hpcapi.prototype.Origin_Server_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/originservers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['origin_server_id'] !== undefined) {
-                    form['origin_server_id'] = parameters['origin_server_id'];
-                }
-
-                if (parameters['url'] !== undefined) {
-                    form['url'] = parameters['url'];
-                }
-
-                if (parameters['contentProvider'] !== undefined) {
-                    form['contentProvider'] = parameters['contentProvider'];
-                }
-
-                if (parameters['authenticated'] !== undefined) {
-                    form['authenticated'] = parameters['authenticated'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['protocol'] !== undefined) {
-                    form['protocol'] = parameters['protocol'];
-                }
-
-                if (parameters['redirects'] !== undefined) {
-                    form['redirects'] = parameters['redirects'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Origin_Server_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Origin_Server_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/originservers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Cdn_Prefix_List_GET
-             * 
-             */
-            hpcapi.prototype.Cdn_Prefix_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/cdnprefixs/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Cdn_Prefix_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {integer} cdn_prefix_id - 
-             * @param {string} prefix - Registered Prefix for Domain
-             * @param {string} contentProvider - 
-             * @param {string} description - Description of Content Provider
-             * @param {string} defaultOriginServer - 
-             * @param {boolean} enabled - 
-             * 
-             */
-            hpcapi.prototype.Cdn_Prefix_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/cdnprefixs/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['cdn_prefix_id'] !== undefined) {
-                    form['cdn_prefix_id'] = parameters['cdn_prefix_id'];
-                }
-
-                if (parameters['prefix'] !== undefined) {
-                    form['prefix'] = parameters['prefix'];
-                }
-
-                if (parameters['prefix'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: prefix'));
-                    return deferred.promise;
-                }
-
-                if (parameters['contentProvider'] !== undefined) {
-                    form['contentProvider'] = parameters['contentProvider'];
-                }
-
-                if (parameters['contentProvider'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: contentProvider'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['defaultOriginServer'] !== undefined) {
-                    form['defaultOriginServer'] = parameters['defaultOriginServer'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Cdn_Prefix_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Cdn_Prefix_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/cdnprefixs/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Cdn_Prefix_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {integer} cdn_prefix_id - 
-             * @param {string} prefix - Registered Prefix for Domain
-             * @param {string} contentProvider - 
-             * @param {string} description - Description of Content Provider
-             * @param {string} defaultOriginServer - 
-             * @param {boolean} enabled - 
-             * 
-             */
-            hpcapi.prototype.Cdn_Prefix_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/cdnprefixs/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['cdn_prefix_id'] !== undefined) {
-                    form['cdn_prefix_id'] = parameters['cdn_prefix_id'];
-                }
-
-                if (parameters['prefix'] !== undefined) {
-                    form['prefix'] = parameters['prefix'];
-                }
-
-                if (parameters['prefix'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: prefix'));
-                    return deferred.promise;
-                }
-
-                if (parameters['contentProvider'] !== undefined) {
-                    form['contentProvider'] = parameters['contentProvider'];
-                }
-
-                if (parameters['contentProvider'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: contentProvider'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['defaultOriginServer'] !== undefined) {
-                    form['defaultOriginServer'] = parameters['defaultOriginServer'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Cdn_Prefix_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {integer} cdn_prefix_id - 
-             * @param {string} prefix - Registered Prefix for Domain
-             * @param {string} contentProvider - 
-             * @param {string} description - Description of Content Provider
-             * @param {string} defaultOriginServer - 
-             * @param {boolean} enabled - 
-             * 
-             */
-            hpcapi.prototype.Cdn_Prefix_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/cdnprefixs/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['cdn_prefix_id'] !== undefined) {
-                    form['cdn_prefix_id'] = parameters['cdn_prefix_id'];
-                }
-
-                if (parameters['prefix'] !== undefined) {
-                    form['prefix'] = parameters['prefix'];
-                }
-
-                if (parameters['contentProvider'] !== undefined) {
-                    form['contentProvider'] = parameters['contentProvider'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['defaultOriginServer'] !== undefined) {
-                    form['defaultOriginServer'] = parameters['defaultOriginServer'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Cdn_Prefix_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Cdn_Prefix_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/cdnprefixs/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Service_Provider_List_GET
-             * 
-             */
-            hpcapi.prototype.Service_Provider_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/serviceproviders/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Service_Provider_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} hpcService - 
-             * @param {integer} service_provider_id - 
-             * @param {string} name - Service Provider Name
-             * @param {string} description - Description of Service Provider
-             * @param {boolean} enabled - 
-             * 
-             */
-            hpcapi.prototype.Service_Provider_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/serviceproviders/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['hpcService'] !== undefined) {
-                    form['hpcService'] = parameters['hpcService'];
-                }
-
-                if (parameters['hpcService'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: hpcService'));
-                    return deferred.promise;
-                }
-
-                if (parameters['service_provider_id'] !== undefined) {
-                    form['service_provider_id'] = parameters['service_provider_id'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Service_Provider_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Service_Provider_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/serviceproviders/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Service_Provider_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} hpcService - 
-             * @param {integer} service_provider_id - 
-             * @param {string} name - Service Provider Name
-             * @param {string} description - Description of Service Provider
-             * @param {boolean} enabled - 
-             * 
-             */
-            hpcapi.prototype.Service_Provider_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/serviceproviders/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['hpcService'] !== undefined) {
-                    form['hpcService'] = parameters['hpcService'];
-                }
-
-                if (parameters['hpcService'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: hpcService'));
-                    return deferred.promise;
-                }
-
-                if (parameters['service_provider_id'] !== undefined) {
-                    form['service_provider_id'] = parameters['service_provider_id'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Service_Provider_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} hpcService - 
-             * @param {integer} service_provider_id - 
-             * @param {string} name - Service Provider Name
-             * @param {string} description - Description of Service Provider
-             * @param {boolean} enabled - 
-             * 
-             */
-            hpcapi.prototype.Service_Provider_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/serviceproviders/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['hpcService'] !== undefined) {
-                    form['hpcService'] = parameters['hpcService'];
-                }
-
-                if (parameters['service_provider_id'] !== undefined) {
-                    form['service_provider_id'] = parameters['service_provider_id'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Service_Provider_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Service_Provider_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/serviceproviders/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Content_Provider_List_GET
-             * 
-             */
-            hpcapi.prototype.Content_Provider_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/contentproviders/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Content_Provider_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {integer} content_provider_id - 
-             * @param {string} name - 
-             * @param {boolean} enabled - 
-             * @param {string} description - Description of Content Provider
-             * @param {string} serviceProvider - 
-             * 
-             */
-            hpcapi.prototype.Content_Provider_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/contentproviders/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['content_provider_id'] !== undefined) {
-                    form['content_provider_id'] = parameters['content_provider_id'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['serviceProvider'] !== undefined) {
-                    form['serviceProvider'] = parameters['serviceProvider'];
-                }
-
-                if (parameters['serviceProvider'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: serviceProvider'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Content_Provider_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Content_Provider_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/contentproviders/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Content_Provider_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {integer} content_provider_id - 
-             * @param {string} name - 
-             * @param {boolean} enabled - 
-             * @param {string} description - Description of Content Provider
-             * @param {string} serviceProvider - 
-             * 
-             */
-            hpcapi.prototype.Content_Provider_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/contentproviders/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['content_provider_id'] !== undefined) {
-                    form['content_provider_id'] = parameters['content_provider_id'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['serviceProvider'] !== undefined) {
-                    form['serviceProvider'] = parameters['serviceProvider'];
-                }
-
-                if (parameters['serviceProvider'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: serviceProvider'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Content_Provider_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {integer} content_provider_id - 
-             * @param {string} name - 
-             * @param {boolean} enabled - 
-             * @param {string} description - Description of Content Provider
-             * @param {string} serviceProvider - 
-             * 
-             */
-            hpcapi.prototype.Content_Provider_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/contentproviders/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['content_provider_id'] !== undefined) {
-                    form['content_provider_id'] = parameters['content_provider_id'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['serviceProvider'] !== undefined) {
-                    form['serviceProvider'] = parameters['serviceProvider'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Content_Provider_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Content_Provider_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/contentproviders/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Access_Map_List_GET
-             * 
-             */
-            hpcapi.prototype.Access_Map_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/accessmaps/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Access_Map_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} contentProvider - 
-             * @param {string} name - Name of the Access Map
-             * @param {string} description - 
-             * @param {string} map - specifies which client requests are allowed
-             * 
-             */
-            hpcapi.prototype.Access_Map_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/accessmaps/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['contentProvider'] !== undefined) {
-                    form['contentProvider'] = parameters['contentProvider'];
-                }
-
-                if (parameters['contentProvider'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: contentProvider'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['map'] !== undefined) {
-                    form['map'] = parameters['map'];
-                }
-
-                if (parameters['map'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: map'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Access_Map_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Access_Map_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/accessmaps/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Access_Map_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} contentProvider - 
-             * @param {string} name - Name of the Access Map
-             * @param {string} description - 
-             * @param {string} map - specifies which client requests are allowed
-             * 
-             */
-            hpcapi.prototype.Access_Map_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/accessmaps/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['contentProvider'] !== undefined) {
-                    form['contentProvider'] = parameters['contentProvider'];
-                }
-
-                if (parameters['contentProvider'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: contentProvider'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['map'] !== undefined) {
-                    form['map'] = parameters['map'];
-                }
-
-                if (parameters['map'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: map'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Access_Map_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} contentProvider - 
-             * @param {string} name - Name of the Access Map
-             * @param {string} description - 
-             * @param {string} map - specifies which client requests are allowed
-             * 
-             */
-            hpcapi.prototype.Access_Map_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/accessmaps/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['contentProvider'] !== undefined) {
-                    form['contentProvider'] = parameters['contentProvider'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['map'] !== undefined) {
-                    form['map'] = parameters['map'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Access_Map_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Access_Map_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/accessmaps/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Site_Map_List_GET
-             * 
-             */
-            hpcapi.prototype.Site_Map_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/sitemaps/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Site_Map_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} contentProvider - 
-             * @param {string} serviceProvider - 
-             * @param {string} cdnPrefix - 
-             * @param {string} hpcService - 
-             * @param {string} name - Name of the Site Map
-             * @param {string} description - 
-             * @param {string} map - specifies how to map requests to hpc instances
-             * @param {integer} map_id - 
-             * 
-             */
-            hpcapi.prototype.Site_Map_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/sitemaps/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['contentProvider'] !== undefined) {
-                    form['contentProvider'] = parameters['contentProvider'];
-                }
-
-                if (parameters['serviceProvider'] !== undefined) {
-                    form['serviceProvider'] = parameters['serviceProvider'];
-                }
-
-                if (parameters['cdnPrefix'] !== undefined) {
-                    form['cdnPrefix'] = parameters['cdnPrefix'];
-                }
-
-                if (parameters['hpcService'] !== undefined) {
-                    form['hpcService'] = parameters['hpcService'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['map'] !== undefined) {
-                    form['map'] = parameters['map'];
-                }
-
-                if (parameters['map'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: map'));
-                    return deferred.promise;
-                }
-
-                if (parameters['map_id'] !== undefined) {
-                    form['map_id'] = parameters['map_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Site_Map_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Site_Map_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/sitemaps/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Site_Map_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} contentProvider - 
-             * @param {string} serviceProvider - 
-             * @param {string} cdnPrefix - 
-             * @param {string} hpcService - 
-             * @param {string} name - Name of the Site Map
-             * @param {string} description - 
-             * @param {string} map - specifies how to map requests to hpc instances
-             * @param {integer} map_id - 
-             * 
-             */
-            hpcapi.prototype.Site_Map_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/sitemaps/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['contentProvider'] !== undefined) {
-                    form['contentProvider'] = parameters['contentProvider'];
-                }
-
-                if (parameters['serviceProvider'] !== undefined) {
-                    form['serviceProvider'] = parameters['serviceProvider'];
-                }
-
-                if (parameters['cdnPrefix'] !== undefined) {
-                    form['cdnPrefix'] = parameters['cdnPrefix'];
-                }
-
-                if (parameters['hpcService'] !== undefined) {
-                    form['hpcService'] = parameters['hpcService'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['map'] !== undefined) {
-                    form['map'] = parameters['map'];
-                }
-
-                if (parameters['map'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: map'));
-                    return deferred.promise;
-                }
-
-                if (parameters['map_id'] !== undefined) {
-                    form['map_id'] = parameters['map_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Site_Map_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} contentProvider - 
-             * @param {string} serviceProvider - 
-             * @param {string} cdnPrefix - 
-             * @param {string} hpcService - 
-             * @param {string} name - Name of the Site Map
-             * @param {string} description - 
-             * @param {string} map - specifies how to map requests to hpc instances
-             * @param {integer} map_id - 
-             * 
-             */
-            hpcapi.prototype.Site_Map_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/sitemaps/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['contentProvider'] !== undefined) {
-                    form['contentProvider'] = parameters['contentProvider'];
-                }
-
-                if (parameters['serviceProvider'] !== undefined) {
-                    form['serviceProvider'] = parameters['serviceProvider'];
-                }
-
-                if (parameters['cdnPrefix'] !== undefined) {
-                    form['cdnPrefix'] = parameters['cdnPrefix'];
-                }
-
-                if (parameters['hpcService'] !== undefined) {
-                    form['hpcService'] = parameters['hpcService'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['map'] !== undefined) {
-                    form['map'] = parameters['map'];
-                }
-
-                if (parameters['map_id'] !== undefined) {
-                    form['map_id'] = parameters['map_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Site_Map_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Site_Map_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/sitemaps/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-
-            return hpcapi;
-        })();
-
-        return hpcapi;
-    }]);
\ No newline at end of file
diff --git a/views/ngXosLib/api/ng-xos.js b/views/ngXosLib/api/ng-xos.js
deleted file mode 100755
index 20ea691..0000000
--- a/views/ngXosLib/api/ng-xos.js
+++ /dev/null
@@ -1,39957 +0,0 @@
-/*jshint -W069 */
-/*global angular:false */
-angular.module('xos.xos', [])
-    .factory('xos', ['$q', '$http', '$rootScope', function($q, $http, $rootScope) {
-        'use strict';
-
-        /**
-         * 
-         * @class xos
-         * @param {(string|object)} [domainOrOptions] - The project domain or options object. If object, see the object's optional properties.
-         * @param {string} [domainOrOptions.domain] - The project domain
-         * @param {string} [domainOrOptions.cache] - An angularjs cache implementation
-         * @param {object} [domainOrOptions.token] - auth token - object with value property and optional headerOrQueryName and isQuery properties
-         * @param {string} [cache] - An angularjs cache implementation
-         */
-        var xos = (function() {
-            function xos(options, cache) {
-                var domain = (typeof options === 'object') ? options.domain : options;
-                this.domain = typeof(domain) === 'string' ? domain : 'http://localhost:9999';
-                cache = cache || ((typeof options === 'object') ? options.cache : cache);
-                this.cache = cache;
-            }
-
-            xos.prototype.$on = function($scope, path, handler) {
-                var url = domain + path;
-                $scope.$on(url, function() {
-                    handler();
-                });
-                return this;
-            };
-
-            xos.prototype.$broadcast = function(path) {
-                var url = domain + path;
-                //cache.remove(url);
-                $rootScope.$broadcast(url);
-                return this;
-            };
-
-            xos.transformRequest = function(obj) {
-                var str = [];
-                for (var p in obj) {
-                    var val = obj[p];
-                    if (angular.isArray(val)) {
-                        val.forEach(function(val) {
-                            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(val));
-                        });
-                    } else {
-                        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(val));
-                    }
-                }
-                return str.join("&");
-            };
-
-            /**
-             * 
-             * @method
-             * @name xos#Api_Root_GET
-             * 
-             */
-            xos.prototype.Api_Root_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Role_List_GET
-             * 
-             */
-            xos.prototype.Site_Role_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/site_roles/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Role_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {choice} role - 
-             * 
-             */
-            xos.prototype.Site_Role_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/site_roles/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Role_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Site_Role_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/site_roles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Role_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {choice} role - 
-             * 
-             */
-            xos.prototype.Site_Role_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/site_roles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Role_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {choice} role - 
-             * 
-             */
-            xos.prototype.Site_Role_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/site_roles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Role_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Site_Role_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/site_roles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Attribute_List_GET
-             * 
-             */
-            xos.prototype.Service_Attribute_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceattributes/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Attribute_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Attribute Name
-             * @param {string} value - Attribute Value
-             * @param {string} service - The Service this attribute is associated with
-             * 
-             */
-            xos.prototype.Service_Attribute_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceattributes/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['value'] !== undefined) {
-                    form['value'] = parameters['value'];
-                }
-
-                if (parameters['value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: value'));
-                    return deferred.promise;
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: service'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Attribute_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Service_Attribute_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceattributes/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Attribute_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Attribute Name
-             * @param {string} value - Attribute Value
-             * @param {string} service - The Service this attribute is associated with
-             * 
-             */
-            xos.prototype.Service_Attribute_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceattributes/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['value'] !== undefined) {
-                    form['value'] = parameters['value'];
-                }
-
-                if (parameters['value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: value'));
-                    return deferred.promise;
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: service'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Attribute_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Attribute Name
-             * @param {string} value - Attribute Value
-             * @param {string} service - The Service this attribute is associated with
-             * 
-             */
-            xos.prototype.Service_Attribute_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceattributes/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['value'] !== undefined) {
-                    form['value'] = parameters['value'];
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Attribute_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Service_Attribute_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceattributes/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Images_List_GET
-             * 
-             */
-            xos.prototype.Controller_Images_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerimages/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Images_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} image - 
-             * @param {string} controller - 
-             * @param {string} glance_image_id - Glance image id
-             * 
-             */
-            xos.prototype.Controller_Images_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerimages/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['image'] !== undefined) {
-                    form['image'] = parameters['image'];
-                }
-
-                if (parameters['image'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: image'));
-                    return deferred.promise;
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['glance_image_id'] !== undefined) {
-                    form['glance_image_id'] = parameters['glance_image_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Images_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Images_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerimages/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Images_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} image - 
-             * @param {string} controller - 
-             * @param {string} glance_image_id - Glance image id
-             * 
-             */
-            xos.prototype.Controller_Images_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerimages/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['image'] !== undefined) {
-                    form['image'] = parameters['image'];
-                }
-
-                if (parameters['image'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: image'));
-                    return deferred.promise;
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['glance_image_id'] !== undefined) {
-                    form['glance_image_id'] = parameters['glance_image_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Images_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} image - 
-             * @param {string} controller - 
-             * @param {string} glance_image_id - Glance image id
-             * 
-             */
-            xos.prototype.Controller_Images_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerimages/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['image'] !== undefined) {
-                    form['image'] = parameters['image'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['glance_image_id'] !== undefined) {
-                    form['glance_image_id'] = parameters['glance_image_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Images_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Images_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerimages/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Site_Privilege_List_GET
-             * 
-             */
-            xos.prototype.Controller_Site_Privilege_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersiteprivileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Site_Privilege_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} controller - 
-             * @param {string} site_privilege - 
-             * @param {string} role_id - Keystone id
-             * 
-             */
-            xos.prototype.Controller_Site_Privilege_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersiteprivileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['site_privilege'] !== undefined) {
-                    form['site_privilege'] = parameters['site_privilege'];
-                }
-
-                if (parameters['site_privilege'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site_privilege'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role_id'] !== undefined) {
-                    form['role_id'] = parameters['role_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Site_Privilege_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Site_Privilege_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersiteprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Site_Privilege_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} controller - 
-             * @param {string} site_privilege - 
-             * @param {string} role_id - Keystone id
-             * 
-             */
-            xos.prototype.Controller_Site_Privilege_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersiteprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['site_privilege'] !== undefined) {
-                    form['site_privilege'] = parameters['site_privilege'];
-                }
-
-                if (parameters['site_privilege'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site_privilege'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role_id'] !== undefined) {
-                    form['role_id'] = parameters['role_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Site_Privilege_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} controller - 
-             * @param {string} site_privilege - 
-             * @param {string} role_id - Keystone id
-             * 
-             */
-            xos.prototype.Controller_Site_Privilege_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersiteprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['site_privilege'] !== undefined) {
-                    form['site_privilege'] = parameters['site_privilege'];
-                }
-
-                if (parameters['role_id'] !== undefined) {
-                    form['role_id'] = parameters['role_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Site_Privilege_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Site_Privilege_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersiteprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Image_List_GET
-             * 
-             */
-            xos.prototype.Image_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/images/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Image_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} disk_format - 
-             * @param {string} container_format - 
-             * @param {string} path - Path to image on local disk
-             * 
-             */
-            xos.prototype.Image_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/images/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['disk_format'] !== undefined) {
-                    form['disk_format'] = parameters['disk_format'];
-                }
-
-                if (parameters['disk_format'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: disk_format'));
-                    return deferred.promise;
-                }
-
-                if (parameters['container_format'] !== undefined) {
-                    form['container_format'] = parameters['container_format'];
-                }
-
-                if (parameters['container_format'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: container_format'));
-                    return deferred.promise;
-                }
-
-                if (parameters['path'] !== undefined) {
-                    form['path'] = parameters['path'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Image_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Image_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/images/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Image_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} disk_format - 
-             * @param {string} container_format - 
-             * @param {string} path - Path to image on local disk
-             * 
-             */
-            xos.prototype.Image_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/images/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['disk_format'] !== undefined) {
-                    form['disk_format'] = parameters['disk_format'];
-                }
-
-                if (parameters['disk_format'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: disk_format'));
-                    return deferred.promise;
-                }
-
-                if (parameters['container_format'] !== undefined) {
-                    form['container_format'] = parameters['container_format'];
-                }
-
-                if (parameters['container_format'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: container_format'));
-                    return deferred.promise;
-                }
-
-                if (parameters['path'] !== undefined) {
-                    form['path'] = parameters['path'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Image_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} disk_format - 
-             * @param {string} container_format - 
-             * @param {string} path - Path to image on local disk
-             * 
-             */
-            xos.prototype.Image_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/images/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['disk_format'] !== undefined) {
-                    form['disk_format'] = parameters['disk_format'];
-                }
-
-                if (parameters['container_format'] !== undefined) {
-                    form['container_format'] = parameters['container_format'];
-                }
-
-                if (parameters['path'] !== undefined) {
-                    form['path'] = parameters['path'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Image_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Image_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/images/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Parameter_List_GET
-             * 
-             */
-            xos.prototype.Network_Parameter_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkparameters/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Parameter_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} parameter - The type of the parameter
-             * @param {string} value - The value of this parameter
-             * @param {string} content_type - 
-             * @param {integer} object_id - 
-             * 
-             */
-            xos.prototype.Network_Parameter_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkparameters/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['parameter'] !== undefined) {
-                    form['parameter'] = parameters['parameter'];
-                }
-
-                if (parameters['parameter'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: parameter'));
-                    return deferred.promise;
-                }
-
-                if (parameters['value'] !== undefined) {
-                    form['value'] = parameters['value'];
-                }
-
-                if (parameters['value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: value'));
-                    return deferred.promise;
-                }
-
-                if (parameters['content_type'] !== undefined) {
-                    form['content_type'] = parameters['content_type'];
-                }
-
-                if (parameters['content_type'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: content_type'));
-                    return deferred.promise;
-                }
-
-                if (parameters['object_id'] !== undefined) {
-                    form['object_id'] = parameters['object_id'];
-                }
-
-                if (parameters['object_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: object_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Parameter_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Network_Parameter_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkparameters/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Parameter_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} parameter - The type of the parameter
-             * @param {string} value - The value of this parameter
-             * @param {string} content_type - 
-             * @param {integer} object_id - 
-             * 
-             */
-            xos.prototype.Network_Parameter_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkparameters/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['parameter'] !== undefined) {
-                    form['parameter'] = parameters['parameter'];
-                }
-
-                if (parameters['parameter'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: parameter'));
-                    return deferred.promise;
-                }
-
-                if (parameters['value'] !== undefined) {
-                    form['value'] = parameters['value'];
-                }
-
-                if (parameters['value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: value'));
-                    return deferred.promise;
-                }
-
-                if (parameters['content_type'] !== undefined) {
-                    form['content_type'] = parameters['content_type'];
-                }
-
-                if (parameters['content_type'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: content_type'));
-                    return deferred.promise;
-                }
-
-                if (parameters['object_id'] !== undefined) {
-                    form['object_id'] = parameters['object_id'];
-                }
-
-                if (parameters['object_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: object_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Parameter_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} parameter - The type of the parameter
-             * @param {string} value - The value of this parameter
-             * @param {string} content_type - 
-             * @param {integer} object_id - 
-             * 
-             */
-            xos.prototype.Network_Parameter_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkparameters/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['parameter'] !== undefined) {
-                    form['parameter'] = parameters['parameter'];
-                }
-
-                if (parameters['value'] !== undefined) {
-                    form['value'] = parameters['value'];
-                }
-
-                if (parameters['content_type'] !== undefined) {
-                    form['content_type'] = parameters['content_type'];
-                }
-
-                if (parameters['object_id'] !== undefined) {
-                    form['object_id'] = parameters['object_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Parameter_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Network_Parameter_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkparameters/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_List_GET
-             * 
-             */
-            xos.prototype.Site_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sites/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name for this Site
-             * @param {string} site_url - Site's Home URL Page
-             * @param {boolean} enabled - Status for this Site
-             * @param {boolean} hosts_nodes - Indicates whether or not the site host nodes
-             * @param {boolean} hosts_users - Indicates whether or not the site manages user accounts
-             * @param {string} location - 
-             * @param {number} longitude - 
-             * @param {number} latitude - 
-             * @param {string} login_base - Prefix for Slices associated with this Site
-             * @param {boolean} is_public - Indicates the visibility of this site to other members
-             * @param {string} abbreviated_name - 
-             * 
-             */
-            xos.prototype.Site_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sites/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['site_url'] !== undefined) {
-                    form['site_url'] = parameters['site_url'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['hosts_nodes'] !== undefined) {
-                    form['hosts_nodes'] = parameters['hosts_nodes'];
-                }
-
-                if (parameters['hosts_users'] !== undefined) {
-                    form['hosts_users'] = parameters['hosts_users'];
-                }
-
-                if (parameters['location'] !== undefined) {
-                    form['location'] = parameters['location'];
-                }
-
-                if (parameters['location'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: location'));
-                    return deferred.promise;
-                }
-
-                if (parameters['longitude'] !== undefined) {
-                    form['longitude'] = parameters['longitude'];
-                }
-
-                if (parameters['latitude'] !== undefined) {
-                    form['latitude'] = parameters['latitude'];
-                }
-
-                if (parameters['login_base'] !== undefined) {
-                    form['login_base'] = parameters['login_base'];
-                }
-
-                if (parameters['login_base'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: login_base'));
-                    return deferred.promise;
-                }
-
-                if (parameters['is_public'] !== undefined) {
-                    form['is_public'] = parameters['is_public'];
-                }
-
-                if (parameters['abbreviated_name'] !== undefined) {
-                    form['abbreviated_name'] = parameters['abbreviated_name'];
-                }
-
-                if (parameters['abbreviated_name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: abbreviated_name'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Site_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sites/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name for this Site
-             * @param {string} site_url - Site's Home URL Page
-             * @param {boolean} enabled - Status for this Site
-             * @param {boolean} hosts_nodes - Indicates whether or not the site host nodes
-             * @param {boolean} hosts_users - Indicates whether or not the site manages user accounts
-             * @param {string} location - 
-             * @param {number} longitude - 
-             * @param {number} latitude - 
-             * @param {string} login_base - Prefix for Slices associated with this Site
-             * @param {boolean} is_public - Indicates the visibility of this site to other members
-             * @param {string} abbreviated_name - 
-             * 
-             */
-            xos.prototype.Site_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sites/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['site_url'] !== undefined) {
-                    form['site_url'] = parameters['site_url'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['hosts_nodes'] !== undefined) {
-                    form['hosts_nodes'] = parameters['hosts_nodes'];
-                }
-
-                if (parameters['hosts_users'] !== undefined) {
-                    form['hosts_users'] = parameters['hosts_users'];
-                }
-
-                if (parameters['location'] !== undefined) {
-                    form['location'] = parameters['location'];
-                }
-
-                if (parameters['location'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: location'));
-                    return deferred.promise;
-                }
-
-                if (parameters['longitude'] !== undefined) {
-                    form['longitude'] = parameters['longitude'];
-                }
-
-                if (parameters['latitude'] !== undefined) {
-                    form['latitude'] = parameters['latitude'];
-                }
-
-                if (parameters['login_base'] !== undefined) {
-                    form['login_base'] = parameters['login_base'];
-                }
-
-                if (parameters['login_base'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: login_base'));
-                    return deferred.promise;
-                }
-
-                if (parameters['is_public'] !== undefined) {
-                    form['is_public'] = parameters['is_public'];
-                }
-
-                if (parameters['abbreviated_name'] !== undefined) {
-                    form['abbreviated_name'] = parameters['abbreviated_name'];
-                }
-
-                if (parameters['abbreviated_name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: abbreviated_name'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name for this Site
-             * @param {string} site_url - Site's Home URL Page
-             * @param {boolean} enabled - Status for this Site
-             * @param {boolean} hosts_nodes - Indicates whether or not the site host nodes
-             * @param {boolean} hosts_users - Indicates whether or not the site manages user accounts
-             * @param {string} location - 
-             * @param {number} longitude - 
-             * @param {number} latitude - 
-             * @param {string} login_base - Prefix for Slices associated with this Site
-             * @param {boolean} is_public - Indicates the visibility of this site to other members
-             * @param {string} abbreviated_name - 
-             * 
-             */
-            xos.prototype.Site_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sites/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['site_url'] !== undefined) {
-                    form['site_url'] = parameters['site_url'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['hosts_nodes'] !== undefined) {
-                    form['hosts_nodes'] = parameters['hosts_nodes'];
-                }
-
-                if (parameters['hosts_users'] !== undefined) {
-                    form['hosts_users'] = parameters['hosts_users'];
-                }
-
-                if (parameters['location'] !== undefined) {
-                    form['location'] = parameters['location'];
-                }
-
-                if (parameters['longitude'] !== undefined) {
-                    form['longitude'] = parameters['longitude'];
-                }
-
-                if (parameters['latitude'] !== undefined) {
-                    form['latitude'] = parameters['latitude'];
-                }
-
-                if (parameters['login_base'] !== undefined) {
-                    form['login_base'] = parameters['login_base'];
-                }
-
-                if (parameters['is_public'] !== undefined) {
-                    form['is_public'] = parameters['is_public'];
-                }
-
-                if (parameters['abbreviated_name'] !== undefined) {
-                    form['abbreviated_name'] = parameters['abbreviated_name'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Site_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sites/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Role_List_GET
-             * 
-             */
-            xos.prototype.Tenant_Root_Role_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantrootroles/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Role_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {choice} role - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Role_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantrootroles/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Role_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Role_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantrootroles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Role_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {choice} role - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Role_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantrootroles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Role_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {choice} role - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Role_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantrootroles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Role_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Role_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantrootroles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Role_List_GET
-             * 
-             */
-            xos.prototype.Slice_Role_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slice_roles/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Role_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {choice} role - 
-             * 
-             */
-            xos.prototype.Slice_Role_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slice_roles/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Role_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Slice_Role_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slice_roles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Role_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {choice} role - 
-             * 
-             */
-            xos.prototype.Slice_Role_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slice_roles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Role_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {choice} role - 
-             * 
-             */
-            xos.prototype.Slice_Role_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slice_roles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Role_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Slice_Role_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slice_roles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tag_List_GET
-             * 
-             */
-            xos.prototype.Tag_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tags/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tag_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} service - The Service this Tag is associated with
-             * @param {string} name - The name of this tag
-             * @param {string} value - The value of this tag
-             * @param {string} content_type - 
-             * @param {integer} object_id - 
-             * 
-             */
-            xos.prototype.Tag_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tags/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['value'] !== undefined) {
-                    form['value'] = parameters['value'];
-                }
-
-                if (parameters['value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: value'));
-                    return deferred.promise;
-                }
-
-                if (parameters['content_type'] !== undefined) {
-                    form['content_type'] = parameters['content_type'];
-                }
-
-                if (parameters['content_type'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: content_type'));
-                    return deferred.promise;
-                }
-
-                if (parameters['object_id'] !== undefined) {
-                    form['object_id'] = parameters['object_id'];
-                }
-
-                if (parameters['object_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: object_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tag_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Tag_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tags/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tag_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} service - The Service this Tag is associated with
-             * @param {string} name - The name of this tag
-             * @param {string} value - The value of this tag
-             * @param {string} content_type - 
-             * @param {integer} object_id - 
-             * 
-             */
-            xos.prototype.Tag_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tags/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['value'] !== undefined) {
-                    form['value'] = parameters['value'];
-                }
-
-                if (parameters['value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: value'));
-                    return deferred.promise;
-                }
-
-                if (parameters['content_type'] !== undefined) {
-                    form['content_type'] = parameters['content_type'];
-                }
-
-                if (parameters['content_type'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: content_type'));
-                    return deferred.promise;
-                }
-
-                if (parameters['object_id'] !== undefined) {
-                    form['object_id'] = parameters['object_id'];
-                }
-
-                if (parameters['object_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: object_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tag_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} service - The Service this Tag is associated with
-             * @param {string} name - The name of this tag
-             * @param {string} value - The value of this tag
-             * @param {string} content_type - 
-             * @param {integer} object_id - 
-             * 
-             */
-            xos.prototype.Tag_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tags/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['value'] !== undefined) {
-                    form['value'] = parameters['value'];
-                }
-
-                if (parameters['content_type'] !== undefined) {
-                    form['content_type'] = parameters['content_type'];
-                }
-
-                if (parameters['object_id'] !== undefined) {
-                    form['object_id'] = parameters['object_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tag_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Tag_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tags/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Invoice_List_GET
-             * 
-             */
-            xos.prototype.Invoice_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/invoices/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Invoice_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} date - 
-             * @param {string} account - 
-             * 
-             */
-            xos.prototype.Invoice_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/invoices/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['date'] !== undefined) {
-                    form['date'] = parameters['date'];
-                }
-
-                if (parameters['date'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: date'));
-                    return deferred.promise;
-                }
-
-                if (parameters['account'] !== undefined) {
-                    form['account'] = parameters['account'];
-                }
-
-                if (parameters['account'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: account'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Invoice_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Invoice_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/invoices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Invoice_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} date - 
-             * @param {string} account - 
-             * 
-             */
-            xos.prototype.Invoice_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/invoices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['date'] !== undefined) {
-                    form['date'] = parameters['date'];
-                }
-
-                if (parameters['date'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: date'));
-                    return deferred.promise;
-                }
-
-                if (parameters['account'] !== undefined) {
-                    form['account'] = parameters['account'];
-                }
-
-                if (parameters['account'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: account'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Invoice_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} date - 
-             * @param {string} account - 
-             * 
-             */
-            xos.prototype.Invoice_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/invoices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['date'] !== undefined) {
-                    form['date'] = parameters['date'];
-                }
-
-                if (parameters['account'] !== undefined) {
-                    form['account'] = parameters['account'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Invoice_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Invoice_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/invoices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Privilege_List_GET
-             * 
-             */
-            xos.prototype.Slice_Privilege_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slice_privileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Privilege_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} slice - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Slice_Privilege_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slice_privileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Privilege_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Slice_Privilege_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slice_privileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Privilege_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} slice - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Slice_Privilege_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slice_privileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Privilege_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} slice - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Slice_Privilege_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slice_privileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Privilege_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Slice_Privilege_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slice_privileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Flavor_List_GET
-             * 
-             */
-            xos.prototype.Flavor_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/flavors/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Flavor_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - name of this flavor, as displayed to users
-             * @param {string} description - 
-             * @param {string} flavor - flavor string used to configure deployments
-             * @param {integer} order - used to order flavors when displayed in a list
-             * @param {boolean} default - make this a default flavor to use when creating new instances
-             * 
-             */
-            xos.prototype.Flavor_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/flavors/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['flavor'] !== undefined) {
-                    form['flavor'] = parameters['flavor'];
-                }
-
-                if (parameters['flavor'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: flavor'));
-                    return deferred.promise;
-                }
-
-                if (parameters['order'] !== undefined) {
-                    form['order'] = parameters['order'];
-                }
-
-                if (parameters['order'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: order'));
-                    return deferred.promise;
-                }
-
-                if (parameters['default'] !== undefined) {
-                    form['default'] = parameters['default'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Flavor_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Flavor_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/flavors/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Flavor_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - name of this flavor, as displayed to users
-             * @param {string} description - 
-             * @param {string} flavor - flavor string used to configure deployments
-             * @param {integer} order - used to order flavors when displayed in a list
-             * @param {boolean} default - make this a default flavor to use when creating new instances
-             * 
-             */
-            xos.prototype.Flavor_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/flavors/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['flavor'] !== undefined) {
-                    form['flavor'] = parameters['flavor'];
-                }
-
-                if (parameters['flavor'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: flavor'));
-                    return deferred.promise;
-                }
-
-                if (parameters['order'] !== undefined) {
-                    form['order'] = parameters['order'];
-                }
-
-                if (parameters['order'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: order'));
-                    return deferred.promise;
-                }
-
-                if (parameters['default'] !== undefined) {
-                    form['default'] = parameters['default'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Flavor_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - name of this flavor, as displayed to users
-             * @param {string} description - 
-             * @param {string} flavor - flavor string used to configure deployments
-             * @param {integer} order - used to order flavors when displayed in a list
-             * @param {boolean} default - make this a default flavor to use when creating new instances
-             * 
-             */
-            xos.prototype.Flavor_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/flavors/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['flavor'] !== undefined) {
-                    form['flavor'] = parameters['flavor'];
-                }
-
-                if (parameters['order'] !== undefined) {
-                    form['order'] = parameters['order'];
-                }
-
-                if (parameters['default'] !== undefined) {
-                    form['default'] = parameters['default'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Flavor_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Flavor_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/flavors/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Port_List_GET
-             * 
-             */
-            xos.prototype.Port_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/ports/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Port_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} network - 
-             * @param {string} instance - 
-             * @param {string} ip - Instance ip address
-             * @param {string} port_id - Quantum port id
-             * @param {string} mac - MAC address associated with this port
-             * 
-             */
-            xos.prototype.Port_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/ports/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['network'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: network'));
-                    return deferred.promise;
-                }
-
-                if (parameters['instance'] !== undefined) {
-                    form['instance'] = parameters['instance'];
-                }
-
-                if (parameters['ip'] !== undefined) {
-                    form['ip'] = parameters['ip'];
-                }
-
-                if (parameters['port_id'] !== undefined) {
-                    form['port_id'] = parameters['port_id'];
-                }
-
-                if (parameters['mac'] !== undefined) {
-                    form['mac'] = parameters['mac'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Port_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Port_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/ports/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Port_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} network - 
-             * @param {string} instance - 
-             * @param {string} ip - Instance ip address
-             * @param {string} port_id - Quantum port id
-             * @param {string} mac - MAC address associated with this port
-             * 
-             */
-            xos.prototype.Port_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/ports/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['network'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: network'));
-                    return deferred.promise;
-                }
-
-                if (parameters['instance'] !== undefined) {
-                    form['instance'] = parameters['instance'];
-                }
-
-                if (parameters['ip'] !== undefined) {
-                    form['ip'] = parameters['ip'];
-                }
-
-                if (parameters['port_id'] !== undefined) {
-                    form['port_id'] = parameters['port_id'];
-                }
-
-                if (parameters['mac'] !== undefined) {
-                    form['mac'] = parameters['mac'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Port_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} network - 
-             * @param {string} instance - 
-             * @param {string} ip - Instance ip address
-             * @param {string} port_id - Quantum port id
-             * @param {string} mac - MAC address associated with this port
-             * 
-             */
-            xos.prototype.Port_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/ports/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['instance'] !== undefined) {
-                    form['instance'] = parameters['instance'];
-                }
-
-                if (parameters['ip'] !== undefined) {
-                    form['ip'] = parameters['ip'];
-                }
-
-                if (parameters['port_id'] !== undefined) {
-                    form['port_id'] = parameters['port_id'];
-                }
-
-                if (parameters['mac'] !== undefined) {
-                    form['mac'] = parameters['mac'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Port_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Port_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/ports/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Site_List_GET
-             * 
-             */
-            xos.prototype.Controller_Site_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersites/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Site_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} site - 
-             * @param {string} controller - 
-             * @param {string} tenant_id - Keystone tenant id
-             * 
-             */
-            xos.prototype.Controller_Site_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersites/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['tenant_id'] !== undefined) {
-                    form['tenant_id'] = parameters['tenant_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Site_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Site_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersites/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Site_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} site - 
-             * @param {string} controller - 
-             * @param {string} tenant_id - Keystone tenant id
-             * 
-             */
-            xos.prototype.Controller_Site_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersites/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['tenant_id'] !== undefined) {
-                    form['tenant_id'] = parameters['tenant_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Site_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} site - 
-             * @param {string} controller - 
-             * @param {string} tenant_id - Keystone tenant id
-             * 
-             */
-            xos.prototype.Controller_Site_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersites/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['tenant_id'] !== undefined) {
-                    form['tenant_id'] = parameters['tenant_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Site_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Site_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersites/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Project_List_GET
-             * 
-             */
-            xos.prototype.Project_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/projects/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Project_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of Project
-             * 
-             */
-            xos.prototype.Project_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/projects/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Project_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Project_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/projects/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Project_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of Project
-             * 
-             */
-            xos.prototype.Project_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/projects/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Project_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of Project
-             * 
-             */
-            xos.prototype.Project_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/projects/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Project_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Project_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/projects/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_List_GET
-             * 
-             */
-            xos.prototype.Slice_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slices/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - The Name of the Slice
-             * @param {boolean} enabled - Status for this Slice
-             * @param {boolean} omf_friendly - 
-             * @param {string} description - High level description of the slice and expected activities
-             * @param {string} slice_url - 
-             * @param {string} site - The Site this Slice belongs to
-             * @param {integer} max_instances - 
-             * @param {string} service - 
-             * @param {string} network - 
-             * @param {string} serviceClass - 
-             * @param {string} creator - 
-             * @param {string} default_flavor - 
-             * @param {string} default_image - 
-             * @param {string} mount_data_sets - 
-             * 
-             */
-            xos.prototype.Slice_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slices/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['omf_friendly'] !== undefined) {
-                    form['omf_friendly'] = parameters['omf_friendly'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['slice_url'] !== undefined) {
-                    form['slice_url'] = parameters['slice_url'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['max_instances'] !== undefined) {
-                    form['max_instances'] = parameters['max_instances'];
-                }
-
-                if (parameters['max_instances'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: max_instances'));
-                    return deferred.promise;
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['serviceClass'] !== undefined) {
-                    form['serviceClass'] = parameters['serviceClass'];
-                }
-
-                if (parameters['creator'] !== undefined) {
-                    form['creator'] = parameters['creator'];
-                }
-
-                if (parameters['default_flavor'] !== undefined) {
-                    form['default_flavor'] = parameters['default_flavor'];
-                }
-
-                if (parameters['default_image'] !== undefined) {
-                    form['default_image'] = parameters['default_image'];
-                }
-
-                if (parameters['mount_data_sets'] !== undefined) {
-                    form['mount_data_sets'] = parameters['mount_data_sets'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Slice_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - The Name of the Slice
-             * @param {boolean} enabled - Status for this Slice
-             * @param {boolean} omf_friendly - 
-             * @param {string} description - High level description of the slice and expected activities
-             * @param {string} slice_url - 
-             * @param {string} site - The Site this Slice belongs to
-             * @param {integer} max_instances - 
-             * @param {string} service - 
-             * @param {string} network - 
-             * @param {string} serviceClass - 
-             * @param {string} creator - 
-             * @param {string} default_flavor - 
-             * @param {string} default_image - 
-             * @param {string} mount_data_sets - 
-             * 
-             */
-            xos.prototype.Slice_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['omf_friendly'] !== undefined) {
-                    form['omf_friendly'] = parameters['omf_friendly'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['slice_url'] !== undefined) {
-                    form['slice_url'] = parameters['slice_url'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['max_instances'] !== undefined) {
-                    form['max_instances'] = parameters['max_instances'];
-                }
-
-                if (parameters['max_instances'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: max_instances'));
-                    return deferred.promise;
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['serviceClass'] !== undefined) {
-                    form['serviceClass'] = parameters['serviceClass'];
-                }
-
-                if (parameters['creator'] !== undefined) {
-                    form['creator'] = parameters['creator'];
-                }
-
-                if (parameters['default_flavor'] !== undefined) {
-                    form['default_flavor'] = parameters['default_flavor'];
-                }
-
-                if (parameters['default_image'] !== undefined) {
-                    form['default_image'] = parameters['default_image'];
-                }
-
-                if (parameters['mount_data_sets'] !== undefined) {
-                    form['mount_data_sets'] = parameters['mount_data_sets'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - The Name of the Slice
-             * @param {boolean} enabled - Status for this Slice
-             * @param {boolean} omf_friendly - 
-             * @param {string} description - High level description of the slice and expected activities
-             * @param {string} slice_url - 
-             * @param {string} site - The Site this Slice belongs to
-             * @param {integer} max_instances - 
-             * @param {string} service - 
-             * @param {string} network - 
-             * @param {string} serviceClass - 
-             * @param {string} creator - 
-             * @param {string} default_flavor - 
-             * @param {string} default_image - 
-             * @param {string} mount_data_sets - 
-             * 
-             */
-            xos.prototype.Slice_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['omf_friendly'] !== undefined) {
-                    form['omf_friendly'] = parameters['omf_friendly'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['slice_url'] !== undefined) {
-                    form['slice_url'] = parameters['slice_url'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['max_instances'] !== undefined) {
-                    form['max_instances'] = parameters['max_instances'];
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['serviceClass'] !== undefined) {
-                    form['serviceClass'] = parameters['serviceClass'];
-                }
-
-                if (parameters['creator'] !== undefined) {
-                    form['creator'] = parameters['creator'];
-                }
-
-                if (parameters['default_flavor'] !== undefined) {
-                    form['default_flavor'] = parameters['default_flavor'];
-                }
-
-                if (parameters['default_image'] !== undefined) {
-                    form['default_image'] = parameters['default_image'];
-                }
-
-                if (parameters['mount_data_sets'] !== undefined) {
-                    form['mount_data_sets'] = parameters['mount_data_sets'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Slice_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_List_GET
-             * 
-             */
-            xos.prototype.Network_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networks/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} template - 
-             * @param {string} subnet - 
-             * @param {string} ports - 
-             * @param {string} labels - 
-             * @param {string} owner - Slice that owns control of this Network
-             * @param {integer} guaranteed_bandwidth - 
-             * @param {boolean} permit_all_slices - 
-             * @param {string} topology_parameters - 
-             * @param {string} controller_url - 
-             * @param {string} controller_parameters - 
-             * @param {string} network_id - Quantum network
-             * @param {string} router_id - Quantum router id
-             * @param {string} subnet_id - Quantum subnet id
-             * @param {boolean} autoconnect - This network can be autoconnected to the slice that owns it
-             * 
-             */
-            xos.prototype.Network_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networks/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['template'] !== undefined) {
-                    form['template'] = parameters['template'];
-                }
-
-                if (parameters['template'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: template'));
-                    return deferred.promise;
-                }
-
-                if (parameters['subnet'] !== undefined) {
-                    form['subnet'] = parameters['subnet'];
-                }
-
-                if (parameters['ports'] !== undefined) {
-                    form['ports'] = parameters['ports'];
-                }
-
-                if (parameters['labels'] !== undefined) {
-                    form['labels'] = parameters['labels'];
-                }
-
-                if (parameters['owner'] !== undefined) {
-                    form['owner'] = parameters['owner'];
-                }
-
-                if (parameters['owner'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: owner'));
-                    return deferred.promise;
-                }
-
-                if (parameters['guaranteed_bandwidth'] !== undefined) {
-                    form['guaranteed_bandwidth'] = parameters['guaranteed_bandwidth'];
-                }
-
-                if (parameters['guaranteed_bandwidth'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: guaranteed_bandwidth'));
-                    return deferred.promise;
-                }
-
-                if (parameters['permit_all_slices'] !== undefined) {
-                    form['permit_all_slices'] = parameters['permit_all_slices'];
-                }
-
-                if (parameters['topology_parameters'] !== undefined) {
-                    form['topology_parameters'] = parameters['topology_parameters'];
-                }
-
-                if (parameters['controller_url'] !== undefined) {
-                    form['controller_url'] = parameters['controller_url'];
-                }
-
-                if (parameters['controller_parameters'] !== undefined) {
-                    form['controller_parameters'] = parameters['controller_parameters'];
-                }
-
-                if (parameters['network_id'] !== undefined) {
-                    form['network_id'] = parameters['network_id'];
-                }
-
-                if (parameters['router_id'] !== undefined) {
-                    form['router_id'] = parameters['router_id'];
-                }
-
-                if (parameters['subnet_id'] !== undefined) {
-                    form['subnet_id'] = parameters['subnet_id'];
-                }
-
-                if (parameters['autoconnect'] !== undefined) {
-                    form['autoconnect'] = parameters['autoconnect'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Network_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networks/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} template - 
-             * @param {string} subnet - 
-             * @param {string} ports - 
-             * @param {string} labels - 
-             * @param {string} owner - Slice that owns control of this Network
-             * @param {integer} guaranteed_bandwidth - 
-             * @param {boolean} permit_all_slices - 
-             * @param {string} topology_parameters - 
-             * @param {string} controller_url - 
-             * @param {string} controller_parameters - 
-             * @param {string} network_id - Quantum network
-             * @param {string} router_id - Quantum router id
-             * @param {string} subnet_id - Quantum subnet id
-             * @param {boolean} autoconnect - This network can be autoconnected to the slice that owns it
-             * 
-             */
-            xos.prototype.Network_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networks/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['template'] !== undefined) {
-                    form['template'] = parameters['template'];
-                }
-
-                if (parameters['template'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: template'));
-                    return deferred.promise;
-                }
-
-                if (parameters['subnet'] !== undefined) {
-                    form['subnet'] = parameters['subnet'];
-                }
-
-                if (parameters['ports'] !== undefined) {
-                    form['ports'] = parameters['ports'];
-                }
-
-                if (parameters['labels'] !== undefined) {
-                    form['labels'] = parameters['labels'];
-                }
-
-                if (parameters['owner'] !== undefined) {
-                    form['owner'] = parameters['owner'];
-                }
-
-                if (parameters['owner'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: owner'));
-                    return deferred.promise;
-                }
-
-                if (parameters['guaranteed_bandwidth'] !== undefined) {
-                    form['guaranteed_bandwidth'] = parameters['guaranteed_bandwidth'];
-                }
-
-                if (parameters['guaranteed_bandwidth'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: guaranteed_bandwidth'));
-                    return deferred.promise;
-                }
-
-                if (parameters['permit_all_slices'] !== undefined) {
-                    form['permit_all_slices'] = parameters['permit_all_slices'];
-                }
-
-                if (parameters['topology_parameters'] !== undefined) {
-                    form['topology_parameters'] = parameters['topology_parameters'];
-                }
-
-                if (parameters['controller_url'] !== undefined) {
-                    form['controller_url'] = parameters['controller_url'];
-                }
-
-                if (parameters['controller_parameters'] !== undefined) {
-                    form['controller_parameters'] = parameters['controller_parameters'];
-                }
-
-                if (parameters['network_id'] !== undefined) {
-                    form['network_id'] = parameters['network_id'];
-                }
-
-                if (parameters['router_id'] !== undefined) {
-                    form['router_id'] = parameters['router_id'];
-                }
-
-                if (parameters['subnet_id'] !== undefined) {
-                    form['subnet_id'] = parameters['subnet_id'];
-                }
-
-                if (parameters['autoconnect'] !== undefined) {
-                    form['autoconnect'] = parameters['autoconnect'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} template - 
-             * @param {string} subnet - 
-             * @param {string} ports - 
-             * @param {string} labels - 
-             * @param {string} owner - Slice that owns control of this Network
-             * @param {integer} guaranteed_bandwidth - 
-             * @param {boolean} permit_all_slices - 
-             * @param {string} topology_parameters - 
-             * @param {string} controller_url - 
-             * @param {string} controller_parameters - 
-             * @param {string} network_id - Quantum network
-             * @param {string} router_id - Quantum router id
-             * @param {string} subnet_id - Quantum subnet id
-             * @param {boolean} autoconnect - This network can be autoconnected to the slice that owns it
-             * 
-             */
-            xos.prototype.Network_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networks/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['template'] !== undefined) {
-                    form['template'] = parameters['template'];
-                }
-
-                if (parameters['subnet'] !== undefined) {
-                    form['subnet'] = parameters['subnet'];
-                }
-
-                if (parameters['ports'] !== undefined) {
-                    form['ports'] = parameters['ports'];
-                }
-
-                if (parameters['labels'] !== undefined) {
-                    form['labels'] = parameters['labels'];
-                }
-
-                if (parameters['owner'] !== undefined) {
-                    form['owner'] = parameters['owner'];
-                }
-
-                if (parameters['guaranteed_bandwidth'] !== undefined) {
-                    form['guaranteed_bandwidth'] = parameters['guaranteed_bandwidth'];
-                }
-
-                if (parameters['permit_all_slices'] !== undefined) {
-                    form['permit_all_slices'] = parameters['permit_all_slices'];
-                }
-
-                if (parameters['topology_parameters'] !== undefined) {
-                    form['topology_parameters'] = parameters['topology_parameters'];
-                }
-
-                if (parameters['controller_url'] !== undefined) {
-                    form['controller_url'] = parameters['controller_url'];
-                }
-
-                if (parameters['controller_parameters'] !== undefined) {
-                    form['controller_parameters'] = parameters['controller_parameters'];
-                }
-
-                if (parameters['network_id'] !== undefined) {
-                    form['network_id'] = parameters['network_id'];
-                }
-
-                if (parameters['router_id'] !== undefined) {
-                    form['router_id'] = parameters['router_id'];
-                }
-
-                if (parameters['subnet_id'] !== undefined) {
-                    form['subnet_id'] = parameters['subnet_id'];
-                }
-
-                if (parameters['autoconnect'] !== undefined) {
-                    form['autoconnect'] = parameters['autoconnect'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Network_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networks/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_List_GET
-             * 
-             */
-            xos.prototype.Service_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/services/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} description - Description of Service
-             * @param {boolean} enabled - 
-             * @param {string} kind - Kind of service
-             * @param {string} name - Service Name
-             * @param {string} versionNumber - Version of Service Definition
-             * @param {boolean} published - 
-             * @param {string} view_url - 
-             * @param {string} icon_url - 
-             * @param {string} public_key - Public key string
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * 
-             */
-            xos.prototype.Service_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/services/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['versionNumber'] !== undefined) {
-                    form['versionNumber'] = parameters['versionNumber'];
-                }
-
-                if (parameters['versionNumber'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: versionNumber'));
-                    return deferred.promise;
-                }
-
-                if (parameters['published'] !== undefined) {
-                    form['published'] = parameters['published'];
-                }
-
-                if (parameters['view_url'] !== undefined) {
-                    form['view_url'] = parameters['view_url'];
-                }
-
-                if (parameters['icon_url'] !== undefined) {
-                    form['icon_url'] = parameters['icon_url'];
-                }
-
-                if (parameters['public_key'] !== undefined) {
-                    form['public_key'] = parameters['public_key'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Service_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/services/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} description - Description of Service
-             * @param {boolean} enabled - 
-             * @param {string} kind - Kind of service
-             * @param {string} name - Service Name
-             * @param {string} versionNumber - Version of Service Definition
-             * @param {boolean} published - 
-             * @param {string} view_url - 
-             * @param {string} icon_url - 
-             * @param {string} public_key - Public key string
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * 
-             */
-            xos.prototype.Service_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/services/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['versionNumber'] !== undefined) {
-                    form['versionNumber'] = parameters['versionNumber'];
-                }
-
-                if (parameters['versionNumber'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: versionNumber'));
-                    return deferred.promise;
-                }
-
-                if (parameters['published'] !== undefined) {
-                    form['published'] = parameters['published'];
-                }
-
-                if (parameters['view_url'] !== undefined) {
-                    form['view_url'] = parameters['view_url'];
-                }
-
-                if (parameters['icon_url'] !== undefined) {
-                    form['icon_url'] = parameters['icon_url'];
-                }
-
-                if (parameters['public_key'] !== undefined) {
-                    form['public_key'] = parameters['public_key'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} description - Description of Service
-             * @param {boolean} enabled - 
-             * @param {string} kind - Kind of service
-             * @param {string} name - Service Name
-             * @param {string} versionNumber - Version of Service Definition
-             * @param {boolean} published - 
-             * @param {string} view_url - 
-             * @param {string} icon_url - 
-             * @param {string} public_key - Public key string
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * 
-             */
-            xos.prototype.Service_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/services/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['versionNumber'] !== undefined) {
-                    form['versionNumber'] = parameters['versionNumber'];
-                }
-
-                if (parameters['published'] !== undefined) {
-                    form['published'] = parameters['published'];
-                }
-
-                if (parameters['view_url'] !== undefined) {
-                    form['view_url'] = parameters['view_url'];
-                }
-
-                if (parameters['icon_url'] !== undefined) {
-                    form['icon_url'] = parameters['icon_url'];
-                }
-
-                if (parameters['public_key'] !== undefined) {
-                    form['public_key'] = parameters['public_key'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Service_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/services/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Class_List_GET
-             * 
-             */
-            xos.prototype.Service_Class_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceclasses/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Class_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} description - 
-             * @param {integer} commitment - 
-             * @param {integer} membershipFee - 
-             * @param {integer} membershipFeeMonths - 
-             * @param {boolean} upgradeRequiresApproval - 
-             * 
-             */
-            xos.prototype.Service_Class_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceclasses/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['description'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: description'));
-                    return deferred.promise;
-                }
-
-                if (parameters['commitment'] !== undefined) {
-                    form['commitment'] = parameters['commitment'];
-                }
-
-                if (parameters['commitment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: commitment'));
-                    return deferred.promise;
-                }
-
-                if (parameters['membershipFee'] !== undefined) {
-                    form['membershipFee'] = parameters['membershipFee'];
-                }
-
-                if (parameters['membershipFee'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: membershipFee'));
-                    return deferred.promise;
-                }
-
-                if (parameters['membershipFeeMonths'] !== undefined) {
-                    form['membershipFeeMonths'] = parameters['membershipFeeMonths'];
-                }
-
-                if (parameters['membershipFeeMonths'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: membershipFeeMonths'));
-                    return deferred.promise;
-                }
-
-                if (parameters['upgradeRequiresApproval'] !== undefined) {
-                    form['upgradeRequiresApproval'] = parameters['upgradeRequiresApproval'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Class_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Service_Class_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceclasses/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Class_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} description - 
-             * @param {integer} commitment - 
-             * @param {integer} membershipFee - 
-             * @param {integer} membershipFeeMonths - 
-             * @param {boolean} upgradeRequiresApproval - 
-             * 
-             */
-            xos.prototype.Service_Class_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceclasses/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['description'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: description'));
-                    return deferred.promise;
-                }
-
-                if (parameters['commitment'] !== undefined) {
-                    form['commitment'] = parameters['commitment'];
-                }
-
-                if (parameters['commitment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: commitment'));
-                    return deferred.promise;
-                }
-
-                if (parameters['membershipFee'] !== undefined) {
-                    form['membershipFee'] = parameters['membershipFee'];
-                }
-
-                if (parameters['membershipFee'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: membershipFee'));
-                    return deferred.promise;
-                }
-
-                if (parameters['membershipFeeMonths'] !== undefined) {
-                    form['membershipFeeMonths'] = parameters['membershipFeeMonths'];
-                }
-
-                if (parameters['membershipFeeMonths'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: membershipFeeMonths'));
-                    return deferred.promise;
-                }
-
-                if (parameters['upgradeRequiresApproval'] !== undefined) {
-                    form['upgradeRequiresApproval'] = parameters['upgradeRequiresApproval'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Class_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} description - 
-             * @param {integer} commitment - 
-             * @param {integer} membershipFee - 
-             * @param {integer} membershipFeeMonths - 
-             * @param {boolean} upgradeRequiresApproval - 
-             * 
-             */
-            xos.prototype.Service_Class_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceclasses/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['commitment'] !== undefined) {
-                    form['commitment'] = parameters['commitment'];
-                }
-
-                if (parameters['membershipFee'] !== undefined) {
-                    form['membershipFee'] = parameters['membershipFee'];
-                }
-
-                if (parameters['membershipFeeMonths'] !== undefined) {
-                    form['membershipFeeMonths'] = parameters['membershipFeeMonths'];
-                }
-
-                if (parameters['upgradeRequiresApproval'] !== undefined) {
-                    form['upgradeRequiresApproval'] = parameters['upgradeRequiresApproval'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Class_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Service_Class_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceclasses/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Payment_List_GET
-             * 
-             */
-            xos.prototype.Payment_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/payments/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Payment_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} account - 
-             * @param {number} amount - 
-             * @param {string} date - 
-             * 
-             */
-            xos.prototype.Payment_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/payments/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['account'] !== undefined) {
-                    form['account'] = parameters['account'];
-                }
-
-                if (parameters['account'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: account'));
-                    return deferred.promise;
-                }
-
-                if (parameters['amount'] !== undefined) {
-                    form['amount'] = parameters['amount'];
-                }
-
-                if (parameters['amount'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: amount'));
-                    return deferred.promise;
-                }
-
-                if (parameters['date'] !== undefined) {
-                    form['date'] = parameters['date'];
-                }
-
-                if (parameters['date'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: date'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Payment_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Payment_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/payments/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Payment_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} account - 
-             * @param {number} amount - 
-             * @param {string} date - 
-             * 
-             */
-            xos.prototype.Payment_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/payments/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['account'] !== undefined) {
-                    form['account'] = parameters['account'];
-                }
-
-                if (parameters['account'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: account'));
-                    return deferred.promise;
-                }
-
-                if (parameters['amount'] !== undefined) {
-                    form['amount'] = parameters['amount'];
-                }
-
-                if (parameters['amount'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: amount'));
-                    return deferred.promise;
-                }
-
-                if (parameters['date'] !== undefined) {
-                    form['date'] = parameters['date'];
-                }
-
-                if (parameters['date'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: date'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Payment_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} account - 
-             * @param {number} amount - 
-             * @param {string} date - 
-             * 
-             */
-            xos.prototype.Payment_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/payments/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['account'] !== undefined) {
-                    form['account'] = parameters['account'];
-                }
-
-                if (parameters['amount'] !== undefined) {
-                    form['amount'] = parameters['amount'];
-                }
-
-                if (parameters['date'] !== undefined) {
-                    form['date'] = parameters['date'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Payment_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Payment_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/payments/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Subscriber_List_GET
-             * 
-             */
-            xos.prototype.Subscriber_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/subscribers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Subscriber_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} name - name
-             * @param {string} service_specific_attribute - 
-             * @param {string} service_specific_id - 
-             * 
-             */
-            xos.prototype.Subscriber_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/subscribers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Subscriber_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Subscriber_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/subscribers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Subscriber_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} name - name
-             * @param {string} service_specific_attribute - 
-             * @param {string} service_specific_id - 
-             * 
-             */
-            xos.prototype.Subscriber_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/subscribers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Subscriber_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} name - name
-             * @param {string} service_specific_attribute - 
-             * @param {string} service_specific_id - 
-             * 
-             */
-            xos.prototype.Subscriber_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/subscribers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Subscriber_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Subscriber_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/subscribers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Instance_List_GET
-             * 
-             */
-            xos.prototype.Instance_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/instances/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Instance_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} instance_id - Nova instance id
-             * @param {string} instance_uuid - Nova instance uuid
-             * @param {string} name - Instance name
-             * @param {string} instance_name - OpenStack generated name
-             * @param {string} ip - Instance ip address
-             * @param {string} image - 
-             * @param {string} creator - 
-             * @param {string} slice - 
-             * @param {string} deployment - 
-             * @param {string} node - 
-             * @param {integer} numberCores - Number of cores for instance
-             * @param {string} flavor - Flavor of this instance
-             * @param {string} userData - user_data passed to instance during creation
-             * 
-             */
-            xos.prototype.Instance_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/instances/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['instance_id'] !== undefined) {
-                    form['instance_id'] = parameters['instance_id'];
-                }
-
-                if (parameters['instance_uuid'] !== undefined) {
-                    form['instance_uuid'] = parameters['instance_uuid'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['instance_name'] !== undefined) {
-                    form['instance_name'] = parameters['instance_name'];
-                }
-
-                if (parameters['ip'] !== undefined) {
-                    form['ip'] = parameters['ip'];
-                }
-
-                if (parameters['image'] !== undefined) {
-                    form['image'] = parameters['image'];
-                }
-
-                if (parameters['image'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: image'));
-                    return deferred.promise;
-                }
-
-                if (parameters['creator'] !== undefined) {
-                    form['creator'] = parameters['creator'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['deployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: deployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters['node'] !== undefined) {
-                    form['node'] = parameters['node'];
-                }
-
-                if (parameters['node'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: node'));
-                    return deferred.promise;
-                }
-
-                if (parameters['numberCores'] !== undefined) {
-                    form['numberCores'] = parameters['numberCores'];
-                }
-
-                if (parameters['numberCores'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: numberCores'));
-                    return deferred.promise;
-                }
-
-                if (parameters['flavor'] !== undefined) {
-                    form['flavor'] = parameters['flavor'];
-                }
-
-                if (parameters['flavor'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: flavor'));
-                    return deferred.promise;
-                }
-
-                if (parameters['userData'] !== undefined) {
-                    form['userData'] = parameters['userData'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Instance_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Instance_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/instances/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Instance_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} instance_id - Nova instance id
-             * @param {string} instance_uuid - Nova instance uuid
-             * @param {string} name - Instance name
-             * @param {string} instance_name - OpenStack generated name
-             * @param {string} ip - Instance ip address
-             * @param {string} image - 
-             * @param {string} creator - 
-             * @param {string} slice - 
-             * @param {string} deployment - 
-             * @param {string} node - 
-             * @param {integer} numberCores - Number of cores for instance
-             * @param {string} flavor - Flavor of this instance
-             * @param {string} userData - user_data passed to instance during creation
-             * 
-             */
-            xos.prototype.Instance_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/instances/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['instance_id'] !== undefined) {
-                    form['instance_id'] = parameters['instance_id'];
-                }
-
-                if (parameters['instance_uuid'] !== undefined) {
-                    form['instance_uuid'] = parameters['instance_uuid'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['instance_name'] !== undefined) {
-                    form['instance_name'] = parameters['instance_name'];
-                }
-
-                if (parameters['ip'] !== undefined) {
-                    form['ip'] = parameters['ip'];
-                }
-
-                if (parameters['image'] !== undefined) {
-                    form['image'] = parameters['image'];
-                }
-
-                if (parameters['image'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: image'));
-                    return deferred.promise;
-                }
-
-                if (parameters['creator'] !== undefined) {
-                    form['creator'] = parameters['creator'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['deployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: deployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters['node'] !== undefined) {
-                    form['node'] = parameters['node'];
-                }
-
-                if (parameters['node'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: node'));
-                    return deferred.promise;
-                }
-
-                if (parameters['numberCores'] !== undefined) {
-                    form['numberCores'] = parameters['numberCores'];
-                }
-
-                if (parameters['numberCores'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: numberCores'));
-                    return deferred.promise;
-                }
-
-                if (parameters['flavor'] !== undefined) {
-                    form['flavor'] = parameters['flavor'];
-                }
-
-                if (parameters['flavor'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: flavor'));
-                    return deferred.promise;
-                }
-
-                if (parameters['userData'] !== undefined) {
-                    form['userData'] = parameters['userData'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Instance_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} instance_id - Nova instance id
-             * @param {string} instance_uuid - Nova instance uuid
-             * @param {string} name - Instance name
-             * @param {string} instance_name - OpenStack generated name
-             * @param {string} ip - Instance ip address
-             * @param {string} image - 
-             * @param {string} creator - 
-             * @param {string} slice - 
-             * @param {string} deployment - 
-             * @param {string} node - 
-             * @param {integer} numberCores - Number of cores for instance
-             * @param {string} flavor - Flavor of this instance
-             * @param {string} userData - user_data passed to instance during creation
-             * 
-             */
-            xos.prototype.Instance_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/instances/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['instance_id'] !== undefined) {
-                    form['instance_id'] = parameters['instance_id'];
-                }
-
-                if (parameters['instance_uuid'] !== undefined) {
-                    form['instance_uuid'] = parameters['instance_uuid'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['instance_name'] !== undefined) {
-                    form['instance_name'] = parameters['instance_name'];
-                }
-
-                if (parameters['ip'] !== undefined) {
-                    form['ip'] = parameters['ip'];
-                }
-
-                if (parameters['image'] !== undefined) {
-                    form['image'] = parameters['image'];
-                }
-
-                if (parameters['creator'] !== undefined) {
-                    form['creator'] = parameters['creator'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['node'] !== undefined) {
-                    form['node'] = parameters['node'];
-                }
-
-                if (parameters['numberCores'] !== undefined) {
-                    form['numberCores'] = parameters['numberCores'];
-                }
-
-                if (parameters['flavor'] !== undefined) {
-                    form['flavor'] = parameters['flavor'];
-                }
-
-                if (parameters['userData'] !== undefined) {
-                    form['userData'] = parameters['userData'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Instance_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Instance_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/instances/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Charge_List_GET
-             * 
-             */
-            xos.prototype.Charge_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/charges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Charge_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} account - 
-             * @param {string} slice - 
-             * @param {choice} kind - 
-             * @param {choice} state - 
-             * @param {string} date - 
-             * @param {string} object - 
-             * @param {number} amount - 
-             * @param {number} coreHours - 
-             * @param {string} invoice - 
-             * 
-             */
-            xos.prototype.Charge_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/charges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['account'] !== undefined) {
-                    form['account'] = parameters['account'];
-                }
-
-                if (parameters['account'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: account'));
-                    return deferred.promise;
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['state'] !== undefined) {
-                    form['state'] = parameters['state'];
-                }
-
-                if (parameters['state'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: state'));
-                    return deferred.promise;
-                }
-
-                if (parameters['date'] !== undefined) {
-                    form['date'] = parameters['date'];
-                }
-
-                if (parameters['date'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: date'));
-                    return deferred.promise;
-                }
-
-                if (parameters['object'] !== undefined) {
-                    form['object'] = parameters['object'];
-                }
-
-                if (parameters['object'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: object'));
-                    return deferred.promise;
-                }
-
-                if (parameters['amount'] !== undefined) {
-                    form['amount'] = parameters['amount'];
-                }
-
-                if (parameters['amount'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: amount'));
-                    return deferred.promise;
-                }
-
-                if (parameters['coreHours'] !== undefined) {
-                    form['coreHours'] = parameters['coreHours'];
-                }
-
-                if (parameters['coreHours'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: coreHours'));
-                    return deferred.promise;
-                }
-
-                if (parameters['invoice'] !== undefined) {
-                    form['invoice'] = parameters['invoice'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Charge_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Charge_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/charges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Charge_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} account - 
-             * @param {string} slice - 
-             * @param {choice} kind - 
-             * @param {choice} state - 
-             * @param {string} date - 
-             * @param {string} object - 
-             * @param {number} amount - 
-             * @param {number} coreHours - 
-             * @param {string} invoice - 
-             * 
-             */
-            xos.prototype.Charge_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/charges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['account'] !== undefined) {
-                    form['account'] = parameters['account'];
-                }
-
-                if (parameters['account'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: account'));
-                    return deferred.promise;
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['state'] !== undefined) {
-                    form['state'] = parameters['state'];
-                }
-
-                if (parameters['state'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: state'));
-                    return deferred.promise;
-                }
-
-                if (parameters['date'] !== undefined) {
-                    form['date'] = parameters['date'];
-                }
-
-                if (parameters['date'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: date'));
-                    return deferred.promise;
-                }
-
-                if (parameters['object'] !== undefined) {
-                    form['object'] = parameters['object'];
-                }
-
-                if (parameters['object'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: object'));
-                    return deferred.promise;
-                }
-
-                if (parameters['amount'] !== undefined) {
-                    form['amount'] = parameters['amount'];
-                }
-
-                if (parameters['amount'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: amount'));
-                    return deferred.promise;
-                }
-
-                if (parameters['coreHours'] !== undefined) {
-                    form['coreHours'] = parameters['coreHours'];
-                }
-
-                if (parameters['coreHours'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: coreHours'));
-                    return deferred.promise;
-                }
-
-                if (parameters['invoice'] !== undefined) {
-                    form['invoice'] = parameters['invoice'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Charge_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} account - 
-             * @param {string} slice - 
-             * @param {choice} kind - 
-             * @param {choice} state - 
-             * @param {string} date - 
-             * @param {string} object - 
-             * @param {number} amount - 
-             * @param {number} coreHours - 
-             * @param {string} invoice - 
-             * 
-             */
-            xos.prototype.Charge_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/charges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['account'] !== undefined) {
-                    form['account'] = parameters['account'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['state'] !== undefined) {
-                    form['state'] = parameters['state'];
-                }
-
-                if (parameters['date'] !== undefined) {
-                    form['date'] = parameters['date'];
-                }
-
-                if (parameters['object'] !== undefined) {
-                    form['object'] = parameters['object'];
-                }
-
-                if (parameters['amount'] !== undefined) {
-                    form['amount'] = parameters['amount'];
-                }
-
-                if (parameters['coreHours'] !== undefined) {
-                    form['coreHours'] = parameters['coreHours'];
-                }
-
-                if (parameters['invoice'] !== undefined) {
-                    form['invoice'] = parameters['invoice'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Charge_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Charge_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/charges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Program_List_GET
-             * 
-             */
-            xos.prototype.Program_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/programs/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Program_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Service Name
-             * @param {string} description - Description of Service
-
-             * @param {choice} command - Command to run
-             * @param {string} owner - 
-             * @param {string} contents - Contents of Program
-             * @param {string} output - Output of Program
-             * @param {string} messages - Debug messages
-             * @param {string} status - Status of program
-             * 
-             */
-            xos.prototype.Program_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/programs/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                form['kind'] = 'tosca';
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['command'] !== undefined) {
-                    form['command'] = parameters['command'];
-                }
-
-                if (parameters['owner'] !== undefined) {
-                    form['owner'] = parameters['owner'];
-                }
-
-                if (parameters['contents'] !== undefined) {
-                    form['contents'] = parameters['contents'];
-                }
-
-                if (parameters['output'] !== undefined) {
-                    form['output'] = parameters['output'];
-                }
-
-                if (parameters['messages'] !== undefined) {
-                    form['messages'] = parameters['messages'];
-                }
-
-                if (parameters['status'] !== undefined) {
-                    form['status'] = parameters['status'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Program_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Program_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/programs/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Program_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Service Name
-             * @param {string} description - Description of Service
-
-             * @param {choice} command - Command to run
-             * @param {string} owner - 
-             * @param {string} contents - Contents of Program
-             * @param {string} output - Output of Program
-             * @param {string} messages - Debug messages
-             * @param {string} status - Status of program
-             * 
-             */
-            xos.prototype.Program_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/programs/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                form['kind'] = 'tosca';
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['command'] !== undefined) {
-                    form['command'] = parameters['command'];
-                }
-
-                if (parameters['owner'] !== undefined) {
-                    form['owner'] = parameters['owner'];
-                }
-
-                if (parameters['contents'] !== undefined) {
-                    form['contents'] = parameters['contents'];
-                }
-
-                if (parameters['output'] !== undefined) {
-                    form['output'] = parameters['output'];
-                }
-
-                if (parameters['messages'] !== undefined) {
-                    form['messages'] = parameters['messages'];
-                }
-
-                if (parameters['status'] !== undefined) {
-                    form['status'] = parameters['status'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Program_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Service Name
-             * @param {string} description - Description of Service
-
-             * @param {choice} command - Command to run
-             * @param {string} owner - 
-             * @param {string} contents - Contents of Program
-             * @param {string} output - Output of Program
-             * @param {string} messages - Debug messages
-             * @param {string} status - Status of program
-             * 
-             */
-            xos.prototype.Program_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/programs/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                form['kind'] = 'tosca';
-
-                if (parameters['command'] !== undefined) {
-                    form['command'] = parameters['command'];
-                }
-
-                if (parameters['owner'] !== undefined) {
-                    form['owner'] = parameters['owner'];
-                }
-
-                if (parameters['contents'] !== undefined) {
-                    form['contents'] = parameters['contents'];
-                }
-
-                if (parameters['output'] !== undefined) {
-                    form['output'] = parameters['output'];
-                }
-
-                if (parameters['messages'] !== undefined) {
-                    form['messages'] = parameters['messages'];
-                }
-
-                if (parameters['status'] !== undefined) {
-                    form['status'] = parameters['status'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Program_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Program_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/programs/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Role_List_GET
-             * 
-             */
-            xos.prototype.Role_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/roles/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Role_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} role_type - 
-             * @param {string} role - 
-             * @param {string} description - 
-             * @param {string} content_type - 
-             * 
-             */
-            xos.prototype.Role_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/roles/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['role_type'] !== undefined) {
-                    form['role_type'] = parameters['role_type'];
-                }
-
-                if (parameters['role_type'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role_type'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['description'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: description'));
-                    return deferred.promise;
-                }
-
-                if (parameters['content_type'] !== undefined) {
-                    form['content_type'] = parameters['content_type'];
-                }
-
-                if (parameters['content_type'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: content_type'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Role_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Role_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/roles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Role_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} role_type - 
-             * @param {string} role - 
-             * @param {string} description - 
-             * @param {string} content_type - 
-             * 
-             */
-            xos.prototype.Role_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/roles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['role_type'] !== undefined) {
-                    form['role_type'] = parameters['role_type'];
-                }
-
-                if (parameters['role_type'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role_type'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['description'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: description'));
-                    return deferred.promise;
-                }
-
-                if (parameters['content_type'] !== undefined) {
-                    form['content_type'] = parameters['content_type'];
-                }
-
-                if (parameters['content_type'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: content_type'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Role_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} role_type - 
-             * @param {string} role - 
-             * @param {string} description - 
-             * @param {string} content_type - 
-             * 
-             */
-            xos.prototype.Role_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/roles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['role_type'] !== undefined) {
-                    form['role_type'] = parameters['role_type'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['content_type'] !== undefined) {
-                    form['content_type'] = parameters['content_type'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Role_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Role_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/roles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Usable_Object_List_GET
-             * 
-             */
-            xos.prototype.Usable_Object_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/usableobjects/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Usable_Object_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * 
-             */
-            xos.prototype.Usable_Object_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/usableobjects/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Usable_Object_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Usable_Object_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/usableobjects/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Usable_Object_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * 
-             */
-            xos.prototype.Usable_Object_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/usableobjects/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Usable_Object_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * 
-             */
-            xos.prototype.Usable_Object_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/usableobjects/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Usable_Object_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Usable_Object_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/usableobjects/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Provider_List_GET
-             * 
-             */
-            xos.prototype.Provider_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/providers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Provider_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} name - name
-             * @param {string} service_specific_attribute - 
-             * @param {string} service_specific_id - 
-             * 
-             */
-            xos.prototype.Provider_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/providers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Provider_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Provider_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/providers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Provider_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} name - name
-             * @param {string} service_specific_attribute - 
-             * @param {string} service_specific_id - 
-             * 
-             */
-            xos.prototype.Provider_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/providers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Provider_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} name - name
-             * @param {string} service_specific_attribute - 
-             * @param {string} service_specific_id - 
-             * 
-             */
-            xos.prototype.Provider_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/providers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Provider_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Provider_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/providers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Credential_List_GET
-             * 
-             */
-            xos.prototype.Slice_Credential_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slicecredentials/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Credential_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} slice - The User this credential is associated with
-             * @param {string} name - The credential type, e.g. ec2
-             * @param {string} key_id - The backend id of this credential
-             * @param {string} enc_value - The key value of this credential
-             * 
-             */
-            xos.prototype.Slice_Credential_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slicecredentials/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['key_id'] !== undefined) {
-                    form['key_id'] = parameters['key_id'];
-                }
-
-                if (parameters['key_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: key_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enc_value'] !== undefined) {
-                    form['enc_value'] = parameters['enc_value'];
-                }
-
-                if (parameters['enc_value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: enc_value'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Credential_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Slice_Credential_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slicecredentials/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Credential_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} slice - The User this credential is associated with
-             * @param {string} name - The credential type, e.g. ec2
-             * @param {string} key_id - The backend id of this credential
-             * @param {string} enc_value - The key value of this credential
-             * 
-             */
-            xos.prototype.Slice_Credential_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slicecredentials/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['key_id'] !== undefined) {
-                    form['key_id'] = parameters['key_id'];
-                }
-
-                if (parameters['key_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: key_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enc_value'] !== undefined) {
-                    form['enc_value'] = parameters['enc_value'];
-                }
-
-                if (parameters['enc_value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: enc_value'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Credential_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} slice - The User this credential is associated with
-             * @param {string} name - The credential type, e.g. ec2
-             * @param {string} key_id - The backend id of this credential
-             * @param {string} enc_value - The key value of this credential
-             * 
-             */
-            xos.prototype.Slice_Credential_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slicecredentials/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['key_id'] !== undefined) {
-                    form['key_id'] = parameters['key_id'];
-                }
-
-                if (parameters['enc_value'] !== undefined) {
-                    form['enc_value'] = parameters['enc_value'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Credential_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Slice_Credential_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slicecredentials/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Node_List_GET
-             * 
-             */
-            xos.prototype.Node_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/nodes/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Node_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of the Node
-             * @param {string} site_deployment - 
-             * @param {string} site - 
-             * 
-             */
-            xos.prototype.Node_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/nodes/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['site_deployment'] !== undefined) {
-                    form['site_deployment'] = parameters['site_deployment'];
-                }
-
-                if (parameters['site_deployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site_deployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Node_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Node_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/nodes/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Node_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of the Node
-             * @param {string} site_deployment - 
-             * @param {string} site - 
-             * 
-             */
-            xos.prototype.Node_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/nodes/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['site_deployment'] !== undefined) {
-                    form['site_deployment'] = parameters['site_deployment'];
-                }
-
-                if (parameters['site_deployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site_deployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Node_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of the Node
-             * @param {string} site_deployment - 
-             * @param {string} site - 
-             * 
-             */
-            xos.prototype.Node_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/nodes/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['site_deployment'] !== undefined) {
-                    form['site_deployment'] = parameters['site_deployment'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Node_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Node_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/nodes/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Dashboard_View_List_GET
-             * 
-             */
-            xos.prototype.Dashboard_View_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/dashboardviews/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Dashboard_View_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of the View
-             * @param {string} url - URL of Dashboard
-             * @param {boolean} enabled - 
-             * 
-             */
-            xos.prototype.Dashboard_View_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/dashboardviews/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url'] !== undefined) {
-                    form['url'] = parameters['url'];
-                }
-
-                if (parameters['url'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Dashboard_View_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Dashboard_View_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/dashboardviews/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Dashboard_View_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of the View
-             * @param {string} url - URL of Dashboard
-             * @param {boolean} enabled - 
-             * 
-             */
-            xos.prototype.Dashboard_View_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/dashboardviews/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url'] !== undefined) {
-                    form['url'] = parameters['url'];
-                }
-
-                if (parameters['url'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Dashboard_View_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of the View
-             * @param {string} url - URL of Dashboard
-             * @param {boolean} enabled - 
-             * 
-             */
-            xos.prototype.Dashboard_View_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/dashboardviews/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['url'] !== undefined) {
-                    form['url'] = parameters['url'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Dashboard_View_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Dashboard_View_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/dashboardviews/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Network_List_GET
-             * 
-             */
-            xos.prototype.Controller_Network_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllernetworks/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Network_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} network - 
-             * @param {string} controller - 
-             * @param {string} net_id - Quantum network
-             * @param {string} router_id - Quantum router id
-             * @param {string} subnet_id - Quantum subnet id
-             * @param {string} subnet - 
-             * 
-             */
-            xos.prototype.Controller_Network_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllernetworks/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['network'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: network'));
-                    return deferred.promise;
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['net_id'] !== undefined) {
-                    form['net_id'] = parameters['net_id'];
-                }
-
-                if (parameters['router_id'] !== undefined) {
-                    form['router_id'] = parameters['router_id'];
-                }
-
-                if (parameters['subnet_id'] !== undefined) {
-                    form['subnet_id'] = parameters['subnet_id'];
-                }
-
-                if (parameters['subnet'] !== undefined) {
-                    form['subnet'] = parameters['subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Network_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Network_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllernetworks/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Network_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} network - 
-             * @param {string} controller - 
-             * @param {string} net_id - Quantum network
-             * @param {string} router_id - Quantum router id
-             * @param {string} subnet_id - Quantum subnet id
-             * @param {string} subnet - 
-             * 
-             */
-            xos.prototype.Controller_Network_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllernetworks/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['network'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: network'));
-                    return deferred.promise;
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['net_id'] !== undefined) {
-                    form['net_id'] = parameters['net_id'];
-                }
-
-                if (parameters['router_id'] !== undefined) {
-                    form['router_id'] = parameters['router_id'];
-                }
-
-                if (parameters['subnet_id'] !== undefined) {
-                    form['subnet_id'] = parameters['subnet_id'];
-                }
-
-                if (parameters['subnet'] !== undefined) {
-                    form['subnet'] = parameters['subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Network_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} network - 
-             * @param {string} controller - 
-             * @param {string} net_id - Quantum network
-             * @param {string} router_id - Quantum router id
-             * @param {string} subnet_id - Quantum subnet id
-             * @param {string} subnet - 
-             * 
-             */
-            xos.prototype.Controller_Network_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllernetworks/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['net_id'] !== undefined) {
-                    form['net_id'] = parameters['net_id'];
-                }
-
-                if (parameters['router_id'] !== undefined) {
-                    form['router_id'] = parameters['router_id'];
-                }
-
-                if (parameters['subnet_id'] !== undefined) {
-                    form['subnet_id'] = parameters['subnet_id'];
-                }
-
-                if (parameters['subnet'] !== undefined) {
-                    form['subnet'] = parameters['subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Network_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Network_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllernetworks/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Image_Deployments_List_GET
-             * 
-             */
-            xos.prototype.Image_Deployments_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/imagedeploymentses/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Image_Deployments_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} image - 
-             * @param {string} deployment - 
-             * 
-             */
-            xos.prototype.Image_Deployments_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/imagedeploymentses/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['image'] !== undefined) {
-                    form['image'] = parameters['image'];
-                }
-
-                if (parameters['image'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: image'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['deployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: deployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Image_Deployments_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Image_Deployments_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/imagedeploymentses/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Image_Deployments_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} image - 
-             * @param {string} deployment - 
-             * 
-             */
-            xos.prototype.Image_Deployments_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/imagedeploymentses/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['image'] !== undefined) {
-                    form['image'] = parameters['image'];
-                }
-
-                if (parameters['image'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: image'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['deployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: deployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Image_Deployments_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} image - 
-             * @param {string} deployment - 
-             * 
-             */
-            xos.prototype.Image_Deployments_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/imagedeploymentses/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['image'] !== undefined) {
-                    form['image'] = parameters['image'];
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Image_Deployments_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Image_Deployments_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/imagedeploymentses/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_User_List_GET
-             * 
-             */
-            xos.prototype.Controller_User_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerusers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_User_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} controller - 
-             * @param {string} kuser_id - Keystone user id
-             * 
-             */
-            xos.prototype.Controller_User_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerusers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['kuser_id'] !== undefined) {
-                    form['kuser_id'] = parameters['kuser_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_User_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_User_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerusers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_User_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} controller - 
-             * @param {string} kuser_id - Keystone user id
-             * 
-             */
-            xos.prototype.Controller_User_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerusers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['kuser_id'] !== undefined) {
-                    form['kuser_id'] = parameters['kuser_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_User_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} controller - 
-             * @param {string} kuser_id - Keystone user id
-             * 
-             */
-            xos.prototype.Controller_User_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerusers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['kuser_id'] !== undefined) {
-                    form['kuser_id'] = parameters['kuser_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_User_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_User_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerusers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Reserved_Resource_List_GET
-             * 
-             */
-            xos.prototype.Reserved_Resource_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/reservedresources/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Reserved_Resource_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} instance - 
-             * @param {string} resource - 
-             * @param {integer} quantity - 
-             * @param {string} reservationSet - 
-             * 
-             */
-            xos.prototype.Reserved_Resource_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/reservedresources/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['instance'] !== undefined) {
-                    form['instance'] = parameters['instance'];
-                }
-
-                if (parameters['instance'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: instance'));
-                    return deferred.promise;
-                }
-
-                if (parameters['resource'] !== undefined) {
-                    form['resource'] = parameters['resource'];
-                }
-
-                if (parameters['resource'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: resource'));
-                    return deferred.promise;
-                }
-
-                if (parameters['quantity'] !== undefined) {
-                    form['quantity'] = parameters['quantity'];
-                }
-
-                if (parameters['quantity'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: quantity'));
-                    return deferred.promise;
-                }
-
-                if (parameters['reservationSet'] !== undefined) {
-                    form['reservationSet'] = parameters['reservationSet'];
-                }
-
-                if (parameters['reservationSet'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: reservationSet'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Reserved_Resource_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Reserved_Resource_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/reservedresources/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Reserved_Resource_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} instance - 
-             * @param {string} resource - 
-             * @param {integer} quantity - 
-             * @param {string} reservationSet - 
-             * 
-             */
-            xos.prototype.Reserved_Resource_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/reservedresources/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['instance'] !== undefined) {
-                    form['instance'] = parameters['instance'];
-                }
-
-                if (parameters['instance'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: instance'));
-                    return deferred.promise;
-                }
-
-                if (parameters['resource'] !== undefined) {
-                    form['resource'] = parameters['resource'];
-                }
-
-                if (parameters['resource'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: resource'));
-                    return deferred.promise;
-                }
-
-                if (parameters['quantity'] !== undefined) {
-                    form['quantity'] = parameters['quantity'];
-                }
-
-                if (parameters['quantity'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: quantity'));
-                    return deferred.promise;
-                }
-
-                if (parameters['reservationSet'] !== undefined) {
-                    form['reservationSet'] = parameters['reservationSet'];
-                }
-
-                if (parameters['reservationSet'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: reservationSet'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Reserved_Resource_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} instance - 
-             * @param {string} resource - 
-             * @param {integer} quantity - 
-             * @param {string} reservationSet - 
-             * 
-             */
-            xos.prototype.Reserved_Resource_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/reservedresources/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['instance'] !== undefined) {
-                    form['instance'] = parameters['instance'];
-                }
-
-                if (parameters['resource'] !== undefined) {
-                    form['resource'] = parameters['resource'];
-                }
-
-                if (parameters['quantity'] !== undefined) {
-                    form['quantity'] = parameters['quantity'];
-                }
-
-                if (parameters['reservationSet'] !== undefined) {
-                    form['reservationSet'] = parameters['reservationSet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Reserved_Resource_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Reserved_Resource_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/reservedresources/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Template_List_GET
-             * 
-             */
-            xos.prototype.Network_Template_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networktemplates/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Template_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} description - 
-             * @param {integer} guaranteed_bandwidth - 
-             * @param {choice} visibility - 
-             * @param {choice} translation - 
-             * @param {string} shared_network_name - 
-             * @param {string} shared_network_id - Quantum network
-             * @param {choice} topology_kind - 
-             * @param {choice} controller_kind - 
-             * 
-             */
-            xos.prototype.Network_Template_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networktemplates/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['guaranteed_bandwidth'] !== undefined) {
-                    form['guaranteed_bandwidth'] = parameters['guaranteed_bandwidth'];
-                }
-
-                if (parameters['guaranteed_bandwidth'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: guaranteed_bandwidth'));
-                    return deferred.promise;
-                }
-
-                if (parameters['visibility'] !== undefined) {
-                    form['visibility'] = parameters['visibility'];
-                }
-
-                if (parameters['visibility'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: visibility'));
-                    return deferred.promise;
-                }
-
-                if (parameters['translation'] !== undefined) {
-                    form['translation'] = parameters['translation'];
-                }
-
-                if (parameters['translation'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: translation'));
-                    return deferred.promise;
-                }
-
-                if (parameters['shared_network_name'] !== undefined) {
-                    form['shared_network_name'] = parameters['shared_network_name'];
-                }
-
-                if (parameters['shared_network_id'] !== undefined) {
-                    form['shared_network_id'] = parameters['shared_network_id'];
-                }
-
-                if (parameters['topology_kind'] !== undefined) {
-                    form['topology_kind'] = parameters['topology_kind'];
-                }
-
-                if (parameters['topology_kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: topology_kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['controller_kind'] !== undefined) {
-                    form['controller_kind'] = parameters['controller_kind'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Template_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Network_Template_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networktemplates/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Template_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} description - 
-             * @param {integer} guaranteed_bandwidth - 
-             * @param {choice} visibility - 
-             * @param {choice} translation - 
-             * @param {string} shared_network_name - 
-             * @param {string} shared_network_id - Quantum network
-             * @param {choice} topology_kind - 
-             * @param {choice} controller_kind - 
-             * 
-             */
-            xos.prototype.Network_Template_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networktemplates/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['guaranteed_bandwidth'] !== undefined) {
-                    form['guaranteed_bandwidth'] = parameters['guaranteed_bandwidth'];
-                }
-
-                if (parameters['guaranteed_bandwidth'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: guaranteed_bandwidth'));
-                    return deferred.promise;
-                }
-
-                if (parameters['visibility'] !== undefined) {
-                    form['visibility'] = parameters['visibility'];
-                }
-
-                if (parameters['visibility'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: visibility'));
-                    return deferred.promise;
-                }
-
-                if (parameters['translation'] !== undefined) {
-                    form['translation'] = parameters['translation'];
-                }
-
-                if (parameters['translation'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: translation'));
-                    return deferred.promise;
-                }
-
-                if (parameters['shared_network_name'] !== undefined) {
-                    form['shared_network_name'] = parameters['shared_network_name'];
-                }
-
-                if (parameters['shared_network_id'] !== undefined) {
-                    form['shared_network_id'] = parameters['shared_network_id'];
-                }
-
-                if (parameters['topology_kind'] !== undefined) {
-                    form['topology_kind'] = parameters['topology_kind'];
-                }
-
-                if (parameters['topology_kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: topology_kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['controller_kind'] !== undefined) {
-                    form['controller_kind'] = parameters['controller_kind'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Template_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} description - 
-             * @param {integer} guaranteed_bandwidth - 
-             * @param {choice} visibility - 
-             * @param {choice} translation - 
-             * @param {string} shared_network_name - 
-             * @param {string} shared_network_id - Quantum network
-             * @param {choice} topology_kind - 
-             * @param {choice} controller_kind - 
-             * 
-             */
-            xos.prototype.Network_Template_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networktemplates/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['guaranteed_bandwidth'] !== undefined) {
-                    form['guaranteed_bandwidth'] = parameters['guaranteed_bandwidth'];
-                }
-
-                if (parameters['visibility'] !== undefined) {
-                    form['visibility'] = parameters['visibility'];
-                }
-
-                if (parameters['translation'] !== undefined) {
-                    form['translation'] = parameters['translation'];
-                }
-
-                if (parameters['shared_network_name'] !== undefined) {
-                    form['shared_network_name'] = parameters['shared_network_name'];
-                }
-
-                if (parameters['shared_network_id'] !== undefined) {
-                    form['shared_network_id'] = parameters['shared_network_id'];
-                }
-
-                if (parameters['topology_kind'] !== undefined) {
-                    form['topology_kind'] = parameters['topology_kind'];
-                }
-
-                if (parameters['controller_kind'] !== undefined) {
-                    form['controller_kind'] = parameters['controller_kind'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Template_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Network_Template_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networktemplates/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Slice_List_GET
-             * 
-             */
-            xos.prototype.Network_Slice_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkslices/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Slice_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} network - 
-             * @param {string} slice - 
-             * 
-             */
-            xos.prototype.Network_Slice_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkslices/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['network'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: network'));
-                    return deferred.promise;
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Slice_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Network_Slice_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkslices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Slice_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} network - 
-             * @param {string} slice - 
-             * 
-             */
-            xos.prototype.Network_Slice_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkslices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['network'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: network'));
-                    return deferred.promise;
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Slice_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} network - 
-             * @param {string} slice - 
-             * 
-             */
-            xos.prototype.Network_Slice_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkslices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Slice_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Network_Slice_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkslices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Dashboard_View_List_GET
-             * 
-             */
-            xos.prototype.User_Dashboard_View_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/userdashboardviews/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Dashboard_View_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} dashboardView - 
-             * @param {integer} order - 
-             * 
-             */
-            xos.prototype.User_Dashboard_View_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/userdashboardviews/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['dashboardView'] !== undefined) {
-                    form['dashboardView'] = parameters['dashboardView'];
-                }
-
-                if (parameters['dashboardView'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: dashboardView'));
-                    return deferred.promise;
-                }
-
-                if (parameters['order'] !== undefined) {
-                    form['order'] = parameters['order'];
-                }
-
-                if (parameters['order'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: order'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Dashboard_View_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.User_Dashboard_View_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/userdashboardviews/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Dashboard_View_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} dashboardView - 
-             * @param {integer} order - 
-             * 
-             */
-            xos.prototype.User_Dashboard_View_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/userdashboardviews/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['dashboardView'] !== undefined) {
-                    form['dashboardView'] = parameters['dashboardView'];
-                }
-
-                if (parameters['dashboardView'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: dashboardView'));
-                    return deferred.promise;
-                }
-
-                if (parameters['order'] !== undefined) {
-                    form['order'] = parameters['order'];
-                }
-
-                if (parameters['order'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: order'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Dashboard_View_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} dashboardView - 
-             * @param {integer} order - 
-             * 
-             */
-            xos.prototype.User_Dashboard_View_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/userdashboardviews/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['dashboardView'] !== undefined) {
-                    form['dashboardView'] = parameters['dashboardView'];
-                }
-
-                if (parameters['order'] !== undefined) {
-                    form['order'] = parameters['order'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Dashboard_View_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.User_Dashboard_View_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/userdashboardviews/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_List_GET
-             * 
-             */
-            xos.prototype.Controller_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of the Controller
-             * @param {string} backend_type - Type of compute controller, e.g. EC2, OpenStack, or OpenStack version
-             * @param {string} version - Controller version
-             * @param {string} auth_url - Auth url for the compute controller
-             * @param {string} admin_user - Username of an admin user at this controller
-             * @param {string} admin_password - Password of theadmin user at this controller
-             * @param {string} admin_tenant - Name of the tenant the admin user belongs to
-             * @param {string} domain - Name of the domain this controller belongs to
-             * @param {string} rabbit_host - IP address of rabbitmq server at this controller
-             * @param {string} rabbit_user - Username of rabbitmq server at this controller
-             * @param {string} rabbit_password - Password of rabbitmq server at this controller
-             * @param {string} deployment - 
-             * 
-             */
-            xos.prototype.Controller_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['backend_type'] !== undefined) {
-                    form['backend_type'] = parameters['backend_type'];
-                }
-
-                if (parameters['backend_type'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_type'));
-                    return deferred.promise;
-                }
-
-                if (parameters['version'] !== undefined) {
-                    form['version'] = parameters['version'];
-                }
-
-                if (parameters['version'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: version'));
-                    return deferred.promise;
-                }
-
-                if (parameters['auth_url'] !== undefined) {
-                    form['auth_url'] = parameters['auth_url'];
-                }
-
-                if (parameters['admin_user'] !== undefined) {
-                    form['admin_user'] = parameters['admin_user'];
-                }
-
-                if (parameters['admin_password'] !== undefined) {
-                    form['admin_password'] = parameters['admin_password'];
-                }
-
-                if (parameters['admin_tenant'] !== undefined) {
-                    form['admin_tenant'] = parameters['admin_tenant'];
-                }
-
-                if (parameters['domain'] !== undefined) {
-                    form['domain'] = parameters['domain'];
-                }
-
-                if (parameters['rabbit_host'] !== undefined) {
-                    form['rabbit_host'] = parameters['rabbit_host'];
-                }
-
-                if (parameters['rabbit_user'] !== undefined) {
-                    form['rabbit_user'] = parameters['rabbit_user'];
-                }
-
-                if (parameters['rabbit_password'] !== undefined) {
-                    form['rabbit_password'] = parameters['rabbit_password'];
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['deployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: deployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of the Controller
-             * @param {string} backend_type - Type of compute controller, e.g. EC2, OpenStack, or OpenStack version
-             * @param {string} version - Controller version
-             * @param {string} auth_url - Auth url for the compute controller
-             * @param {string} admin_user - Username of an admin user at this controller
-             * @param {string} admin_password - Password of theadmin user at this controller
-             * @param {string} admin_tenant - Name of the tenant the admin user belongs to
-             * @param {string} domain - Name of the domain this controller belongs to
-             * @param {string} rabbit_host - IP address of rabbitmq server at this controller
-             * @param {string} rabbit_user - Username of rabbitmq server at this controller
-             * @param {string} rabbit_password - Password of rabbitmq server at this controller
-             * @param {string} deployment - 
-             * 
-             */
-            xos.prototype.Controller_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['backend_type'] !== undefined) {
-                    form['backend_type'] = parameters['backend_type'];
-                }
-
-                if (parameters['backend_type'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_type'));
-                    return deferred.promise;
-                }
-
-                if (parameters['version'] !== undefined) {
-                    form['version'] = parameters['version'];
-                }
-
-                if (parameters['version'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: version'));
-                    return deferred.promise;
-                }
-
-                if (parameters['auth_url'] !== undefined) {
-                    form['auth_url'] = parameters['auth_url'];
-                }
-
-                if (parameters['admin_user'] !== undefined) {
-                    form['admin_user'] = parameters['admin_user'];
-                }
-
-                if (parameters['admin_password'] !== undefined) {
-                    form['admin_password'] = parameters['admin_password'];
-                }
-
-                if (parameters['admin_tenant'] !== undefined) {
-                    form['admin_tenant'] = parameters['admin_tenant'];
-                }
-
-                if (parameters['domain'] !== undefined) {
-                    form['domain'] = parameters['domain'];
-                }
-
-                if (parameters['rabbit_host'] !== undefined) {
-                    form['rabbit_host'] = parameters['rabbit_host'];
-                }
-
-                if (parameters['rabbit_user'] !== undefined) {
-                    form['rabbit_user'] = parameters['rabbit_user'];
-                }
-
-                if (parameters['rabbit_password'] !== undefined) {
-                    form['rabbit_password'] = parameters['rabbit_password'];
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['deployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: deployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of the Controller
-             * @param {string} backend_type - Type of compute controller, e.g. EC2, OpenStack, or OpenStack version
-             * @param {string} version - Controller version
-             * @param {string} auth_url - Auth url for the compute controller
-             * @param {string} admin_user - Username of an admin user at this controller
-             * @param {string} admin_password - Password of theadmin user at this controller
-             * @param {string} admin_tenant - Name of the tenant the admin user belongs to
-             * @param {string} domain - Name of the domain this controller belongs to
-             * @param {string} rabbit_host - IP address of rabbitmq server at this controller
-             * @param {string} rabbit_user - Username of rabbitmq server at this controller
-             * @param {string} rabbit_password - Password of rabbitmq server at this controller
-             * @param {string} deployment - 
-             * 
-             */
-            xos.prototype.Controller_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['backend_type'] !== undefined) {
-                    form['backend_type'] = parameters['backend_type'];
-                }
-
-                if (parameters['version'] !== undefined) {
-                    form['version'] = parameters['version'];
-                }
-
-                if (parameters['auth_url'] !== undefined) {
-                    form['auth_url'] = parameters['auth_url'];
-                }
-
-                if (parameters['admin_user'] !== undefined) {
-                    form['admin_user'] = parameters['admin_user'];
-                }
-
-                if (parameters['admin_password'] !== undefined) {
-                    form['admin_password'] = parameters['admin_password'];
-                }
-
-                if (parameters['admin_tenant'] !== undefined) {
-                    form['admin_tenant'] = parameters['admin_tenant'];
-                }
-
-                if (parameters['domain'] !== undefined) {
-                    form['domain'] = parameters['domain'];
-                }
-
-                if (parameters['rabbit_host'] !== undefined) {
-                    form['rabbit_host'] = parameters['rabbit_host'];
-                }
-
-                if (parameters['rabbit_user'] !== undefined) {
-                    form['rabbit_user'] = parameters['rabbit_user'];
-                }
-
-                if (parameters['rabbit_password'] !== undefined) {
-                    form['rabbit_password'] = parameters['rabbit_password'];
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_List_GET
-             * 
-             */
-            xos.prototype.User_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/users/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_List_POST
-             * @param {string} password - 
-             * @param {string} last_login - 
-             * @param {string} email - 
-             * @param {string} username - 
-             * @param {string} firstname - person's given name
-             * @param {string} lastname - person's surname
-             * @param {string} phone - phone number contact
-             * @param {string} user_url - 
-             * @param {string} site - Site this user will be homed too
-             * @param {string} public_key - Public key string
-             * @param {boolean} is_active - 
-             * @param {boolean} is_admin - 
-             * @param {boolean} is_staff - 
-             * @param {boolean} is_readonly - 
-             * @param {boolean} is_registering - 
-             * @param {boolean} is_appuser - 
-             * @param {string} login_page - send this user to a specific page on login
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {choice} timezone - 
-             * 
-             */
-            xos.prototype.User_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/users/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['password'] !== undefined) {
-                    form['password'] = parameters['password'];
-                }
-
-                if (parameters['password'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: password'));
-                    return deferred.promise;
-                }
-
-                if (parameters['last_login'] !== undefined) {
-                    form['last_login'] = parameters['last_login'];
-                }
-
-                if (parameters['last_login'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: last_login'));
-                    return deferred.promise;
-                }
-
-                if (parameters['email'] !== undefined) {
-                    form['email'] = parameters['email'];
-                }
-
-                if (parameters['email'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: email'));
-                    return deferred.promise;
-                }
-
-                if (parameters['username'] !== undefined) {
-                    form['username'] = parameters['username'];
-                }
-
-                if (parameters['username'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: username'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firstname'] !== undefined) {
-                    form['firstname'] = parameters['firstname'];
-                }
-
-                if (parameters['firstname'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firstname'));
-                    return deferred.promise;
-                }
-
-                if (parameters['lastname'] !== undefined) {
-                    form['lastname'] = parameters['lastname'];
-                }
-
-                if (parameters['lastname'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: lastname'));
-                    return deferred.promise;
-                }
-
-                if (parameters['phone'] !== undefined) {
-                    form['phone'] = parameters['phone'];
-                }
-
-                if (parameters['user_url'] !== undefined) {
-                    form['user_url'] = parameters['user_url'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['public_key'] !== undefined) {
-                    form['public_key'] = parameters['public_key'];
-                }
-
-                if (parameters['is_active'] !== undefined) {
-                    form['is_active'] = parameters['is_active'];
-                }
-
-                if (parameters['is_admin'] !== undefined) {
-                    form['is_admin'] = parameters['is_admin'];
-                }
-
-                if (parameters['is_staff'] !== undefined) {
-                    form['is_staff'] = parameters['is_staff'];
-                }
-
-                if (parameters['is_readonly'] !== undefined) {
-                    form['is_readonly'] = parameters['is_readonly'];
-                }
-
-                if (parameters['is_registering'] !== undefined) {
-                    form['is_registering'] = parameters['is_registering'];
-                }
-
-                if (parameters['is_appuser'] !== undefined) {
-                    form['is_appuser'] = parameters['is_appuser'];
-                }
-
-                if (parameters['login_page'] !== undefined) {
-                    form['login_page'] = parameters['login_page'];
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['timezone'] !== undefined) {
-                    form['timezone'] = parameters['timezone'];
-                }
-
-                if (parameters['timezone'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: timezone'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.User_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/users/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} password - 
-             * @param {string} last_login - 
-             * @param {string} email - 
-             * @param {string} username - 
-             * @param {string} firstname - person's given name
-             * @param {string} lastname - person's surname
-             * @param {string} phone - phone number contact
-             * @param {string} user_url - 
-             * @param {string} site - Site this user will be homed too
-             * @param {string} public_key - Public key string
-             * @param {boolean} is_active - 
-             * @param {boolean} is_admin - 
-             * @param {boolean} is_staff - 
-             * @param {boolean} is_readonly - 
-             * @param {boolean} is_registering - 
-             * @param {boolean} is_appuser - 
-             * @param {string} login_page - send this user to a specific page on login
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {choice} timezone - 
-             * 
-             */
-            xos.prototype.User_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/users/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['password'] !== undefined) {
-                    form['password'] = parameters['password'];
-                }
-
-                if (parameters['password'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: password'));
-                    return deferred.promise;
-                }
-
-                if (parameters['last_login'] !== undefined) {
-                    form['last_login'] = parameters['last_login'];
-                }
-
-                if (parameters['last_login'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: last_login'));
-                    return deferred.promise;
-                }
-
-                if (parameters['email'] !== undefined) {
-                    form['email'] = parameters['email'];
-                }
-
-                if (parameters['email'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: email'));
-                    return deferred.promise;
-                }
-
-                if (parameters['username'] !== undefined) {
-                    form['username'] = parameters['username'];
-                }
-
-                if (parameters['username'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: username'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firstname'] !== undefined) {
-                    form['firstname'] = parameters['firstname'];
-                }
-
-                if (parameters['firstname'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firstname'));
-                    return deferred.promise;
-                }
-
-                if (parameters['lastname'] !== undefined) {
-                    form['lastname'] = parameters['lastname'];
-                }
-
-                if (parameters['lastname'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: lastname'));
-                    return deferred.promise;
-                }
-
-                if (parameters['phone'] !== undefined) {
-                    form['phone'] = parameters['phone'];
-                }
-
-                if (parameters['user_url'] !== undefined) {
-                    form['user_url'] = parameters['user_url'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['public_key'] !== undefined) {
-                    form['public_key'] = parameters['public_key'];
-                }
-
-                if (parameters['is_active'] !== undefined) {
-                    form['is_active'] = parameters['is_active'];
-                }
-
-                if (parameters['is_admin'] !== undefined) {
-                    form['is_admin'] = parameters['is_admin'];
-                }
-
-                if (parameters['is_staff'] !== undefined) {
-                    form['is_staff'] = parameters['is_staff'];
-                }
-
-                if (parameters['is_readonly'] !== undefined) {
-                    form['is_readonly'] = parameters['is_readonly'];
-                }
-
-                if (parameters['is_registering'] !== undefined) {
-                    form['is_registering'] = parameters['is_registering'];
-                }
-
-                if (parameters['is_appuser'] !== undefined) {
-                    form['is_appuser'] = parameters['is_appuser'];
-                }
-
-                if (parameters['login_page'] !== undefined) {
-                    form['login_page'] = parameters['login_page'];
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['timezone'] !== undefined) {
-                    form['timezone'] = parameters['timezone'];
-                }
-
-                if (parameters['timezone'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: timezone'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} password - 
-             * @param {string} last_login - 
-             * @param {string} email - 
-             * @param {string} username - 
-             * @param {string} firstname - person's given name
-             * @param {string} lastname - person's surname
-             * @param {string} phone - phone number contact
-             * @param {string} user_url - 
-             * @param {string} site - Site this user will be homed too
-             * @param {string} public_key - Public key string
-             * @param {boolean} is_active - 
-             * @param {boolean} is_admin - 
-             * @param {boolean} is_staff - 
-             * @param {boolean} is_readonly - 
-             * @param {boolean} is_registering - 
-             * @param {boolean} is_appuser - 
-             * @param {string} login_page - send this user to a specific page on login
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {choice} timezone - 
-             * 
-             */
-            xos.prototype.User_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/users/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['password'] !== undefined) {
-                    form['password'] = parameters['password'];
-                }
-
-                if (parameters['last_login'] !== undefined) {
-                    form['last_login'] = parameters['last_login'];
-                }
-
-                if (parameters['email'] !== undefined) {
-                    form['email'] = parameters['email'];
-                }
-
-                if (parameters['username'] !== undefined) {
-                    form['username'] = parameters['username'];
-                }
-
-                if (parameters['firstname'] !== undefined) {
-                    form['firstname'] = parameters['firstname'];
-                }
-
-                if (parameters['lastname'] !== undefined) {
-                    form['lastname'] = parameters['lastname'];
-                }
-
-                if (parameters['phone'] !== undefined) {
-                    form['phone'] = parameters['phone'];
-                }
-
-                if (parameters['user_url'] !== undefined) {
-                    form['user_url'] = parameters['user_url'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['public_key'] !== undefined) {
-                    form['public_key'] = parameters['public_key'];
-                }
-
-                if (parameters['is_active'] !== undefined) {
-                    form['is_active'] = parameters['is_active'];
-                }
-
-                if (parameters['is_admin'] !== undefined) {
-                    form['is_admin'] = parameters['is_admin'];
-                }
-
-                if (parameters['is_staff'] !== undefined) {
-                    form['is_staff'] = parameters['is_staff'];
-                }
-
-                if (parameters['is_readonly'] !== undefined) {
-                    form['is_readonly'] = parameters['is_readonly'];
-                }
-
-                if (parameters['is_registering'] !== undefined) {
-                    form['is_registering'] = parameters['is_registering'];
-                }
-
-                if (parameters['is_appuser'] !== undefined) {
-                    form['is_appuser'] = parameters['is_appuser'];
-                }
-
-                if (parameters['login_page'] !== undefined) {
-                    form['login_page'] = parameters['login_page'];
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['timezone'] !== undefined) {
-                    form['timezone'] = parameters['timezone'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.User_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/users/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_List_GET
-             * 
-             */
-            xos.prototype.Deployment_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deployments/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of the Deployment
-             * @param {string} accessControl - Access control list that specifies which sites/users may use nodes in this deployment
-             * 
-             */
-            xos.prototype.Deployment_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deployments/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['accessControl'] !== undefined) {
-                    form['accessControl'] = parameters['accessControl'];
-                }
-
-                if (parameters['accessControl'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: accessControl'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Deployment_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deployments/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of the Deployment
-             * @param {string} accessControl - Access control list that specifies which sites/users may use nodes in this deployment
-             * 
-             */
-            xos.prototype.Deployment_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deployments/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['accessControl'] !== undefined) {
-                    form['accessControl'] = parameters['accessControl'];
-                }
-
-                if (parameters['accessControl'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: accessControl'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of the Deployment
-             * @param {string} accessControl - Access control list that specifies which sites/users may use nodes in this deployment
-             * 
-             */
-            xos.prototype.Deployment_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deployments/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['accessControl'] !== undefined) {
-                    form['accessControl'] = parameters['accessControl'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Deployment_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deployments/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Reservation_List_GET
-             * 
-             */
-            xos.prototype.Reservation_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/reservations/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Reservation_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} startTime - 
-             * @param {string} slice - 
-             * @param {integer} duration - 
-             * 
-             */
-            xos.prototype.Reservation_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/reservations/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['startTime'] !== undefined) {
-                    form['startTime'] = parameters['startTime'];
-                }
-
-                if (parameters['startTime'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: startTime'));
-                    return deferred.promise;
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters['duration'] !== undefined) {
-                    form['duration'] = parameters['duration'];
-                }
-
-                if (parameters['duration'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: duration'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Reservation_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Reservation_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/reservations/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Reservation_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} startTime - 
-             * @param {string} slice - 
-             * @param {integer} duration - 
-             * 
-             */
-            xos.prototype.Reservation_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/reservations/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['startTime'] !== undefined) {
-                    form['startTime'] = parameters['startTime'];
-                }
-
-                if (parameters['startTime'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: startTime'));
-                    return deferred.promise;
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters['duration'] !== undefined) {
-                    form['duration'] = parameters['duration'];
-                }
-
-                if (parameters['duration'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: duration'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Reservation_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} startTime - 
-             * @param {string} slice - 
-             * @param {integer} duration - 
-             * 
-             */
-            xos.prototype.Reservation_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/reservations/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['startTime'] !== undefined) {
-                    form['startTime'] = parameters['startTime'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['duration'] !== undefined) {
-                    form['duration'] = parameters['duration'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Reservation_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Reservation_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/reservations/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Privilege_List_GET
-             * 
-             */
-            xos.prototype.Site_Privilege_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/siteprivileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Privilege_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} site - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Site_Privilege_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/siteprivileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Privilege_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Site_Privilege_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/siteprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Privilege_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} site - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Site_Privilege_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/siteprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Privilege_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} site - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Site_Privilege_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/siteprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Privilege_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Site_Privilege_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/siteprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Slice_List_GET
-             * 
-             */
-            xos.prototype.Controller_Slice_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerslices/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Slice_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} controller - 
-             * @param {string} slice - 
-             * @param {string} tenant_id - Keystone tenant id
-             * 
-             */
-            xos.prototype.Controller_Slice_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerslices/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters['tenant_id'] !== undefined) {
-                    form['tenant_id'] = parameters['tenant_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Slice_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Slice_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerslices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Slice_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} controller - 
-             * @param {string} slice - 
-             * @param {string} tenant_id - Keystone tenant id
-             * 
-             */
-            xos.prototype.Controller_Slice_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerslices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters['tenant_id'] !== undefined) {
-                    form['tenant_id'] = parameters['tenant_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Slice_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} controller - 
-             * @param {string} slice - 
-             * @param {string} tenant_id - Keystone tenant id
-             * 
-             */
-            xos.prototype.Controller_Slice_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerslices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['tenant_id'] !== undefined) {
-                    form['tenant_id'] = parameters['tenant_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Slice_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Slice_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerslices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_List_GET
-             * 
-             */
-            xos.prototype.Tenant_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenants/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} provider_service - 
-             * @param {string} subscriber_service - 
-             * @param {string} subscriber_tenant - 
-             * @param {string} subscriber_user - 
-             * @param {string} subscriber_root - 
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * @param {choice} connect_method - 
-             * 
-             */
-            xos.prototype.Tenant_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenants/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['provider_service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: provider_service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['subscriber_service'] !== undefined) {
-                    form['subscriber_service'] = parameters['subscriber_service'];
-                }
-
-                if (parameters['subscriber_tenant'] !== undefined) {
-                    form['subscriber_tenant'] = parameters['subscriber_tenant'];
-                }
-
-                if (parameters['subscriber_user'] !== undefined) {
-                    form['subscriber_user'] = parameters['subscriber_user'];
-                }
-
-                if (parameters['subscriber_root'] !== undefined) {
-                    form['subscriber_root'] = parameters['subscriber_root'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['connect_method'] !== undefined) {
-                    form['connect_method'] = parameters['connect_method'];
-                }
-
-                if (parameters['connect_method'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: connect_method'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Tenant_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenants/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} provider_service - 
-             * @param {string} subscriber_service - 
-             * @param {string} subscriber_tenant - 
-             * @param {string} subscriber_user - 
-             * @param {string} subscriber_root - 
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * @param {choice} connect_method - 
-             * 
-             */
-            xos.prototype.Tenant_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenants/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['provider_service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: provider_service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['subscriber_service'] !== undefined) {
-                    form['subscriber_service'] = parameters['subscriber_service'];
-                }
-
-                if (parameters['subscriber_tenant'] !== undefined) {
-                    form['subscriber_tenant'] = parameters['subscriber_tenant'];
-                }
-
-                if (parameters['subscriber_user'] !== undefined) {
-                    form['subscriber_user'] = parameters['subscriber_user'];
-                }
-
-                if (parameters['subscriber_root'] !== undefined) {
-                    form['subscriber_root'] = parameters['subscriber_root'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['connect_method'] !== undefined) {
-                    form['connect_method'] = parameters['connect_method'];
-                }
-
-                if (parameters['connect_method'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: connect_method'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} provider_service - 
-             * @param {string} subscriber_service - 
-             * @param {string} subscriber_tenant - 
-             * @param {string} subscriber_user - 
-             * @param {string} subscriber_root - 
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * @param {choice} connect_method - 
-             * 
-             */
-            xos.prototype.Tenant_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenants/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['subscriber_service'] !== undefined) {
-                    form['subscriber_service'] = parameters['subscriber_service'];
-                }
-
-                if (parameters['subscriber_tenant'] !== undefined) {
-                    form['subscriber_tenant'] = parameters['subscriber_tenant'];
-                }
-
-                if (parameters['subscriber_user'] !== undefined) {
-                    form['subscriber_user'] = parameters['subscriber_user'];
-                }
-
-                if (parameters['subscriber_root'] !== undefined) {
-                    form['subscriber_root'] = parameters['subscriber_root'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['connect_method'] !== undefined) {
-                    form['connect_method'] = parameters['connect_method'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Tenant_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenants/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Dashboard_View_List_GET
-             * 
-             */
-            xos.prototype.Controller_Dashboard_View_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerdashboardviews/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Dashboard_View_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} controller - 
-             * @param {string} dashboardView - 
-             * @param {boolean} enabled - 
-             * @param {string} url - URL of Dashboard
-             * 
-             */
-            xos.prototype.Controller_Dashboard_View_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerdashboardviews/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['dashboardView'] !== undefined) {
-                    form['dashboardView'] = parameters['dashboardView'];
-                }
-
-                if (parameters['dashboardView'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: dashboardView'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['url'] !== undefined) {
-                    form['url'] = parameters['url'];
-                }
-
-                if (parameters['url'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Dashboard_View_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Dashboard_View_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerdashboardviews/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Dashboard_View_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} controller - 
-             * @param {string} dashboardView - 
-             * @param {boolean} enabled - 
-             * @param {string} url - URL of Dashboard
-             * 
-             */
-            xos.prototype.Controller_Dashboard_View_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerdashboardviews/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['dashboardView'] !== undefined) {
-                    form['dashboardView'] = parameters['dashboardView'];
-                }
-
-                if (parameters['dashboardView'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: dashboardView'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['url'] !== undefined) {
-                    form['url'] = parameters['url'];
-                }
-
-                if (parameters['url'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Dashboard_View_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} controller - 
-             * @param {string} dashboardView - 
-             * @param {boolean} enabled - 
-             * @param {string} url - URL of Dashboard
-             * 
-             */
-            xos.prototype.Controller_Dashboard_View_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerdashboardviews/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['dashboardView'] !== undefined) {
-                    form['dashboardView'] = parameters['dashboardView'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['url'] !== undefined) {
-                    form['url'] = parameters['url'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Dashboard_View_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Dashboard_View_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerdashboardviews/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Account_List_GET
-             * 
-             */
-            xos.prototype.Account_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/accounts/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Account_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} site - Site for this account
-             * 
-             */
-            xos.prototype.Account_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/accounts/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Account_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Account_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/accounts/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Account_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} site - Site for this account
-             * 
-             */
-            xos.prototype.Account_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/accounts/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Account_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} site - Site for this account
-             * 
-             */
-            xos.prototype.Account_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/accounts/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Account_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Account_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/accounts/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_List_GET
-             * 
-             */
-            xos.prototype.Tenant_Root_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantroots/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} name - name
-             * @param {string} service_specific_attribute - 
-             * @param {string} service_specific_id - 
-             * 
-             */
-            xos.prototype.Tenant_Root_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantroots/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantroots/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} name - name
-             * @param {string} service_specific_attribute - 
-             * @param {string} service_specific_id - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantroots/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} name - name
-             * @param {string} service_specific_attribute - 
-             * @param {string} service_specific_id - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantroots/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantroots/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Role_List_GET
-             * 
-             */
-            xos.prototype.Controller_Role_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerroles/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Role_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-
-             * 
-             */
-            xos.prototype.Controller_Role_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerroles/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                form['role'] = 'admin';
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Role_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Role_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerroles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Role_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-
-             * 
-             */
-            xos.prototype.Controller_Role_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerroles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                form['role'] = 'admin';
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Role_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-
-             * 
-             */
-            xos.prototype.Controller_Role_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerroles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                form['role'] = 'admin';
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Role_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Role_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerroles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Parameter_Type_List_GET
-             * 
-             */
-            xos.prototype.Network_Parameter_Type_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkparametertypes/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Parameter_Type_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - The name of this parameter
-             * @param {string} description - 
-             * 
-             */
-            xos.prototype.Network_Parameter_Type_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkparametertypes/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['description'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: description'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Parameter_Type_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Network_Parameter_Type_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkparametertypes/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Parameter_Type_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - The name of this parameter
-             * @param {string} description - 
-             * 
-             */
-            xos.prototype.Network_Parameter_Type_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkparametertypes/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['description'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: description'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Parameter_Type_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - The name of this parameter
-             * @param {string} description - 
-             * 
-             */
-            xos.prototype.Network_Parameter_Type_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkparametertypes/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Parameter_Type_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Network_Parameter_Type_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkparametertypes/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Credential_List_GET
-             * 
-             */
-            xos.prototype.Site_Credential_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sitecredentials/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Credential_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} site - The User this credential is associated with
-             * @param {string} name - The credential type, e.g. ec2
-             * @param {string} key_id - The backend id of this credential
-             * @param {string} enc_value - The key value of this credential
-             * 
-             */
-            xos.prototype.Site_Credential_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sitecredentials/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['key_id'] !== undefined) {
-                    form['key_id'] = parameters['key_id'];
-                }
-
-                if (parameters['key_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: key_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enc_value'] !== undefined) {
-                    form['enc_value'] = parameters['enc_value'];
-                }
-
-                if (parameters['enc_value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: enc_value'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Credential_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Site_Credential_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sitecredentials/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Credential_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} site - The User this credential is associated with
-             * @param {string} name - The credential type, e.g. ec2
-             * @param {string} key_id - The backend id of this credential
-             * @param {string} enc_value - The key value of this credential
-             * 
-             */
-            xos.prototype.Site_Credential_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sitecredentials/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['key_id'] !== undefined) {
-                    form['key_id'] = parameters['key_id'];
-                }
-
-                if (parameters['key_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: key_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enc_value'] !== undefined) {
-                    form['enc_value'] = parameters['enc_value'];
-                }
-
-                if (parameters['enc_value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: enc_value'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Credential_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} site - The User this credential is associated with
-             * @param {string} name - The credential type, e.g. ec2
-             * @param {string} key_id - The backend id of this credential
-             * @param {string} enc_value - The key value of this credential
-             * 
-             */
-            xos.prototype.Site_Credential_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sitecredentials/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['key_id'] !== undefined) {
-                    form['key_id'] = parameters['key_id'];
-                }
-
-                if (parameters['enc_value'] !== undefined) {
-                    form['enc_value'] = parameters['enc_value'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Credential_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Site_Credential_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sitecredentials/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Privilege_List_GET
-             * 
-             */
-            xos.prototype.Deployment_Privilege_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deploymentprivileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Privilege_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} deployment - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Deployment_Privilege_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deploymentprivileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['deployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: deployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Privilege_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Deployment_Privilege_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deploymentprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Privilege_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} deployment - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Deployment_Privilege_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deploymentprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['deployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: deployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Privilege_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} deployment - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Deployment_Privilege_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deploymentprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Privilege_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Deployment_Privilege_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deploymentprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Slice_Privilege_List_GET
-             * 
-             */
-            xos.prototype.Controller_Slice_Privilege_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersliceprivileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Slice_Privilege_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} controller - 
-             * @param {string} slice_privilege - 
-             * @param {string} role_id - Keystone id
-             * 
-             */
-            xos.prototype.Controller_Slice_Privilege_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersliceprivileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['slice_privilege'] !== undefined) {
-                    form['slice_privilege'] = parameters['slice_privilege'];
-                }
-
-                if (parameters['slice_privilege'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice_privilege'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role_id'] !== undefined) {
-                    form['role_id'] = parameters['role_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Slice_Privilege_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Slice_Privilege_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersliceprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Slice_Privilege_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} controller - 
-             * @param {string} slice_privilege - 
-             * @param {string} role_id - Keystone id
-             * 
-             */
-            xos.prototype.Controller_Slice_Privilege_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersliceprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['slice_privilege'] !== undefined) {
-                    form['slice_privilege'] = parameters['slice_privilege'];
-                }
-
-                if (parameters['slice_privilege'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice_privilege'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role_id'] !== undefined) {
-                    form['role_id'] = parameters['role_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Slice_Privilege_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} controller - 
-             * @param {string} slice_privilege - 
-             * @param {string} role_id - Keystone id
-             * 
-             */
-            xos.prototype.Controller_Slice_Privilege_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersliceprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['slice_privilege'] !== undefined) {
-                    form['slice_privilege'] = parameters['slice_privilege'];
-                }
-
-                if (parameters['role_id'] !== undefined) {
-                    form['role_id'] = parameters['role_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Slice_Privilege_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Slice_Privilege_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersliceprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Deployment_List_GET
-             * 
-             */
-            xos.prototype.Site_Deployment_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sitedeployments/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Deployment_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} site - 
-             * @param {string} deployment - 
-             * @param {string} controller - 
-             * @param {string} availability_zone - OpenStack availability zone
-             * 
-             */
-            xos.prototype.Site_Deployment_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sitedeployments/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['deployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: deployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['availability_zone'] !== undefined) {
-                    form['availability_zone'] = parameters['availability_zone'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Deployment_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Site_Deployment_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sitedeployments/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Deployment_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} site - 
-             * @param {string} deployment - 
-             * @param {string} controller - 
-             * @param {string} availability_zone - OpenStack availability zone
-             * 
-             */
-            xos.prototype.Site_Deployment_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sitedeployments/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['deployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: deployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['availability_zone'] !== undefined) {
-                    form['availability_zone'] = parameters['availability_zone'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Deployment_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} site - 
-             * @param {string} deployment - 
-             * @param {string} controller - 
-             * @param {string} availability_zone - OpenStack availability zone
-             * 
-             */
-            xos.prototype.Site_Deployment_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sitedeployments/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['availability_zone'] !== undefined) {
-                    form['availability_zone'] = parameters['availability_zone'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Deployment_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Site_Deployment_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sitedeployments/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_With_Container_List_GET
-             * 
-             */
-            xos.prototype.Tenant_With_Container_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantwithcontainers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_With_Container_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} provider_service - 
-             * @param {string} subscriber_service - 
-             * @param {string} subscriber_tenant - 
-             * @param {string} subscriber_user - 
-             * @param {string} subscriber_root - 
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * @param {choice} connect_method - 
-             * 
-             */
-            xos.prototype.Tenant_With_Container_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantwithcontainers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['provider_service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: provider_service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['subscriber_service'] !== undefined) {
-                    form['subscriber_service'] = parameters['subscriber_service'];
-                }
-
-                if (parameters['subscriber_tenant'] !== undefined) {
-                    form['subscriber_tenant'] = parameters['subscriber_tenant'];
-                }
-
-                if (parameters['subscriber_user'] !== undefined) {
-                    form['subscriber_user'] = parameters['subscriber_user'];
-                }
-
-                if (parameters['subscriber_root'] !== undefined) {
-                    form['subscriber_root'] = parameters['subscriber_root'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['connect_method'] !== undefined) {
-                    form['connect_method'] = parameters['connect_method'];
-                }
-
-                if (parameters['connect_method'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: connect_method'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_With_Container_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Tenant_With_Container_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantwithcontainers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_With_Container_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} provider_service - 
-             * @param {string} subscriber_service - 
-             * @param {string} subscriber_tenant - 
-             * @param {string} subscriber_user - 
-             * @param {string} subscriber_root - 
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * @param {choice} connect_method - 
-             * 
-             */
-            xos.prototype.Tenant_With_Container_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantwithcontainers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['provider_service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: provider_service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['subscriber_service'] !== undefined) {
-                    form['subscriber_service'] = parameters['subscriber_service'];
-                }
-
-                if (parameters['subscriber_tenant'] !== undefined) {
-                    form['subscriber_tenant'] = parameters['subscriber_tenant'];
-                }
-
-                if (parameters['subscriber_user'] !== undefined) {
-                    form['subscriber_user'] = parameters['subscriber_user'];
-                }
-
-                if (parameters['subscriber_root'] !== undefined) {
-                    form['subscriber_root'] = parameters['subscriber_root'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['connect_method'] !== undefined) {
-                    form['connect_method'] = parameters['connect_method'];
-                }
-
-                if (parameters['connect_method'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: connect_method'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_With_Container_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} provider_service - 
-             * @param {string} subscriber_service - 
-             * @param {string} subscriber_tenant - 
-             * @param {string} subscriber_user - 
-             * @param {string} subscriber_root - 
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * @param {choice} connect_method - 
-             * 
-             */
-            xos.prototype.Tenant_With_Container_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantwithcontainers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['subscriber_service'] !== undefined) {
-                    form['subscriber_service'] = parameters['subscriber_service'];
-                }
-
-                if (parameters['subscriber_tenant'] !== undefined) {
-                    form['subscriber_tenant'] = parameters['subscriber_tenant'];
-                }
-
-                if (parameters['subscriber_user'] !== undefined) {
-                    form['subscriber_user'] = parameters['subscriber_user'];
-                }
-
-                if (parameters['subscriber_root'] !== undefined) {
-                    form['subscriber_root'] = parameters['subscriber_root'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['connect_method'] !== undefined) {
-                    form['connect_method'] = parameters['connect_method'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_With_Container_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Tenant_With_Container_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantwithcontainers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Role_List_GET
-             * 
-             */
-            xos.prototype.Deployment_Role_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deploymentroles/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Role_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-
-             * 
-             */
-            xos.prototype.Deployment_Role_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deploymentroles/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                form['role'] = 'admin';
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Role_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Deployment_Role_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deploymentroles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Role_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-
-             * 
-             */
-            xos.prototype.Deployment_Role_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deploymentroles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                form['role'] = 'admin';
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Role_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-
-             * 
-             */
-            xos.prototype.Deployment_Role_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deploymentroles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                form['role'] = 'admin';
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Role_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Deployment_Role_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deploymentroles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Credential_List_GET
-             * 
-             */
-            xos.prototype.User_Credential_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/usercredentials/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Credential_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - The User this credential is associated with
-             * @param {string} name - The credential type, e.g. ec2
-             * @param {string} key_id - The backend id of this credential
-             * @param {string} enc_value - The key value of this credential
-             * 
-             */
-            xos.prototype.User_Credential_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/usercredentials/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['key_id'] !== undefined) {
-                    form['key_id'] = parameters['key_id'];
-                }
-
-                if (parameters['key_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: key_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enc_value'] !== undefined) {
-                    form['enc_value'] = parameters['enc_value'];
-                }
-
-                if (parameters['enc_value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: enc_value'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Credential_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.User_Credential_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/usercredentials/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Credential_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - The User this credential is associated with
-             * @param {string} name - The credential type, e.g. ec2
-             * @param {string} key_id - The backend id of this credential
-             * @param {string} enc_value - The key value of this credential
-             * 
-             */
-            xos.prototype.User_Credential_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/usercredentials/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['key_id'] !== undefined) {
-                    form['key_id'] = parameters['key_id'];
-                }
-
-                if (parameters['key_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: key_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enc_value'] !== undefined) {
-                    form['enc_value'] = parameters['enc_value'];
-                }
-
-                if (parameters['enc_value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: enc_value'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Credential_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - The User this credential is associated with
-             * @param {string} name - The credential type, e.g. ec2
-             * @param {string} key_id - The backend id of this credential
-             * @param {string} enc_value - The key value of this credential
-             * 
-             */
-            xos.prototype.User_Credential_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/usercredentials/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['key_id'] !== undefined) {
-                    form['key_id'] = parameters['key_id'];
-                }
-
-                if (parameters['enc_value'] !== undefined) {
-                    form['enc_value'] = parameters['enc_value'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Credential_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.User_Credential_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/usercredentials/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Privilege_List_GET
-             * 
-             */
-            xos.prototype.Tenant_Root_Privilege_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantrootprivileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Privilege_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} tenant_root - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Privilege_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantrootprivileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['tenant_root'] !== undefined) {
-                    form['tenant_root'] = parameters['tenant_root'];
-                }
-
-                if (parameters['tenant_root'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: tenant_root'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Privilege_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Privilege_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantrootprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Privilege_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} tenant_root - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Privilege_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantrootprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['tenant_root'] !== undefined) {
-                    form['tenant_root'] = parameters['tenant_root'];
-                }
-
-                if (parameters['tenant_root'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: tenant_root'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Privilege_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} tenant_root - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Privilege_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantrootprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['tenant_root'] !== undefined) {
-                    form['tenant_root'] = parameters['tenant_root'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Privilege_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Privilege_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantrootprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Tag_List_GET
-             * 
-             */
-            xos.prototype.Slice_Tag_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slicetags/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Tag_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} slice - 
-             * @param {choice} name - The name of this tag
-             * @param {string} value - The value of this tag
-             * 
-             */
-            xos.prototype.Slice_Tag_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slicetags/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['value'] !== undefined) {
-                    form['value'] = parameters['value'];
-                }
-
-                if (parameters['value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: value'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Tag_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Slice_Tag_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slicetags/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Tag_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} slice - 
-             * @param {choice} name - The name of this tag
-             * @param {string} value - The value of this tag
-             * 
-             */
-            xos.prototype.Slice_Tag_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slicetags/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['value'] !== undefined) {
-                    form['value'] = parameters['value'];
-                }
-
-                if (parameters['value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: value'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Tag_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} slice - 
-             * @param {choice} name - The name of this tag
-             * @param {string} value - The value of this tag
-             * 
-             */
-            xos.prototype.Slice_Tag_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slicetags/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['value'] !== undefined) {
-                    form['value'] = parameters['value'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Tag_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Slice_Tag_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slicetags/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Coarse_Tenant_List_GET
-             * 
-             */
-            xos.prototype.Coarse_Tenant_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/coarsetenants/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Coarse_Tenant_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} provider_service - 
-             * @param {string} subscriber_service - 
-             * @param {string} subscriber_tenant - 
-             * @param {string} subscriber_user - 
-             * @param {string} subscriber_root - 
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * @param {choice} connect_method - 
-             * 
-             */
-            xos.prototype.Coarse_Tenant_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/coarsetenants/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['provider_service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: provider_service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['subscriber_service'] !== undefined) {
-                    form['subscriber_service'] = parameters['subscriber_service'];
-                }
-
-                if (parameters['subscriber_tenant'] !== undefined) {
-                    form['subscriber_tenant'] = parameters['subscriber_tenant'];
-                }
-
-                if (parameters['subscriber_user'] !== undefined) {
-                    form['subscriber_user'] = parameters['subscriber_user'];
-                }
-
-                if (parameters['subscriber_root'] !== undefined) {
-                    form['subscriber_root'] = parameters['subscriber_root'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['connect_method'] !== undefined) {
-                    form['connect_method'] = parameters['connect_method'];
-                }
-
-                if (parameters['connect_method'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: connect_method'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Coarse_Tenant_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Coarse_Tenant_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/coarsetenants/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Coarse_Tenant_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} provider_service - 
-             * @param {string} subscriber_service - 
-             * @param {string} subscriber_tenant - 
-             * @param {string} subscriber_user - 
-             * @param {string} subscriber_root - 
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * @param {choice} connect_method - 
-             * 
-             */
-            xos.prototype.Coarse_Tenant_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/coarsetenants/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['provider_service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: provider_service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['subscriber_service'] !== undefined) {
-                    form['subscriber_service'] = parameters['subscriber_service'];
-                }
-
-                if (parameters['subscriber_tenant'] !== undefined) {
-                    form['subscriber_tenant'] = parameters['subscriber_tenant'];
-                }
-
-                if (parameters['subscriber_user'] !== undefined) {
-                    form['subscriber_user'] = parameters['subscriber_user'];
-                }
-
-                if (parameters['subscriber_root'] !== undefined) {
-                    form['subscriber_root'] = parameters['subscriber_root'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['connect_method'] !== undefined) {
-                    form['connect_method'] = parameters['connect_method'];
-                }
-
-                if (parameters['connect_method'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: connect_method'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Coarse_Tenant_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} provider_service - 
-             * @param {string} subscriber_service - 
-             * @param {string} subscriber_tenant - 
-             * @param {string} subscriber_user - 
-             * @param {string} subscriber_root - 
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * @param {choice} connect_method - 
-             * 
-             */
-            xos.prototype.Coarse_Tenant_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/coarsetenants/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['subscriber_service'] !== undefined) {
-                    form['subscriber_service'] = parameters['subscriber_service'];
-                }
-
-                if (parameters['subscriber_tenant'] !== undefined) {
-                    form['subscriber_tenant'] = parameters['subscriber_tenant'];
-                }
-
-                if (parameters['subscriber_user'] !== undefined) {
-                    form['subscriber_user'] = parameters['subscriber_user'];
-                }
-
-                if (parameters['subscriber_root'] !== undefined) {
-                    form['subscriber_root'] = parameters['subscriber_root'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['connect_method'] !== undefined) {
-                    form['connect_method'] = parameters['connect_method'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Coarse_Tenant_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Coarse_Tenant_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/coarsetenants/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Router_List_GET
-             * 
-             */
-            xos.prototype.Router_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/routers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Router_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} owner - 
-             * 
-             */
-            xos.prototype.Router_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/routers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['owner'] !== undefined) {
-                    form['owner'] = parameters['owner'];
-                }
-
-                if (parameters['owner'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: owner'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Router_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Router_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/routers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Router_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} owner - 
-             * 
-             */
-            xos.prototype.Router_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/routers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['owner'] !== undefined) {
-                    form['owner'] = parameters['owner'];
-                }
-
-                if (parameters['owner'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: owner'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Router_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} owner - 
-             * 
-             */
-            xos.prototype.Router_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/routers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['owner'] !== undefined) {
-                    form['owner'] = parameters['owner'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Router_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Router_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/routers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Resource_List_GET
-             * 
-             */
-            xos.prototype.Service_Resource_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceresources/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Resource_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} serviceClass - 
-             * @param {string} name - 
-             * @param {integer} maxUnitsDeployment - 
-             * @param {integer} maxUnitsNode - 
-             * @param {integer} maxDuration - 
-             * @param {integer} bucketInRate - 
-             * @param {integer} bucketMaxSize - 
-             * @param {integer} cost - 
-             * @param {boolean} calendarReservable - 
-             * 
-             */
-            xos.prototype.Service_Resource_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceresources/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['serviceClass'] !== undefined) {
-                    form['serviceClass'] = parameters['serviceClass'];
-                }
-
-                if (parameters['serviceClass'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: serviceClass'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['maxUnitsDeployment'] !== undefined) {
-                    form['maxUnitsDeployment'] = parameters['maxUnitsDeployment'];
-                }
-
-                if (parameters['maxUnitsDeployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: maxUnitsDeployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters['maxUnitsNode'] !== undefined) {
-                    form['maxUnitsNode'] = parameters['maxUnitsNode'];
-                }
-
-                if (parameters['maxUnitsNode'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: maxUnitsNode'));
-                    return deferred.promise;
-                }
-
-                if (parameters['maxDuration'] !== undefined) {
-                    form['maxDuration'] = parameters['maxDuration'];
-                }
-
-                if (parameters['maxDuration'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: maxDuration'));
-                    return deferred.promise;
-                }
-
-                if (parameters['bucketInRate'] !== undefined) {
-                    form['bucketInRate'] = parameters['bucketInRate'];
-                }
-
-                if (parameters['bucketInRate'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: bucketInRate'));
-                    return deferred.promise;
-                }
-
-                if (parameters['bucketMaxSize'] !== undefined) {
-                    form['bucketMaxSize'] = parameters['bucketMaxSize'];
-                }
-
-                if (parameters['bucketMaxSize'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: bucketMaxSize'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cost'] !== undefined) {
-                    form['cost'] = parameters['cost'];
-                }
-
-                if (parameters['cost'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cost'));
-                    return deferred.promise;
-                }
-
-                if (parameters['calendarReservable'] !== undefined) {
-                    form['calendarReservable'] = parameters['calendarReservable'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Resource_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Service_Resource_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceresources/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Resource_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} serviceClass - 
-             * @param {string} name - 
-             * @param {integer} maxUnitsDeployment - 
-             * @param {integer} maxUnitsNode - 
-             * @param {integer} maxDuration - 
-             * @param {integer} bucketInRate - 
-             * @param {integer} bucketMaxSize - 
-             * @param {integer} cost - 
-             * @param {boolean} calendarReservable - 
-             * 
-             */
-            xos.prototype.Service_Resource_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceresources/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['serviceClass'] !== undefined) {
-                    form['serviceClass'] = parameters['serviceClass'];
-                }
-
-                if (parameters['serviceClass'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: serviceClass'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['maxUnitsDeployment'] !== undefined) {
-                    form['maxUnitsDeployment'] = parameters['maxUnitsDeployment'];
-                }
-
-                if (parameters['maxUnitsDeployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: maxUnitsDeployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters['maxUnitsNode'] !== undefined) {
-                    form['maxUnitsNode'] = parameters['maxUnitsNode'];
-                }
-
-                if (parameters['maxUnitsNode'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: maxUnitsNode'));
-                    return deferred.promise;
-                }
-
-                if (parameters['maxDuration'] !== undefined) {
-                    form['maxDuration'] = parameters['maxDuration'];
-                }
-
-                if (parameters['maxDuration'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: maxDuration'));
-                    return deferred.promise;
-                }
-
-                if (parameters['bucketInRate'] !== undefined) {
-                    form['bucketInRate'] = parameters['bucketInRate'];
-                }
-
-                if (parameters['bucketInRate'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: bucketInRate'));
-                    return deferred.promise;
-                }
-
-                if (parameters['bucketMaxSize'] !== undefined) {
-                    form['bucketMaxSize'] = parameters['bucketMaxSize'];
-                }
-
-                if (parameters['bucketMaxSize'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: bucketMaxSize'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cost'] !== undefined) {
-                    form['cost'] = parameters['cost'];
-                }
-
-                if (parameters['cost'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cost'));
-                    return deferred.promise;
-                }
-
-                if (parameters['calendarReservable'] !== undefined) {
-                    form['calendarReservable'] = parameters['calendarReservable'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Resource_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} serviceClass - 
-             * @param {string} name - 
-             * @param {integer} maxUnitsDeployment - 
-             * @param {integer} maxUnitsNode - 
-             * @param {integer} maxDuration - 
-             * @param {integer} bucketInRate - 
-             * @param {integer} bucketMaxSize - 
-             * @param {integer} cost - 
-             * @param {boolean} calendarReservable - 
-             * 
-             */
-            xos.prototype.Service_Resource_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceresources/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['serviceClass'] !== undefined) {
-                    form['serviceClass'] = parameters['serviceClass'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['maxUnitsDeployment'] !== undefined) {
-                    form['maxUnitsDeployment'] = parameters['maxUnitsDeployment'];
-                }
-
-                if (parameters['maxUnitsNode'] !== undefined) {
-                    form['maxUnitsNode'] = parameters['maxUnitsNode'];
-                }
-
-                if (parameters['maxDuration'] !== undefined) {
-                    form['maxDuration'] = parameters['maxDuration'];
-                }
-
-                if (parameters['bucketInRate'] !== undefined) {
-                    form['bucketInRate'] = parameters['bucketInRate'];
-                }
-
-                if (parameters['bucketMaxSize'] !== undefined) {
-                    form['bucketMaxSize'] = parameters['bucketMaxSize'];
-                }
-
-                if (parameters['cost'] !== undefined) {
-                    form['cost'] = parameters['cost'];
-                }
-
-                if (parameters['calendarReservable'] !== undefined) {
-                    form['calendarReservable'] = parameters['calendarReservable'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Resource_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Service_Resource_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceresources/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Privilege_List_GET
-             * 
-             */
-            xos.prototype.Service_Privilege_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceprivileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Privilege_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} service - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Service_Privilege_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceprivileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Privilege_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Service_Privilege_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Privilege_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} service - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Service_Privilege_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Privilege_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} service - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Service_Privilege_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Privilege_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Service_Privilege_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-
-            return xos;
-        })();
-
-        return xos;
-    }]);
\ No newline at end of file
diff --git a/views/ngXosLib/api/ng-xoslib.js b/views/ngXosLib/api/ng-xoslib.js
deleted file mode 100755
index 16b5d9b..0000000
--- a/views/ngXosLib/api/ng-xoslib.js
+++ /dev/null
@@ -1,6174 +0,0 @@
-/*jshint -W069 */
-/*global angular:false */
-angular.module('xos.xoslib', [])
-    .factory('xoslib', ['$q', '$http', '$rootScope', function($q, $http, $rootScope) {
-        'use strict';
-
-        /**
-         * 
-         * @class xoslib
-         * @param {(string|object)} [domainOrOptions] - The project domain or options object. If object, see the object's optional properties.
-         * @param {string} [domainOrOptions.domain] - The project domain
-         * @param {string} [domainOrOptions.cache] - An angularjs cache implementation
-         * @param {object} [domainOrOptions.token] - auth token - object with value property and optional headerOrQueryName and isQuery properties
-         * @param {string} [cache] - An angularjs cache implementation
-         */
-        var xoslib = (function() {
-            function xoslib(options, cache) {
-                var domain = (typeof options === 'object') ? options.domain : options;
-                this.domain = typeof(domain) === 'string' ? domain : 'http://localhost:9999';
-                cache = cache || ((typeof options === 'object') ? options.cache : cache);
-                this.cache = cache;
-            }
-
-            xoslib.prototype.$on = function($scope, path, handler) {
-                var url = domain + path;
-                $scope.$on(url, function() {
-                    handler();
-                });
-                return this;
-            };
-
-            xoslib.prototype.$broadcast = function(path) {
-                var url = domain + path;
-                //cache.remove(url);
-                $rootScope.$broadcast(url);
-                return this;
-            };
-
-            xoslib.transformRequest = function(obj) {
-                var str = [];
-                for (var p in obj) {
-                    var val = obj[p];
-                    if (angular.isArray(val)) {
-                        val.forEach(function(val) {
-                            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(val));
-                        });
-                    } else {
-                        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(val));
-                    }
-                }
-                return str.join("&");
-            };
-
-            /**
-             * 
-             * @method
-             * @name xoslib#Monitoring_Channel_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Monitoring_Channel_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/monitoringchannel/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Monitoring_Channel_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} provider_service - 
-             * 
-             */
-            xoslib.prototype.Monitoring_Channel_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/monitoringchannel/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['provider_service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: provider_service'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Monitoring_Channel_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} provider_service - 
-             * 
-             */
-            xoslib.prototype.Monitoring_Channel_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/monitoringchannel/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Monitoring_Channel_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Monitoring_Channel_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/monitoringchannel/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Monitoring_Channel_List_GET
-             * 
-             */
-            xoslib.prototype.Monitoring_Channel_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/monitoringchannel/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Monitoring_Channel_List_POST
-             * @param {string} provider_service - 
-             * 
-             */
-            xoslib.prototype.Monitoring_Channel_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/monitoringchannel/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['provider_service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: provider_service'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Hpc_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Hpc_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/hpcview/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Hpc_List_GET
-             * 
-             */
-            xoslib.prototype.Hpc_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/hpcview/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Tenant_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Tenant_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/tenantview/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Tenant_List_GET
-             * 
-             */
-            xoslib.prototype.Tenant_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/tenantview/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Port_Forwarding_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Port_Forwarding_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/portforwarding/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Port_Forwarding_List_GET
-             * 
-             */
-            xoslib.prototype.Port_Forwarding_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/portforwarding/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Ssh_Key_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Ssh_Key_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/sshkeys/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Ssh_Key_List_GET
-             * 
-             */
-            xoslib.prototype.Ssh_Key_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/sshkeys/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Debug_get_vbng_dump
-             * 
-             */
-            xoslib.prototype.Cord_Debug_get_vbng_dump = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/cord_debug/vbng_dump/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/cordsubscriber/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_Detail_PUT
-             * @param {string} pk - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/cordsubscriber/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_Detail_PATCH
-             * @param {string} pk - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/cordsubscriber/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/cordsubscriber/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_List_GET
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/cordsubscriber/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_List_POST
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/cordsubscriber/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_list
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_list = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_update
-             * @param {string} pk - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_update = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_partial_update
-             * @param {string} pk - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_partial_update = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_update
-             * @param {string} pk - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_update = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_destroy
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_destroy = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_retrieve
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_retrieve = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_get_vcpe_synced
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_get_vcpe_synced = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/vcpe_synced/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_get_url_filter
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_get_url_filter = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/url_filter/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_set_url_filter
-             * @param {string} pk - 
-             * @param {string} level - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_set_url_filter = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/url_filter/{level}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                path = path.replace('{level}', parameters['level']);
-
-                if (parameters['level'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: level'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_get_services
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_get_services = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/services/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_get_service
-             * @param {string} pk - 
-             * @param {string} service - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_get_service = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/services/{service}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                path = path.replace('{service}', parameters['service']);
-
-                if (parameters['service'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: service'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_enable_service
-             * @param {string} pk - 
-             * @param {string} service - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_enable_service = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/services/{service}/true/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                path = path.replace('{service}', parameters['service']);
-
-                if (parameters['service'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_disable_service
-             * @param {string} pk - 
-             * @param {string} service - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_disable_service = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/services/{service}/false/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                path = path.replace('{service}', parameters['service']);
-
-                if (parameters['service'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_create_user
-             * @param {string} pk - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_create_user = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/users/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_get_users
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_get_users = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/users/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_clear_users
-             * @param {string} pk - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_clear_users = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/users/clearusers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_clear_users
-             * @param {string} pk - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_clear_users = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/users/clearusers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_clear_users
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_clear_users = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/users/clearusers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_create_user
-             * @param {string} pk - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_create_user = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/users/newuser/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_create_user
-             * @param {string} pk - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_create_user = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/users/newuser/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_delete_user
-             * @param {string} pk - 
-             * @param {string} uid - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_delete_user = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/users/{uid}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                path = path.replace('{uid}', parameters['uid']);
-
-                if (parameters['uid'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: uid'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_get_user_level
-             * @param {string} pk - 
-             * @param {string} uid - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_get_user_level = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/users/{uid}/url_filter/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                path = path.replace('{uid}', parameters['uid']);
-
-                if (parameters['uid'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: uid'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_set_user_level
-             * @param {string} pk - 
-             * @param {string} uid - 
-             * @param {string} level - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_set_user_level = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/users/{uid}/url_filter/{level}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                path = path.replace('{uid}', parameters['uid']);
-
-                if (parameters['uid'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: uid'));
-                    return deferred.promise;
-                }
-
-                path = path.replace('{level}', parameters['level']);
-
-                if (parameters['level'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: level'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_get_bbsdump
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_get_bbsdump = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/bbsdump/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_initdemo
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_initdemo = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/initdemo/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_initdemo
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_initdemo = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/initdemo/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_ssiddetail
-             * @param {string} ssid - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_ssiddetail = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subidlookup/{ssid}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{ssid}', parameters['ssid']);
-
-                if (parameters['ssid'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: ssid'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_ssidlist
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_ssidlist = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subidlookup/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_get_vbng_mapping
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_get_vbng_mapping = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/vbng_mapping/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_User_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_User_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/corduser/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_User_Detail_PUT
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_User_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/corduser/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_User_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_User_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/corduser/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_User_List_GET
-             * 
-             */
-            xoslib.prototype.Cord_User_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/corduser/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_User_List_POST
-             * 
-             */
-            xoslib.prototype.Cord_User_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/corduser/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Volt_Tenant_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Volt_Tenant_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/volttenant/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Volt_Tenant_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} provider_service - 
-             * @param {string} service_specific_id - 
-             * @param {string} vlan_id - 
-             * 
-             */
-            xoslib.prototype.Volt_Tenant_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/volttenant/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['provider_service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: provider_service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: service_specific_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters['vlan_id'] !== undefined) {
-                    form['vlan_id'] = parameters['vlan_id'];
-                }
-
-                if (parameters['vlan_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vlan_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Volt_Tenant_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} provider_service - 
-             * @param {string} service_specific_id - 
-             * @param {string} vlan_id - 
-             * 
-             */
-            xoslib.prototype.Volt_Tenant_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/volttenant/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['vlan_id'] !== undefined) {
-                    form['vlan_id'] = parameters['vlan_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Volt_Tenant_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Volt_Tenant_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/volttenant/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Volt_Tenant_List_GET
-             * 
-             */
-            xoslib.prototype.Volt_Tenant_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/volttenant/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Volt_Tenant_List_POST
-             * @param {string} provider_service - 
-             * @param {string} service_specific_id - 
-             * @param {string} vlan_id - 
-             * 
-             */
-            xoslib.prototype.Volt_Tenant_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/volttenant/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['provider_service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: provider_service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: service_specific_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters['vlan_id'] !== undefined) {
-                    form['vlan_id'] = parameters['vlan_id'];
-                }
-
-                if (parameters['vlan_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vlan_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Slice_Plus_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Slice_Plus_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/slicesplus/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Slice_Plus_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} name - The Name of the Slice
-             * @param {boolean} enabled - Status for this Slice
-             * @param {boolean} omf_friendly - 
-             * @param {string} description - High level description of the slice and expected activities
-             * @param {string} slice_url - 
-             * @param {string} site - The Site this Slice belongs to
-             * @param {integer} max_instances - 
-             * @param {string} service - 
-             * @param {string} network - 
-             * @param {string} mount_data_sets - 
-             * @param {string} default_image - 
-             * @param {string} default_flavor - 
-             * @param {string} serviceClass - 
-             * @param {string} creator - 
-             * @param {string} network_ports - 
-             * @param {string} site_allocation - 
-             * @param {string} site_ready - 
-             * @param {string} users - 
-             * @param {string} user_names - 
-             * 
-             */
-            xoslib.prototype.Slice_Plus_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/slicesplus/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['omf_friendly'] !== undefined) {
-                    form['omf_friendly'] = parameters['omf_friendly'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['slice_url'] !== undefined) {
-                    form['slice_url'] = parameters['slice_url'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['max_instances'] !== undefined) {
-                    form['max_instances'] = parameters['max_instances'];
-                }
-
-                if (parameters['max_instances'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: max_instances'));
-                    return deferred.promise;
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['mount_data_sets'] !== undefined) {
-                    form['mount_data_sets'] = parameters['mount_data_sets'];
-                }
-
-                if (parameters['default_image'] !== undefined) {
-                    form['default_image'] = parameters['default_image'];
-                }
-
-                if (parameters['default_flavor'] !== undefined) {
-                    form['default_flavor'] = parameters['default_flavor'];
-                }
-
-                if (parameters['serviceClass'] !== undefined) {
-                    form['serviceClass'] = parameters['serviceClass'];
-                }
-
-                if (parameters['creator'] !== undefined) {
-                    form['creator'] = parameters['creator'];
-                }
-
-                if (parameters['network_ports'] !== undefined) {
-                    form['network_ports'] = parameters['network_ports'];
-                }
-
-                if (parameters['site_allocation'] !== undefined) {
-                    form['site_allocation'] = parameters['site_allocation'];
-                }
-
-                if (parameters['site_ready'] !== undefined) {
-                    form['site_ready'] = parameters['site_ready'];
-                }
-
-                if (parameters['users'] !== undefined) {
-                    form['users'] = parameters['users'];
-                }
-
-                if (parameters['user_names'] !== undefined) {
-                    form['user_names'] = parameters['user_names'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Slice_Plus_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} name - The Name of the Slice
-             * @param {boolean} enabled - Status for this Slice
-             * @param {boolean} omf_friendly - 
-             * @param {string} description - High level description of the slice and expected activities
-             * @param {string} slice_url - 
-             * @param {string} site - The Site this Slice belongs to
-             * @param {integer} max_instances - 
-             * @param {string} service - 
-             * @param {string} network - 
-             * @param {string} mount_data_sets - 
-             * @param {string} default_image - 
-             * @param {string} default_flavor - 
-             * @param {string} serviceClass - 
-             * @param {string} creator - 
-             * @param {string} network_ports - 
-             * @param {string} site_allocation - 
-             * @param {string} site_ready - 
-             * @param {string} users - 
-             * @param {string} user_names - 
-             * 
-             */
-            xoslib.prototype.Slice_Plus_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/slicesplus/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['omf_friendly'] !== undefined) {
-                    form['omf_friendly'] = parameters['omf_friendly'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['slice_url'] !== undefined) {
-                    form['slice_url'] = parameters['slice_url'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['max_instances'] !== undefined) {
-                    form['max_instances'] = parameters['max_instances'];
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['mount_data_sets'] !== undefined) {
-                    form['mount_data_sets'] = parameters['mount_data_sets'];
-                }
-
-                if (parameters['default_image'] !== undefined) {
-                    form['default_image'] = parameters['default_image'];
-                }
-
-                if (parameters['default_flavor'] !== undefined) {
-                    form['default_flavor'] = parameters['default_flavor'];
-                }
-
-                if (parameters['serviceClass'] !== undefined) {
-                    form['serviceClass'] = parameters['serviceClass'];
-                }
-
-                if (parameters['creator'] !== undefined) {
-                    form['creator'] = parameters['creator'];
-                }
-
-                if (parameters['network_ports'] !== undefined) {
-                    form['network_ports'] = parameters['network_ports'];
-                }
-
-                if (parameters['site_allocation'] !== undefined) {
-                    form['site_allocation'] = parameters['site_allocation'];
-                }
-
-                if (parameters['site_ready'] !== undefined) {
-                    form['site_ready'] = parameters['site_ready'];
-                }
-
-                if (parameters['users'] !== undefined) {
-                    form['users'] = parameters['users'];
-                }
-
-                if (parameters['user_names'] !== undefined) {
-                    form['user_names'] = parameters['user_names'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Slice_Plus_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Slice_Plus_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/slicesplus/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Slice_Plus_List_GET
-             * 
-             */
-            xoslib.prototype.Slice_Plus_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/slicesplus/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Slice_Plus_List_POST
-             * @param {string} enacted - 
-             * @param {string} name - The Name of the Slice
-             * @param {boolean} enabled - Status for this Slice
-             * @param {boolean} omf_friendly - 
-             * @param {string} description - High level description of the slice and expected activities
-             * @param {string} slice_url - 
-             * @param {string} site - The Site this Slice belongs to
-             * @param {integer} max_instances - 
-             * @param {string} service - 
-             * @param {string} network - 
-             * @param {string} mount_data_sets - 
-             * @param {string} default_image - 
-             * @param {string} default_flavor - 
-             * @param {string} serviceClass - 
-             * @param {string} creator - 
-             * @param {string} network_ports - 
-             * @param {string} site_allocation - 
-             * @param {string} site_ready - 
-             * @param {string} users - 
-             * @param {string} user_names - 
-             * 
-             */
-            xoslib.prototype.Slice_Plus_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/slicesplus/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['omf_friendly'] !== undefined) {
-                    form['omf_friendly'] = parameters['omf_friendly'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['slice_url'] !== undefined) {
-                    form['slice_url'] = parameters['slice_url'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['max_instances'] !== undefined) {
-                    form['max_instances'] = parameters['max_instances'];
-                }
-
-                if (parameters['max_instances'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: max_instances'));
-                    return deferred.promise;
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['mount_data_sets'] !== undefined) {
-                    form['mount_data_sets'] = parameters['mount_data_sets'];
-                }
-
-                if (parameters['default_image'] !== undefined) {
-                    form['default_image'] = parameters['default_image'];
-                }
-
-                if (parameters['default_flavor'] !== undefined) {
-                    form['default_flavor'] = parameters['default_flavor'];
-                }
-
-                if (parameters['serviceClass'] !== undefined) {
-                    form['serviceClass'] = parameters['serviceClass'];
-                }
-
-                if (parameters['creator'] !== undefined) {
-                    form['creator'] = parameters['creator'];
-                }
-
-                if (parameters['network_ports'] !== undefined) {
-                    form['network_ports'] = parameters['network_ports'];
-                }
-
-                if (parameters['site_allocation'] !== undefined) {
-                    form['site_allocation'] = parameters['site_allocation'];
-                }
-
-                if (parameters['site_ready'] !== undefined) {
-                    form['site_ready'] = parameters['site_ready'];
-                }
-
-                if (parameters['users'] !== undefined) {
-                    form['users'] = parameters['users'];
-                }
-
-                if (parameters['user_names'] !== undefined) {
-                    form['user_names'] = parameters['user_names'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-
-            return xoslib;
-        })();
-
-        return xoslib;
-    }]);
\ No newline at end of file
diff --git a/views/ngXosLib/apiTemplates/custom-angular-class.mustache b/views/ngXosLib/apiTemplates/custom-angular-class.mustache
deleted file mode 100644
index c92a870..0000000
--- a/views/ngXosLib/apiTemplates/custom-angular-class.mustache
+++ /dev/null
@@ -1,82 +0,0 @@
-/*jshint -W069 */
-/*global angular:false */
-angular.module('{{&moduleName}}', [])
-.factory('{{&className}}', ['$q', '$http', '$rootScope', function($q, $http, $rootScope){
-    'use strict';
-
-    /**
-     * {{&description}}
-     * @class {{&className}}
-     * @param {(string|object)} [domainOrOptions] - The project domain or options object. If object, see the object's optional properties.
-     * @param {string} [domainOrOptions.domain] - The project domain
-     * @param {string} [domainOrOptions.cache] - An angularjs cache implementation
-     * @param {object} [domainOrOptions.token] - auth token - object with value property and optional headerOrQueryName and isQuery properties
-     * @param {string} [cache] - An angularjs cache implementation
-     */
-    var {{&className}} = (function(){
-        function {{&className}}(options, cache){
-            var domain = (typeof options === 'object') ? options.domain : options;
-            this.domain = typeof(domain) === 'string' ? domain : '{{&domain}}';
-            cache = cache || ((typeof options === 'object') ? options.cache : cache);
-            this.cache = cache;
-            {{#isSecure}}
-                this.token = (typeof options === 'object') ? (options.token ? options.token : {}) : {};
-            {{/isSecure}}
-        }
-
-        {{&className}}.prototype.$on = function($scope, path, handler){
-            var url = domain + path;
-            $scope.$on(url, function(){
-                handler();
-            });
-            return this;
-        };
-
-        {{&className}}.prototype.$broadcast = function(path){
-            var url = domain + path;
-            //cache.remove(url);
-            $rootScope.$broadcast(url);
-            return this;
-        };
-
-        {{&className}}.transformRequest = function(obj) {
-            var str = [];
-            for(var p in obj) {
-                var val = obj[p];
-                if(angular.isArray(val)) {
-                   val.forEach(function(val){
-                     str.push(encodeURIComponent(p) + "=" + encodeURIComponent(val));
-                   });
-                } else {
-                   str.push(encodeURIComponent(p) + "=" + encodeURIComponent(val));
-                }
-            }
-            return str.join("&");
-        };
-
-        {{#isSecure}}
-            /**
-             * Set Token
-             * @method
-             * @name {{&className}}#setToken
-             * @param {string} value - token's value
-             * @param {string} headerOrQueryName - the header or query name to send the token at
-             * @param {boolean} isQuery - true if send the token as query param, otherwise, send as header param
-             *
-             */
-            {{&className}}.prototype.setToken = function (value, headerOrQueryName, isQuery) {
-                this.token.value = value;
-                this.token.headerOrQueryName = headerOrQueryName;
-                this.token.isQuery = isQuery;
-            };
-        {{/isSecure}}
-
-        {{#methods}}
-            {{> method}}
-        {{/methods}}
-
-  return {{&className}};
-    })();
-
-    return {{&className}};
-}]);
diff --git a/views/ngXosLib/apigen/blueprintToNgResource.js b/views/ngXosLib/apigen/blueprintToNgResource.js
new file mode 100644
index 0000000..2a3cde3
--- /dev/null
+++ b/views/ngXosLib/apigen/blueprintToNgResource.js
@@ -0,0 +1,78 @@
+'use strict';
+
+const protagonist = require('protagonist');
+const fs = require('fs');
+const P = require('bluebird');
+const _ = require('lodash');
+const chalk = require('chalk');
+const Handlebars = require('handlebars');
+const util = require('util');
+const path = require('path');
+
+P.promisifyAll(fs);
+P.promisifyAll(protagonist);
+
+const angualarModuleName = 'xos.helpers'
+
+// format href in angular format
+const formatHref = url => url.replace('{', ':').replace('}', '');
+
+const formatTitle = title => title.split(' ').join('-');
+
+const getParamName = url => url.match(/\{([^)]+)\}/) ? url.match(/\{([^)]+)\}/)[1] : '';
+
+// Get Group description
+const getGroupDescription = (group) => _.find(group, {element: 'copy'}) ? _.find(group, {element: 'copy'}).content.replace(/\n$/, '') : '';
+
+// Loop APIs endpoint
+const loopApiEndpoint = (group) => {
+  // {name: 'ResourceName', attributes: {href: '/ahhsiiis'}}
+  _.remove(group, {element: 'copy'})
+  // console.log(group);
+  // _.forEach(group, d => console.log(d));
+
+  return _.map(group, g => {
+    return {
+      name: formatTitle(g.meta.title),
+      param: {href: formatHref(g.attributes.href), name: getParamName(g.attributes.href)},
+    }
+  })
+};
+
+// Loop APIs groups
+const loopApiGroups = (defs) => {
+  if (!Array.isArray(defs)) {
+    return;
+  }
+  _.forEach(defs, d => {
+    console.info(chalk.blue.bold(`Parsing Group: ${d.meta.title}`));
+    var data = {
+      description: getGroupDescription(d.content),
+      ngModule: angualarModuleName,
+      resources: loopApiEndpoint(d.content)
+    };
+    fs.writeFileSync(path.join(__dirname, `../xosHelpers/src/services/rest/${formatTitle(d.meta.title)}.js`), handlebarsTemplate(data));
+  });
+
+  console.info(chalk.green.bold(`Api Generated`));
+
+};
+
+// Loop the top level definitions
+const loopApiDefinitions = (defs) => {
+  // console.log(util.inspect(defs, false, null));
+  _.forEach(defs, d => loopApiGroups(d.content));
+};
+
+let handlebarsTemplate;
+
+// read blueprint docs and parse
+fs.readFileAsync(path.join(__dirname, './ngResourceTemplate.handlebars'), 'utf8')
+.then((template) => {
+  handlebarsTemplate = Handlebars.compile(template);
+  return fs.readFileAsync(path.join(__dirname, '../../../xos/tests/api/apiary.apib'), 'utf8')
+})
+.then(data => protagonist.parseAsync(data))
+.then(result => loopApiDefinitions(result.content))
+.catch(console.warn);
+
diff --git a/views/ngXosLib/apigen/ngResourceTemplate.handlebars b/views/ngXosLib/apigen/ngResourceTemplate.handlebars
new file mode 100644
index 0000000..6176633
--- /dev/null
+++ b/views/ngXosLib/apigen/ngResourceTemplate.handlebars
@@ -0,0 +1,15 @@
+(function() {
+  'use strict';
+
+  angular.module('{{ngModule}}')
+  {{#each resources}}
+  /**
+  * @ngdoc service
+  * @name {{../ngModule}}.{{name}}
+  * @description Angular resource to fetch {{param.href}}
+  **/
+  .service('{{name}}', function($resource){
+    return $resource('{{param.href}}'{{#if param.name}}, { {{param.name}}: '@id' }{{/if}});
+  })
+  {{/each}}
+})();
\ No newline at end of file
diff --git a/views/ngXosLib/bower.json b/views/ngXosLib/bower.json
index 71777ba..3726b85 100644
--- a/views/ngXosLib/bower.json
+++ b/views/ngXosLib/bower.json
@@ -18,7 +18,8 @@
     "angular-ui-router": "0.2.15",
     "angular-resource": "1.4.7",
     "ng-lodash": "0.3.0",
-    "angular-cookies": "1.4.7"
+    "angular-cookies": "1.4.7",
+    "angular-animate": "1.4.7"
   },
   "devDependencies": {
     "angular-mocks": "1.4.7"
diff --git a/views/ngXosLib/generator-xos/app/templates/bower.json b/views/ngXosLib/generator-xos/app/templates/bower.json
index 667a7ac..721e2de 100644
--- a/views/ngXosLib/generator-xos/app/templates/bower.json
+++ b/views/ngXosLib/generator-xos/app/templates/bower.json
@@ -22,8 +22,9 @@
     "angular": "1.4.7",
     "angular-ui-router": "0.2.15",
     "angular-cookies": "1.4.7",
+    "angular-animate": "1.4.7",
     "angular-resource": "1.4.7",
     "ng-lodash": "0.3.0",
-    "bootstrap-css": "2.3.2"
+    "bootstrap-css": "3.3.6"
   }
 }
diff --git a/views/ngXosLib/generator-xos/app/templates/gulp/build.js b/views/ngXosLib/generator-xos/app/templates/gulp/build.js
index bd31a25..663a4cf 100644
--- a/views/ngXosLib/generator-xos/app/templates/gulp/build.js
+++ b/views/ngXosLib/generator-xos/app/templates/gulp/build.js
@@ -13,7 +13,7 @@
 var uglify = require('gulp-uglify');
 var templateCache = require('gulp-angular-templatecache');
 var runSequence = require('run-sequence');
-var concat = require('gulp-concat');
+var concat = require('gulp-concat-util');
 var del = require('del');
 var wiredep = require('wiredep');
 var angularFilesort = require('gulp-angular-filesort');
@@ -22,31 +22,31 @@
 var inject = require('gulp-inject');
 var rename = require('gulp-rename');
 var replace = require('gulp-replace');
+var postcss = require('gulp-postcss');
+var autoprefixer = require('autoprefixer');
+var mqpacker = require('css-mqpacker');
+var csswring = require('csswring');
+
+const TEMPLATE_FOOTER = `
+angular.module('xos.<%= name %>')
+.run(['$location', function(a){
+  a.path('/');
+}])
+`
 
 module.exports = function(options){
   
   // delete previous builded file
   gulp.task('clean', function(){
     return del(
-      [options.dashboards + 'xos<%= fileName %>.html'],
+      [
+        options.dashboards + 'xos<%= fileName %>.html',
+        options.static + 'css/xos<%= fileName %>.css'
+      ],
       {force: true}
     );
   });
 
-  // inject CSS
-  gulp.task('injectCss', function(){
-    return gulp.src(options.src + 'index.html')
-      .pipe(
-        inject(
-          gulp.src(options.src + 'css/*.css'),
-          {
-            ignorePath: [options.src]
-          }
-          )
-        )
-      .pipe(gulp.dest(options.src));
-  });
-
   // minify css
   gulp.task('css', function () {
     var processors = [
@@ -64,9 +64,9 @@
   });
 
   // copy css in correct folder
-  gulp.task('copyCss', ['css'], function(){
+  gulp.task('copyCss', ['wait'], function(){
     return gulp.src([`${options.tmp}/css/*.css`])
-    .pipe(concat('xosDiagnostic.css'))
+    .pipe(concat('xos<%= fileName %>.css'))
     .pipe(gulp.dest(options.static + 'css/'))
   });
 
@@ -78,6 +78,8 @@
     .pipe(ngAnnotate())
     .pipe(angularFilesort())
     .pipe(concat('xos<%= fileName %>.js'))
+    .pipe(concat.header('//Autogenerated, do not edit!!!\n'))
+    .pipe(concat.footer(TEMPLATE_FOOTER))
     .pipe(uglify())
     .pipe(gulp.dest(options.static + 'js/'));
   });
@@ -93,7 +95,7 @@
   });
 
   // copy html index to Django Folder
-  gulp.task('copyHtml', ['clean'], function(){
+  gulp.task('copyHtml', function(){
     return gulp.src(options.src + 'index.html')
       // remove dev dependencies from html
       .pipe(replace(/<!-- bower:css -->(\n.*)*\n<!-- endbower --><!-- endcss -->/, ''))
@@ -103,7 +105,8 @@
         inject(
           gulp.src([
             options.static + 'js/vendor/xos<%= fileName %>Vendor.js',
-            options.static + 'js/xos<%= fileName %>.js'
+            options.static + 'js/xos<%= fileName %>.js',
+            options.static + 'css/xos<%= fileName %>.css'
           ]),
           {ignorePath: '/../../../xos/core/xoslib'}
         )
@@ -137,13 +140,22 @@
       .pipe(eslint.failAfterError());
   });
 
+  gulp.task('wait', function (cb) {
+    // setTimeout could be any async task
+    setTimeout(function () {
+      cb();
+    }, 1000);
+  });
+
   gulp.task('build', function() {
     runSequence(
+      'clean',
       'templates',
       'babel',
       'scripts',
       'wiredep',
-      'injectCss',
+      'css',
+      'copyCss',
       'copyHtml',
       'cleanTmp'
     );
diff --git a/views/ngXosLib/generator-xos/app/templates/gulp/server.js b/views/ngXosLib/generator-xos/app/templates/gulp/server.js
index 1c8a0a7..13659c3 100644
--- a/views/ngXosLib/generator-xos/app/templates/gulp/server.js
+++ b/views/ngXosLib/generator-xos/app/templates/gulp/server.js
@@ -50,14 +50,14 @@
       server: {
         baseDir: options.src,
         routes: {
-          '/api': options.api,
           '/xosHelpers/src': options.helpers
         },
         middleware: function(req, res, next){
           if(
             req.url.indexOf('/xos/') !== -1 ||
             req.url.indexOf('/xoslib/') !== -1 ||
-            req.url.indexOf('/hpcapi/') !== -1
+            req.url.indexOf('/hpcapi/') !== -1 ||
+            req.url.indexOf('/api/') !== -1
           ){
             if(conf.xoscsrftoken && conf.xossessionid){
               req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
@@ -108,8 +108,7 @@
         inject(
           gulp.src([
             options.tmp + '**/*.js',
-            options.api + '*.js',
-            options.helpers + '**/*.js'
+            options.helpers + '**/*.js' // todo add Babel here
           ])
           .pipe(angularFilesort()),
           {
diff --git a/views/ngXosLib/generator-xos/app/templates/karma.conf.js b/views/ngXosLib/generator-xos/app/templates/karma.conf.js
index 83d3f63..4123be9 100644
--- a/views/ngXosLib/generator-xos/app/templates/karma.conf.js
+++ b/views/ngXosLib/generator-xos/app/templates/karma.conf.js
@@ -26,8 +26,8 @@
 
     // list of files / patterns to load in the browser
     files: bowerComponents.concat([
-      '../../static/js/xosApi.js',
-      '../../static/js/vendor/ngXosHelpers.js',
+      '../../../xos/core/xoslib/static/js/vendor/ngXosVendor.js',
+      '../../../xos/core/xoslib/static/js/vendor/ngXosHelpers.js',
       'src/js/**/*.js',
       'spec/**/*.mock.js',
       'spec/**/*.test.js',
diff --git a/views/ngXosLib/generator-xos/app/templates/package.json b/views/ngXosLib/generator-xos/app/templates/package.json
index 7c0a016..b0f77f9 100644
--- a/views/ngXosLib/generator-xos/app/templates/package.json
+++ b/views/ngXosLib/generator-xos/app/templates/package.json
@@ -8,6 +8,7 @@
     "prebuild": "npm install && bower install",
     "build": "gulp",
     "test": "karma start",
+    "test:ci": "karma start --single-run",
     "lint": "eslint src/js/"
   },
   "keywords": [
@@ -19,28 +20,44 @@
   "license": "MIT",
   "dependencies": {},
   "devDependencies": {
+    "autoprefixer": "^6.3.3",
     "browser-sync": "^2.9.11",
+    "css-mqpacker": "^4.0.0",
+    "csswring": "^4.2.1",
     "del": "^2.0.2",
+    "easy-mocker": "^1.2.0",
+    "eslint": "^1.8.0",
+    "eslint-plugin-angular": "linkmesrl/eslint-plugin-angular",
     "gulp": "^3.9.0",
     "gulp-angular-filesort": "^1.1.1",
     "gulp-angular-templatecache": "^1.8.0",
     "gulp-babel": "^5.3.0",
     "gulp-concat": "^2.6.0",
+    "gulp-concat-util": "^0.5.5",
+    "gulp-eslint": "^1.0.0",
     "gulp-inject": "^3.0.0",
     "gulp-minify-html": "^1.0.4",
+    "gulp-ng-annotate": "^1.1.0",
+    "gulp-postcss": "^6.0.1",
     "gulp-rename": "^1.2.2",
     "gulp-replace": "^0.5.4",
     "gulp-sass": "^2.2.0",
     "gulp-uglify": "^1.4.2",
     "http-proxy": "^1.12.0",
+    "ink-docstrap": "^0.5.2",
+    "jasmine-core": "~2.3.4",
+    "karma": "^0.13.14",
+    "karma-babel-preprocessor": "~5.2.2",
+    "karma-coverage": "^0.5.3",
+    "karma-jasmine": "~0.3.6",
+    "karma-mocha-reporter": "~1.1.1",
+    "karma-ng-html2js-preprocessor": "^0.2.0",
+    "karma-phantomjs-launcher": "~0.2.1",
+    "lodash": "^3.10.1",
+    "phantomjs": "^1.9.19",
     "proxy-middleware": "^0.15.0",
     "run-sequence": "^1.1.4",
     "wiredep": "^3.0.0-beta",
-    "wrench": "^1.5.8",
-    "gulp-ng-annotate": "^1.1.0",
-    "lodash": "^3.10.1",
-    "eslint": "^1.8.0",
-    "eslint-plugin-angular": "linkmesrl/eslint-plugin-angular",
-    "gulp-eslint": "^1.0.0"
+    "wrench": "^1.5.8"
   }
 }
diff --git a/views/ngXosLib/generator-xos/app/templates/spec/sample.test.js b/views/ngXosLib/generator-xos/app/templates/spec/sample.test.js
index 10a74ad..b00fa5e 100644
--- a/views/ngXosLib/generator-xos/app/templates/spec/sample.test.js
+++ b/views/ngXosLib/generator-xos/app/templates/spec/sample.test.js
@@ -11,7 +11,7 @@
     
     httpBackend = $httpBackend;
     // Setting up mock request
-    $httpBackend.expectGET('/xos/users/?no_hyperlinks=1').respond([
+    $httpBackend.expectGET('/api/core/users/?no_hyperlinks=1').respond([
       {
         email: '<%= user.email %>',
         firstname: '<%= user.firstname %>',
diff --git a/views/ngXosLib/generator-xos/app/templates/src/index.html b/views/ngXosLib/generator-xos/app/templates/src/index.html
index b271d11..eeb0dec 100644
--- a/views/ngXosLib/generator-xos/app/templates/src/index.html
+++ b/views/ngXosLib/generator-xos/app/templates/src/index.html
@@ -4,7 +4,7 @@
 <!-- inject:css -->
 <!-- endinject -->
 
-<div ng-app="xos.<%= name %>" id="xos<%= fileName %>">
+<div ng-app="xos.<%= name %>" id="xos<%= fileName %>" class="container-fluid">
     <div ui-view></div>
 </div>
 
diff --git a/views/ngXosLib/generator-xos/app/templates/src/js/main.js b/views/ngXosLib/generator-xos/app/templates/src/js/main.js
index 878e4eb..9c0e259 100644
--- a/views/ngXosLib/generator-xos/app/templates/src/js/main.js
+++ b/views/ngXosLib/generator-xos/app/templates/src/js/main.js
@@ -24,9 +24,9 @@
     bindToController: true,
     controllerAs: 'vm',
     templateUrl: 'templates/users-list.tpl.html',
-    controller: function(XosApi){
+    controller: function(Users){
       // retrieving user list
-      XosApi.User_List_GET()
+      Users.query().$promise
       .then((users) => {
         this.users = users;
       })
diff --git a/views/ngXosLib/generator-xos/app/templates/src/sass/main.scss b/views/ngXosLib/generator-xos/app/templates/src/sass/main.scss
index 3d8a1c1..6f8a791 100644
--- a/views/ngXosLib/generator-xos/app/templates/src/sass/main.scss
+++ b/views/ngXosLib/generator-xos/app/templates/src/sass/main.scss
@@ -1,3 +1,5 @@
+@import '../../../../style/sass/lib/_variables.scss';
+
 #xos<%=fileName%> {
   
 }
\ No newline at end of file
diff --git a/views/ngXosLib/generator-xos/app/templates/src/templates/users-list.tpl.html b/views/ngXosLib/generator-xos/app/templates/src/templates/users-list.tpl.html
index 2983ad0..fd8d208 100644
--- a/views/ngXosLib/generator-xos/app/templates/src/templates/users-list.tpl.html
+++ b/views/ngXosLib/generator-xos/app/templates/src/templates/users-list.tpl.html
@@ -1,14 +1,16 @@
 <div class="row">
-  <h1>Users List</h1>
-  <p>This is only an example view.</p>
+  <div class="col-xs-12">
+    <h1>Users List</h1>
+    <p>This is only an example view.</p>
+  </div>
 </div>
 <div class="row">
-  <div class="span4">Email</div>
-  <div class="span4">First Name</div>
-  <div class="span4">Last Name</div>
+  <div class="col-xs-4">Email</div>
+  <div class="col-xs-4">First Name</div>
+  <div class="col-xs-4">Last Name</div>
 </div>  
 <div class="row" ng-repeat="user in vm.users">
-  <div class="span4">{{user.email}}</div>
-  <div class="span4">{{user.firstname}}</div>
-  <div class="span4">{{user.lastname}}</div>
+  <div class="col-xs-4">{{user.email}}</div>
+  <div class="col-xs-4">{{user.firstname}}</div>
+  <div class="col-xs-4">{{user.lastname}}</div>
 </div>  
\ No newline at end of file
diff --git a/views/ngXosLib/gulp/ngXosHelpers.js b/views/ngXosLib/gulp/ngXosHelpers.js
index cc68513..f046681 100644
--- a/views/ngXosLib/gulp/ngXosHelpers.js
+++ b/views/ngXosLib/gulp/ngXosHelpers.js
@@ -1,16 +1,119 @@
 var gulp = require('gulp');
 var uglify = require('gulp-uglify');
-var concat = require("gulp-concat");
+var concat = require('gulp-concat');
 var ngAnnotate = require('gulp-ng-annotate');
 var angularFilesort = require('gulp-angular-filesort');
+var gulpDocs = require('gulp-ngdocs');
+var del = require('del');
+var babel = require('gulp-babel');
+const sourcemaps = require('gulp-sourcemaps');
+var browserSync = require('browser-sync').create();
 
 module.exports = function(options){
-  gulp.task('helpers', function(){
-    return gulp.src([options.xosHelperSource + '**/*.js'])
+
+  // transpile js with sourceMaps
+  gulp.task('babel', function(){
+    return gulp.src(options.xosHelperSource + '**/*.js')
+      .pipe(babel({
+        presets: ['es2015']
+      }))
+      .pipe(gulp.dest(options.xosHelperTmp));
+  });
+
+  gulp.task('babelDev', function(){
+    return gulp.src(options.xosHelperSource + '**/*.js')
+      .pipe(sourcemaps.init())
+      .pipe(babel({
+        presets: ['es2015']
+      }))
+      .pipe(sourcemaps.write('./maps'))
+      .pipe(gulp.dest(options.xosHelperTmp));
+  });
+
+  // build
+  gulp.task('helpers', ['babel'], function(){
+    return gulp.src([options.xosHelperTmp + '**/*.js'])
       .pipe(angularFilesort())
       .pipe(concat('ngXosHelpers.js'))
       .pipe(ngAnnotate())
       .pipe(uglify())
       .pipe(gulp.dest(options.ngXosVendor));
   });
+
+  // build Dev (no minify, sourcemaps)
+  gulp.task('helpersDev', ['babelDev'], function(){
+    return gulp.src([options.xosHelperTmp + '**/*.js'])
+      .pipe(angularFilesort())
+      .pipe(concat('ngXosHelpers.js'))
+      .pipe(ngAnnotate())
+      .pipe(gulp.dest(options.ngXosVendor));
+  });
+
+  gulp.task('cleanDocs', function(){
+    return del([options.docs + '**/*']);
+  });
+
+  gulp.task('makeDocs', ['cleanDocs'], function(){
+    var ngOptions = {
+      scripts: [
+        'http://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js',
+        'http://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-animate.min.js',
+        `${options.ngXosVendor}ngXosHelpers.js`
+      ],
+      styles: [
+        'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css',
+      ],
+      html5Mode: false,
+      title: 'XOS Helpers documentation',
+      startPage: '/module',
+    }
+
+    return gulpDocs.sections({
+      module: {
+        glob: [
+          options.xosHelperSource + '*.js',
+          options.xosHelperSource + 'services/*.js',
+          options.xosHelperSource + 'ui_components/**/*.js'
+        ],
+        title: 'Module Documentation',
+      },
+      'rest-api': {
+        glob: [
+          options.xosHelperSource + 'services/rest/*.js'
+        ],
+        api: true,
+        title: 'API Documentation',
+      }
+    }).pipe(gulpDocs.process(ngOptions)).pipe(gulp.dest('./docs'));
+  });
+
+  gulp.task('serveDocs', function(){
+    browserSync.init({
+      server: {
+        baseDir: './docs',
+        routes: {
+          '/xos/core/xoslib/static/js/vendor': options.ngXosVendor
+        }
+      }
+    });
+  });
+
+  gulp.task('docs', ['makeDocs', 'serveDocs'], function(){
+    
+    var files = [
+      options.xosHelperSource + '*.js',
+      options.xosHelperSource + 'services/*.js',
+      options.xosHelperSource + 'ui_components/**/*.js'
+    ];
+
+    gulp.watch(files, ['makeDocs']);
+
+    gulp.watch(files, function(){
+      browserSync.reload();
+    });
+  })
+
+  gulp.task('dev', function(){
+    gulp.watch(options.xosHelperSource + '**/*.js', ['helpersDev']);
+  });
 };
\ No newline at end of file
diff --git a/views/ngXosLib/gulpfile.js b/views/ngXosLib/gulpfile.js
index d893755..77a5c07 100644
--- a/views/ngXosLib/gulpfile.js
+++ b/views/ngXosLib/gulpfile.js
@@ -5,7 +5,9 @@
 
 var options = {
   ngXosVendor: '../../xos/core/xoslib/static/js/vendor/', //save here the minfied vendor file, this is automatically loaded in the django page
-  xosHelperSource: './xosHelpers/src/'
+  xosHelperSource: './xosHelpers/src/',
+  xosHelperTmp: './xosHelpers/.tmp/',
+  docs: './docs'
 };
 
 wrench.readdirSyncRecursive('./gulp')
diff --git a/views/ngXosLib/karma.conf.js b/views/ngXosLib/karma.conf.js
index 060e7dd..b602d66 100644
--- a/views/ngXosLib/karma.conf.js
+++ b/views/ngXosLib/karma.conf.js
@@ -13,7 +13,7 @@
 
 var files = bowerComponents.concat([
   'api/**/*.js',
-  'xosHelpers/src/*.module.js',
+  'xosHelpers/src/**/*.module.js',
   'xosHelpers/src/**/*.js',
   'xosHelpers/spec/**/*.test.js'
 ]);
@@ -43,7 +43,7 @@
     // preprocess matching files before serving them to the browser
     // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
     preprocessors: {
-      '**/*.test.js': ['babel']
+      'xosHelpers/**/*.js': ['babel'],
     },
 
     babelPreprocessor: {
@@ -86,7 +86,7 @@
 
     // start these browsers
     // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
-    browsers: ['PhantomJS', 'Chrome'],
+    browsers: ['PhantomJS'],
 
 
     // Continuous Integration mode
diff --git a/views/ngXosLib/package.json b/views/ngXosLib/package.json
index b2d092d..9e8906c 100644
--- a/views/ngXosLib/package.json
+++ b/views/ngXosLib/package.json
@@ -5,10 +5,11 @@
   "main": "index.js",
   "scripts": {
     "test": "karma start",
-    "apigen": "node xos-resource-generator.js",
+    "apigen": "node apigen/blueprintToNgResource.js",
     "swagger": "node xos-swagger-def.js",
-    "apidoc": "jsdoc api/ng-xos.js api/ng-xoslib.js api/ng-hpcapi.js -d api/docs",
-    "build": "gulp vendor && gulp helpers"
+    "doc": "gulp docs; cd ./docs",
+    "build": "gulp vendor && gulp helpers",
+    "dev": "gulp dev"
   },
   "author": "Matteo Scandolo",
   "license": "ISC",
@@ -24,10 +25,16 @@
   },
   "devDependencies": {
     "babel-preset-es2015": "^6.6.0",
+    "browser-sync": "^2.12.3",
+    "concat": "^2.0.0",
+    "del": "^2.2.0",
     "gulp": "^3.9.0",
     "gulp-angular-filesort": "^1.1.1",
+    "gulp-babel": "^6.1.2",
     "gulp-concat": "^2.6.0",
     "gulp-ng-annotate": "^1.1.0",
+    "gulp-ngdocs": "^0.2.13",
+    "gulp-sourcemaps": "^1.6.0",
     "gulp-uglify": "^1.4.2",
     "jasmine-core": "^2.4.1",
     "karma": "^0.13.19",
diff --git a/views/ngXosLib/xos-resource-generator.js b/views/ngXosLib/xos-resource-generator.js
deleted file mode 100644
index b24bf9b..0000000
--- a/views/ngXosLib/xos-resource-generator.js
+++ /dev/null
@@ -1,99 +0,0 @@
-'use strict';
-
-var fs = require('fs');
-var CodeGen = require('swagger-js-codegen').CodeGen;
-var fetchSchema = require('fetch-swagger-schema');
-var P = require('bluebird');
-var chalk = require('chalk');
-var concat = require('concat')
-
-/////////////
-// HELPERS //
-/////////////
-
-var fetchSwagger = P.promisify(function(url, done){
-  fetchSchema(url, function(error, schema){
-    if(error) {
-      return done(error);
-    }
-    done(null, schema);
-  });
-});
-
-// Write to file promisified
-var writeToFile = P.promisify(function(file, content, done) {
-  fs.writeFile(file, content, function(err) {
-    if(err) {
-        return done(err);
-    }
-
-    done(null, file + ' has been saved');
-  }); 
-});
-
-var concatFiles = P.promisify(function(files, dest, done){
-  concat(files, dest, function (error) {
-    if(error){
-      return done(error);
-    }
-    done();
-  })
-});
-
-////////////////////
-// generator loop //
-////////////////////
-
-P.coroutine(function*(){
-  
-  var generatedFiles = [];
-
-  console.log(chalk.green('Generating APIs '));
-
-  let mainDef = yield fetchSwagger('http://localhost:9999/docs/api-docs/');
-
-  for(let i = 0; i < mainDef.apis.length; i++){
-    
-    const path = mainDef.apis[i].path.replace('/', '');
-  
-    process.stdout.write(chalk.green(`Starting ${path} generation `));
-    
-    let loader = setInterval(function(){
-      process.stdout.write(chalk.green('.'));
-    }, 500);
-
-
-    let def = yield fetchSwagger(`http://localhost:9999/docs/api-docs/${path}`);
-    yield writeToFile(`api/ng-${path}.js`, CodeGen.getAngularCode({ 
-      moduleName: `xos.${path}`, 
-      className: `${path}`, 
-      swagger: def,
-      lint: false,
-      template: {
-        class: fs.readFileSync('apiTemplates/custom-angular-class.mustache', 'utf-8'),
-        method: fs.readFileSync('node_modules/swagger-js-codegen/templates/method.mustache', 'utf-8'),
-        request: fs.readFileSync('node_modules/swagger-js-codegen/templates/angular-request.mustache', 'utf-8')
-    }
-    }));
-  
-    generatedFiles.push(`api/ng-${path}.js`);
-
-    clearInterval(loader);
-    process.stdout.write('\n');
-  }
-
-  // TODO rewrite concat to minify API
-  // evaluate to use gulp instead to manage this
-  // at least minify
-  yield concatFiles(generatedFiles, '../static/js/xosApi.js');
-
-  console.log(chalk.green('APIs Ready!'));
-
-  process.exit();
-
-})()
-.catch(function(e){
-  process.stdout.write('\n');
-  console.error(e);
-  process.exit(e.code);
-});
diff --git a/views/ngXosLib/xos-swagger-def.js b/views/ngXosLib/xos-swagger-def.js
deleted file mode 100644
index 64db936..0000000
--- a/views/ngXosLib/xos-swagger-def.js
+++ /dev/null
@@ -1,96 +0,0 @@
-// This was generating the files needed from https://github.com/signalfx/swagger-angular-client
-// But this module is not parsing the format in which our Swagger is generating JSON files
-
-'use strict';
-
-var fs = require('fs');
-var CodeGen = require('swagger-js-codegen').CodeGen;
-var fetchSchema = require('fetch-swagger-schema');
-var P = require('bluebird');
-var chalk = require('chalk');
-var concat = require('concat')
-
-/////////////
-// HELPERS //
-/////////////
-
-var fetchSwagger = P.promisify(function(url, done){
-  fetchSchema(url, function(error, schema){
-    if(error) {
-      return done(error);
-    }
-    done(null, schema);
-  });
-});
-
-// Write to file promisified
-var writeToFile = P.promisify(function(file, content, done) {
-  fs.writeFile(file, content, function(err) {
-    if(err) {
-        return done(err);
-    }
-
-    done(null, file + ' has been saved');
-  }); 
-});
-
-var concatFiles = P.promisify(function(files, dest, done){
-  concat(files, dest, function (error) {
-    if(error){
-      return done(error);
-    }
-    done();
-  })
-});
-
-////////////////////
-// generator loop //
-////////////////////
-
-P.coroutine(function*(){
-  
-  var generatedFiles = [];
-
-  console.log(chalk.green('Generating APIs '));
-
-  let mainDef = yield fetchSwagger('http://localhost:9999/docs/api-docs/');
-
-  yield writeToFile(`api/ngXosApi-runtime.js`, `window.XosApi = ${JSON.stringify(mainDef)}`)
-
-  for(let i = 0; i < mainDef.apis.length; i++){
-    
-    const path = mainDef.apis[i].path.replace('/', '');
-  
-    process.stdout.write(chalk.green(`Starting ${path} generation `));
-    
-    let loader = setInterval(function(){
-      process.stdout.write(chalk.green('.'));
-    }, 500);
-
-
-    let def = yield fetchSwagger(`http://localhost:9999/docs/api-docs/${path}`);
-    
-    yield writeToFile(`api/ng-${path}.json`, JSON.stringify(def));
-    yield writeToFile(`api/ng-${path}.js`, `window.${path}Api = ${JSON.stringify(def)}`)
-  
-    generatedFiles.push(`api/ng-${path}.js`);
-
-    clearInterval(loader);
-    process.stdout.write('\n');
-  }
-
-  // TODO rewrite concat to minify API
-  // evaluate to use gulp instead to manage this
-  // at least minify
-  yield concatFiles(generatedFiles, '../static/js/xosApi.js');
-
-  console.log(chalk.green('APIs Ready!'));
-
-  process.exit();
-
-})()
-.catch(function(e){
-  process.stdout.write('\n');
-  console.error(e);
-  process.exit(e.code);
-});
diff --git a/views/ngXosLib/xosHelpers/spec/csrftoken.test.js b/views/ngXosLib/xosHelpers/spec/csrftoken.test.js
index 200014d..8901bde 100644
--- a/views/ngXosLib/xosHelpers/spec/csrftoken.test.js
+++ b/views/ngXosLib/xosHelpers/spec/csrftoken.test.js
@@ -34,6 +34,7 @@
       httpBackend.when('POST', 'http://example.com', null, function(headers) {
         expect(headers['X-CSRFToken']).toBe(fakeToken);
         done();
+        return headers;
       }).respond(200, {name: 'example' });
 
       http.post('http://example.com');
diff --git a/views/ngXosLib/xosHelpers/spec/noHyperlinks.test.js b/views/ngXosLib/xosHelpers/spec/noHyperlinks.test.js
index 560f4dc..7b6e9d0 100644
--- a/views/ngXosLib/xosHelpers/spec/noHyperlinks.test.js
+++ b/views/ngXosLib/xosHelpers/spec/noHyperlinks.test.js
@@ -9,43 +9,43 @@
 (function () {
   'use strict';
 
-  describe('The NoHyperlinks factory', () => {
+  describe('The xos.helper module', function(){
+    describe('The NoHyperlinks factory', () => {
 
-    let httpProviderObj, httpBackend, http, noHyperlinks;
+      let httpProviderObj, noHyperlinks;
 
-    beforeEach(() => {
-      module(
-        'xos.helpers',
-        ($httpProvider) => {
-          //save our interceptor
-          httpProviderObj = $httpProvider;
-        }
-      );
+      beforeEach(() => {
+        module(
+          'xos.helpers',
+          ($httpProvider) => {
+            //save our interceptor
+            httpProviderObj = $httpProvider;
+          }
+        );
 
-      inject(function (_$httpBackend_, _$http_, _NoHyperlinks_) {
-        httpBackend = _$httpBackend_;
-        http = _$http_;
-        noHyperlinks = _NoHyperlinks_
+        inject(function (_NoHyperlinks_) {
+          noHyperlinks = _NoHyperlinks_
+        });
+
+        httpProviderObj.interceptors.push('NoHyperlinks');
+
       });
 
-      httpProviderObj.interceptors.push('NoHyperlinks');
+      it('should set NoHyperlinks interceptor', () => {
+        expect(httpProviderObj.interceptors).toContain('NoHyperlinks');
+      });
+
+      it('should attach ?no_hyperlinks=1 to the request url', () => {
+        let result = noHyperlinks.request({url: 'sample.url'});
+        expect(result.url).toEqual('sample.url?no_hyperlinks=1');
+      });
+
+      it('should NOT attach ?no_hyperlinks=1 to the request url if is HTML', () => {
+        let result = noHyperlinks.request({url: 'sample.html'});
+        expect(result.url).toEqual('sample.html');
+      });
 
     });
-
-    it('should set NoHyperlinks interceptor', () => {
-      expect(httpProviderObj.interceptors).toContain('NoHyperlinks');
-    });
-
-    it('should attach ?no_hyperlinks=1 to the request url', () => {
-      let result = noHyperlinks.request({url: 'sample.url'});
-      expect(result.url).toEqual('sample.url?no_hyperlinks=1');
-    });
-
-    it('should NOT attach ?no_hyperlinks=1 to the request url if is HTML', () => {
-      let result = noHyperlinks.request({url: 'sample.html'});
-      expect(result.url).toEqual('sample.html');
-    });
-
   });
 })();
 
diff --git a/views/ngXosLib/xosHelpers/spec/ui/alert.test.js b/views/ngXosLib/xosHelpers/spec/ui/alert.test.js
new file mode 100644
index 0000000..c67d03a
--- /dev/null
+++ b/views/ngXosLib/xosHelpers/spec/ui/alert.test.js
@@ -0,0 +1,131 @@
+/**
+ * © OpenCORD
+ *
+ * Created by teone on 4/15/16.
+ */
+
+(function () {
+  'use strict';
+
+  describe('The xos.helper module', function(){
+    describe('The xos-alert component', () => {
+
+      let element, scope, isolatedScope;
+
+      let message = 'Test Error Message';
+
+      beforeEach(module('xos.helpers'));
+
+      it('should throw an error if no config is specified', inject(($compile, $rootScope) => {
+        function errorFunctionWrapper(){
+          $compile(angular.element('<xos-alert></xos-alert>'))($rootScope);
+          $rootScope.$digest();
+        }
+        expect(errorFunctionWrapper).toThrow(new Error('[xosAlert] Please provide a configuration via the "config" attribute'));
+      }));
+
+      describe('when correctly configured', () => {
+        beforeEach(inject(($compile, $rootScope) => {
+
+          scope = $rootScope.$new();
+
+          scope.config = {
+            type: 'danger',
+            closeBtn: true
+          };
+
+          element = angular.element(`<xos-alert config="config">${message}</xos-alert>`);
+          $compile(element)(scope);
+          scope.$digest();
+          isolatedScope = element.isolateScope().vm;
+        }));
+
+        it('should transclude the message', () => {
+          let textContainer = element[0].getElementsByTagName('p')[0];
+          let text = angular.element(textContainer).text();
+          expect(text).toEqual(message)
+        });
+
+        it('should have a close button', () => {
+          let btn = element[0].getElementsByTagName('button');
+          expect(btn.length).toEqual(1);
+        });
+
+        describe('when the close button is clicked', () => {
+          it('should hide the alert', () => {
+            let btn = element[0].getElementsByTagName('button')[0];
+            btn.click();
+            let alert = angular.element(element[0].querySelectorAll('.alert')[0]);
+            expect(alert.hasClass('ng-hide')).toBeTruthy();
+          });
+        });
+
+        describe('when autoHide is set', () => {
+
+          let to;
+
+          beforeEach(inject(($compile, $rootScope, $timeout) => {
+            scope = $rootScope.$new();
+
+            scope.config = {
+              type: 'danger',
+              closeBtn: true,
+              autoHide: 500
+            };
+
+            to = $timeout;
+
+            element = angular.element(`<xos-alert config="config">${message}</xos-alert>`);
+            $compile(element)(scope);
+            scope.$digest();
+            isolatedScope = element.isolateScope().vm;
+          }));
+
+          it('should hide the alert', () => {
+            to.flush();
+            expect(isolatedScope.show).toBeFalsy();
+            let alert = angular.element(element[0].querySelectorAll('.alert')[0]);
+            expect(alert.hasClass('ng-hide')).toBeTruthy();
+          });
+        });
+
+        describe('when show is set to false', () => {
+
+          beforeEach(inject(($compile, $rootScope) => {
+            scope = $rootScope.$new();
+
+            scope.config = {
+              type: 'danger',
+              closeBtn: true
+            };
+
+            scope.show = false;
+
+            element = angular.element(`<xos-alert config="config" show="show">${message}</xos-alert>`);
+            $compile(element)(scope);
+            scope.$digest();
+            isolatedScope = element.isolateScope().vm;
+          }));
+
+          it('should hide the alert', () => {
+            let alert = angular.element(element[0].querySelectorAll('.alert')[0]);
+            expect(alert.hasClass('ng-hide')).toBeTruthy();
+          });
+
+          describe('when show is changed to true', () => {
+            beforeEach(() => {
+              scope.show = true;
+              scope.$digest();
+            });
+
+            it('should show the alert', () => {
+              let alert = angular.element(element[0].querySelectorAll('.alert')[0]);
+              expect(alert.hasClass('ng-hide')).toBeFalsy();
+            });
+          });
+        });
+
+      });
+    });
+  });
+})();
\ No newline at end of file
diff --git a/views/ngXosLib/xosHelpers/spec/ui/pagination.test.js b/views/ngXosLib/xosHelpers/spec/ui/pagination.test.js
new file mode 100644
index 0000000..a8a7482
--- /dev/null
+++ b/views/ngXosLib/xosHelpers/spec/ui/pagination.test.js
@@ -0,0 +1,53 @@
+(function () {
+  'use strict';
+
+  describe('The xos.helper module', function(){
+    describe('The xos-pagination component', () => {
+
+      let scope, element, isolatedScope;
+      let cb = jasmine.createSpy('callback')
+
+      beforeEach(module('xos.helpers'));
+
+      beforeEach(inject(function ($compile, $rootScope) {
+        scope = $rootScope.$new();
+
+        scope.pageSize = 2;
+
+        scope.totalElements = 5;
+
+        scope.change = cb;
+
+        element = angular.element('<xos-pagination page-size="pageSize" total-elements="totalElements" change="change"></xos-table>');
+        $compile(element)(scope);
+        scope.$digest();
+        isolatedScope = element.isolateScope().vm;
+      }));
+
+      it('should contain 3 pages', function() {
+        var li = element[0].getElementsByTagName('li');
+        expect(li.length).toEqual(5);
+      });
+
+      it('should call the change function', () => {
+        var li = element[0].getElementsByTagName('li')[3];
+        let link = li.getElementsByTagName('a')[0];
+        link.click();
+        expect(cb).toHaveBeenCalledWith(2);
+      });
+
+      describe('when elements number is less than page size', () => {
+        beforeEach(() => {
+          isolatedScope.pageSize = 10;
+          isolatedScope.totalElements = 9;
+          scope.$digest();
+        });
+
+        it('should not be rendered', () => {
+          var pagination = element[0].getElementsByClassName('pagination');
+          expect(pagination.length).toEqual(0);
+        });
+      });
+    });
+  });
+})();
\ No newline at end of file
diff --git a/views/ngXosLib/xosHelpers/spec/ui/table.test.js b/views/ngXosLib/xosHelpers/spec/ui/table.test.js
new file mode 100644
index 0000000..50a5d72
--- /dev/null
+++ b/views/ngXosLib/xosHelpers/spec/ui/table.test.js
@@ -0,0 +1,200 @@
+/**
+ * © OpenCORD
+ *
+ * Created by teone on 3/24/16.
+ */
+
+(function () {
+  'use strict';
+
+  describe('The xos.helper module', function(){
+    describe('The xos-table component', () => {
+
+      beforeEach(module('xos.helpers'));
+
+      it('should throw an error if no config is specified', inject(($compile, $rootScope) => {
+        function errorFunctionWrapper(){
+          $compile(angular.element('<xos-table></xos-table>'))($rootScope);
+          $rootScope.$digest();
+        }
+        expect(errorFunctionWrapper).toThrow(new Error('[xosTable] Please provide a configuration via the "config" attribute'));
+      }));
+
+      it('should throw an error if no config columns are specified', inject(($compile, $rootScope) => {
+        function errorFunctionWrapper(){
+          // setup the parent scope
+          let scope = $rootScope.$new();
+          scope.config = 'green';
+          $compile(angular.element('<xos-table config="config"></xos-table>'))(scope);
+          $rootScope.$digest();
+        }
+        expect(errorFunctionWrapper).toThrow(new Error('[xosTable] Please provide a columns list in the configuration'));
+      }));
+
+      describe('when basicly configured', function() {
+        var scope, element, isolatedScope;
+
+        beforeEach(inject(function ($compile, $rootScope) {
+          scope = $rootScope.$new();
+
+          scope.config = {
+            columns: [
+              {
+                label: 'Label 1',
+                prop: 'label-1'
+              },
+              {
+                label: 'Label 2',
+                prop: 'label-2'
+              }
+            ]
+          };
+
+          scope.data = [
+            {
+              'label-1': 'Sample 1.1',
+              'label-2': 'Sample 1.2'
+            },
+            {
+              'label-1': 'Sample 2.1',
+              'label-2': 'Sample 2.2'
+            }
+          ]
+
+          element = angular.element('<xos-table config="config" data="data"></xos-table>');
+          $compile(element)(scope);
+          scope.$digest();
+          isolatedScope = element.isolateScope().vm;
+        }));
+
+        it('should contain 2 columns', function() {
+          var th = element[0].getElementsByTagName('th');
+          expect(th.length).toEqual(2);
+          expect(isolatedScope.columns.length).toEqual(2);
+        });
+
+        it('should contain 3 rows', function() {
+          var tr = element[0].getElementsByTagName('tr');
+          expect(tr.length).toEqual(3);
+        });
+
+        describe('when no data are provided', () => {
+          beforeEach(() => {
+            isolatedScope.data = [];
+            scope.$digest();
+          });
+          it('should render an alert', () => {
+            let alert = element[0].getElementsByClassName('alert');
+            let table = element[0].getElementsByTagName('table');
+            expect(alert.length).toEqual(1);
+            expect(table.length).toEqual(1);
+          });
+        });
+
+        describe('when actions are passed', () => {
+
+          let cb = jasmine.createSpy('callback')
+
+          beforeEach(() => {
+            isolatedScope.config.actions = [
+              {
+                label: 'delete',
+                icon: 'remove',
+                cb: cb,
+                color: 'red'
+              }
+            ];
+            scope.$digest();
+          });
+
+          it('should have 3 columns', () => {
+            var th = element[0].getElementsByTagName('th');
+            expect(th.length).toEqual(3);
+            expect(isolatedScope.columns.length).toEqual(2);
+          });
+
+          it('when clicking on action should invoke callback', () => {
+            var link = element[0].getElementsByTagName('a')[0];
+            link.click();
+            expect(cb).toHaveBeenCalledWith(scope.data[0]);
+          });
+        });
+
+        describe('when filter is fulltext', () => {
+          beforeEach(() => {
+            isolatedScope.config.filter = 'fulltext';
+            scope.$digest();
+          });
+
+          it('should render a text field', () => {
+            var textField = element[0].getElementsByTagName('input');
+            expect(textField.length).toEqual(1);
+          });
+
+          describe('and a value is enterd', () => {
+            beforeEach(() => {
+              isolatedScope.query = '2.2';
+              scope.$digest();
+            });
+
+            it('should contain 2 rows', function() {
+              var tr = element[0].getElementsByTagName('tr');
+              expect(tr.length).toEqual(2);
+            });
+          });
+        });
+
+        describe('when filter is field', () => {
+          beforeEach(() => {
+            isolatedScope.config.filter = 'field';
+            scope.$digest();
+          });
+
+          it('should render a text field for each column', () => {
+            var textField = element[0].getElementsByTagName('input');
+            expect(textField.length).toEqual(2);
+          });
+
+          describe('and a value is enterd', () => {
+            beforeEach(() => {
+              isolatedScope.query = {'label-1': '2.1'};
+              scope.$digest();
+            });
+
+            it('should contain 3 rows', function() {
+              var tr = element[0].getElementsByTagName('tr');
+              expect(tr.length).toEqual(3);
+            });
+          });
+        });
+
+        describe('when order is true', () => {
+          beforeEach(() => {
+            isolatedScope.config.order = true;
+            scope.$digest();
+          });
+
+          it('should render a arrows beside', () => {
+            var arrows = element[0].getElementsByTagName('i');
+            expect(arrows.length).toEqual(4);
+          });
+
+          describe('and an order is set', () => {
+            beforeEach(() => {
+              isolatedScope.orderBy = 'label-1';
+              isolatedScope.reverse = true;
+              scope.$digest();
+            });
+
+            it('should orderBy', function() {
+              var tr = element[0].getElementsByTagName('tr');
+              expect(angular.element(tr[1]).text()).toContain('Sample 2.2');
+              expect(angular.element(tr[2]).text()).toContain('Sample 1.1');
+            });
+          });
+        });
+      });
+    });
+  });
+})();
+
diff --git a/views/ngXosLib/xosHelpers/src/services/api.services.js b/views/ngXosLib/xosHelpers/src/services/api.services.js
deleted file mode 100644
index c7384f8..0000000
--- a/views/ngXosLib/xosHelpers/src/services/api.services.js
+++ /dev/null
@@ -1,32 +0,0 @@
-(function() {
-  'use strict';
-
-    angular
-        .module('xos.helpers')
-        .service('XosApi', xosApi)
-        .service('XoslibApi', xoslibApi)
-        .service('HpcApi', hpcApi);
-
-    var xosApiCache, xoslibApiCache, hpcApiCache;
-
-    function xosApi(xos) { 
-      if(!xosApiCache){
-        xosApiCache = new xos({domain: ''});
-      }
-      return xosApiCache;
-    }
-
-    function xoslibApi(xoslib) { 
-      if(!xoslibApiCache){
-        xoslibApiCache = new xoslib({domain: ''});
-      }
-      return xoslibApiCache;
-    }
-
-    function hpcApi(hpcapi) { 
-      if(!hpcApiCache){
-        hpcApiCache = new hpcapi({domain: ''});
-      }
-      return hpcApiCache;
-    }
-})();
diff --git a/views/ngXosLib/xosHelpers/src/services/csrfToken.interceptor.js b/views/ngXosLib/xosHelpers/src/services/csrfToken.interceptor.js
index 283e90d..5f87de5 100644
--- a/views/ngXosLib/xosHelpers/src/services/csrfToken.interceptor.js
+++ b/views/ngXosLib/xosHelpers/src/services/csrfToken.interceptor.js
@@ -1,18 +1,24 @@
 (function() {
-    'use strict';
+  'use strict';
 
-    angular
-        .module('xos.helpers')
-        .factory('SetCSRFToken', setCSRFToken);
+  /**
+  * @ngdoc service
+  * @name xos.helpers.SetCSRFToken
+  * @description This factory is automatically loaded trough xos.helpers and will add an $http interceptor that will the CSRF-Token to your request headers
+  **/
 
-    function setCSRFToken($cookies) {
-      return {
-        request: function(request){
-          if(request.method !== 'GET'){
-            request.headers['X-CSRFToken'] = $cookies.get('xoscsrftoken');
-          }
-          return request;
+  angular
+      .module('xos.helpers')
+      .factory('SetCSRFToken', setCSRFToken);
+
+  function setCSRFToken($cookies) {
+    return {
+      request: function(request){
+        if(request.method !== 'GET'){
+          request.headers['X-CSRFToken'] = $cookies.get('xoscsrftoken');
         }
-      };
-    }
+        return request;
+      }
+    };
+  }
 })();
diff --git a/views/ngXosLib/xosHelpers/src/services/noHyperlinks.interceptor.js b/views/ngXosLib/xosHelpers/src/services/noHyperlinks.interceptor.js
index 6059f99..af3dd63 100644
--- a/views/ngXosLib/xosHelpers/src/services/noHyperlinks.interceptor.js
+++ b/views/ngXosLib/xosHelpers/src/services/noHyperlinks.interceptor.js
@@ -1,18 +1,24 @@
 (function() {
-    'use strict';
+  'use strict';
 
-    angular
-        .module('xos.helpers')
-        .factory('NoHyperlinks', noHyperlinks);
+  /**
+  * @ngdoc service
+  * @name xos.helpers.NoHyperlinks
+  * @description This factory is automatically loaded trough xos.helpers and will add an $http interceptor that will add ?no_hyperlinks=1 to your api request, that is required by django
+  **/
 
-    function noHyperlinks() { 
-      return {
-        request: function(request){
-          if(request.url.indexOf('.html') === -1){
-            request.url += '?no_hyperlinks=1';
-          }
-          return request;
+  angular
+      .module('xos.helpers')
+      .factory('NoHyperlinks', noHyperlinks);
+
+  function noHyperlinks() {
+    return {
+      request: function(request){
+        if(request.url.indexOf('.html') === -1){
+          request.url += '?no_hyperlinks=1';
         }
-      };
-    }
+        return request;
+      }
+    };
+  }
 })();
\ No newline at end of file
diff --git a/views/ngXosLib/xosHelpers/src/services/rest/ONOS-Apps.js b/views/ngXosLib/xosHelpers/src/services/rest/ONOS-Apps.js
new file mode 100644
index 0000000..8d4c104
--- /dev/null
+++ b/views/ngXosLib/xosHelpers/src/services/rest/ONOS-Apps.js
@@ -0,0 +1,13 @@
+(function() {
+  'use strict';
+
+  angular.module('xos.helpers')
+  /**
+  * @ngdoc service
+  * @name xos.helpers.ONOS-App-Collection
+  * @description Angular resource to fetch /api/tenant/onos/app/
+  **/
+  .service('ONOS-App-Collection', function($resource){
+    return $resource('/api/tenant/onos/app/');
+  })
+})();
\ No newline at end of file
diff --git a/views/ngXosLib/xosHelpers/src/services/rest/ONOS-Services.js b/views/ngXosLib/xosHelpers/src/services/rest/ONOS-Services.js
new file mode 100644
index 0000000..19270e1
--- /dev/null
+++ b/views/ngXosLib/xosHelpers/src/services/rest/ONOS-Services.js
@@ -0,0 +1,13 @@
+(function() {
+  'use strict';
+
+  angular.module('xos.helpers')
+  /**
+  * @ngdoc service
+  * @name xos.helpers.ONOS-Services-Collection
+  * @description Angular resource to fetch /api/service/onos/
+  **/
+  .service('ONOS-Services-Collection', function($resource){
+    return $resource('/api/service/onos/');
+  })
+})();
\ No newline at end of file
diff --git a/views/ngXosLib/xosHelpers/src/services/rest/Subscribers.js b/views/ngXosLib/xosHelpers/src/services/rest/Subscribers.js
new file mode 100644
index 0000000..ebab252
--- /dev/null
+++ b/views/ngXosLib/xosHelpers/src/services/rest/Subscribers.js
@@ -0,0 +1,61 @@
+(function() {
+  'use strict';
+
+  angular.module('xos.helpers')
+  /**
+  * @ngdoc service
+  * @name xos.helpers.Subscribers
+  * @description Angular resource to fetch /api/tenant/cord/subscriber/:subscriber_id/
+  **/
+  .service('Subscribers', function($resource){
+    return $resource('/api/tenant/cord/subscriber/:subscriber_id/', { subscriber_id: '@id' });
+  })
+  /**
+  * @ngdoc service
+  * @name xos.helpers.Subscriber-features
+  * @description Angular resource to fetch /api/tenant/cord/subscriber/:subscriber_id/features/
+  **/
+  .service('Subscriber-features', function($resource){
+    return $resource('/api/tenant/cord/subscriber/:subscriber_id/features/', { subscriber_id: '@id' });
+  })
+  /**
+  * @ngdoc service
+  * @name xos.helpers.Subscriber-features-uplink_speed
+  * @description Angular resource to fetch /api/tenant/cord/subscriber/:subscriber_id/features/uplink_speed/
+  **/
+  .service('Subscriber-features-uplink_speed', function($resource){
+    return $resource('/api/tenant/cord/subscriber/:subscriber_id/features/uplink_speed/', { subscriber_id: '@id' });
+  })
+  /**
+  * @ngdoc service
+  * @name xos.helpers.Subscriber-features-downlink_speed
+  * @description Angular resource to fetch /api/tenant/cord/subscriber/:subscriber_id/features/downlink_speed/
+  **/
+  .service('Subscriber-features-downlink_speed', function($resource){
+    return $resource('/api/tenant/cord/subscriber/:subscriber_id/features/downlink_speed/', { subscriber_id: '@id' });
+  })
+  /**
+  * @ngdoc service
+  * @name xos.helpers.Subscriber-features-cdn
+  * @description Angular resource to fetch /api/tenant/cord/subscriber/:subscriber_id/features/cdn/
+  **/
+  .service('Subscriber-features-cdn', function($resource){
+    return $resource('/api/tenant/cord/subscriber/:subscriber_id/features/cdn/', { subscriber_id: '@id' });
+  })
+  /**
+  * @ngdoc service
+  * @name xos.helpers.Subscriber-features-uverse
+  * @description Angular resource to fetch /api/tenant/cord/subscriber/:subscriber_id/features/uverse/
+  **/
+  .service('Subscriber-features-uverse', function($resource){
+    return $resource('/api/tenant/cord/subscriber/:subscriber_id/features/uverse/', { subscriber_id: '@id' });
+  })
+  /**
+  * @ngdoc service
+  * @name xos.helpers.Subscriber-features-status
+  * @description Angular resource to fetch /api/tenant/cord/subscriber/:subscriber_id/features/status/
+  **/
+  .service('Subscriber-features-status', function($resource){
+    return $resource('/api/tenant/cord/subscriber/:subscriber_id/features/status/', { subscriber_id: '@id' });
+  })
+})();
\ No newline at end of file
diff --git a/views/ngXosLib/xosHelpers/src/services/rest/Truckroll.js b/views/ngXosLib/xosHelpers/src/services/rest/Truckroll.js
new file mode 100644
index 0000000..9927967
--- /dev/null
+++ b/views/ngXosLib/xosHelpers/src/services/rest/Truckroll.js
@@ -0,0 +1,13 @@
+(function() {
+  'use strict';
+
+  angular.module('xos.helpers')
+  /**
+  * @ngdoc service
+  * @name xos.helpers.Truckroll-Collection
+  * @description Angular resource to fetch /api/tenant/truckroll/:truckroll_id/
+  **/
+  .service('Truckroll-Collection', function($resource){
+    return $resource('/api/tenant/truckroll/:truckroll_id/', { truckroll_id: '@id' });
+  })
+})();
\ No newline at end of file
diff --git a/views/ngXosLib/xosHelpers/src/services/rest/Users.js b/views/ngXosLib/xosHelpers/src/services/rest/Users.js
new file mode 100644
index 0000000..deb18a4
--- /dev/null
+++ b/views/ngXosLib/xosHelpers/src/services/rest/Users.js
@@ -0,0 +1,13 @@
+(function() {
+  'use strict';
+
+  angular.module('xos.helpers')
+  /**
+  * @ngdoc service
+  * @name xos.helpers.Users
+  * @description Angular resource to fetch /api/core/users/
+  **/
+  .service('Users', function($resource){
+    return $resource('/api/core/users/');
+  })
+})();
\ No newline at end of file
diff --git a/views/ngXosLib/xosHelpers/src/services/rest/vOLT.js b/views/ngXosLib/xosHelpers/src/services/rest/vOLT.js
new file mode 100644
index 0000000..f182c06
--- /dev/null
+++ b/views/ngXosLib/xosHelpers/src/services/rest/vOLT.js
@@ -0,0 +1,13 @@
+(function() {
+  'use strict';
+
+  angular.module('xos.helpers')
+  /**
+  * @ngdoc service
+  * @name xos.helpers.vOLT-Collection
+  * @description Angular resource to fetch /api/tenant/cord/volt/:volt_id/
+  **/
+  .service('vOLT-Collection', function($resource){
+    return $resource('/api/tenant/cord/volt/:volt_id/', { volt_id: '@id' });
+  })
+})();
\ No newline at end of file
diff --git a/views/ngXosLib/xosHelpers/src/services/rest/vSG.js b/views/ngXosLib/xosHelpers/src/services/rest/vSG.js
new file mode 100644
index 0000000..121b5a3
--- /dev/null
+++ b/views/ngXosLib/xosHelpers/src/services/rest/vSG.js
@@ -0,0 +1,13 @@
+(function() {
+  'use strict';
+
+  angular.module('xos.helpers')
+  /**
+  * @ngdoc service
+  * @name xos.helpers.vSG-Collection
+  * @description Angular resource to fetch /api/service/vsg/
+  **/
+  .service('vSG-Collection', function($resource){
+    return $resource('/api/service/vsg/');
+  })
+})();
\ No newline at end of file
diff --git a/views/ngXosLib/xosHelpers/src/ui_components/table/alert.component.js b/views/ngXosLib/xosHelpers/src/ui_components/table/alert.component.js
new file mode 100644
index 0000000..7d7a026
--- /dev/null
+++ b/views/ngXosLib/xosHelpers/src/ui_components/table/alert.component.js
@@ -0,0 +1,145 @@
+/**
+ * © OpenCORD
+ *
+ * Visit http://guide.xosproject.org/devguide/addview/ for more information
+ *
+ * Created by teone on 4/15/16.
+ */
+
+(function () {
+  'use strict';
+
+  angular.module('xos.uiComponents')
+
+  /**
+    * @ngdoc directive
+    * @name xos.uiComponents.directive:xosAlert
+    * @restrict E
+    * @description The xos-alert directive
+    * @param {Object} config The configuration object
+    * ```
+    * {
+    *   type: 'danger', //info, success, warning
+    *   closeBtn: true, //default false
+    *   autoHide: 3000 //delay to automatically hide the alert
+    * }
+    * ```
+    * @param {Boolean=} show Binding to show and hide the alert, default to true
+    * @element ANY
+    * @scope
+    * @example
+  <example module="sampleAlert1">
+    <file name="index.html">
+      <div ng-controller="SampleCtrl1 as vm">
+        <xos-alert config="vm.config1">
+          A sample alert message
+        </xos-alert>
+        <xos-alert config="vm.config2">
+          A sample alert message (with close button)
+        </xos-alert>
+        <xos-alert config="vm.config3">
+          A sample info message
+        </xos-alert>
+        <xos-alert config="vm.config4">
+          A sample success message
+        </xos-alert>
+        <xos-alert config="vm.config5">
+          A sample warning message
+        </xos-alert>
+      </div>
+    </file>
+    <file name="script.js">
+      angular.module('sampleAlert1', ['xos.uiComponents'])
+      .controller('SampleCtrl1', function(){
+        this.config1 = {
+          type: 'danger'
+        };
+
+        this.config2 = {
+          type: 'danger',
+          closeBtn: true
+        };
+
+        this.config3 = {
+          type: 'info'
+        };
+
+        this.config4 = {
+          type: 'success'
+        };
+
+        this.config5 = {
+          type: 'warning'
+        };
+      });
+    </file>
+  </example>
+
+  <example module="sampleAlert2">
+    <file name="index.html">
+      <div ng-controller="SampleCtrl as vm" class="row">
+        <div class="col-sm-4">
+          <a class="btn btn-default btn-block" ng-show="!vm.show" ng-click="vm.show = true">Show Alert</a>
+          <a class="btn btn-default btn-block" ng-show="vm.show" ng-click="vm.show = false">Hide Alert</a>
+        </div>
+        <div class="col-sm-8">
+          <xos-alert config="vm.config1" show="vm.show">
+            A sample alert message, not displayed by default.
+          </xos-alert>
+        </div>
+      </div>
+    </file>
+    <file name="script.js">
+      angular.module('sampleAlert2', ['xos.uiComponents'])
+      .controller('SampleCtrl', function(){
+        this.config1 = {
+          type: 'success'
+        };
+
+        this.show = false;
+      });
+    </file>
+  </example>
+  **/
+
+  .directive('xosAlert', function(){
+    return {
+      restrict: 'E',
+      scope: {
+        config: '=',
+        show: '=?'
+      },
+      template: `
+        <div class="alert alert-{{vm.config.type}}" ng-show="vm.show">
+          <button type="button" class="close" ng-if="vm.config.closeBtn" ng-click="vm.dismiss()">
+            <span aria-hidden="true">&times;</span>
+          </button>
+          <p ng-transclude></p>
+        </div>
+      `,
+      transclude: true,
+      bindToController: true,
+      controllerAs: 'vm',
+      controller: function($timeout){
+
+        if(!this.config){
+          throw new Error('[xosAlert] Please provide a configuration via the "config" attribute');
+        }
+
+        // default the value to true
+        this.show = this.show !== false;
+        
+        this.dismiss = () => {
+          this.show = false;
+        }
+
+        if(this.config.autoHide){
+          let to = $timeout(() => {
+            this.dismiss();
+            $timeout.cancel(to);
+          }, this.config.autoHide);
+        }
+      }
+    }
+  })
+})();
diff --git a/views/ngXosLib/xosHelpers/src/ui_components/table/pagination.component.js b/views/ngXosLib/xosHelpers/src/ui_components/table/pagination.component.js
new file mode 100644
index 0000000..b7b1701
--- /dev/null
+++ b/views/ngXosLib/xosHelpers/src/ui_components/table/pagination.component.js
@@ -0,0 +1,122 @@
+/**
+ * © OpenCORD
+ *
+ * Visit http://guide.xosproject.org/devguide/addview/ for more information
+ *
+ * Created by teone on 4/15/16.
+ */
+
+(function () {
+  'use strict';
+
+  angular.module('xos.uiComponents')
+
+  /**
+    * @ngdoc directive
+    * @name xos.uiComponents.directive:xosPagination
+    * @restrict E
+    * @description The xos-table directive
+    * @param {Number} pageSize Number of elements per page
+    * @param {Number} totalElements Number of total elements in the collection
+    * @param {Function} change The callback to be triggered on page change.
+    * * @element ANY
+    * @scope
+    * @example
+  <example module="samplePagination">
+    <file name="index.html">
+      <div ng-controller="SampleCtrl1 as vm">
+        <xos-pagination
+          page-size="vm.pageSize"
+          total-elements="vm.totalElements"
+          change="vm.change">
+        </xos-pagination>
+      </div>
+    </file>
+    <file name="script.js">
+      angular.module('samplePagination', ['xos.uiComponents'])
+      .controller('SampleCtrl1', function(){
+        this.pageSize = 10;
+        this.totalElements = 35;
+        this.change = (pageNumber) => {
+          console.log(pageNumber);
+        }
+      });
+    </file>
+  </example>
+  **/
+
+  .directive('xosPagination', function(){
+    return {
+      restrict: 'E',
+      scope: {
+        pageSize: '=',
+        totalElements: '=',
+        change: '='
+      },
+      template: `
+        <div class="row" ng-if="vm.pageList.length > 1">
+          <div class="col-xs-12 text-center">
+            <ul class="pagination">
+              <li
+                ng-click="vm.goToPage(vm.currentPage - 1)"
+                ng-class="{disabled: vm.currentPage == 0}">
+                <a href="" aria-label="Previous">
+                    <span aria-hidden="true">&laquo;</span>
+                </a>
+              </li>
+              <li ng-repeat="i in vm.pageList" ng-class="{active: i === vm.currentPage}">
+                <a href="" ng-click="vm.goToPage(i)">{{i + 1}}</a>
+              </li>
+              <li
+                ng-click="vm.goToPage(vm.currentPage + 1)"
+                ng-class="{disabled: vm.currentPage == vm.pages - 1}">
+                <a href="" aria-label="Next">
+                    <span aria-hidden="true">&raquo;</span>
+                </a>
+              </li>
+            </ul>
+          </div>
+        </div>
+      `,
+      bindToController: true,
+      controllerAs: 'vm',
+      controller: function($scope){
+        
+        this.currentPage = 0;
+
+        this.goToPage = (n) => {
+          if(n < 0 || n === this.pages){
+            return;
+          }
+          this.currentPage = n;
+          this.change(n);
+        }
+
+        this.createPages = (pages) => {
+          let arr = [];
+          for(var i = 0; i < pages; i++){
+            arr.push(i);
+          }
+          return arr;
+        }
+
+        // watch for data changes
+        $scope.$watch(() => this.totalElements, () => {
+          if(this.totalElements){
+            this.pages = Math.ceil(this.totalElements / this.pageSize);
+            this.pageList = this.createPages(this.pages);
+          }
+        });
+      }
+    }
+  })
+  .filter('pagination', function(){
+    return function(input, start) {
+      if(!input || !angular.isArray(input)){
+        return input;
+      }
+      start = parseInt(start, 10);
+      return input.slice(start);
+    };
+  });
+})();
diff --git a/views/ngXosLib/xosHelpers/src/ui_components/table/table.component.js b/views/ngXosLib/xosHelpers/src/ui_components/table/table.component.js
index faeaa26..daca484 100644
--- a/views/ngXosLib/xosHelpers/src/ui_components/table/table.component.js
+++ b/views/ngXosLib/xosHelpers/src/ui_components/table/table.component.js
@@ -9,33 +9,276 @@
 (function () {
   'use strict';
 
-  angular.module('xos.uiComponents.table', [])
+  angular.module('xos.uiComponents')
+
+    /**
+    * @ngdoc directive
+    * @name xos.uiComponents.directive:xosTable
+    * @restrict E
+    * @description The xos-table directive
+    * @param {Object} config The configuration for the component.
+    * ```
+    * {
+    *   columns: [
+    *     {
+    *       label: 'Human readable name',
+    *       prop: 'Property to read in the model object'
+    *     }
+    *   ],
+    *   classes: 'table table-striped table-bordered',
+    *   actions: [ // if defined add an action column
+          {
+            label: 'delete',
+            icon: 'remove', // refers to bootstraps glyphicon
+            cb: (user) => { // receive the model
+              console.log(user);
+            },
+            color: 'red'
+          }
+        ],
+        filter: 'field', // can be by `field` or `fulltext`
+        order: true // whether to show ordering arrows
+    * }
+    * ```
+    * @param {Array} data The data that should be rendered
+    * @element ANY
+    * @scope
+    * @example
+
+  <example module="sampleTable1">
+    <file name="index.html">
+      <div ng-controller="SampleCtrl1 as vm">
+        <xos-table data="vm.data" config="vm.config"></xos-table>
+      </div>
+    </file>
+    <file name="script.js">
+      angular.module('sampleTable1', ['xos.uiComponents'])
+      .controller('SampleCtrl1', function(){
+        this.config = {
+          columns: [
+            {
+              label: 'First Name', // column title
+              prop: 'name' // property to read in the data array
+            },
+            {
+              label: 'Last Name',
+              prop: 'lastname'
+            }
+          ]
+        };
+
+        this.data = [
+          {
+            name: 'John',
+            lastname: 'Doe'
+          },
+          {
+            name: 'Gili',
+            lastname: 'Fereydoun'
+          }
+        ]
+      });
+    </file>
+  </example>
+
+  <example module="sampleTable2">
+    <file name="index.html">
+      <div ng-controller="SampleCtrl2 as vm">
+        <xos-table data="vm.data" config="vm.config"></xos-table>
+      </div>
+    </file>
+    <file name="script.js">
+      angular.module('sampleTable2', ['xos.uiComponents'])
+      .controller('SampleCtrl2', function(){
+        this.config = {
+          columns: [
+            {
+              label: 'First Name', // column title
+              prop: 'name' // property to read in the data array
+            },
+            {
+              label: 'Last Name',
+              prop: 'lastname'
+            }
+          ],
+          classes: 'table table-striped table-condensed', // table classes, default to `table table-striped table-bordered`
+          actions: [ // if defined add an action column
+            {
+              label: 'delete', // label
+              icon: 'remove', // icons, refers to bootstraps glyphicon
+              cb: (user) => { // callback, get feeded with the full object
+                console.log(user);
+              },
+              color: 'red' // icon color
+            }
+          ],
+          filter: 'field', // can be by `field` or `fulltext`
+          order: true
+        };
+
+        this.data = [
+          {
+            name: 'John',
+            lastname: 'Doe'
+          },
+          {
+            name: 'Gili',
+            lastname: 'Fereydoun'
+          }
+        ]
+      });
+    </file>
+  </example>
+
+  <example module="sampleTable3">
+    <file name="index.html">
+      <div ng-controller="SampleCtrl3 as vm">
+        <xos-table data="vm.data" config="vm.config"></xos-table>
+      </div>
+    </file>
+    <file name="script.js">
+      angular.module('sampleTable3', ['xos.uiComponents'])
+      .controller('SampleCtrl3', function(){
+        this.config = {
+          columns: [
+            {
+              label: 'First Name', // column title
+              prop: 'name' // property to read in the data array
+            },
+            {
+              label: 'Last Name',
+              prop: 'lastname'
+            }
+          ],
+          pagination: {
+            pageSize: 2
+          }
+        };
+
+        this.data = [
+          {
+            name: 'John',
+            lastname: 'Doe'
+          },
+          {
+            name: 'Gili',
+            lastname: 'Fereydoun'
+          },
+          {
+            name: 'Lucky',
+            lastname: 'Clarkson'
+          },
+          {
+            name: 'Tate',
+            lastname: 'Spalding'
+          }
+        ]
+      });
+    </file>
+  </example>
+    **/
+
     .directive('xosTable', function(){
       return {
         restrict: 'E',
         scope: {
           data: '=',
-          columns: '='
+          config: '='
         },
-        template: [
-          '<!--<pre>{{vm.data | json}}</pre>-->',
-          '<table class="table table-striped" ng-show="vm.data.length > 0">',
-            '<thead>',
-              '<tr>',
-                '<th ng-repeat="col in vm.columns">{{col}}</th>',
-              '</tr>',
-            '</thead>',
-            '<tbody>',
-              '<tr ng-repeat="item in vm.data">',
-                '<td ng-repeat="col in vm.columns">{{item[col]}}</td>',
-              '</tr>',
-            '</tbody>',
-          '</table>'
-        ].join(),
+        template: `
+          <div ng-show="vm.data.length > 0">
+            <div class="row" ng-if="vm.config.filter == 'fulltext'">
+              <div class="col-xs-12">
+                <input
+                  class="form-control"
+                  placeholder="Type to search.."
+                  type="text"
+                  ng-model="vm.query"/>
+              </div>
+            </div>
+            <table ng-class="vm.classes" ng-show="vm.data.length > 0">
+              <thead>
+                <tr>
+                  <th ng-repeat="col in vm.columns">
+                    {{col.label}}
+                    <span ng-if="vm.config.order">
+                      <a href="" ng-click="vm.orderBy = col.prop; vm.reverse = false">
+                        <i class="glyphicon glyphicon-chevron-up"></i>
+                      </a>
+                      <a href="" ng-click="vm.orderBy = col.prop; vm.reverse = true">
+                        <i class="glyphicon glyphicon-chevron-down"></i>
+                      </a>
+                    </span>
+                  </th>
+                  <th ng-if="vm.config.actions">Actions</th>
+                </tr>
+              </thead>
+              <tbody ng-if="vm.config.filter == 'field'">
+                <tr>
+                  <td ng-repeat="col in vm.columns">
+                    <input
+                      class="form-control"
+                      placeholder="Type to search by {{col.label}}"
+                      type="text"
+                      ng-model="vm.query[col.prop]"/>
+                  </td>
+                  <td ng-if="vm.config.actions"></td>
+                </tr>
+              </tbody>
+              <tbody>
+                <tr ng-repeat="item in vm.data | filter:vm.query | orderBy:vm.orderBy:vm.reverse | pagination:vm.currentPage * vm.config.pagination.pageSize | limitTo: (vm.config.pagination.pageSize || vm.data.length) track by $index">
+                  <td ng-repeat="col in vm.columns">{{item[col.prop]}}</td>
+                  <td ng-if="vm.config.actions">
+                    <a href=""
+                      ng-repeat="action in vm.config.actions"
+                      ng-click="action.cb(item)"
+                      title="{{action.label}}">
+                      <i
+                        class="glyphicon glyphicon-{{action.icon}}"
+                        style="color: {{action.color}};"></i>
+                    </a>
+                  </td>
+                </tr>
+              </tbody>
+            </table>
+            <xos-pagination
+              ng-if="vm.config.pagination"
+              page-size="vm.config.pagination.pageSize"
+              total-elements="vm.data.length"
+              change="vm.goToPage">
+              </xos-pagination>
+          </div>
+          <div ng-show="vm.data.length == 0 || !vm.data">
+            <div class="alert alert-info">
+              No data to show.
+            </div>
+          </div>
+        `,
         bindToController: true,
         controllerAs: 'vm',
         controller: function(){
-          console.log(this.data, this.columns);
+
+          if(!this.config){
+            throw new Error('[xosTable] Please provide a configuration via the "config" attribute');
+          }
+
+          if(!this.config.columns){
+            throw new Error('[xosTable] Please provide a columns list in the configuration');
+          }
+
+          this.columns = this.config.columns;
+          this.classes = this.config.classes || 'table table-striped table-bordered';
+
+          if(this.config.actions){
+            // TODO validate action format
+          }
+          if(this.config.pagination){
+            this.currentPage = 0;
+            this.goToPage = (n) => {
+              this.currentPage = n;
+            };
+          }
+
         }
       }
     })
diff --git a/views/ngXosLib/xosHelpers/src/ui_components/ui-components.module.js b/views/ngXosLib/xosHelpers/src/ui_components/ui-components.module.js
index 0cbe70e..bd07908 100644
--- a/views/ngXosLib/xosHelpers/src/ui_components/ui-components.module.js
+++ b/views/ngXosLib/xosHelpers/src/ui_components/ui-components.module.js
@@ -9,7 +9,12 @@
 (function () {
   'use strict';
 
+  /**
+  * @ngdoc overview
+  * @name xos.uiComponents
+  * @description A collection of UI components useful for Dashboard development
+  **/
+
   angular.module('xos.uiComponents', [
-    'xos.uiComponents.table'
   ])
 })();
diff --git a/views/ngXosLib/xosHelpers/src/xosHelpers.module.js b/views/ngXosLib/xosHelpers/src/xosHelpers.module.js
index c4b40f1..d7cd958 100644
--- a/views/ngXosLib/xosHelpers/src/xosHelpers.module.js
+++ b/views/ngXosLib/xosHelpers/src/xosHelpers.module.js
@@ -1,36 +1,39 @@
 (function() {
-    'use strict';
-    console.log('XOS Helpers Module')
-    angular.module('bugSnag', []).factory('$exceptionHandler', function () {
-      return function (exception, cause) {
-        if( window.Bugsnag ){
-          Bugsnag.notifyException(exception, {diagnostics:{cause: cause}});
-        }
-        else{
-          console.error(exception, cause, exception.stack);
-        }
-      };
-    });
+  'use strict';
+  
+  angular.module('bugSnag', []).factory('$exceptionHandler', function () {
+    return function (exception, cause) {
+      if( window.Bugsnag ){
+        Bugsnag.notifyException(exception, {diagnostics: {cause: cause}});
+      }
+      else{
+        console.error(exception, cause, exception.stack);
+      }
+    };
+  });
 
-    angular
-        .module('xos.helpers',[
-          'ngCookies',
-          'ngResource',
-          'xos.xos',
-          'xos.hpcapi',
-          'xos.xoslib',
-          'bugSnag',
-          'xos.uiComponents'
-        ])
-        .config(config);
+  /**
+  * @ngdoc overview
+  * @name xos.helpers
+  * @description this is the module that group all the helpers service and components for XOS
+  **/
 
-    function config($httpProvider, $interpolateProvider, $resourceProvider) {
-      $httpProvider.interceptors.push('SetCSRFToken');
+  angular
+      .module('xos.helpers', [
+        'ngCookies',
+        'ngResource',
+        'bugSnag',
+        'xos.uiComponents'
+      ])
+      .config(config);
 
-      $interpolateProvider.startSymbol('{$');
-      $interpolateProvider.endSymbol('$}');
+  function config($httpProvider, $interpolateProvider, $resourceProvider) {
+    $httpProvider.interceptors.push('SetCSRFToken');
 
-      // NOTE http://www.masnun.com/2013/09/18/django-rest-framework-angularjs-resource-trailing-slash-problem.html
-      $resourceProvider.defaults.stripTrailingSlashes = false;
-    }
+    $interpolateProvider.startSymbol('{$');
+    $interpolateProvider.endSymbol('$}');
+
+    // NOTE http://www.masnun.com/2013/09/18/django-rest-framework-angularjs-resource-trailing-slash-problem.html
+    $resourceProvider.defaults.stripTrailingSlashes = false;
+  }
 })();
\ No newline at end of file
diff --git a/views/ngXosViews/diagnostic/gulp/build.js b/views/ngXosViews/diagnostic/gulp/build.js
index c9cd9cb..1a1be80 100644
--- a/views/ngXosViews/diagnostic/gulp/build.js
+++ b/views/ngXosViews/diagnostic/gulp/build.js
@@ -27,6 +27,13 @@
 var mqpacker = require('css-mqpacker');
 var csswring = require('csswring');
 
+const TEMPLATE_FOOTER = `
+angular.module('xos.diagnostic')
+.run(['$location', function(a){
+  a.path('/');
+}])
+`
+
 module.exports = function(options){
   
   // delete previous builded file
@@ -69,8 +76,8 @@
     .pipe(angularFilesort())
     .pipe(concat('xosDiagnostic.js'))
     .pipe(concat.header('//Autogenerated, do not edit!!!\n'))
-    // .pipe(concat.footer(TEMPLATE_FOOTER))
-    // .pipe(uglify())
+    .pipe(concat.footer(TEMPLATE_FOOTER))
+    .pipe(uglify())
     .pipe(gulp.dest(options.static + 'js/'));
   });
 
diff --git a/views/ngXosViews/sampleView/.bowerrc b/views/ngXosViews/sampleView/.bowerrc
new file mode 100644
index 0000000..e491038
--- /dev/null
+++ b/views/ngXosViews/sampleView/.bowerrc
@@ -0,0 +1,3 @@
+{
+  "directory": "src/vendor/"
+}
\ No newline at end of file
diff --git a/views/ngXosViews/sampleView/.eslintrc b/views/ngXosViews/sampleView/.eslintrc
new file mode 100644
index 0000000..c852748
--- /dev/null
+++ b/views/ngXosViews/sampleView/.eslintrc
@@ -0,0 +1,42 @@
+{
+    "ecmaFeatures": {
+        "blockBindings": true,
+        "forOf": true,
+        "destructuring": true,
+        "arrowFunctions": true,
+        "templateStrings": true
+    },
+    "env": { 
+        "browser": true,
+        "node": true,
+        "es6": true
+    },
+    "plugins": [
+        //"angular"
+    ],
+    "rules": {
+        "quotes": [2, "single"],
+        "camelcase": [1, {"properties": "always"}],
+        "no-underscore-dangle": 1,
+        "eqeqeq": [2, "smart"],
+        "no-alert": 1,
+        "key-spacing": [1, { "beforeColon": false, "afterColon": true }],
+        "indent": [2, 2],
+        "no-irregular-whitespace": 1,
+        "eol-last": 0,
+        "max-nested-callbacks": [2, 4],
+        "comma-spacing": [1, {"before": false, "after": true}],
+        "no-trailing-spaces": [1, { skipBlankLines: true }],
+        "no-unused-vars": [1, {"vars": "all", "args": "after-used"}],
+        "new-cap": 0,
+
+        //"angular/ng_module_name": [2, '/^xos\.*[a-z]*$/'],
+        //"angular/ng_controller_name": [2, '/^[a-z].*Ctrl$/'],
+        //"angular/ng_service_name": [2, '/^[A-Z].*Service$/'],
+        //"angular/ng_directive_name": [2, '/^[a-z]+[[A-Z].*]*$/'],
+        //"angular/ng_di": [0, "function or array"]
+    },
+    "globals" :{
+        "angular": true
+    } 
+}
\ No newline at end of file
diff --git a/views/ngXosViews/sampleView/.gitignore b/views/ngXosViews/sampleView/.gitignore
new file mode 100644
index 0000000..567aee4
--- /dev/null
+++ b/views/ngXosViews/sampleView/.gitignore
@@ -0,0 +1,6 @@
+dist/
+src/vendor
+.tmp
+node_modules
+npm-debug.log
+dist/
\ No newline at end of file
diff --git a/views/ngXosViews/sampleView/bower.json b/views/ngXosViews/sampleView/bower.json
new file mode 100644
index 0000000..cc5fc64
--- /dev/null
+++ b/views/ngXosViews/sampleView/bower.json
@@ -0,0 +1,29 @@
+{
+  "name": "xos-sampleView",
+  "version": "0.0.0",
+  "authors": [
+    "Matteo Scandolo <teo@onlab.us>"
+  ],
+  "description": "The sampleView view",
+  "license": "MIT",
+  "ignore": [
+    "**/.*",
+    "node_modules",
+    "bower_components",
+    "static/js/vendor/",
+    "test",
+    "tests"
+  ],
+  "dependencies": {
+  },
+  "devDependencies": {
+    "jquery": "2.1.4",
+    "angular-mocks": "1.4.7",
+    "angular": "1.4.7",
+    "angular-ui-router": "0.2.15",
+    "angular-cookies": "1.4.7",
+    "angular-resource": "1.4.7",
+    "ng-lodash": "0.3.0",
+    "bootstrap-css": "3.3.6"
+  }
+}
diff --git a/views/ngXosViews/sampleView/env/default.js b/views/ngXosViews/sampleView/env/default.js
new file mode 100644
index 0000000..353973a
--- /dev/null
+++ b/views/ngXosViews/sampleView/env/default.js
@@ -0,0 +1,13 @@
+// This is a default configuration for your development environment.
+// You can duplicate this configuration for any of your Backend Environments.
+// Different configurations are loaded setting a NODE_ENV variable that contain the config file name.
+// `NODE_ENV=local npm start`
+//
+// If xoscsrftoken or xossessionid are not specified the browser value are used
+// (works only for local environment as both application are served on the same domain)
+
+module.exports = {
+  host: 'http://xos.dev:9999/',
+  xoscsrftoken: 'jkKF9NebQoyaxKFT42l1EFjGx6ESPuP4',
+  xossessionid: 'kvq9qxycbu0298wxfg8v11at94q9m819'
+};
diff --git a/views/ngXosViews/sampleView/gulp/build.js b/views/ngXosViews/sampleView/gulp/build.js
new file mode 100644
index 0000000..36534fb
--- /dev/null
+++ b/views/ngXosViews/sampleView/gulp/build.js
@@ -0,0 +1,163 @@
+'use strict';
+
+// BUILD
+//
+// The only purpose of this gulpfile is to build a XOS view and copy the correct files into
+// .html => dashboards
+// .js (minified and concat) => static/js
+//
+// The template are parsed and added to js with angular $templateCache
+
+var gulp = require('gulp');
+var ngAnnotate = require('gulp-ng-annotate');
+var uglify = require('gulp-uglify');
+var templateCache = require('gulp-angular-templatecache');
+var runSequence = require('run-sequence');
+var concat = require('gulp-concat-util');
+var del = require('del');
+var wiredep = require('wiredep');
+var angularFilesort = require('gulp-angular-filesort');
+var _ = require('lodash');
+var eslint = require('gulp-eslint');
+var inject = require('gulp-inject');
+var rename = require('gulp-rename');
+var replace = require('gulp-replace');
+var postcss = require('gulp-postcss');
+var autoprefixer = require('autoprefixer');
+var mqpacker = require('css-mqpacker');
+var csswring = require('csswring');
+
+const TEMPLATE_FOOTER = `
+angular.module('xos.sampleView')
+.run(['$location', function(a){
+  a.path('/');
+}])
+`
+
+module.exports = function(options){
+  
+  // delete previous builded file
+  gulp.task('clean', function(){
+    return del(
+      [
+        options.dashboards + 'xosSampleView.html',
+        options.static + 'css/xosSampleView.css'
+      ],
+      {force: true}
+    );
+  });
+
+  // minify css
+  gulp.task('css', function () {
+    var processors = [
+      autoprefixer({browsers: ['last 1 version']}),
+      mqpacker,
+      csswring
+    ];
+
+    gulp.src([
+      `${options.css}**/*.css`,
+      `!${options.css}dev.css`
+    ])
+    .pipe(postcss(processors))
+    .pipe(gulp.dest(options.tmp + '/css/'));
+  });
+
+  // copy css in correct folder
+  gulp.task('copyCss', ['wait'], function(){
+    return gulp.src([`${options.tmp}/css/*.css`])
+    .pipe(concat('xosSampleView.css'))
+    .pipe(gulp.dest(options.static + 'css/'))
+  });
+
+  // compile and minify scripts
+  gulp.task('scripts', function() {
+    return gulp.src([
+      options.tmp + '**/*.js'
+    ])
+    .pipe(ngAnnotate())
+    .pipe(angularFilesort())
+    .pipe(concat('xosSampleView.js'))
+    .pipe(concat.header('//Autogenerated, do not edit!!!\n'))
+    .pipe(concat.footer(TEMPLATE_FOOTER))
+    .pipe(uglify())
+    .pipe(gulp.dest(options.static + 'js/'));
+  });
+
+  // set templates in cache
+  gulp.task('templates', function(){
+    return gulp.src('./src/templates/*.html')
+      .pipe(templateCache({
+        module: 'xos.sampleView',
+        root: 'templates/'
+      }))
+      .pipe(gulp.dest(options.tmp));
+  });
+
+  // copy html index to Django Folder
+  gulp.task('copyHtml', function(){
+    return gulp.src(options.src + 'index.html')
+      // remove dev dependencies from html
+      .pipe(replace(/<!-- bower:css -->(\n.*)*\n<!-- endbower --><!-- endcss -->/, ''))
+      .pipe(replace(/<!-- bower:js -->(\n.*)*\n<!-- endbower --><!-- endjs -->/, ''))
+      // injecting minified files
+      .pipe(
+        inject(
+          gulp.src([
+            options.static + 'js/vendor/xosSampleViewVendor.js',
+            options.static + 'js/xosSampleView.js',
+            options.static + 'css/xosSampleView.css'
+          ]),
+          {ignorePath: '/../../../xos/core/xoslib'}
+        )
+      )
+      .pipe(rename('xosSampleView.html'))
+      .pipe(gulp.dest(options.dashboards));
+  });
+
+  // minify vendor js files
+  gulp.task('wiredep', function(){
+    var bowerDeps = wiredep().js;
+    if(!bowerDeps){
+      return;
+    }
+
+    // remove angular (it's already loaded)
+    _.remove(bowerDeps, function(dep){
+      return dep.indexOf('angular/angular.js') !== -1;
+    });
+
+    return gulp.src(bowerDeps)
+      .pipe(concat('xosSampleViewVendor.js'))
+      .pipe(uglify())
+      .pipe(gulp.dest(options.static + 'js/vendor/'));
+  });
+
+  gulp.task('lint', function () {
+    return gulp.src(['src/js/**/*.js'])
+      .pipe(eslint())
+      .pipe(eslint.format())
+      .pipe(eslint.failAfterError());
+  });
+
+  gulp.task('wait', function (cb) {
+    // setTimeout could be any async task
+    setTimeout(function () {
+      cb();
+    }, 1000);
+  });
+
+  gulp.task('build', function() {
+    runSequence(
+      'clean',
+      'templates',
+      'babel',
+      'scripts',
+      'wiredep',
+      'css',
+      'copyCss',
+      'copyHtml',
+      'cleanTmp'
+    );
+  });
+};
\ No newline at end of file
diff --git a/views/ngXosViews/sampleView/gulp/server.js b/views/ngXosViews/sampleView/gulp/server.js
new file mode 100644
index 0000000..3c6a8e3
--- /dev/null
+++ b/views/ngXosViews/sampleView/gulp/server.js
@@ -0,0 +1,192 @@
+'use strict';
+
+var gulp = require('gulp');
+var browserSync = require('browser-sync').create();
+var inject = require('gulp-inject');
+var es = require('event-stream');
+var runSequence = require('run-sequence');
+var angularFilesort = require('gulp-angular-filesort');
+var babel = require('gulp-babel');
+var wiredep = require('wiredep').stream;
+var httpProxy = require('http-proxy');
+var del = require('del');
+var sass = require('gulp-sass');
+var debug = require('gulp-debug');
+
+const environment = process.env.NODE_ENV;
+
+if (environment){
+  var conf = require(`../env/${environment}.js`);
+}
+else{
+  var conf = require('../env/default.js')
+}
+
+var proxy = httpProxy.createProxyServer({
+  target: conf.host || 'http://0.0.0.0:9999'
+});
+
+
+proxy.on('error', function(error, req, res) {
+  res.writeHead(500, {
+    'Content-Type': 'text/plain'
+  });
+
+  console.error('[Proxy]', error);
+});
+
+module.exports = function(options){
+  // open in browser with sync and proxy to 0.0.0.0
+  gulp.task('browser', function() {
+    console.log(options.helpers);
+    browserSync.init({
+      // reloadDelay: 500,
+      // logLevel: 'debug',
+      // logConnections: true,
+      startPath: '#/',
+      snippetOptions: {
+        rule: {
+          match: /<!-- browserSync -->/i
+        }
+      },
+      server: {
+        baseDir: options.src,
+        routes: {
+          // '/xosHelpers/src': options.helpers,
+          '/xos/core/xoslib/static/js/vendor': options.helpers
+        },
+        middleware: function(req, res, next){
+          if(
+            // req.url.indexOf('/xos/') !== -1 ||
+            // req.url.indexOf('/xoslib/') !== -1 ||
+            // req.url.indexOf('/hpcapi/') !== -1 ||
+            req.url.indexOf('/api/') !== -1
+          ){
+            if(conf.xoscsrftoken && conf.xossessionid){
+              req.headers.cookie = `xoscsrftoken=${conf.xoscsrftoken}; xossessionid=${conf.xossessionid}`;
+              req.headers['x-csrftoken'] = conf.xoscsrftoken;
+            }
+            proxy.web(req, res);
+          }
+          else{
+            next();
+          }
+        }
+      }
+    });
+
+    gulp.watch(options.src + 'js/**/*.js', ['js-watch']);
+    gulp.watch(options.src + 'vendor/**/*.js', ['bower'], function(){
+      browserSync.reload();
+    });
+    gulp.watch(options.src + '**/*.html', function(){
+      browserSync.reload();
+    });
+    gulp.watch(options.css + '**/*.css', function(){
+      browserSync.reload();
+    });
+
+    gulp.watch(options.helpers + 'ngXosHelpers.js', function(){
+      browserSync.reload();
+    });
+    
+    gulp.watch(`${options.sass}/**/*.scss`, ['sass'], function(){
+      browserSync.reload();
+    });
+  });
+
+  // compile sass
+  gulp.task('sass', function () {
+    return gulp.src(`${options.sass}/**/*.scss`)
+      .pipe(sass().on('error', sass.logError))
+      .pipe(gulp.dest(options.css));
+  });
+
+  // transpile js with sourceMaps
+  gulp.task('babel', function(){
+    return gulp.src(options.scripts + '**/*.js')
+      .pipe(babel({sourceMaps: true}))
+      .pipe(gulp.dest(options.tmp));
+  });
+
+  // // inject sourceMap
+  // gulp.task('injectMaps', function(){
+  //   return gulp.src(options.src + 'index.html')
+  //     .pipe(
+  //       inject(
+  //         gulp.src([
+  //           options.helpersSourceMaps + '**/*.js.map'
+  //         ], {read: false}).pipe(debug()),
+  //         {
+  //           starttag: '<!-- inject:maps -->',
+  //           // ignorePath: [options.src, '/../../ngXosLib']
+  //         }
+  //       )
+  //     )
+  //     .pipe(gulp.dest(options.src));
+  // });
+
+  // inject scripts
+  gulp.task('injectScript', ['cleanTmp', 'babel'], function(){
+
+    var appScripts = gulp.src([
+      options.tmp + '**/*.js',
+      options.helpers + 'ngXosHelpers.js'
+    ])
+    .pipe(angularFilesort()).pipe(debug());
+
+    var helpersSourceMaps = gulp.src([
+      options.helpersSourceMaps + '**/*.js.map'
+    ]).pipe(debug());
+
+    return gulp.src(options.src + 'index.html')
+      .pipe(
+        inject(
+          es.merge(appScripts, helpersSourceMaps),
+          {
+            ignorePath: [options.src, '/../../ngXosLib']
+          }
+        )
+      )
+      .pipe(gulp.dest(options.src));
+  });
+
+  // inject CSS
+  gulp.task('injectCss', function(){
+    return gulp.src(options.src + 'index.html')
+      .pipe(
+        inject(
+          gulp.src(options.src + 'css/*.css'),
+          {
+            ignorePath: [options.src]
+          }
+          )
+        )
+      .pipe(gulp.dest(options.src));
+  });
+
+  // inject bower dependencies with wiredep
+  gulp.task('bower', function () {
+    return gulp.src(options.src + 'index.html')
+    .pipe(wiredep({devDependencies: true}))
+    .pipe(gulp.dest(options.src));
+  });
+
+  gulp.task('js-watch', ['injectScript'], function(){
+    browserSync.reload();
+  });
+
+  gulp.task('cleanTmp', function(){
+    return del([options.tmp + '**/*']);
+  });
+
+  gulp.task('serve', function() {
+    runSequence(
+      'sass',
+      'bower',
+      'injectScript',
+      'injectCss',
+      ['browser']
+    );
+  });
+};
diff --git a/views/ngXosViews/sampleView/gulpfile.js b/views/ngXosViews/sampleView/gulpfile.js
new file mode 100644
index 0000000..8b50345
--- /dev/null
+++ b/views/ngXosViews/sampleView/gulpfile.js
@@ -0,0 +1,27 @@
+'use strict';
+
+var gulp = require('gulp');
+var wrench = require('wrench');
+
+var options = {
+  src: 'src/',
+  css: 'src/css/',
+  sass: 'src/sass/',
+  scripts: 'src/js/',
+  tmp: 'src/.tmp',
+  dist: 'dist/',
+  api: '../../ngXosLib/api/',
+  helpers: '../../../xos/core/xoslib/static/js/vendor/',
+  helpersSourceMaps: '../../ngXosLib/xosHelpers/.tmp/maps/',
+  static: '../../../xos/core/xoslib/static/', // this is the django static folder
+  dashboards: '../../../xos/core/xoslib/dashboards/' // this is the django html folder
+};
+
+wrench.readdirSyncRecursive('./gulp')
+.map(function(file) {
+  require('./gulp/' + file)(options);
+});
+
+gulp.task('default', function () {
+  gulp.start('build');
+});
diff --git a/views/ngXosViews/sampleView/karma.conf.js b/views/ngXosViews/sampleView/karma.conf.js
new file mode 100644
index 0000000..4123be9
--- /dev/null
+++ b/views/ngXosViews/sampleView/karma.conf.js
@@ -0,0 +1,88 @@
+// Karma configuration
+// Generated on Tue Oct 06 2015 09:27:10 GMT+0000 (UTC)
+
+/* eslint indent: [2,2], quotes: [2, "single"]*/
+
+/*eslint-disable*/
+var wiredep = require('wiredep');
+var path = require('path');
+
+var bowerComponents = wiredep( {devDependencies: true} )[ 'js' ].map(function( file ){
+  return path.relative(process.cwd(), file);
+});
+
+module.exports = function(config) {
+/*eslint-enable*/
+  config.set({
+
+    // base path that will be used to resolve all patterns (eg. files, exclude)
+    basePath: '',
+
+
+    // frameworks to use
+    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
+    frameworks: ['jasmine'],
+
+
+    // list of files / patterns to load in the browser
+    files: bowerComponents.concat([
+      '../../../xos/core/xoslib/static/js/vendor/ngXosVendor.js',
+      '../../../xos/core/xoslib/static/js/vendor/ngXosHelpers.js',
+      'src/js/**/*.js',
+      'spec/**/*.mock.js',
+      'spec/**/*.test.js',
+      'src/**/*.html'
+    ]),
+
+
+    // list of files to exclude
+    exclude: [
+    ],
+
+
+    // preprocess matching files before serving them to the browser
+    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
+    preprocessors: {
+      'src/js/**/*.js': ['babel'],
+      'spec/**/*.test.js': ['babel'],
+      'src/**/*.html': ['ng-html2js']
+    },
+
+    ngHtml2JsPreprocessor: {
+      stripPrefix: 'src/', //strip the src path from template url (http://stackoverflow.com/questions/22869668/karma-unexpected-request-when-testing-angular-directive-even-with-ng-html2js)
+      moduleName: 'templates' // define the template module name
+    },
+
+    // test results reporter to use
+    // possible values: 'dots', 'progress'
+    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
+    reporters: ['mocha'],
+
+
+    // web server port
+    port: 9876,
+
+
+    // enable / disable colors in the output (reporters and logs)
+    colors: true,
+
+
+    // level of logging
+    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
+    logLevel: config.LOG_INFO,
+
+
+    // enable / disable watching file and executing tests whenever any file changes
+    autoWatch: true,
+
+
+    // start these browsers
+    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
+    browsers: ['PhantomJS'],
+
+
+    // Continuous Integration mode
+    // if true, Karma captures browsers, runs the tests and exits
+    singleRun: false
+  });
+};
diff --git a/views/ngXosViews/sampleView/package.json b/views/ngXosViews/sampleView/package.json
new file mode 100644
index 0000000..e92f937
--- /dev/null
+++ b/views/ngXosViews/sampleView/package.json
@@ -0,0 +1,65 @@
+{
+  "name": "xos-sampleView",
+  "version": "1.0.0",
+  "description": "Angular Application for XOS, created with generator-xos",
+  "scripts": {
+    "prestart": "npm install && bower install",
+    "start": "gulp serve",
+    "prebuild": "npm install && bower install",
+    "build": "gulp",
+    "test": "karma start",
+    "test:ci": "karma start --single-run",
+    "lint": "eslint src/js/"
+  },
+  "keywords": [
+    "XOS",
+    "Angular",
+    "XOSlib"
+  ],
+  "author": "Matteo Scandolo",
+  "license": "MIT",
+  "dependencies": {},
+  "devDependencies": {
+    "autoprefixer": "^6.3.3",
+    "browser-sync": "^2.9.11",
+    "css-mqpacker": "^4.0.0",
+    "csswring": "^4.2.1",
+    "del": "^2.0.2",
+    "easy-mocker": "^1.2.0",
+    "eslint": "^1.8.0",
+    "eslint-plugin-angular": "linkmesrl/eslint-plugin-angular",
+    "event-stream": "^3.3.2",
+    "gulp": "^3.9.0",
+    "gulp-angular-filesort": "^1.1.1",
+    "gulp-angular-templatecache": "^1.8.0",
+    "gulp-babel": "^5.3.0",
+    "gulp-concat": "^2.6.0",
+    "gulp-concat-util": "^0.5.5",
+    "gulp-debug": "^2.1.2",
+    "gulp-eslint": "^1.0.0",
+    "gulp-inject": "^3.0.0",
+    "gulp-minify-html": "^1.0.4",
+    "gulp-ng-annotate": "^1.1.0",
+    "gulp-postcss": "^6.0.1",
+    "gulp-rename": "^1.2.2",
+    "gulp-replace": "^0.5.4",
+    "gulp-sass": "^2.2.0",
+    "gulp-uglify": "^1.4.2",
+    "http-proxy": "^1.12.0",
+    "ink-docstrap": "^0.5.2",
+    "jasmine-core": "~2.3.4",
+    "karma": "^0.13.14",
+    "karma-babel-preprocessor": "~5.2.2",
+    "karma-coverage": "^0.5.3",
+    "karma-jasmine": "~0.3.6",
+    "karma-mocha-reporter": "~1.1.1",
+    "karma-ng-html2js-preprocessor": "^0.2.0",
+    "karma-phantomjs-launcher": "~0.2.1",
+    "lodash": "^3.10.1",
+    "phantomjs": "^1.9.19",
+    "proxy-middleware": "^0.15.0",
+    "run-sequence": "^1.1.4",
+    "wiredep": "^3.0.0-beta",
+    "wrench": "^1.5.8"
+  }
+}
diff --git a/views/ngXosViews/sampleView/spec/sample.test.js b/views/ngXosViews/sampleView/spec/sample.test.js
new file mode 100644
index 0000000..6005af7
--- /dev/null
+++ b/views/ngXosViews/sampleView/spec/sample.test.js
@@ -0,0 +1,37 @@
+'use strict';
+
+describe('The User List', () => {
+  
+  var scope, element, isolatedScope, httpBackend;
+
+  beforeEach(module('xos.sampleView'));
+  beforeEach(module('templates'));
+
+  beforeEach(inject(function($httpBackend, $compile, $rootScope){
+    
+    httpBackend = $httpBackend;
+    // Setting up mock request
+    $httpBackend.expectGET('/api/core/users/?no_hyperlinks=1').respond([
+      {
+        email: 'teo@onlab.us',
+        firstname: 'Matteo',
+        lastname: 'Scandolo'
+      }
+    ]);
+  
+    scope = $rootScope.$new();
+    element = angular.element('<users-list></users-list>');
+    $compile(element)(scope);
+    scope.$digest();
+    isolatedScope = element.isolateScope().vm;
+  }));
+
+  it('should load 1 users', () => {
+    httpBackend.flush();
+    expect(isolatedScope.users.length).toBe(1);
+    expect(isolatedScope.users[0].email).toEqual('teo@onlab.us');
+    expect(isolatedScope.users[0].firstname).toEqual('Matteo');
+    expect(isolatedScope.users[0].lastname).toEqual('Scandolo');
+  });
+
+});
\ No newline at end of file
diff --git a/views/ngXosLib/api/.gitignore b/views/ngXosViews/sampleView/src/css/main.css
similarity index 100%
rename from views/ngXosLib/api/.gitignore
rename to views/ngXosViews/sampleView/src/css/main.css
diff --git a/views/ngXosViews/sampleView/src/index.html b/views/ngXosViews/sampleView/src/index.html
new file mode 100644
index 0000000..d04fc3b
--- /dev/null
+++ b/views/ngXosViews/sampleView/src/index.html
@@ -0,0 +1,29 @@
+<!-- browserSync -->
+<!-- bower:css -->
+<link rel="stylesheet" href="vendor/bootstrap-css/css/bootstrap.min.css" />
+<!-- endbower --><!-- endcss -->
+<!-- inject:css -->
+<link rel="stylesheet" href="/css/main.css">
+<!-- endinject -->
+
+<div ng-app="xos.sampleView" id="xosSampleView" class="container-fluid">
+    <div ui-view></div>
+</div>
+
+<!-- bower:js -->
+<script src="vendor/jquery/dist/jquery.js"></script>
+<script src="vendor/angular/angular.js"></script>
+<script src="vendor/angular-mocks/angular-mocks.js"></script>
+<script src="vendor/angular-ui-router/release/angular-ui-router.js"></script>
+<script src="vendor/angular-cookies/angular-cookies.js"></script>
+<script src="vendor/angular-resource/angular-resource.js"></script>
+<script src="vendor/ng-lodash/build/ng-lodash.js"></script>
+<script src="vendor/bootstrap-css/js/bootstrap.min.js"></script>
+<!-- endbower --><!-- endjs -->
+<!-- inject:js -->
+<script src="/../../../xos/core/xoslib/static/js/vendor/ngXosHelpers.js"></script>
+<script src="/.tmp/main.js"></script>
+<!-- endinject -->
+
+<!-- inject:map -->
+<!-- endinject -->
\ No newline at end of file
diff --git a/views/ngXosViews/sampleView/src/js/main.js b/views/ngXosViews/sampleView/src/js/main.js
new file mode 100644
index 0000000..b00aa0b
--- /dev/null
+++ b/views/ngXosViews/sampleView/src/js/main.js
@@ -0,0 +1,78 @@
+'use strict';
+
+angular.module('xos.sampleView', [
+  'ngResource',
+  'ngCookies',
+  'ngLodash',
+  'ui.router',
+  'xos.helpers'
+])
+.config(($stateProvider) => {
+  $stateProvider
+  .state('user-list', {
+    url: '/',
+    template: '<users-list></users-list>'
+  });
+})
+.config(function($httpProvider){
+  $httpProvider.interceptors.push('NoHyperlinks');
+})
+.directive('usersList', function(){
+  return {
+    restrict: 'E',
+    scope: {},
+    bindToController: true,
+    controllerAs: 'vm',
+    templateUrl: 'templates/users-list.tpl.html',
+    controller: function(Users){
+
+      this.tableConfig = {
+        columns: [
+          {
+            label: 'E-Mail',
+            prop: 'email'
+          },
+          {
+            label: 'First Name',
+            prop: 'firstname'
+          },
+          {
+            label: 'Last Name',
+            prop: 'lastname'
+          }
+        ],
+        classes: 'table table-striped table-condensed',
+        actions: [
+          {
+            label: 'delete',
+            icon: 'remove',
+            cb: (user) => {
+              console.log(user);
+            },
+            color: 'red'
+          }
+        ],
+        filter: 'field',
+        order: true,
+        // pagination: {
+        //   pageSize: 6
+        // }
+      };
+
+      this.alertConfig = {
+        type: 'danger',
+        closeBtn: true
+      }
+
+
+      // retrieving user list
+      Users.query().$promise
+      .then((users) => {
+        this.users = users;
+      })
+      .catch((e) => {
+        throw new Error(e);
+      });
+    }
+  };
+});
\ No newline at end of file
diff --git a/views/ngXosViews/sampleView/src/sass/main.scss b/views/ngXosViews/sampleView/src/sass/main.scss
new file mode 100644
index 0000000..efe6cdc
--- /dev/null
+++ b/views/ngXosViews/sampleView/src/sass/main.scss
@@ -0,0 +1,5 @@
+@import '../../../../style/sass/lib/_variables.scss';
+
+#xosSampleView {
+
+}
\ No newline at end of file
diff --git a/views/ngXosViews/sampleView/src/templates/users-list.tpl.html b/views/ngXosViews/sampleView/src/templates/users-list.tpl.html
new file mode 100644
index 0000000..c0c58cb
--- /dev/null
+++ b/views/ngXosViews/sampleView/src/templates/users-list.tpl.html
@@ -0,0 +1,14 @@
+<div class="row">
+  <div class="col-xs-12">
+    <h1>Users List</h1>
+    <p>This is only an example view.</p>
+  </div>
+</div>
+
+  <xos-alert config="vm.alertConfig">Alert message Here</xos-alert>
+
+<div class="row">
+  <div class="col-xs-12">
+    <xos-table data="vm.users" config="vm.tableConfig"></xos-table>
+  </div>
+</div>
\ No newline at end of file
diff --git a/xos/core/xoslib/dashboards/xosDiagnostic.html b/xos/core/xoslib/dashboards/xosDiagnostic.html
index f988d9a..f87baae 100644
--- a/xos/core/xoslib/dashboards/xosDiagnostic.html
+++ b/xos/core/xoslib/dashboards/xosDiagnostic.html
@@ -4,7 +4,7 @@
 <link rel="stylesheet" href="/static/css/xosDiagnostic.css">
 <!-- endinject -->
 
-<div id="xosDiagnostic">
+<div ng-app="xos.diagnostic" id="xosDiagnostic">
     <div ui-view></div>
 </div>
 
diff --git a/xos/core/xoslib/dashboards/xosSampleView.html b/xos/core/xoslib/dashboards/xosSampleView.html
index 92d6f9a..6ebd9f3 100644
--- a/xos/core/xoslib/dashboards/xosSampleView.html
+++ b/xos/core/xoslib/dashboards/xosSampleView.html
@@ -1,14 +1,14 @@
 <!-- browserSync -->
 
 <!-- inject:css -->
-<link rel="stylesheet" href="/css/dev.css">
+<link rel="stylesheet" href="/static/css/xosSampleView.css">
 <!-- endinject -->
 
-<div id="xosSampleView">
+<div ng-app="xos.sampleView" id="xosSampleView" class="container-fluid">
     <div ui-view></div>
 </div>
 
 
 <!-- inject:js -->
-<script src="/../../static/js/xosSampleView.js"></script>
+<script src="/static/js/xosSampleView.js"></script>
 <!-- endinject -->
diff --git a/xos/core/xoslib/static/css/xosDiagnostic.css b/xos/core/xoslib/static/css/xosDiagnostic.css
index 0c1de20..7aee881 100644
--- a/xos/core/xoslib/static/css/xosDiagnostic.css
+++ b/xos/core/xoslib/static/css/xosDiagnostic.css
@@ -1 +1 @@
-#xosDiagnostic,[ui-view]{min-height:700px;position:relative}diagnostic-container .form-control.small-padding{padding:6px}diagnostic-container .half-height{position:relative;height:50%}diagnostic-container .onethird-height{position:relative;height:33%;border-bottom:1px solid #999}diagnostic-container .twothird-height{position:relative;height:67%}diagnostic-container .subscriber-select{max-width:200px;position:absolute;top:20px;right:20px;z-index:1}diagnostic-container .onethird-height .well,diagnostic-container .twothird-height .well{font-weight:700;max-width:165px;text-align:center;margin-top:15px;background:#eee;border-color:steelblue;padding:10px}diagnostic-container .onethird-height .well.pull-right{position:absolute;right:0;top:-15px;cursor:pointer;z-index:200}subscriber-status-modal .row+.row{margin-top:20px}.half-height+.half-height{border-top:1px solid black}service-topology,logic-topology{height:100%;width:100%;display:block;position:absolute;top:0}logic-topology .subscriber circle,logic-topology .device circle{fill:#fff;stroke:green;stroke-width:1px}logic-topology>svg{position:absolute;top:0}logic-topology .network .cloud{fill:#fff;stroke:green;stroke-width:1px}logic-topology .node.rack>g>rect{fill:#ccc;stroke:steelblue;stroke-width:1px}logic-topology .compute-node>rect{fill:#fff;stroke:steelblue;stroke-width:1px}logic-topology .compute-node>text{font-size:16px}logic-topology .instance>rect{fill:#eee;stroke:steelblue;stroke-width:1px}logic-topology .node .instance.active rect{fill:lightsteelblue;stroke:steelblue;stroke-width:1px}logic-topology .node .instance.active.good>rect{fill:green}logic-topology .node .instance.active.provisioning>rect{fill:yellow}logic-topology .node .instance.active.bad>rect{fill:red}logic-topology .node .instance .stats-container rect{fill:white}logic-topology .node .instance .stats-container text.name{font-weight:700}logic-topology .node .instance .stats-container text.ip{font-style:italic;font-size:10px}logic-topology .node .instance .stats-container .container rect{fill:#eee;stroke:steelblue;stroke-width:1px}.legend{fill:#fff;stroke:#ccc;stroke-width:1px;position:relative}.legend text{stroke:#000}.node{cursor:pointer}.node circle,.node rect{fill:#fff;stroke:steelblue;stroke-width:1px}.node.subscriber circle,.node.subscriber rect,.node.router circle,.node.router rect{stroke:#05ffcb}.node.slice rect{stroke:#b01dff}.node.instance rect{stroke:#ccc}.node.instance rect.active{stroke:#ff8b00}.node rect.slice-detail{fill:#fff;stroke:steelblue;stroke-width:3px}.node text{font:18px sans-serif}.node .instance text{font:12px sans-serif}.node text.small{font-size:10px}.link,.device-link{fill:none;stroke:#ccc;stroke-width:2px}.link.slice{stroke:rgba(157,4,183,.29)}.link.instance{stroke:#ccc}.link.instance.active{stroke:rgba(255,138,0,.65)}.service-details{width:200px;position:absolute;top:20px;right:20px}.animate.ng-hide-remove{animation:.5s bounceInRight ease}.animate.ng-hide-add{animation:.5s bounceOutRight ease}.loader{font-size:10px;margin:150px auto;text-indent:-9999em;width:11em;height:11em;border-radius:50%;background:#fff;background:linear-gradient(to right,#fff 10%,rgba(255,255,255,0) 42%);position:relative;animation:load3 1.4s infinite linear;transform:translateZ(0)}.loader:before{width:50%;height:50%;background:#105e9e;border-radius:100% 0 0;position:absolute;top:0;left:0;content:''}.loader:after{background:#fff;width:75%;height:75%;border-radius:50%;content:'';margin:auto;position:absolute;top:0;left:0;bottom:0;right:0}@keyframes load3{0%{transform:rotate(0deg)}100%{transform:rotate(360deg)}}.modal.fade.in{display:block}@keyframes bounceInRight{from,60%,75%,90%,to{animation-timing-function:cubic-bezier(.215,.61,.355,1.000)}from{opacity:0;transform:translate3d(3000px,0,0)}60%{opacity:1;transform:translate3d(-25px,0,0)}75%{transform:translate3d(10px,0,0)}90%{transform:translate3d(-5px,0,0)}to{transform:none}}@keyframes bounceOutRight{20%{opacity:1;transform:translate3d(-20px,0,0)}to{opacity:0;transform:translate3d(2000px,0,0)}}
\ No newline at end of file
+#xosDiagnostic,[ui-view]{min-height:700px;position:relative}diagnostic-container .form-control.small-padding{padding:6px}diagnostic-container .half-height{position:relative;height:50%}diagnostic-container .onethird-height{position:relative;height:33%;border-bottom:1px solid #999}diagnostic-container .twothird-height{position:relative;height:67%}diagnostic-container .subscriber-select{max-width:200px;position:absolute;top:20px;right:20px;z-index:1}diagnostic-container .onethird-height .well,diagnostic-container .twothird-height .well{font-weight:700;max-width:165px;text-align:center;margin-top:15px;background:#eee;border-color:steelblue;padding:10px}diagnostic-container .onethird-height .well.pull-right{position:absolute;right:0;top:-15px;cursor:pointer;z-index:200}subscriber-status-modal .row+.row{margin-top:20px}.half-height+.half-height{border-top:1px solid black}service-topology,logic-topology{height:100%;width:100%;display:block;position:absolute;top:0}logic-topology .subscriber circle,logic-topology .device circle{fill:#fff;stroke:green;stroke-width:1px}logic-topology>svg{position:absolute;top:0}logic-topology .network .cloud{fill:#fff;stroke:green;stroke-width:1px}logic-topology .node.rack>g>rect{fill:#ccc;stroke:steelblue;stroke-width:1px}logic-topology .compute-node>rect{fill:#fff;stroke:steelblue;stroke-width:1px}logic-topology .compute-node>text{font-size:16px}logic-topology .instance>rect{fill:#eee;stroke:steelblue;stroke-width:1px}logic-topology .node .instance.active rect{fill:lightsteelblue;stroke:steelblue;stroke-width:1px}logic-topology .node .instance.active.good>rect{fill:green}logic-topology .node .instance.active.provisioning>rect{fill:yellow}logic-topology .node .instance.active.bad>rect{fill:red}logic-topology .node .instance .stats-container rect{fill:white}logic-topology .node .instance .stats-container text.name{font-weight:700}logic-topology .node .instance .stats-container text.ip{font-style:italic;font-size:10px}logic-topology .node .instance .stats-container .container rect{fill:#eee;stroke:steelblue;stroke-width:1px}.legend{fill:#fff;stroke:#ccc;stroke-width:1px;position:relative}.legend text{stroke:#000}.node{cursor:pointer}.node circle,.node rect{fill:#fff;stroke:steelblue;stroke-width:1px}.node.subscriber circle,.node.subscriber rect,.node.router circle,.node.router rect{stroke:#05ffcb}.node.slice rect{stroke:#b01dff}.node.instance rect{stroke:#ccc}.node.instance rect.active{stroke:#ff8b00}.node rect.slice-detail{fill:#fff;stroke:steelblue;stroke-width:3px}.node text{font:18px sans-serif}.node .instance text{font:12px sans-serif}.node text.small{font-size:10px}.link,.device-link{fill:none;stroke:#ccc;stroke-width:2px}.link.slice{stroke:rgba(157,4,183,.29)}.link.instance{stroke:#ccc}.link.instance.active{stroke:rgba(255,138,0,.65)}.service-details{width:200px;position:absolute;top:20px;right:20px}.animate.ng-hide-remove{animation:.5s bounceInRight ease}.animate.ng-hide-add{animation:.5s bounceOutRight ease}.loader{font-size:10px;margin:150px auto;text-indent:-9999em;width:11em;height:11em;border-radius:50%;background:#fff;background:linear-gradient(to right,#fff 10%,rgba(255,255,255,0) 42%);position:relative;animation:load3 1.4s infinite linear;transform:translateZ(0)}.loader:before{width:50%;height:50%;background:#105e9e;border-radius:100% 0 0;position:absolute;top:0;left:0;content:''}.loader:after{background:#fff;width:75%;height:75%;border-radius:50%;content:'';margin:auto;position:absolute;top:0;left:0;bottom:0;right:0}@keyframes load3{0%{transform:rotate(0deg)}100%{transform:rotate(360deg)}}.modal.fade.in{display:block}@keyframes bounceInRight{from,60%,75%,90%,to{animation-timing-function:cubic-bezier(.215,.61,.355,1)}from{opacity:0;transform:translate3d(3000px,0,0)}60%{opacity:1;transform:translate3d(-25px,0,0)}75%{transform:translate3d(10px,0,0)}90%{transform:translate3d(-5px,0,0)}to{transform:none}}@keyframes bounceOutRight{20%{opacity:1;transform:translate3d(-20px,0,0)}to{opacity:0;transform:translate3d(2000px,0,0)}}
\ No newline at end of file
diff --git a/views/ngXosLib/api/.gitignore b/xos/core/xoslib/static/css/xosSampleView.css
similarity index 100%
copy from views/ngXosLib/api/.gitignore
copy to xos/core/xoslib/static/css/xosSampleView.css
diff --git a/xos/core/xoslib/static/js/vendor/ngXosHelpers.js b/xos/core/xoslib/static/js/vendor/ngXosHelpers.js
index 69cbbf4..f981551 100644
--- a/xos/core/xoslib/static/js/vendor/ngXosHelpers.js
+++ b/xos/core/xoslib/static/js/vendor/ngXosHelpers.js
@@ -1 +1 @@
-!function(){"use strict";function e(e,n,o){e.interceptors.push("SetCSRFToken"),n.startSymbol("{$"),n.endSymbol("$}"),o.defaults.stripTrailingSlashes=!1}e.$inject=["$httpProvider","$interpolateProvider","$resourceProvider"],angular.module("bugSnag",[]).factory("$exceptionHandler",function(){return function(e,n){window.Bugsnag?Bugsnag.notifyException(e,{diagnostics:{cause:n}}):console.error(e,n,e.stack)}}),angular.module("xos.helpers",["ngCookies","xos.xos","xos.hpcapi","xos.xoslib","bugSnag"]).config(e)}(),function(){"use strict";function e(){return{request:function(e){return-1===e.url.indexOf(".html")&&(e.url+="?no_hyperlinks=1"),e}}}angular.module("xos.helpers").factory("NoHyperlinks",e)}(),function(){"use strict";function e(e){return{request:function(n){return"GET"!==n.method&&(n.headers["X-CSRFToken"]=e.get("xoscsrftoken")),n}}}e.$inject=["$cookies"],angular.module("xos.helpers").factory("SetCSRFToken",e)}(),function(){"use strict";function e(e){return r||(r=new e({domain:""})),r}function n(e){return t||(t=new e({domain:""})),t}function o(e){return i||(i=new e({domain:""})),i}e.$inject=["xos"],n.$inject=["xoslib"],o.$inject=["hpcapi"],angular.module("xos.helpers").service("XosApi",e).service("XoslibApi",n).service("HpcApi",o);var r,t,i}();
\ No newline at end of file
+"use strict";!function(){angular.module("xos.uiComponents",[])}(),function(){angular.module("xos.uiComponents").directive("xosTable",function(){return{restrict:"E",scope:{data:"=",config:"="},template:'\n          <div ng-show="vm.data.length > 0">\n            <div class="row" ng-if="vm.config.filter == \'fulltext\'">\n              <div class="col-xs-12">\n                <input\n                  class="form-control"\n                  placeholder="Type to search.."\n                  type="text"\n                  ng-model="vm.query"/>\n              </div>\n            </div>\n            <table ng-class="vm.classes" ng-show="vm.data.length > 0">\n              <thead>\n                <tr>\n                  <th ng-repeat="col in vm.columns">\n                    {{col.label}}\n                    <span ng-if="vm.config.order">\n                      <a href="" ng-click="vm.orderBy = col.prop; vm.reverse = false">\n                        <i class="glyphicon glyphicon-chevron-up"></i>\n                      </a>\n                      <a href="" ng-click="vm.orderBy = col.prop; vm.reverse = true">\n                        <i class="glyphicon glyphicon-chevron-down"></i>\n                      </a>\n                    </span>\n                  </th>\n                  <th ng-if="vm.config.actions">Actions</th>\n                </tr>\n              </thead>\n              <tbody ng-if="vm.config.filter == \'field\'">\n                <tr>\n                  <td ng-repeat="col in vm.columns">\n                    <input\n                      class="form-control"\n                      placeholder="Type to search by {{col.label}}"\n                      type="text"\n                      ng-model="vm.query[col.prop]"/>\n                  </td>\n                  <td ng-if="vm.config.actions"></td>\n                </tr>\n              </tbody>\n              <tbody>\n                <tr ng-repeat="item in vm.data | filter:vm.query | orderBy:vm.orderBy:vm.reverse | pagination:vm.currentPage * vm.config.pagination.pageSize | limitTo: (vm.config.pagination.pageSize || vm.data.length) track by $index">\n                  <td ng-repeat="col in vm.columns">{{item[col.prop]}}</td>\n                  <td ng-if="vm.config.actions">\n                    <a href=""\n                      ng-repeat="action in vm.config.actions"\n                      ng-click="action.cb(item)"\n                      title="{{action.label}}">\n                      <i\n                        class="glyphicon glyphicon-{{action.icon}}"\n                        style="color: {{action.color}};"></i>\n                    </a>\n                  </td>\n                </tr>\n              </tbody>\n            </table>\n            <xos-pagination\n              ng-if="vm.config.pagination"\n              page-size="vm.config.pagination.pageSize"\n              total-elements="vm.data.length"\n              change="vm.goToPage">\n              </xos-pagination>\n          </div>\n          <div ng-show="vm.data.length == 0 || !vm.data">\n            <div class="alert alert-info">\n              No data to show.\n            </div>\n          </div>\n        ',bindToController:!0,controllerAs:"vm",controller:function(){var n=this;if(!this.config)throw new Error('[xosTable] Please provide a configuration via the "config" attribute');if(!this.config.columns)throw new Error("[xosTable] Please provide a columns list in the configuration");this.columns=this.config.columns,this.classes=this.config.classes||"table table-striped table-bordered",this.config.actions,this.config.pagination&&(this.currentPage=0,this.goToPage=function(e){n.currentPage=e})}}})}(),function(){angular.module("xos.uiComponents").directive("xosPagination",function(){return{restrict:"E",scope:{pageSize:"=",totalElements:"=",change:"="},template:'\n        <div class="row" ng-if="vm.pageList.length > 1">\n          <div class="col-xs-12 text-center">\n            <ul class="pagination">\n              <li\n                ng-click="vm.goToPage(vm.currentPage - 1)"\n                ng-class="{disabled: vm.currentPage == 0}">\n                <a href="" aria-label="Previous">\n                    <span aria-hidden="true">&laquo;</span>\n                </a>\n              </li>\n              <li ng-repeat="i in vm.pageList" ng-class="{active: i === vm.currentPage}">\n                <a href="" ng-click="vm.goToPage(i)">{{i + 1}}</a>\n              </li>\n              <li\n                ng-click="vm.goToPage(vm.currentPage + 1)"\n                ng-class="{disabled: vm.currentPage == vm.pages - 1}">\n                <a href="" aria-label="Next">\n                    <span aria-hidden="true">&raquo;</span>\n                </a>\n              </li>\n            </ul>\n          </div>\n        </div>\n      ',bindToController:!0,controllerAs:"vm",controller:["$scope",function(n){var e=this;this.currentPage=0,this.goToPage=function(n){0>n||n===e.pages||(e.currentPage=n,e.change(n))},this.createPages=function(n){for(var e=[],r=0;n>r;r++)e.push(r);return e},n.$watch(function(){return e.totalElements},function(){e.totalElements&&(e.pages=Math.ceil(e.totalElements/e.pageSize),e.pageList=e.createPages(e.pages))})}]}}).filter("pagination",function(){return function(n,e){return n&&angular.isArray(n)?(e=parseInt(e,10),n.slice(e)):n}})}(),function(){angular.module("xos.uiComponents").directive("xosAlert",function(){return{restrict:"E",scope:{config:"=",show:"=?"},template:'\n        <div class="alert alert-{{vm.config.type}}" ng-show="vm.show">\n          <button type="button" class="close" ng-if="vm.config.closeBtn" ng-click="vm.dismiss()">\n            <span aria-hidden="true">&times;</span>\n          </button>\n          <p ng-transclude></p>\n        </div>\n      ',transclude:!0,bindToController:!0,controllerAs:"vm",controller:["$timeout",function(n){var e=this;if(!this.config)throw new Error('[xosAlert] Please provide a configuration via the "config" attribute');this.show=this.show!==!1,this.dismiss=function(){e.show=!1},this.config.autoHide&&!function(){var r=n(function(){e.dismiss(),n.cancel(r)},e.config.autoHide)}()}]}})}(),function(){function n(n,e,r){n.interceptors.push("SetCSRFToken"),e.startSymbol("{$"),e.endSymbol("$}"),r.defaults.stripTrailingSlashes=!1}n.$inject=["$httpProvider","$interpolateProvider","$resourceProvider"],angular.module("bugSnag",[]).factory("$exceptionHandler",function(){return function(n,e){window.Bugsnag?Bugsnag.notifyException(n,{diagnostics:{cause:e}}):console.error(n,e,n.stack)}}),angular.module("xos.helpers",["ngCookies","ngResource","bugSnag","xos.uiComponents"]).config(n)}(),function(){angular.module("xos.helpers").service("vSG-Collection",["$resource",function(n){return n("/api/service/vsg/")}])}(),function(){angular.module("xos.helpers").service("vOLT-Collection",["$resource",function(n){return n("/api/tenant/cord/volt/:volt_id/",{volt_id:"@id"})}])}(),function(){angular.module("xos.helpers").service("Users",["$resource",function(n){return n("/api/core/users/")}])}(),function(){angular.module("xos.helpers").service("Truckroll-Collection",["$resource",function(n){return n("/api/tenant/truckroll/:truckroll_id/",{truckroll_id:"@id"})}])}(),function(){angular.module("xos.helpers").service("Subscribers",["$resource",function(n){return n("/api/tenant/cord/subscriber/:subscriber_id/",{subscriber_id:"@id"})}]).service("Subscriber-features",["$resource",function(n){return n("/api/tenant/cord/subscriber/:subscriber_id/features/",{subscriber_id:"@id"})}]).service("Subscriber-features-uplink_speed",["$resource",function(n){return n("/api/tenant/cord/subscriber/:subscriber_id/features/uplink_speed/",{subscriber_id:"@id"})}]).service("Subscriber-features-downlink_speed",["$resource",function(n){return n("/api/tenant/cord/subscriber/:subscriber_id/features/downlink_speed/",{subscriber_id:"@id"})}]).service("Subscriber-features-cdn",["$resource",function(n){return n("/api/tenant/cord/subscriber/:subscriber_id/features/cdn/",{subscriber_id:"@id"})}]).service("Subscriber-features-uverse",["$resource",function(n){return n("/api/tenant/cord/subscriber/:subscriber_id/features/uverse/",{subscriber_id:"@id"})}]).service("Subscriber-features-status",["$resource",function(n){return n("/api/tenant/cord/subscriber/:subscriber_id/features/status/",{subscriber_id:"@id"})}])}(),function(){angular.module("xos.helpers").service("ONOS-Services-Collection",["$resource",function(n){return n("/api/service/onos/")}])}(),function(){angular.module("xos.helpers").service("ONOS-App-Collection",["$resource",function(n){return n("/api/tenant/onos/app/")}])}(),function(){function n(){return{request:function(n){return-1===n.url.indexOf(".html")&&(n.url+="?no_hyperlinks=1"),n}}}angular.module("xos.helpers").factory("NoHyperlinks",n)}(),function(){function n(n){return{request:function(e){return"GET"!==e.method&&(e.headers["X-CSRFToken"]=n.get("xoscsrftoken")),e}}}n.$inject=["$cookies"],angular.module("xos.helpers").factory("SetCSRFToken",n)}();
\ No newline at end of file
diff --git a/xos/core/xoslib/static/js/vendor/ngXosVendor.js b/xos/core/xoslib/static/js/vendor/ngXosVendor.js
index 6f92beb..f537697 100644
--- a/xos/core/xoslib/static/js/vendor/ngXosVendor.js
+++ b/xos/core/xoslib/static/js/vendor/ngXosVendor.js
@@ -45536,3 +45536,3932 @@
 
 
 })(window, window.angular);
+
+/**
+ * @license AngularJS v1.4.7
+ * (c) 2010-2015 Google, Inc. http://angularjs.org
+ * License: MIT
+ */
+(function(window, angular, undefined) {'use strict';
+
+/* jshint ignore:start */
+var noop        = angular.noop;
+var extend      = angular.extend;
+var jqLite      = angular.element;
+var forEach     = angular.forEach;
+var isArray     = angular.isArray;
+var isString    = angular.isString;
+var isObject    = angular.isObject;
+var isUndefined = angular.isUndefined;
+var isDefined   = angular.isDefined;
+var isFunction  = angular.isFunction;
+var isElement   = angular.isElement;
+
+var ELEMENT_NODE = 1;
+var COMMENT_NODE = 8;
+
+var ADD_CLASS_SUFFIX = '-add';
+var REMOVE_CLASS_SUFFIX = '-remove';
+var EVENT_CLASS_PREFIX = 'ng-';
+var ACTIVE_CLASS_SUFFIX = '-active';
+
+var NG_ANIMATE_CLASSNAME = 'ng-animate';
+var NG_ANIMATE_CHILDREN_DATA = '$$ngAnimateChildren';
+
+// Detect proper transitionend/animationend event names.
+var CSS_PREFIX = '', TRANSITION_PROP, TRANSITIONEND_EVENT, ANIMATION_PROP, ANIMATIONEND_EVENT;
+
+// If unprefixed events are not supported but webkit-prefixed are, use the latter.
+// Otherwise, just use W3C names, browsers not supporting them at all will just ignore them.
+// Note: Chrome implements `window.onwebkitanimationend` and doesn't implement `window.onanimationend`
+// but at the same time dispatches the `animationend` event and not `webkitAnimationEnd`.
+// Register both events in case `window.onanimationend` is not supported because of that,
+// do the same for `transitionend` as Safari is likely to exhibit similar behavior.
+// Also, the only modern browser that uses vendor prefixes for transitions/keyframes is webkit
+// therefore there is no reason to test anymore for other vendor prefixes:
+// http://caniuse.com/#search=transition
+if (isUndefined(window.ontransitionend) && isDefined(window.onwebkittransitionend)) {
+  CSS_PREFIX = '-webkit-';
+  TRANSITION_PROP = 'WebkitTransition';
+  TRANSITIONEND_EVENT = 'webkitTransitionEnd transitionend';
+} else {
+  TRANSITION_PROP = 'transition';
+  TRANSITIONEND_EVENT = 'transitionend';
+}
+
+if (isUndefined(window.onanimationend) && isDefined(window.onwebkitanimationend)) {
+  CSS_PREFIX = '-webkit-';
+  ANIMATION_PROP = 'WebkitAnimation';
+  ANIMATIONEND_EVENT = 'webkitAnimationEnd animationend';
+} else {
+  ANIMATION_PROP = 'animation';
+  ANIMATIONEND_EVENT = 'animationend';
+}
+
+var DURATION_KEY = 'Duration';
+var PROPERTY_KEY = 'Property';
+var DELAY_KEY = 'Delay';
+var TIMING_KEY = 'TimingFunction';
+var ANIMATION_ITERATION_COUNT_KEY = 'IterationCount';
+var ANIMATION_PLAYSTATE_KEY = 'PlayState';
+var SAFE_FAST_FORWARD_DURATION_VALUE = 9999;
+
+var ANIMATION_DELAY_PROP = ANIMATION_PROP + DELAY_KEY;
+var ANIMATION_DURATION_PROP = ANIMATION_PROP + DURATION_KEY;
+var TRANSITION_DELAY_PROP = TRANSITION_PROP + DELAY_KEY;
+var TRANSITION_DURATION_PROP = TRANSITION_PROP + DURATION_KEY;
+
+var isPromiseLike = function(p) {
+  return p && p.then ? true : false;
+};
+
+function assertArg(arg, name, reason) {
+  if (!arg) {
+    throw ngMinErr('areq', "Argument '{0}' is {1}", (name || '?'), (reason || "required"));
+  }
+  return arg;
+}
+
+function mergeClasses(a,b) {
+  if (!a && !b) return '';
+  if (!a) return b;
+  if (!b) return a;
+  if (isArray(a)) a = a.join(' ');
+  if (isArray(b)) b = b.join(' ');
+  return a + ' ' + b;
+}
+
+function packageStyles(options) {
+  var styles = {};
+  if (options && (options.to || options.from)) {
+    styles.to = options.to;
+    styles.from = options.from;
+  }
+  return styles;
+}
+
+function pendClasses(classes, fix, isPrefix) {
+  var className = '';
+  classes = isArray(classes)
+      ? classes
+      : classes && isString(classes) && classes.length
+          ? classes.split(/\s+/)
+          : [];
+  forEach(classes, function(klass, i) {
+    if (klass && klass.length > 0) {
+      className += (i > 0) ? ' ' : '';
+      className += isPrefix ? fix + klass
+                            : klass + fix;
+    }
+  });
+  return className;
+}
+
+function removeFromArray(arr, val) {
+  var index = arr.indexOf(val);
+  if (val >= 0) {
+    arr.splice(index, 1);
+  }
+}
+
+function stripCommentsFromElement(element) {
+  if (element instanceof jqLite) {
+    switch (element.length) {
+      case 0:
+        return [];
+        break;
+
+      case 1:
+        // there is no point of stripping anything if the element
+        // is the only element within the jqLite wrapper.
+        // (it's important that we retain the element instance.)
+        if (element[0].nodeType === ELEMENT_NODE) {
+          return element;
+        }
+        break;
+
+      default:
+        return jqLite(extractElementNode(element));
+        break;
+    }
+  }
+
+  if (element.nodeType === ELEMENT_NODE) {
+    return jqLite(element);
+  }
+}
+
+function extractElementNode(element) {
+  if (!element[0]) return element;
+  for (var i = 0; i < element.length; i++) {
+    var elm = element[i];
+    if (elm.nodeType == ELEMENT_NODE) {
+      return elm;
+    }
+  }
+}
+
+function $$addClass($$jqLite, element, className) {
+  forEach(element, function(elm) {
+    $$jqLite.addClass(elm, className);
+  });
+}
+
+function $$removeClass($$jqLite, element, className) {
+  forEach(element, function(elm) {
+    $$jqLite.removeClass(elm, className);
+  });
+}
+
+function applyAnimationClassesFactory($$jqLite) {
+  return function(element, options) {
+    if (options.addClass) {
+      $$addClass($$jqLite, element, options.addClass);
+      options.addClass = null;
+    }
+    if (options.removeClass) {
+      $$removeClass($$jqLite, element, options.removeClass);
+      options.removeClass = null;
+    }
+  }
+}
+
+function prepareAnimationOptions(options) {
+  options = options || {};
+  if (!options.$$prepared) {
+    var domOperation = options.domOperation || noop;
+    options.domOperation = function() {
+      options.$$domOperationFired = true;
+      domOperation();
+      domOperation = noop;
+    };
+    options.$$prepared = true;
+  }
+  return options;
+}
+
+function applyAnimationStyles(element, options) {
+  applyAnimationFromStyles(element, options);
+  applyAnimationToStyles(element, options);
+}
+
+function applyAnimationFromStyles(element, options) {
+  if (options.from) {
+    element.css(options.from);
+    options.from = null;
+  }
+}
+
+function applyAnimationToStyles(element, options) {
+  if (options.to) {
+    element.css(options.to);
+    options.to = null;
+  }
+}
+
+function mergeAnimationOptions(element, target, newOptions) {
+  var toAdd = (target.addClass || '') + ' ' + (newOptions.addClass || '');
+  var toRemove = (target.removeClass || '') + ' ' + (newOptions.removeClass || '');
+  var classes = resolveElementClasses(element.attr('class'), toAdd, toRemove);
+
+  if (newOptions.preparationClasses) {
+    target.preparationClasses = concatWithSpace(newOptions.preparationClasses, target.preparationClasses);
+    delete newOptions.preparationClasses;
+  }
+
+  // noop is basically when there is no callback; otherwise something has been set
+  var realDomOperation = target.domOperation !== noop ? target.domOperation : null;
+
+  extend(target, newOptions);
+
+  // TODO(matsko or sreeramu): proper fix is to maintain all animation callback in array and call at last,but now only leave has the callback so no issue with this.
+  if (realDomOperation) {
+    target.domOperation = realDomOperation;
+  }
+
+  if (classes.addClass) {
+    target.addClass = classes.addClass;
+  } else {
+    target.addClass = null;
+  }
+
+  if (classes.removeClass) {
+    target.removeClass = classes.removeClass;
+  } else {
+    target.removeClass = null;
+  }
+
+  return target;
+}
+
+function resolveElementClasses(existing, toAdd, toRemove) {
+  var ADD_CLASS = 1;
+  var REMOVE_CLASS = -1;
+
+  var flags = {};
+  existing = splitClassesToLookup(existing);
+
+  toAdd = splitClassesToLookup(toAdd);
+  forEach(toAdd, function(value, key) {
+    flags[key] = ADD_CLASS;
+  });
+
+  toRemove = splitClassesToLookup(toRemove);
+  forEach(toRemove, function(value, key) {
+    flags[key] = flags[key] === ADD_CLASS ? null : REMOVE_CLASS;
+  });
+
+  var classes = {
+    addClass: '',
+    removeClass: ''
+  };
+
+  forEach(flags, function(val, klass) {
+    var prop, allow;
+    if (val === ADD_CLASS) {
+      prop = 'addClass';
+      allow = !existing[klass];
+    } else if (val === REMOVE_CLASS) {
+      prop = 'removeClass';
+      allow = existing[klass];
+    }
+    if (allow) {
+      if (classes[prop].length) {
+        classes[prop] += ' ';
+      }
+      classes[prop] += klass;
+    }
+  });
+
+  function splitClassesToLookup(classes) {
+    if (isString(classes)) {
+      classes = classes.split(' ');
+    }
+
+    var obj = {};
+    forEach(classes, function(klass) {
+      // sometimes the split leaves empty string values
+      // incase extra spaces were applied to the options
+      if (klass.length) {
+        obj[klass] = true;
+      }
+    });
+    return obj;
+  }
+
+  return classes;
+}
+
+function getDomNode(element) {
+  return (element instanceof angular.element) ? element[0] : element;
+}
+
+function applyGeneratedPreparationClasses(element, event, options) {
+  var classes = '';
+  if (event) {
+    classes = pendClasses(event, EVENT_CLASS_PREFIX, true);
+  }
+  if (options.addClass) {
+    classes = concatWithSpace(classes, pendClasses(options.addClass, ADD_CLASS_SUFFIX));
+  }
+  if (options.removeClass) {
+    classes = concatWithSpace(classes, pendClasses(options.removeClass, REMOVE_CLASS_SUFFIX));
+  }
+  if (classes.length) {
+    options.preparationClasses = classes;
+    element.addClass(classes);
+  }
+}
+
+function clearGeneratedClasses(element, options) {
+  if (options.preparationClasses) {
+    element.removeClass(options.preparationClasses);
+    options.preparationClasses = null;
+  }
+  if (options.activeClasses) {
+    element.removeClass(options.activeClasses);
+    options.activeClasses = null;
+  }
+}
+
+function blockTransitions(node, duration) {
+  // we use a negative delay value since it performs blocking
+  // yet it doesn't kill any existing transitions running on the
+  // same element which makes this safe for class-based animations
+  var value = duration ? '-' + duration + 's' : '';
+  applyInlineStyle(node, [TRANSITION_DELAY_PROP, value]);
+  return [TRANSITION_DELAY_PROP, value];
+}
+
+function blockKeyframeAnimations(node, applyBlock) {
+  var value = applyBlock ? 'paused' : '';
+  var key = ANIMATION_PROP + ANIMATION_PLAYSTATE_KEY;
+  applyInlineStyle(node, [key, value]);
+  return [key, value];
+}
+
+function applyInlineStyle(node, styleTuple) {
+  var prop = styleTuple[0];
+  var value = styleTuple[1];
+  node.style[prop] = value;
+}
+
+function concatWithSpace(a,b) {
+  if (!a) return b;
+  if (!b) return a;
+  return a + ' ' + b;
+}
+
+var $$rAFSchedulerFactory = ['$$rAF', function($$rAF) {
+  var queue, cancelFn;
+
+  function scheduler(tasks) {
+    // we make a copy since RAFScheduler mutates the state
+    // of the passed in array variable and this would be difficult
+    // to track down on the outside code
+    queue = queue.concat(tasks);
+    nextTick();
+  }
+
+  queue = scheduler.queue = [];
+
+  /* waitUntilQuiet does two things:
+   * 1. It will run the FINAL `fn` value only when an uncancelled RAF has passed through
+   * 2. It will delay the next wave of tasks from running until the quiet `fn` has run.
+   *
+   * The motivation here is that animation code can request more time from the scheduler
+   * before the next wave runs. This allows for certain DOM properties such as classes to
+   * be resolved in time for the next animation to run.
+   */
+  scheduler.waitUntilQuiet = function(fn) {
+    if (cancelFn) cancelFn();
+
+    cancelFn = $$rAF(function() {
+      cancelFn = null;
+      fn();
+      nextTick();
+    });
+  };
+
+  return scheduler;
+
+  function nextTick() {
+    if (!queue.length) return;
+
+    var items = queue.shift();
+    for (var i = 0; i < items.length; i++) {
+      items[i]();
+    }
+
+    if (!cancelFn) {
+      $$rAF(function() {
+        if (!cancelFn) nextTick();
+      });
+    }
+  }
+}];
+
+var $$AnimateChildrenDirective = [function() {
+  return function(scope, element, attrs) {
+    var val = attrs.ngAnimateChildren;
+    if (angular.isString(val) && val.length === 0) { //empty attribute
+      element.data(NG_ANIMATE_CHILDREN_DATA, true);
+    } else {
+      attrs.$observe('ngAnimateChildren', function(value) {
+        value = value === 'on' || value === 'true';
+        element.data(NG_ANIMATE_CHILDREN_DATA, value);
+      });
+    }
+  };
+}];
+
+var ANIMATE_TIMER_KEY = '$$animateCss';
+
+/**
+ * @ngdoc service
+ * @name $animateCss
+ * @kind object
+ *
+ * @description
+ * The `$animateCss` service is a useful utility to trigger customized CSS-based transitions/keyframes
+ * from a JavaScript-based animation or directly from a directive. The purpose of `$animateCss` is NOT
+ * to side-step how `$animate` and ngAnimate work, but the goal is to allow pre-existing animations or
+ * directives to create more complex animations that can be purely driven using CSS code.
+ *
+ * Note that only browsers that support CSS transitions and/or keyframe animations are capable of
+ * rendering animations triggered via `$animateCss` (bad news for IE9 and lower).
+ *
+ * ## Usage
+ * Once again, `$animateCss` is designed to be used inside of a registered JavaScript animation that
+ * is powered by ngAnimate. It is possible to use `$animateCss` directly inside of a directive, however,
+ * any automatic control over cancelling animations and/or preventing animations from being run on
+ * child elements will not be handled by Angular. For this to work as expected, please use `$animate` to
+ * trigger the animation and then setup a JavaScript animation that injects `$animateCss` to trigger
+ * the CSS animation.
+ *
+ * The example below shows how we can create a folding animation on an element using `ng-if`:
+ *
+ * ```html
+ * <!-- notice the `fold-animation` CSS class -->
+ * <div ng-if="onOff" class="fold-animation">
+ *   This element will go BOOM
+ * </div>
+ * <button ng-click="onOff=true">Fold In</button>
+ * ```
+ *
+ * Now we create the **JavaScript animation** that will trigger the CSS transition:
+ *
+ * ```js
+ * ngModule.animation('.fold-animation', ['$animateCss', function($animateCss) {
+ *   return {
+ *     enter: function(element, doneFn) {
+ *       var height = element[0].offsetHeight;
+ *       return $animateCss(element, {
+ *         from: { height:'0px' },
+ *         to: { height:height + 'px' },
+ *         duration: 1 // one second
+ *       });
+ *     }
+ *   }
+ * }]);
+ * ```
+ *
+ * ## More Advanced Uses
+ *
+ * `$animateCss` is the underlying code that ngAnimate uses to power **CSS-based animations** behind the scenes. Therefore CSS hooks
+ * like `.ng-EVENT`, `.ng-EVENT-active`, `.ng-EVENT-stagger` are all features that can be triggered using `$animateCss` via JavaScript code.
+ *
+ * This also means that just about any combination of adding classes, removing classes, setting styles, dynamically setting a keyframe animation,
+ * applying a hardcoded duration or delay value, changing the animation easing or applying a stagger animation are all options that work with
+ * `$animateCss`. The service itself is smart enough to figure out the combination of options and examine the element styling properties in order
+ * to provide a working animation that will run in CSS.
+ *
+ * The example below showcases a more advanced version of the `.fold-animation` from the example above:
+ *
+ * ```js
+ * ngModule.animation('.fold-animation', ['$animateCss', function($animateCss) {
+ *   return {
+ *     enter: function(element, doneFn) {
+ *       var height = element[0].offsetHeight;
+ *       return $animateCss(element, {
+ *         addClass: 'red large-text pulse-twice',
+ *         easing: 'ease-out',
+ *         from: { height:'0px' },
+ *         to: { height:height + 'px' },
+ *         duration: 1 // one second
+ *       });
+ *     }
+ *   }
+ * }]);
+ * ```
+ *
+ * Since we're adding/removing CSS classes then the CSS transition will also pick those up:
+ *
+ * ```css
+ * /&#42; since a hardcoded duration value of 1 was provided in the JavaScript animation code,
+ * the CSS classes below will be transitioned despite them being defined as regular CSS classes &#42;/
+ * .red { background:red; }
+ * .large-text { font-size:20px; }
+ *
+ * /&#42; we can also use a keyframe animation and $animateCss will make it work alongside the transition &#42;/
+ * .pulse-twice {
+ *   animation: 0.5s pulse linear 2;
+ *   -webkit-animation: 0.5s pulse linear 2;
+ * }
+ *
+ * @keyframes pulse {
+ *   from { transform: scale(0.5); }
+ *   to { transform: scale(1.5); }
+ * }
+ *
+ * @-webkit-keyframes pulse {
+ *   from { -webkit-transform: scale(0.5); }
+ *   to { -webkit-transform: scale(1.5); }
+ * }
+ * ```
+ *
+ * Given this complex combination of CSS classes, styles and options, `$animateCss` will figure everything out and make the animation happen.
+ *
+ * ## How the Options are handled
+ *
+ * `$animateCss` is very versatile and intelligent when it comes to figuring out what configurations to apply to the element to ensure the animation
+ * works with the options provided. Say for example we were adding a class that contained a keyframe value and we wanted to also animate some inline
+ * styles using the `from` and `to` properties.
+ *
+ * ```js
+ * var animator = $animateCss(element, {
+ *   from: { background:'red' },
+ *   to: { background:'blue' }
+ * });
+ * animator.start();
+ * ```
+ *
+ * ```css
+ * .rotating-animation {
+ *   animation:0.5s rotate linear;
+ *   -webkit-animation:0.5s rotate linear;
+ * }
+ *
+ * @keyframes rotate {
+ *   from { transform: rotate(0deg); }
+ *   to { transform: rotate(360deg); }
+ * }
+ *
+ * @-webkit-keyframes rotate {
+ *   from { -webkit-transform: rotate(0deg); }
+ *   to { -webkit-transform: rotate(360deg); }
+ * }
+ * ```
+ *
+ * The missing pieces here are that we do not have a transition set (within the CSS code nor within the `$animateCss` options) and the duration of the animation is
+ * going to be detected from what the keyframe styles on the CSS class are. In this event, `$animateCss` will automatically create an inline transition
+ * style matching the duration detected from the keyframe style (which is present in the CSS class that is being added) and then prepare both the transition
+ * and keyframe animations to run in parallel on the element. Then when the animation is underway the provided `from` and `to` CSS styles will be applied
+ * and spread across the transition and keyframe animation.
+ *
+ * ## What is returned
+ *
+ * `$animateCss` works in two stages: a preparation phase and an animation phase. Therefore when `$animateCss` is first called it will NOT actually
+ * start the animation. All that is going on here is that the element is being prepared for the animation (which means that the generated CSS classes are
+ * added and removed on the element). Once `$animateCss` is called it will return an object with the following properties:
+ *
+ * ```js
+ * var animator = $animateCss(element, { ... });
+ * ```
+ *
+ * Now what do the contents of our `animator` variable look like:
+ *
+ * ```js
+ * {
+ *   // starts the animation
+ *   start: Function,
+ *
+ *   // ends (aborts) the animation
+ *   end: Function
+ * }
+ * ```
+ *
+ * To actually start the animation we need to run `animation.start()` which will then return a promise that we can hook into to detect when the animation ends.
+ * If we choose not to run the animation then we MUST run `animation.end()` to perform a cleanup on the element (since some CSS classes and stlyes may have been
+ * applied to the element during the preparation phase). Note that all other properties such as duration, delay, transitions and keyframes are just properties
+ * and that changing them will not reconfigure the parameters of the animation.
+ *
+ * ### runner.done() vs runner.then()
+ * It is documented that `animation.start()` will return a promise object and this is true, however, there is also an additional method available on the
+ * runner called `.done(callbackFn)`. The done method works the same as `.finally(callbackFn)`, however, it does **not trigger a digest to occur**.
+ * Therefore, for performance reasons, it's always best to use `runner.done(callback)` instead of `runner.then()`, `runner.catch()` or `runner.finally()`
+ * unless you really need a digest to kick off afterwards.
+ *
+ * Keep in mind that, to make this easier, ngAnimate has tweaked the JS animations API to recognize when a runner instance is returned from $animateCss
+ * (so there is no need to call `runner.done(doneFn)` inside of your JavaScript animation code).
+ * Check the {@link ngAnimate.$animateCss#usage animation code above} to see how this works.
+ *
+ * @param {DOMElement} element the element that will be animated
+ * @param {object} options the animation-related options that will be applied during the animation
+ *
+ * * `event` - The DOM event (e.g. enter, leave, move). When used, a generated CSS class of `ng-EVENT` and `ng-EVENT-active` will be applied
+ * to the element during the animation. Multiple events can be provided when spaces are used as a separator. (Note that this will not perform any DOM operation.)
+ * * `easing` - The CSS easing value that will be applied to the transition or keyframe animation (or both).
+ * * `transitionStyle` - The raw CSS transition style that will be used (e.g. `1s linear all`).
+ * * `keyframeStyle` - The raw CSS keyframe animation style that will be used (e.g. `1s my_animation linear`).
+ * * `from` - The starting CSS styles (a key/value object) that will be applied at the start of the animation.
+ * * `to` - The ending CSS styles (a key/value object) that will be applied across the animation via a CSS transition.
+ * * `addClass` - A space separated list of CSS classes that will be added to the element and spread across the animation.
+ * * `removeClass` - A space separated list of CSS classes that will be removed from the element and spread across the animation.
+ * * `duration` - A number value representing the total duration of the transition and/or keyframe (note that a value of 1 is 1000ms). If a value of `0`
+ * is provided then the animation will be skipped entirely.
+ * * `delay` - A number value representing the total delay of the transition and/or keyframe (note that a value of 1 is 1000ms). If a value of `true` is
+ * used then whatever delay value is detected from the CSS classes will be mirrored on the elements styles (e.g. by setting delay true then the style value
+ * of the element will be `transition-delay: DETECTED_VALUE`). Using `true` is useful when you want the CSS classes and inline styles to all share the same
+ * CSS delay value.
+ * * `stagger` - A numeric time value representing the delay between successively animated elements
+ * ({@link ngAnimate#css-staggering-animations Click here to learn how CSS-based staggering works in ngAnimate.})
+ * * `staggerIndex` - The numeric index representing the stagger item (e.g. a value of 5 is equal to the sixth item in the stagger; therefore when a
+ * * `stagger` option value of `0.1` is used then there will be a stagger delay of `600ms`)
+ * * `applyClassesEarly` - Whether or not the classes being added or removed will be used when detecting the animation. This is set by `$animate` when enter/leave/move animations are fired to ensure that the CSS classes are resolved in time. (Note that this will prevent any transitions from occuring on the classes being added and removed.)
+ * * `cleanupStyles` - Whether or not the provided `from` and `to` styles will be removed once
+ *    the animation is closed. This is useful for when the styles are used purely for the sake of
+ *    the animation and do not have a lasting visual effect on the element (e.g. a colapse and open animation).
+ *    By default this value is set to `false`.
+ *
+ * @return {object} an object with start and end methods and details about the animation.
+ *
+ * * `start` - The method to start the animation. This will return a `Promise` when called.
+ * * `end` - This method will cancel the animation and remove all applied CSS classes and styles.
+ */
+var ONE_SECOND = 1000;
+var BASE_TEN = 10;
+
+var ELAPSED_TIME_MAX_DECIMAL_PLACES = 3;
+var CLOSING_TIME_BUFFER = 1.5;
+
+var DETECT_CSS_PROPERTIES = {
+  transitionDuration:      TRANSITION_DURATION_PROP,
+  transitionDelay:         TRANSITION_DELAY_PROP,
+  transitionProperty:      TRANSITION_PROP + PROPERTY_KEY,
+  animationDuration:       ANIMATION_DURATION_PROP,
+  animationDelay:          ANIMATION_DELAY_PROP,
+  animationIterationCount: ANIMATION_PROP + ANIMATION_ITERATION_COUNT_KEY
+};
+
+var DETECT_STAGGER_CSS_PROPERTIES = {
+  transitionDuration:      TRANSITION_DURATION_PROP,
+  transitionDelay:         TRANSITION_DELAY_PROP,
+  animationDuration:       ANIMATION_DURATION_PROP,
+  animationDelay:          ANIMATION_DELAY_PROP
+};
+
+function getCssKeyframeDurationStyle(duration) {
+  return [ANIMATION_DURATION_PROP, duration + 's'];
+}
+
+function getCssDelayStyle(delay, isKeyframeAnimation) {
+  var prop = isKeyframeAnimation ? ANIMATION_DELAY_PROP : TRANSITION_DELAY_PROP;
+  return [prop, delay + 's'];
+}
+
+function computeCssStyles($window, element, properties) {
+  var styles = Object.create(null);
+  var detectedStyles = $window.getComputedStyle(element) || {};
+  forEach(properties, function(formalStyleName, actualStyleName) {
+    var val = detectedStyles[formalStyleName];
+    if (val) {
+      var c = val.charAt(0);
+
+      // only numerical-based values have a negative sign or digit as the first value
+      if (c === '-' || c === '+' || c >= 0) {
+        val = parseMaxTime(val);
+      }
+
+      // by setting this to null in the event that the delay is not set or is set directly as 0
+      // then we can still allow for zegative values to be used later on and not mistake this
+      // value for being greater than any other negative value.
+      if (val === 0) {
+        val = null;
+      }
+      styles[actualStyleName] = val;
+    }
+  });
+
+  return styles;
+}
+
+function parseMaxTime(str) {
+  var maxValue = 0;
+  var values = str.split(/\s*,\s*/);
+  forEach(values, function(value) {
+    // it's always safe to consider only second values and omit `ms` values since
+    // getComputedStyle will always handle the conversion for us
+    if (value.charAt(value.length - 1) == 's') {
+      value = value.substring(0, value.length - 1);
+    }
+    value = parseFloat(value) || 0;
+    maxValue = maxValue ? Math.max(value, maxValue) : value;
+  });
+  return maxValue;
+}
+
+function truthyTimingValue(val) {
+  return val === 0 || val != null;
+}
+
+function getCssTransitionDurationStyle(duration, applyOnlyDuration) {
+  var style = TRANSITION_PROP;
+  var value = duration + 's';
+  if (applyOnlyDuration) {
+    style += DURATION_KEY;
+  } else {
+    value += ' linear all';
+  }
+  return [style, value];
+}
+
+function createLocalCacheLookup() {
+  var cache = Object.create(null);
+  return {
+    flush: function() {
+      cache = Object.create(null);
+    },
+
+    count: function(key) {
+      var entry = cache[key];
+      return entry ? entry.total : 0;
+    },
+
+    get: function(key) {
+      var entry = cache[key];
+      return entry && entry.value;
+    },
+
+    put: function(key, value) {
+      if (!cache[key]) {
+        cache[key] = { total: 1, value: value };
+      } else {
+        cache[key].total++;
+      }
+    }
+  };
+}
+
+// we do not reassign an already present style value since
+// if we detect the style property value again we may be
+// detecting styles that were added via the `from` styles.
+// We make use of `isDefined` here since an empty string
+// or null value (which is what getPropertyValue will return
+// for a non-existing style) will still be marked as a valid
+// value for the style (a falsy value implies that the style
+// is to be removed at the end of the animation). If we had a simple
+// "OR" statement then it would not be enough to catch that.
+function registerRestorableStyles(backup, node, properties) {
+  forEach(properties, function(prop) {
+    backup[prop] = isDefined(backup[prop])
+        ? backup[prop]
+        : node.style.getPropertyValue(prop);
+  });
+}
+
+var $AnimateCssProvider = ['$animateProvider', function($animateProvider) {
+  var gcsLookup = createLocalCacheLookup();
+  var gcsStaggerLookup = createLocalCacheLookup();
+
+  this.$get = ['$window', '$$jqLite', '$$AnimateRunner', '$timeout',
+               '$$forceReflow', '$sniffer', '$$rAFScheduler', '$animate',
+       function($window,   $$jqLite,   $$AnimateRunner,   $timeout,
+                $$forceReflow,   $sniffer,   $$rAFScheduler, $animate) {
+
+    var applyAnimationClasses = applyAnimationClassesFactory($$jqLite);
+
+    var parentCounter = 0;
+    function gcsHashFn(node, extraClasses) {
+      var KEY = "$$ngAnimateParentKey";
+      var parentNode = node.parentNode;
+      var parentID = parentNode[KEY] || (parentNode[KEY] = ++parentCounter);
+      return parentID + '-' + node.getAttribute('class') + '-' + extraClasses;
+    }
+
+    function computeCachedCssStyles(node, className, cacheKey, properties) {
+      var timings = gcsLookup.get(cacheKey);
+
+      if (!timings) {
+        timings = computeCssStyles($window, node, properties);
+        if (timings.animationIterationCount === 'infinite') {
+          timings.animationIterationCount = 1;
+        }
+      }
+
+      // we keep putting this in multiple times even though the value and the cacheKey are the same
+      // because we're keeping an interal tally of how many duplicate animations are detected.
+      gcsLookup.put(cacheKey, timings);
+      return timings;
+    }
+
+    function computeCachedCssStaggerStyles(node, className, cacheKey, properties) {
+      var stagger;
+
+      // if we have one or more existing matches of matching elements
+      // containing the same parent + CSS styles (which is how cacheKey works)
+      // then staggering is possible
+      if (gcsLookup.count(cacheKey) > 0) {
+        stagger = gcsStaggerLookup.get(cacheKey);
+
+        if (!stagger) {
+          var staggerClassName = pendClasses(className, '-stagger');
+
+          $$jqLite.addClass(node, staggerClassName);
+
+          stagger = computeCssStyles($window, node, properties);
+
+          // force the conversion of a null value to zero incase not set
+          stagger.animationDuration = Math.max(stagger.animationDuration, 0);
+          stagger.transitionDuration = Math.max(stagger.transitionDuration, 0);
+
+          $$jqLite.removeClass(node, staggerClassName);
+
+          gcsStaggerLookup.put(cacheKey, stagger);
+        }
+      }
+
+      return stagger || {};
+    }
+
+    var cancelLastRAFRequest;
+    var rafWaitQueue = [];
+    function waitUntilQuiet(callback) {
+      rafWaitQueue.push(callback);
+      $$rAFScheduler.waitUntilQuiet(function() {
+        gcsLookup.flush();
+        gcsStaggerLookup.flush();
+
+        // DO NOT REMOVE THIS LINE OR REFACTOR OUT THE `pageWidth` variable.
+        // PLEASE EXAMINE THE `$$forceReflow` service to understand why.
+        var pageWidth = $$forceReflow();
+
+        // we use a for loop to ensure that if the queue is changed
+        // during this looping then it will consider new requests
+        for (var i = 0; i < rafWaitQueue.length; i++) {
+          rafWaitQueue[i](pageWidth);
+        }
+        rafWaitQueue.length = 0;
+      });
+    }
+
+    function computeTimings(node, className, cacheKey) {
+      var timings = computeCachedCssStyles(node, className, cacheKey, DETECT_CSS_PROPERTIES);
+      var aD = timings.animationDelay;
+      var tD = timings.transitionDelay;
+      timings.maxDelay = aD && tD
+          ? Math.max(aD, tD)
+          : (aD || tD);
+      timings.maxDuration = Math.max(
+          timings.animationDuration * timings.animationIterationCount,
+          timings.transitionDuration);
+
+      return timings;
+    }
+
+    return function init(element, options) {
+      var restoreStyles = {};
+      var node = getDomNode(element);
+      if (!node
+          || !node.parentNode
+          || !$animate.enabled()) {
+        return closeAndReturnNoopAnimator();
+      }
+
+      options = prepareAnimationOptions(options);
+
+      var temporaryStyles = [];
+      var classes = element.attr('class');
+      var styles = packageStyles(options);
+      var animationClosed;
+      var animationPaused;
+      var animationCompleted;
+      var runner;
+      var runnerHost;
+      var maxDelay;
+      var maxDelayTime;
+      var maxDuration;
+      var maxDurationTime;
+
+      if (options.duration === 0 || (!$sniffer.animations && !$sniffer.transitions)) {
+        return closeAndReturnNoopAnimator();
+      }
+
+      var method = options.event && isArray(options.event)
+            ? options.event.join(' ')
+            : options.event;
+
+      var isStructural = method && options.structural;
+      var structuralClassName = '';
+      var addRemoveClassName = '';
+
+      if (isStructural) {
+        structuralClassName = pendClasses(method, EVENT_CLASS_PREFIX, true);
+      } else if (method) {
+        structuralClassName = method;
+      }
+
+      if (options.addClass) {
+        addRemoveClassName += pendClasses(options.addClass, ADD_CLASS_SUFFIX);
+      }
+
+      if (options.removeClass) {
+        if (addRemoveClassName.length) {
+          addRemoveClassName += ' ';
+        }
+        addRemoveClassName += pendClasses(options.removeClass, REMOVE_CLASS_SUFFIX);
+      }
+
+      // there may be a situation where a structural animation is combined together
+      // with CSS classes that need to resolve before the animation is computed.
+      // However this means that there is no explicit CSS code to block the animation
+      // from happening (by setting 0s none in the class name). If this is the case
+      // we need to apply the classes before the first rAF so we know to continue if
+      // there actually is a detected transition or keyframe animation
+      if (options.applyClassesEarly && addRemoveClassName.length) {
+        applyAnimationClasses(element, options);
+      }
+
+      var preparationClasses = [structuralClassName, addRemoveClassName].join(' ').trim();
+      var fullClassName = classes + ' ' + preparationClasses;
+      var activeClasses = pendClasses(preparationClasses, ACTIVE_CLASS_SUFFIX);
+      var hasToStyles = styles.to && Object.keys(styles.to).length > 0;
+      var containsKeyframeAnimation = (options.keyframeStyle || '').length > 0;
+
+      // there is no way we can trigger an animation if no styles and
+      // no classes are being applied which would then trigger a transition,
+      // unless there a is raw keyframe value that is applied to the element.
+      if (!containsKeyframeAnimation
+           && !hasToStyles
+           && !preparationClasses) {
+        return closeAndReturnNoopAnimator();
+      }
+
+      var cacheKey, stagger;
+      if (options.stagger > 0) {
+        var staggerVal = parseFloat(options.stagger);
+        stagger = {
+          transitionDelay: staggerVal,
+          animationDelay: staggerVal,
+          transitionDuration: 0,
+          animationDuration: 0
+        };
+      } else {
+        cacheKey = gcsHashFn(node, fullClassName);
+        stagger = computeCachedCssStaggerStyles(node, preparationClasses, cacheKey, DETECT_STAGGER_CSS_PROPERTIES);
+      }
+
+      if (!options.$$skipPreparationClasses) {
+        $$jqLite.addClass(element, preparationClasses);
+      }
+
+      var applyOnlyDuration;
+
+      if (options.transitionStyle) {
+        var transitionStyle = [TRANSITION_PROP, options.transitionStyle];
+        applyInlineStyle(node, transitionStyle);
+        temporaryStyles.push(transitionStyle);
+      }
+
+      if (options.duration >= 0) {
+        applyOnlyDuration = node.style[TRANSITION_PROP].length > 0;
+        var durationStyle = getCssTransitionDurationStyle(options.duration, applyOnlyDuration);
+
+        // we set the duration so that it will be picked up by getComputedStyle later
+        applyInlineStyle(node, durationStyle);
+        temporaryStyles.push(durationStyle);
+      }
+
+      if (options.keyframeStyle) {
+        var keyframeStyle = [ANIMATION_PROP, options.keyframeStyle];
+        applyInlineStyle(node, keyframeStyle);
+        temporaryStyles.push(keyframeStyle);
+      }
+
+      var itemIndex = stagger
+          ? options.staggerIndex >= 0
+              ? options.staggerIndex
+              : gcsLookup.count(cacheKey)
+          : 0;
+
+      var isFirst = itemIndex === 0;
+
+      // this is a pre-emptive way of forcing the setup classes to be added and applied INSTANTLY
+      // without causing any combination of transitions to kick in. By adding a negative delay value
+      // it forces the setup class' transition to end immediately. We later then remove the negative
+      // transition delay to allow for the transition to naturally do it's thing. The beauty here is
+      // that if there is no transition defined then nothing will happen and this will also allow
+      // other transitions to be stacked on top of each other without any chopping them out.
+      if (isFirst && !options.skipBlocking) {
+        blockTransitions(node, SAFE_FAST_FORWARD_DURATION_VALUE);
+      }
+
+      var timings = computeTimings(node, fullClassName, cacheKey);
+      var relativeDelay = timings.maxDelay;
+      maxDelay = Math.max(relativeDelay, 0);
+      maxDuration = timings.maxDuration;
+
+      var flags = {};
+      flags.hasTransitions          = timings.transitionDuration > 0;
+      flags.hasAnimations           = timings.animationDuration > 0;
+      flags.hasTransitionAll        = flags.hasTransitions && timings.transitionProperty == 'all';
+      flags.applyTransitionDuration = hasToStyles && (
+                                        (flags.hasTransitions && !flags.hasTransitionAll)
+                                         || (flags.hasAnimations && !flags.hasTransitions));
+      flags.applyAnimationDuration  = options.duration && flags.hasAnimations;
+      flags.applyTransitionDelay    = truthyTimingValue(options.delay) && (flags.applyTransitionDuration || flags.hasTransitions);
+      flags.applyAnimationDelay     = truthyTimingValue(options.delay) && flags.hasAnimations;
+      flags.recalculateTimingStyles = addRemoveClassName.length > 0;
+
+      if (flags.applyTransitionDuration || flags.applyAnimationDuration) {
+        maxDuration = options.duration ? parseFloat(options.duration) : maxDuration;
+
+        if (flags.applyTransitionDuration) {
+          flags.hasTransitions = true;
+          timings.transitionDuration = maxDuration;
+          applyOnlyDuration = node.style[TRANSITION_PROP + PROPERTY_KEY].length > 0;
+          temporaryStyles.push(getCssTransitionDurationStyle(maxDuration, applyOnlyDuration));
+        }
+
+        if (flags.applyAnimationDuration) {
+          flags.hasAnimations = true;
+          timings.animationDuration = maxDuration;
+          temporaryStyles.push(getCssKeyframeDurationStyle(maxDuration));
+        }
+      }
+
+      if (maxDuration === 0 && !flags.recalculateTimingStyles) {
+        return closeAndReturnNoopAnimator();
+      }
+
+      if (options.delay != null) {
+        var delayStyle = parseFloat(options.delay);
+
+        if (flags.applyTransitionDelay) {
+          temporaryStyles.push(getCssDelayStyle(delayStyle));
+        }
+
+        if (flags.applyAnimationDelay) {
+          temporaryStyles.push(getCssDelayStyle(delayStyle, true));
+        }
+      }
+
+      // we need to recalculate the delay value since we used a pre-emptive negative
+      // delay value and the delay value is required for the final event checking. This
+      // property will ensure that this will happen after the RAF phase has passed.
+      if (options.duration == null && timings.transitionDuration > 0) {
+        flags.recalculateTimingStyles = flags.recalculateTimingStyles || isFirst;
+      }
+
+      maxDelayTime = maxDelay * ONE_SECOND;
+      maxDurationTime = maxDuration * ONE_SECOND;
+      if (!options.skipBlocking) {
+        flags.blockTransition = timings.transitionDuration > 0;
+        flags.blockKeyframeAnimation = timings.animationDuration > 0 &&
+                                       stagger.animationDelay > 0 &&
+                                       stagger.animationDuration === 0;
+      }
+
+      if (options.from) {
+        if (options.cleanupStyles) {
+          registerRestorableStyles(restoreStyles, node, Object.keys(options.from));
+        }
+        applyAnimationFromStyles(element, options);
+      }
+
+      if (flags.blockTransition || flags.blockKeyframeAnimation) {
+        applyBlocking(maxDuration);
+      } else if (!options.skipBlocking) {
+        blockTransitions(node, false);
+      }
+
+      // TODO(matsko): for 1.5 change this code to have an animator object for better debugging
+      return {
+        $$willAnimate: true,
+        end: endFn,
+        start: function() {
+          if (animationClosed) return;
+
+          runnerHost = {
+            end: endFn,
+            cancel: cancelFn,
+            resume: null, //this will be set during the start() phase
+            pause: null
+          };
+
+          runner = new $$AnimateRunner(runnerHost);
+
+          waitUntilQuiet(start);
+
+          // we don't have access to pause/resume the animation
+          // since it hasn't run yet. AnimateRunner will therefore
+          // set noop functions for resume and pause and they will
+          // later be overridden once the animation is triggered
+          return runner;
+        }
+      };
+
+      function endFn() {
+        close();
+      }
+
+      function cancelFn() {
+        close(true);
+      }
+
+      function close(rejected) { // jshint ignore:line
+        // if the promise has been called already then we shouldn't close
+        // the animation again
+        if (animationClosed || (animationCompleted && animationPaused)) return;
+        animationClosed = true;
+        animationPaused = false;
+
+        if (!options.$$skipPreparationClasses) {
+          $$jqLite.removeClass(element, preparationClasses);
+        }
+        $$jqLite.removeClass(element, activeClasses);
+
+        blockKeyframeAnimations(node, false);
+        blockTransitions(node, false);
+
+        forEach(temporaryStyles, function(entry) {
+          // There is only one way to remove inline style properties entirely from elements.
+          // By using `removeProperty` this works, but we need to convert camel-cased CSS
+          // styles down to hyphenated values.
+          node.style[entry[0]] = '';
+        });
+
+        applyAnimationClasses(element, options);
+        applyAnimationStyles(element, options);
+
+        if (Object.keys(restoreStyles).length) {
+          forEach(restoreStyles, function(value, prop) {
+            value ? node.style.setProperty(prop, value)
+                  : node.style.removeProperty(prop);
+          });
+        }
+
+        // the reason why we have this option is to allow a synchronous closing callback
+        // that is fired as SOON as the animation ends (when the CSS is removed) or if
+        // the animation never takes off at all. A good example is a leave animation since
+        // the element must be removed just after the animation is over or else the element
+        // will appear on screen for one animation frame causing an overbearing flicker.
+        if (options.onDone) {
+          options.onDone();
+        }
+
+        // if the preparation function fails then the promise is not setup
+        if (runner) {
+          runner.complete(!rejected);
+        }
+      }
+
+      function applyBlocking(duration) {
+        if (flags.blockTransition) {
+          blockTransitions(node, duration);
+        }
+
+        if (flags.blockKeyframeAnimation) {
+          blockKeyframeAnimations(node, !!duration);
+        }
+      }
+
+      function closeAndReturnNoopAnimator() {
+        runner = new $$AnimateRunner({
+          end: endFn,
+          cancel: cancelFn
+        });
+
+        // should flush the cache animation
+        waitUntilQuiet(noop);
+        close();
+
+        return {
+          $$willAnimate: false,
+          start: function() {
+            return runner;
+          },
+          end: endFn
+        };
+      }
+
+      function start() {
+        if (animationClosed) return;
+        if (!node.parentNode) {
+          close();
+          return;
+        }
+
+        var startTime, events = [];
+
+        // even though we only pause keyframe animations here the pause flag
+        // will still happen when transitions are used. Only the transition will
+        // not be paused since that is not possible. If the animation ends when
+        // paused then it will not complete until unpaused or cancelled.
+        var playPause = function(playAnimation) {
+          if (!animationCompleted) {
+            animationPaused = !playAnimation;
+            if (timings.animationDuration) {
+              var value = blockKeyframeAnimations(node, animationPaused);
+              animationPaused
+                  ? temporaryStyles.push(value)
+                  : removeFromArray(temporaryStyles, value);
+            }
+          } else if (animationPaused && playAnimation) {
+            animationPaused = false;
+            close();
+          }
+        };
+
+        // checking the stagger duration prevents an accidently cascade of the CSS delay style
+        // being inherited from the parent. If the transition duration is zero then we can safely
+        // rely that the delay value is an intential stagger delay style.
+        var maxStagger = itemIndex > 0
+                         && ((timings.transitionDuration && stagger.transitionDuration === 0) ||
+                            (timings.animationDuration && stagger.animationDuration === 0))
+                         && Math.max(stagger.animationDelay, stagger.transitionDelay);
+        if (maxStagger) {
+          $timeout(triggerAnimationStart,
+                   Math.floor(maxStagger * itemIndex * ONE_SECOND),
+                   false);
+        } else {
+          triggerAnimationStart();
+        }
+
+        // this will decorate the existing promise runner with pause/resume methods
+        runnerHost.resume = function() {
+          playPause(true);
+        };
+
+        runnerHost.pause = function() {
+          playPause(false);
+        };
+
+        function triggerAnimationStart() {
+          // just incase a stagger animation kicks in when the animation
+          // itself was cancelled entirely
+          if (animationClosed) return;
+
+          applyBlocking(false);
+
+          forEach(temporaryStyles, function(entry) {
+            var key = entry[0];
+            var value = entry[1];
+            node.style[key] = value;
+          });
+
+          applyAnimationClasses(element, options);
+          $$jqLite.addClass(element, activeClasses);
+
+          if (flags.recalculateTimingStyles) {
+            fullClassName = node.className + ' ' + preparationClasses;
+            cacheKey = gcsHashFn(node, fullClassName);
+
+            timings = computeTimings(node, fullClassName, cacheKey);
+            relativeDelay = timings.maxDelay;
+            maxDelay = Math.max(relativeDelay, 0);
+            maxDuration = timings.maxDuration;
+
+            if (maxDuration === 0) {
+              close();
+              return;
+            }
+
+            flags.hasTransitions = timings.transitionDuration > 0;
+            flags.hasAnimations = timings.animationDuration > 0;
+          }
+
+          if (flags.applyAnimationDelay) {
+            relativeDelay = typeof options.delay !== "boolean" && truthyTimingValue(options.delay)
+                  ? parseFloat(options.delay)
+                  : relativeDelay;
+
+            maxDelay = Math.max(relativeDelay, 0);
+            timings.animationDelay = relativeDelay;
+            delayStyle = getCssDelayStyle(relativeDelay, true);
+            temporaryStyles.push(delayStyle);
+            node.style[delayStyle[0]] = delayStyle[1];
+          }
+
+          maxDelayTime = maxDelay * ONE_SECOND;
+          maxDurationTime = maxDuration * ONE_SECOND;
+
+          if (options.easing) {
+            var easeProp, easeVal = options.easing;
+            if (flags.hasTransitions) {
+              easeProp = TRANSITION_PROP + TIMING_KEY;
+              temporaryStyles.push([easeProp, easeVal]);
+              node.style[easeProp] = easeVal;
+            }
+            if (flags.hasAnimations) {
+              easeProp = ANIMATION_PROP + TIMING_KEY;
+              temporaryStyles.push([easeProp, easeVal]);
+              node.style[easeProp] = easeVal;
+            }
+          }
+
+          if (timings.transitionDuration) {
+            events.push(TRANSITIONEND_EVENT);
+          }
+
+          if (timings.animationDuration) {
+            events.push(ANIMATIONEND_EVENT);
+          }
+
+          startTime = Date.now();
+          var timerTime = maxDelayTime + CLOSING_TIME_BUFFER * maxDurationTime;
+          var endTime = startTime + timerTime;
+
+          var animationsData = element.data(ANIMATE_TIMER_KEY) || [];
+          var setupFallbackTimer = true;
+          if (animationsData.length) {
+            var currentTimerData = animationsData[0];
+            setupFallbackTimer = endTime > currentTimerData.expectedEndTime;
+            if (setupFallbackTimer) {
+              $timeout.cancel(currentTimerData.timer);
+            } else {
+              animationsData.push(close);
+            }
+          }
+
+          if (setupFallbackTimer) {
+            var timer = $timeout(onAnimationExpired, timerTime, false);
+            animationsData[0] = {
+              timer: timer,
+              expectedEndTime: endTime
+            };
+            animationsData.push(close);
+            element.data(ANIMATE_TIMER_KEY, animationsData);
+          }
+
+          element.on(events.join(' '), onAnimationProgress);
+          if (options.to) {
+            if (options.cleanupStyles) {
+              registerRestorableStyles(restoreStyles, node, Object.keys(options.to));
+            }
+            applyAnimationToStyles(element, options);
+          }
+        }
+
+        function onAnimationExpired() {
+          var animationsData = element.data(ANIMATE_TIMER_KEY);
+
+          // this will be false in the event that the element was
+          // removed from the DOM (via a leave animation or something
+          // similar)
+          if (animationsData) {
+            for (var i = 1; i < animationsData.length; i++) {
+              animationsData[i]();
+            }
+            element.removeData(ANIMATE_TIMER_KEY);
+          }
+        }
+
+        function onAnimationProgress(event) {
+          event.stopPropagation();
+          var ev = event.originalEvent || event;
+          var timeStamp = ev.$manualTimeStamp || ev.timeStamp || Date.now();
+
+          /* Firefox (or possibly just Gecko) likes to not round values up
+           * when a ms measurement is used for the animation */
+          var elapsedTime = parseFloat(ev.elapsedTime.toFixed(ELAPSED_TIME_MAX_DECIMAL_PLACES));
+
+          /* $manualTimeStamp is a mocked timeStamp value which is set
+           * within browserTrigger(). This is only here so that tests can
+           * mock animations properly. Real events fallback to event.timeStamp,
+           * or, if they don't, then a timeStamp is automatically created for them.
+           * We're checking to see if the timeStamp surpasses the expected delay,
+           * but we're using elapsedTime instead of the timeStamp on the 2nd
+           * pre-condition since animations sometimes close off early */
+          if (Math.max(timeStamp - startTime, 0) >= maxDelayTime && elapsedTime >= maxDuration) {
+            // we set this flag to ensure that if the transition is paused then, when resumed,
+            // the animation will automatically close itself since transitions cannot be paused.
+            animationCompleted = true;
+            close();
+          }
+        }
+      }
+    };
+  }];
+}];
+
+var $$AnimateCssDriverProvider = ['$$animationProvider', function($$animationProvider) {
+  $$animationProvider.drivers.push('$$animateCssDriver');
+
+  var NG_ANIMATE_SHIM_CLASS_NAME = 'ng-animate-shim';
+  var NG_ANIMATE_ANCHOR_CLASS_NAME = 'ng-anchor';
+
+  var NG_OUT_ANCHOR_CLASS_NAME = 'ng-anchor-out';
+  var NG_IN_ANCHOR_CLASS_NAME = 'ng-anchor-in';
+
+  function isDocumentFragment(node) {
+    return node.parentNode && node.parentNode.nodeType === 11;
+  }
+
+  this.$get = ['$animateCss', '$rootScope', '$$AnimateRunner', '$rootElement', '$sniffer', '$$jqLite', '$document',
+       function($animateCss,   $rootScope,   $$AnimateRunner,   $rootElement,   $sniffer,   $$jqLite,   $document) {
+
+    // only browsers that support these properties can render animations
+    if (!$sniffer.animations && !$sniffer.transitions) return noop;
+
+    var bodyNode = $document[0].body;
+    var rootNode = getDomNode($rootElement);
+
+    var rootBodyElement = jqLite(
+      // this is to avoid using something that exists outside of the body
+      // we also special case the doc fragement case because our unit test code
+      // appends the $rootElement to the body after the app has been bootstrapped
+      isDocumentFragment(rootNode) || bodyNode.contains(rootNode) ? rootNode : bodyNode
+    );
+
+    var applyAnimationClasses = applyAnimationClassesFactory($$jqLite);
+
+    return function initDriverFn(animationDetails) {
+      return animationDetails.from && animationDetails.to
+          ? prepareFromToAnchorAnimation(animationDetails.from,
+                                         animationDetails.to,
+                                         animationDetails.classes,
+                                         animationDetails.anchors)
+          : prepareRegularAnimation(animationDetails);
+    };
+
+    function filterCssClasses(classes) {
+      //remove all the `ng-` stuff
+      return classes.replace(/\bng-\S+\b/g, '');
+    }
+
+    function getUniqueValues(a, b) {
+      if (isString(a)) a = a.split(' ');
+      if (isString(b)) b = b.split(' ');
+      return a.filter(function(val) {
+        return b.indexOf(val) === -1;
+      }).join(' ');
+    }
+
+    function prepareAnchoredAnimation(classes, outAnchor, inAnchor) {
+      var clone = jqLite(getDomNode(outAnchor).cloneNode(true));
+      var startingClasses = filterCssClasses(getClassVal(clone));
+
+      outAnchor.addClass(NG_ANIMATE_SHIM_CLASS_NAME);
+      inAnchor.addClass(NG_ANIMATE_SHIM_CLASS_NAME);
+
+      clone.addClass(NG_ANIMATE_ANCHOR_CLASS_NAME);
+
+      rootBodyElement.append(clone);
+
+      var animatorIn, animatorOut = prepareOutAnimation();
+
+      // the user may not end up using the `out` animation and
+      // only making use of the `in` animation or vice-versa.
+      // In either case we should allow this and not assume the
+      // animation is over unless both animations are not used.
+      if (!animatorOut) {
+        animatorIn = prepareInAnimation();
+        if (!animatorIn) {
+          return end();
+        }
+      }
+
+      var startingAnimator = animatorOut || animatorIn;
+
+      return {
+        start: function() {
+          var runner;
+
+          var currentAnimation = startingAnimator.start();
+          currentAnimation.done(function() {
+            currentAnimation = null;
+            if (!animatorIn) {
+              animatorIn = prepareInAnimation();
+              if (animatorIn) {
+                currentAnimation = animatorIn.start();
+                currentAnimation.done(function() {
+                  currentAnimation = null;
+                  end();
+                  runner.complete();
+                });
+                return currentAnimation;
+              }
+            }
+            // in the event that there is no `in` animation
+            end();
+            runner.complete();
+          });
+
+          runner = new $$AnimateRunner({
+            end: endFn,
+            cancel: endFn
+          });
+
+          return runner;
+
+          function endFn() {
+            if (currentAnimation) {
+              currentAnimation.end();
+            }
+          }
+        }
+      };
+
+      function calculateAnchorStyles(anchor) {
+        var styles = {};
+
+        var coords = getDomNode(anchor).getBoundingClientRect();
+
+        // we iterate directly since safari messes up and doesn't return
+        // all the keys for the coods object when iterated
+        forEach(['width','height','top','left'], function(key) {
+          var value = coords[key];
+          switch (key) {
+            case 'top':
+              value += bodyNode.scrollTop;
+              break;
+            case 'left':
+              value += bodyNode.scrollLeft;
+              break;
+          }
+          styles[key] = Math.floor(value) + 'px';
+        });
+        return styles;
+      }
+
+      function prepareOutAnimation() {
+        var animator = $animateCss(clone, {
+          addClass: NG_OUT_ANCHOR_CLASS_NAME,
+          delay: true,
+          from: calculateAnchorStyles(outAnchor)
+        });
+
+        // read the comment within `prepareRegularAnimation` to understand
+        // why this check is necessary
+        return animator.$$willAnimate ? animator : null;
+      }
+
+      function getClassVal(element) {
+        return element.attr('class') || '';
+      }
+
+      function prepareInAnimation() {
+        var endingClasses = filterCssClasses(getClassVal(inAnchor));
+        var toAdd = getUniqueValues(endingClasses, startingClasses);
+        var toRemove = getUniqueValues(startingClasses, endingClasses);
+
+        var animator = $animateCss(clone, {
+          to: calculateAnchorStyles(inAnchor),
+          addClass: NG_IN_ANCHOR_CLASS_NAME + ' ' + toAdd,
+          removeClass: NG_OUT_ANCHOR_CLASS_NAME + ' ' + toRemove,
+          delay: true
+        });
+
+        // read the comment within `prepareRegularAnimation` to understand
+        // why this check is necessary
+        return animator.$$willAnimate ? animator : null;
+      }
+
+      function end() {
+        clone.remove();
+        outAnchor.removeClass(NG_ANIMATE_SHIM_CLASS_NAME);
+        inAnchor.removeClass(NG_ANIMATE_SHIM_CLASS_NAME);
+      }
+    }
+
+    function prepareFromToAnchorAnimation(from, to, classes, anchors) {
+      var fromAnimation = prepareRegularAnimation(from, noop);
+      var toAnimation = prepareRegularAnimation(to, noop);
+
+      var anchorAnimations = [];
+      forEach(anchors, function(anchor) {
+        var outElement = anchor['out'];
+        var inElement = anchor['in'];
+        var animator = prepareAnchoredAnimation(classes, outElement, inElement);
+        if (animator) {
+          anchorAnimations.push(animator);
+        }
+      });
+
+      // no point in doing anything when there are no elements to animate
+      if (!fromAnimation && !toAnimation && anchorAnimations.length === 0) return;
+
+      return {
+        start: function() {
+          var animationRunners = [];
+
+          if (fromAnimation) {
+            animationRunners.push(fromAnimation.start());
+          }
+
+          if (toAnimation) {
+            animationRunners.push(toAnimation.start());
+          }
+
+          forEach(anchorAnimations, function(animation) {
+            animationRunners.push(animation.start());
+          });
+
+          var runner = new $$AnimateRunner({
+            end: endFn,
+            cancel: endFn // CSS-driven animations cannot be cancelled, only ended
+          });
+
+          $$AnimateRunner.all(animationRunners, function(status) {
+            runner.complete(status);
+          });
+
+          return runner;
+
+          function endFn() {
+            forEach(animationRunners, function(runner) {
+              runner.end();
+            });
+          }
+        }
+      };
+    }
+
+    function prepareRegularAnimation(animationDetails) {
+      var element = animationDetails.element;
+      var options = animationDetails.options || {};
+
+      if (animationDetails.structural) {
+        options.event = animationDetails.event;
+        options.structural = true;
+        options.applyClassesEarly = true;
+
+        // we special case the leave animation since we want to ensure that
+        // the element is removed as soon as the animation is over. Otherwise
+        // a flicker might appear or the element may not be removed at all
+        if (animationDetails.event === 'leave') {
+          options.onDone = options.domOperation;
+        }
+      }
+
+      // We assign the preparationClasses as the actual animation event since
+      // the internals of $animateCss will just suffix the event token values
+      // with `-active` to trigger the animation.
+      if (options.preparationClasses) {
+        options.event = concatWithSpace(options.event, options.preparationClasses);
+      }
+
+      var animator = $animateCss(element, options);
+
+      // the driver lookup code inside of $$animation attempts to spawn a
+      // driver one by one until a driver returns a.$$willAnimate animator object.
+      // $animateCss will always return an object, however, it will pass in
+      // a flag as a hint as to whether an animation was detected or not
+      return animator.$$willAnimate ? animator : null;
+    }
+  }];
+}];
+
+// TODO(matsko): use caching here to speed things up for detection
+// TODO(matsko): add documentation
+//  by the time...
+
+var $$AnimateJsProvider = ['$animateProvider', function($animateProvider) {
+  this.$get = ['$injector', '$$AnimateRunner', '$$jqLite',
+       function($injector,   $$AnimateRunner,   $$jqLite) {
+
+    var applyAnimationClasses = applyAnimationClassesFactory($$jqLite);
+         // $animateJs(element, 'enter');
+    return function(element, event, classes, options) {
+      // the `classes` argument is optional and if it is not used
+      // then the classes will be resolved from the element's className
+      // property as well as options.addClass/options.removeClass.
+      if (arguments.length === 3 && isObject(classes)) {
+        options = classes;
+        classes = null;
+      }
+
+      options = prepareAnimationOptions(options);
+      if (!classes) {
+        classes = element.attr('class') || '';
+        if (options.addClass) {
+          classes += ' ' + options.addClass;
+        }
+        if (options.removeClass) {
+          classes += ' ' + options.removeClass;
+        }
+      }
+
+      var classesToAdd = options.addClass;
+      var classesToRemove = options.removeClass;
+
+      // the lookupAnimations function returns a series of animation objects that are
+      // matched up with one or more of the CSS classes. These animation objects are
+      // defined via the module.animation factory function. If nothing is detected then
+      // we don't return anything which then makes $animation query the next driver.
+      var animations = lookupAnimations(classes);
+      var before, after;
+      if (animations.length) {
+        var afterFn, beforeFn;
+        if (event == 'leave') {
+          beforeFn = 'leave';
+          afterFn = 'afterLeave'; // TODO(matsko): get rid of this
+        } else {
+          beforeFn = 'before' + event.charAt(0).toUpperCase() + event.substr(1);
+          afterFn = event;
+        }
+
+        if (event !== 'enter' && event !== 'move') {
+          before = packageAnimations(element, event, options, animations, beforeFn);
+        }
+        after  = packageAnimations(element, event, options, animations, afterFn);
+      }
+
+      // no matching animations
+      if (!before && !after) return;
+
+      function applyOptions() {
+        options.domOperation();
+        applyAnimationClasses(element, options);
+      }
+
+      return {
+        start: function() {
+          var closeActiveAnimations;
+          var chain = [];
+
+          if (before) {
+            chain.push(function(fn) {
+              closeActiveAnimations = before(fn);
+            });
+          }
+
+          if (chain.length) {
+            chain.push(function(fn) {
+              applyOptions();
+              fn(true);
+            });
+          } else {
+            applyOptions();
+          }
+
+          if (after) {
+            chain.push(function(fn) {
+              closeActiveAnimations = after(fn);
+            });
+          }
+
+          var animationClosed = false;
+          var runner = new $$AnimateRunner({
+            end: function() {
+              endAnimations();
+            },
+            cancel: function() {
+              endAnimations(true);
+            }
+          });
+
+          $$AnimateRunner.chain(chain, onComplete);
+          return runner;
+
+          function onComplete(success) {
+            animationClosed = true;
+            applyOptions();
+            applyAnimationStyles(element, options);
+            runner.complete(success);
+          }
+
+          function endAnimations(cancelled) {
+            if (!animationClosed) {
+              (closeActiveAnimations || noop)(cancelled);
+              onComplete(cancelled);
+            }
+          }
+        }
+      };
+
+      function executeAnimationFn(fn, element, event, options, onDone) {
+        var args;
+        switch (event) {
+          case 'animate':
+            args = [element, options.from, options.to, onDone];
+            break;
+
+          case 'setClass':
+            args = [element, classesToAdd, classesToRemove, onDone];
+            break;
+
+          case 'addClass':
+            args = [element, classesToAdd, onDone];
+            break;
+
+          case 'removeClass':
+            args = [element, classesToRemove, onDone];
+            break;
+
+          default:
+            args = [element, onDone];
+            break;
+        }
+
+        args.push(options);
+
+        var value = fn.apply(fn, args);
+        if (value) {
+          if (isFunction(value.start)) {
+            value = value.start();
+          }
+
+          if (value instanceof $$AnimateRunner) {
+            value.done(onDone);
+          } else if (isFunction(value)) {
+            // optional onEnd / onCancel callback
+            return value;
+          }
+        }
+
+        return noop;
+      }
+
+      function groupEventedAnimations(element, event, options, animations, fnName) {
+        var operations = [];
+        forEach(animations, function(ani) {
+          var animation = ani[fnName];
+          if (!animation) return;
+
+          // note that all of these animations will run in parallel
+          operations.push(function() {
+            var runner;
+            var endProgressCb;
+
+            var resolved = false;
+            var onAnimationComplete = function(rejected) {
+              if (!resolved) {
+                resolved = true;
+                (endProgressCb || noop)(rejected);
+                runner.complete(!rejected);
+              }
+            };
+
+            runner = new $$AnimateRunner({
+              end: function() {
+                onAnimationComplete();
+              },
+              cancel: function() {
+                onAnimationComplete(true);
+              }
+            });
+
+            endProgressCb = executeAnimationFn(animation, element, event, options, function(result) {
+              var cancelled = result === false;
+              onAnimationComplete(cancelled);
+            });
+
+            return runner;
+          });
+        });
+
+        return operations;
+      }
+
+      function packageAnimations(element, event, options, animations, fnName) {
+        var operations = groupEventedAnimations(element, event, options, animations, fnName);
+        if (operations.length === 0) {
+          var a,b;
+          if (fnName === 'beforeSetClass') {
+            a = groupEventedAnimations(element, 'removeClass', options, animations, 'beforeRemoveClass');
+            b = groupEventedAnimations(element, 'addClass', options, animations, 'beforeAddClass');
+          } else if (fnName === 'setClass') {
+            a = groupEventedAnimations(element, 'removeClass', options, animations, 'removeClass');
+            b = groupEventedAnimations(element, 'addClass', options, animations, 'addClass');
+          }
+
+          if (a) {
+            operations = operations.concat(a);
+          }
+          if (b) {
+            operations = operations.concat(b);
+          }
+        }
+
+        if (operations.length === 0) return;
+
+        // TODO(matsko): add documentation
+        return function startAnimation(callback) {
+          var runners = [];
+          if (operations.length) {
+            forEach(operations, function(animateFn) {
+              runners.push(animateFn());
+            });
+          }
+
+          runners.length ? $$AnimateRunner.all(runners, callback) : callback();
+
+          return function endFn(reject) {
+            forEach(runners, function(runner) {
+              reject ? runner.cancel() : runner.end();
+            });
+          };
+        };
+      }
+    };
+
+    function lookupAnimations(classes) {
+      classes = isArray(classes) ? classes : classes.split(' ');
+      var matches = [], flagMap = {};
+      for (var i=0; i < classes.length; i++) {
+        var klass = classes[i],
+            animationFactory = $animateProvider.$$registeredAnimations[klass];
+        if (animationFactory && !flagMap[klass]) {
+          matches.push($injector.get(animationFactory));
+          flagMap[klass] = true;
+        }
+      }
+      return matches;
+    }
+  }];
+}];
+
+var $$AnimateJsDriverProvider = ['$$animationProvider', function($$animationProvider) {
+  $$animationProvider.drivers.push('$$animateJsDriver');
+  this.$get = ['$$animateJs', '$$AnimateRunner', function($$animateJs, $$AnimateRunner) {
+    return function initDriverFn(animationDetails) {
+      if (animationDetails.from && animationDetails.to) {
+        var fromAnimation = prepareAnimation(animationDetails.from);
+        var toAnimation = prepareAnimation(animationDetails.to);
+        if (!fromAnimation && !toAnimation) return;
+
+        return {
+          start: function() {
+            var animationRunners = [];
+
+            if (fromAnimation) {
+              animationRunners.push(fromAnimation.start());
+            }
+
+            if (toAnimation) {
+              animationRunners.push(toAnimation.start());
+            }
+
+            $$AnimateRunner.all(animationRunners, done);
+
+            var runner = new $$AnimateRunner({
+              end: endFnFactory(),
+              cancel: endFnFactory()
+            });
+
+            return runner;
+
+            function endFnFactory() {
+              return function() {
+                forEach(animationRunners, function(runner) {
+                  // at this point we cannot cancel animations for groups just yet. 1.5+
+                  runner.end();
+                });
+              };
+            }
+
+            function done(status) {
+              runner.complete(status);
+            }
+          }
+        };
+      } else {
+        return prepareAnimation(animationDetails);
+      }
+    };
+
+    function prepareAnimation(animationDetails) {
+      // TODO(matsko): make sure to check for grouped animations and delegate down to normal animations
+      var element = animationDetails.element;
+      var event = animationDetails.event;
+      var options = animationDetails.options;
+      var classes = animationDetails.classes;
+      return $$animateJs(element, event, classes, options);
+    }
+  }];
+}];
+
+var NG_ANIMATE_ATTR_NAME = 'data-ng-animate';
+var NG_ANIMATE_PIN_DATA = '$ngAnimatePin';
+var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {
+  var PRE_DIGEST_STATE = 1;
+  var RUNNING_STATE = 2;
+
+  var rules = this.rules = {
+    skip: [],
+    cancel: [],
+    join: []
+  };
+
+  function isAllowed(ruleType, element, currentAnimation, previousAnimation) {
+    return rules[ruleType].some(function(fn) {
+      return fn(element, currentAnimation, previousAnimation);
+    });
+  }
+
+  function hasAnimationClasses(options, and) {
+    options = options || {};
+    var a = (options.addClass || '').length > 0;
+    var b = (options.removeClass || '').length > 0;
+    return and ? a && b : a || b;
+  }
+
+  rules.join.push(function(element, newAnimation, currentAnimation) {
+    // if the new animation is class-based then we can just tack that on
+    return !newAnimation.structural && hasAnimationClasses(newAnimation.options);
+  });
+
+  rules.skip.push(function(element, newAnimation, currentAnimation) {
+    // there is no need to animate anything if no classes are being added and
+    // there is no structural animation that will be triggered
+    return !newAnimation.structural && !hasAnimationClasses(newAnimation.options);
+  });
+
+  rules.skip.push(function(element, newAnimation, currentAnimation) {
+    // why should we trigger a new structural animation if the element will
+    // be removed from the DOM anyway?
+    return currentAnimation.event == 'leave' && newAnimation.structural;
+  });
+
+  rules.skip.push(function(element, newAnimation, currentAnimation) {
+    // if there is an ongoing current animation then don't even bother running the class-based animation
+    return currentAnimation.structural && currentAnimation.state === RUNNING_STATE && !newAnimation.structural;
+  });
+
+  rules.cancel.push(function(element, newAnimation, currentAnimation) {
+    // there can never be two structural animations running at the same time
+    return currentAnimation.structural && newAnimation.structural;
+  });
+
+  rules.cancel.push(function(element, newAnimation, currentAnimation) {
+    // if the previous animation is already running, but the new animation will
+    // be triggered, but the new animation is structural
+    return currentAnimation.state === RUNNING_STATE && newAnimation.structural;
+  });
+
+  rules.cancel.push(function(element, newAnimation, currentAnimation) {
+    var nO = newAnimation.options;
+    var cO = currentAnimation.options;
+
+    // if the exact same CSS class is added/removed then it's safe to cancel it
+    return (nO.addClass && nO.addClass === cO.removeClass) || (nO.removeClass && nO.removeClass === cO.addClass);
+  });
+
+  this.$get = ['$$rAF', '$rootScope', '$rootElement', '$document', '$$HashMap',
+               '$$animation', '$$AnimateRunner', '$templateRequest', '$$jqLite', '$$forceReflow',
+       function($$rAF,   $rootScope,   $rootElement,   $document,   $$HashMap,
+                $$animation,   $$AnimateRunner,   $templateRequest,   $$jqLite,   $$forceReflow) {
+
+    var activeAnimationsLookup = new $$HashMap();
+    var disabledElementsLookup = new $$HashMap();
+    var animationsEnabled = null;
+
+    function postDigestTaskFactory() {
+      var postDigestCalled = false;
+      return function(fn) {
+        // we only issue a call to postDigest before
+        // it has first passed. This prevents any callbacks
+        // from not firing once the animation has completed
+        // since it will be out of the digest cycle.
+        if (postDigestCalled) {
+          fn();
+        } else {
+          $rootScope.$$postDigest(function() {
+            postDigestCalled = true;
+            fn();
+          });
+        }
+      };
+    }
+
+    // Wait until all directive and route-related templates are downloaded and
+    // compiled. The $templateRequest.totalPendingRequests variable keeps track of
+    // all of the remote templates being currently downloaded. If there are no
+    // templates currently downloading then the watcher will still fire anyway.
+    var deregisterWatch = $rootScope.$watch(
+      function() { return $templateRequest.totalPendingRequests === 0; },
+      function(isEmpty) {
+        if (!isEmpty) return;
+        deregisterWatch();
+
+        // Now that all templates have been downloaded, $animate will wait until
+        // the post digest queue is empty before enabling animations. By having two
+        // calls to $postDigest calls we can ensure that the flag is enabled at the
+        // very end of the post digest queue. Since all of the animations in $animate
+        // use $postDigest, it's important that the code below executes at the end.
+        // This basically means that the page is fully downloaded and compiled before
+        // any animations are triggered.
+        $rootScope.$$postDigest(function() {
+          $rootScope.$$postDigest(function() {
+            // we check for null directly in the event that the application already called
+            // .enabled() with whatever arguments that it provided it with
+            if (animationsEnabled === null) {
+              animationsEnabled = true;
+            }
+          });
+        });
+      }
+    );
+
+    var callbackRegistry = {};
+
+    // remember that the classNameFilter is set during the provider/config
+    // stage therefore we can optimize here and setup a helper function
+    var classNameFilter = $animateProvider.classNameFilter();
+    var isAnimatableClassName = !classNameFilter
+              ? function() { return true; }
+              : function(className) {
+                return classNameFilter.test(className);
+              };
+
+    var applyAnimationClasses = applyAnimationClassesFactory($$jqLite);
+
+    function normalizeAnimationOptions(element, options) {
+      return mergeAnimationOptions(element, options, {});
+    }
+
+    function findCallbacks(element, event) {
+      var targetNode = getDomNode(element);
+
+      var matches = [];
+      var entries = callbackRegistry[event];
+      if (entries) {
+        forEach(entries, function(entry) {
+          if (entry.node.contains(targetNode)) {
+            matches.push(entry.callback);
+          }
+        });
+      }
+
+      return matches;
+    }
+
+    return {
+      on: function(event, container, callback) {
+        var node = extractElementNode(container);
+        callbackRegistry[event] = callbackRegistry[event] || [];
+        callbackRegistry[event].push({
+          node: node,
+          callback: callback
+        });
+      },
+
+      off: function(event, container, callback) {
+        var entries = callbackRegistry[event];
+        if (!entries) return;
+
+        callbackRegistry[event] = arguments.length === 1
+            ? null
+            : filterFromRegistry(entries, container, callback);
+
+        function filterFromRegistry(list, matchContainer, matchCallback) {
+          var containerNode = extractElementNode(matchContainer);
+          return list.filter(function(entry) {
+            var isMatch = entry.node === containerNode &&
+                            (!matchCallback || entry.callback === matchCallback);
+            return !isMatch;
+          });
+        }
+      },
+
+      pin: function(element, parentElement) {
+        assertArg(isElement(element), 'element', 'not an element');
+        assertArg(isElement(parentElement), 'parentElement', 'not an element');
+        element.data(NG_ANIMATE_PIN_DATA, parentElement);
+      },
+
+      push: function(element, event, options, domOperation) {
+        options = options || {};
+        options.domOperation = domOperation;
+        return queueAnimation(element, event, options);
+      },
+
+      // this method has four signatures:
+      //  () - global getter
+      //  (bool) - global setter
+      //  (element) - element getter
+      //  (element, bool) - element setter<F37>
+      enabled: function(element, bool) {
+        var argCount = arguments.length;
+
+        if (argCount === 0) {
+          // () - Global getter
+          bool = !!animationsEnabled;
+        } else {
+          var hasElement = isElement(element);
+
+          if (!hasElement) {
+            // (bool) - Global setter
+            bool = animationsEnabled = !!element;
+          } else {
+            var node = getDomNode(element);
+            var recordExists = disabledElementsLookup.get(node);
+
+            if (argCount === 1) {
+              // (element) - Element getter
+              bool = !recordExists;
+            } else {
+              // (element, bool) - Element setter
+              bool = !!bool;
+              if (!bool) {
+                disabledElementsLookup.put(node, true);
+              } else if (recordExists) {
+                disabledElementsLookup.remove(node);
+              }
+            }
+          }
+        }
+
+        return bool;
+      }
+    };
+
+    function queueAnimation(element, event, options) {
+      var node, parent;
+      element = stripCommentsFromElement(element);
+      if (element) {
+        node = getDomNode(element);
+        parent = element.parent();
+      }
+
+      options = prepareAnimationOptions(options);
+
+      // we create a fake runner with a working promise.
+      // These methods will become available after the digest has passed
+      var runner = new $$AnimateRunner();
+
+      // this is used to trigger callbacks in postDigest mode
+      var runInNextPostDigestOrNow = postDigestTaskFactory();
+
+      if (isArray(options.addClass)) {
+        options.addClass = options.addClass.join(' ');
+      }
+
+      if (options.addClass && !isString(options.addClass)) {
+        options.addClass = null;
+      }
+
+      if (isArray(options.removeClass)) {
+        options.removeClass = options.removeClass.join(' ');
+      }
+
+      if (options.removeClass && !isString(options.removeClass)) {
+        options.removeClass = null;
+      }
+
+      if (options.from && !isObject(options.from)) {
+        options.from = null;
+      }
+
+      if (options.to && !isObject(options.to)) {
+        options.to = null;
+      }
+
+      // there are situations where a directive issues an animation for
+      // a jqLite wrapper that contains only comment nodes... If this
+      // happens then there is no way we can perform an animation
+      if (!node) {
+        close();
+        return runner;
+      }
+
+      var className = [node.className, options.addClass, options.removeClass].join(' ');
+      if (!isAnimatableClassName(className)) {
+        close();
+        return runner;
+      }
+
+      var isStructural = ['enter', 'move', 'leave'].indexOf(event) >= 0;
+
+      // this is a hard disable of all animations for the application or on
+      // the element itself, therefore  there is no need to continue further
+      // past this point if not enabled
+      var skipAnimations = !animationsEnabled || disabledElementsLookup.get(node);
+      var existingAnimation = (!skipAnimations && activeAnimationsLookup.get(node)) || {};
+      var hasExistingAnimation = !!existingAnimation.state;
+
+      // there is no point in traversing the same collection of parent ancestors if a followup
+      // animation will be run on the same element that already did all that checking work
+      if (!skipAnimations && (!hasExistingAnimation || existingAnimation.state != PRE_DIGEST_STATE)) {
+        skipAnimations = !areAnimationsAllowed(element, parent, event);
+      }
+
+      if (skipAnimations) {
+        close();
+        return runner;
+      }
+
+      if (isStructural) {
+        closeChildAnimations(element);
+      }
+
+      var newAnimation = {
+        structural: isStructural,
+        element: element,
+        event: event,
+        close: close,
+        options: options,
+        runner: runner
+      };
+
+      if (hasExistingAnimation) {
+        var skipAnimationFlag = isAllowed('skip', element, newAnimation, existingAnimation);
+        if (skipAnimationFlag) {
+          if (existingAnimation.state === RUNNING_STATE) {
+            close();
+            return runner;
+          } else {
+            mergeAnimationOptions(element, existingAnimation.options, options);
+            return existingAnimation.runner;
+          }
+        }
+
+        var cancelAnimationFlag = isAllowed('cancel', element, newAnimation, existingAnimation);
+        if (cancelAnimationFlag) {
+          if (existingAnimation.state === RUNNING_STATE) {
+            // this will end the animation right away and it is safe
+            // to do so since the animation is already running and the
+            // runner callback code will run in async
+            existingAnimation.runner.end();
+          } else if (existingAnimation.structural) {
+            // this means that the animation is queued into a digest, but
+            // hasn't started yet. Therefore it is safe to run the close
+            // method which will call the runner methods in async.
+            existingAnimation.close();
+          } else {
+            // this will merge the new animation options into existing animation options
+            mergeAnimationOptions(element, existingAnimation.options, newAnimation.options);
+            return existingAnimation.runner;
+          }
+        } else {
+          // a joined animation means that this animation will take over the existing one
+          // so an example would involve a leave animation taking over an enter. Then when
+          // the postDigest kicks in the enter will be ignored.
+          var joinAnimationFlag = isAllowed('join', element, newAnimation, existingAnimation);
+          if (joinAnimationFlag) {
+            if (existingAnimation.state === RUNNING_STATE) {
+              normalizeAnimationOptions(element, options);
+            } else {
+              applyGeneratedPreparationClasses(element, isStructural ? event : null, options);
+
+              event = newAnimation.event = existingAnimation.event;
+              options = mergeAnimationOptions(element, existingAnimation.options, newAnimation.options);
+
+              //we return the same runner since only the option values of this animation will
+              //be fed into the `existingAnimation`.
+              return existingAnimation.runner;
+            }
+          }
+        }
+      } else {
+        // normalization in this case means that it removes redundant CSS classes that
+        // already exist (addClass) or do not exist (removeClass) on the element
+        normalizeAnimationOptions(element, options);
+      }
+
+      // when the options are merged and cleaned up we may end up not having to do
+      // an animation at all, therefore we should check this before issuing a post
+      // digest callback. Structural animations will always run no matter what.
+      var isValidAnimation = newAnimation.structural;
+      if (!isValidAnimation) {
+        // animate (from/to) can be quickly checked first, otherwise we check if any classes are present
+        isValidAnimation = (newAnimation.event === 'animate' && Object.keys(newAnimation.options.to || {}).length > 0)
+                            || hasAnimationClasses(newAnimation.options);
+      }
+
+      if (!isValidAnimation) {
+        close();
+        clearElementAnimationState(element);
+        return runner;
+      }
+
+      // the counter keeps track of cancelled animations
+      var counter = (existingAnimation.counter || 0) + 1;
+      newAnimation.counter = counter;
+
+      markElementAnimationState(element, PRE_DIGEST_STATE, newAnimation);
+
+      $rootScope.$$postDigest(function() {
+        var animationDetails = activeAnimationsLookup.get(node);
+        var animationCancelled = !animationDetails;
+        animationDetails = animationDetails || {};
+
+        // if addClass/removeClass is called before something like enter then the
+        // registered parent element may not be present. The code below will ensure
+        // that a final value for parent element is obtained
+        var parentElement = element.parent() || [];
+
+        // animate/structural/class-based animations all have requirements. Otherwise there
+        // is no point in performing an animation. The parent node must also be set.
+        var isValidAnimation = parentElement.length > 0
+                                && (animationDetails.event === 'animate'
+                                    || animationDetails.structural
+                                    || hasAnimationClasses(animationDetails.options));
+
+        // this means that the previous animation was cancelled
+        // even if the follow-up animation is the same event
+        if (animationCancelled || animationDetails.counter !== counter || !isValidAnimation) {
+          // if another animation did not take over then we need
+          // to make sure that the domOperation and options are
+          // handled accordingly
+          if (animationCancelled) {
+            applyAnimationClasses(element, options);
+            applyAnimationStyles(element, options);
+          }
+
+          // if the event changed from something like enter to leave then we do
+          // it, otherwise if it's the same then the end result will be the same too
+          if (animationCancelled || (isStructural && animationDetails.event !== event)) {
+            options.domOperation();
+            runner.end();
+          }
+
+          // in the event that the element animation was not cancelled or a follow-up animation
+          // isn't allowed to animate from here then we need to clear the state of the element
+          // so that any future animations won't read the expired animation data.
+          if (!isValidAnimation) {
+            clearElementAnimationState(element);
+          }
+
+          return;
+        }
+
+        // this combined multiple class to addClass / removeClass into a setClass event
+        // so long as a structural event did not take over the animation
+        event = !animationDetails.structural && hasAnimationClasses(animationDetails.options, true)
+            ? 'setClass'
+            : animationDetails.event;
+
+        markElementAnimationState(element, RUNNING_STATE);
+        var realRunner = $$animation(element, event, animationDetails.options);
+
+        realRunner.done(function(status) {
+          close(!status);
+          var animationDetails = activeAnimationsLookup.get(node);
+          if (animationDetails && animationDetails.counter === counter) {
+            clearElementAnimationState(getDomNode(element));
+          }
+          notifyProgress(runner, event, 'close', {});
+        });
+
+        // this will update the runner's flow-control events based on
+        // the `realRunner` object.
+        runner.setHost(realRunner);
+        notifyProgress(runner, event, 'start', {});
+      });
+
+      return runner;
+
+      function notifyProgress(runner, event, phase, data) {
+        runInNextPostDigestOrNow(function() {
+          var callbacks = findCallbacks(element, event);
+          if (callbacks.length) {
+            // do not optimize this call here to RAF because
+            // we don't know how heavy the callback code here will
+            // be and if this code is buffered then this can
+            // lead to a performance regression.
+            $$rAF(function() {
+              forEach(callbacks, function(callback) {
+                callback(element, phase, data);
+              });
+            });
+          }
+        });
+        runner.progress(event, phase, data);
+      }
+
+      function close(reject) { // jshint ignore:line
+        clearGeneratedClasses(element, options);
+        applyAnimationClasses(element, options);
+        applyAnimationStyles(element, options);
+        options.domOperation();
+        runner.complete(!reject);
+      }
+    }
+
+    function closeChildAnimations(element) {
+      var node = getDomNode(element);
+      var children = node.querySelectorAll('[' + NG_ANIMATE_ATTR_NAME + ']');
+      forEach(children, function(child) {
+        var state = parseInt(child.getAttribute(NG_ANIMATE_ATTR_NAME));
+        var animationDetails = activeAnimationsLookup.get(child);
+        switch (state) {
+          case RUNNING_STATE:
+            animationDetails.runner.end();
+            /* falls through */
+          case PRE_DIGEST_STATE:
+            if (animationDetails) {
+              activeAnimationsLookup.remove(child);
+            }
+            break;
+        }
+      });
+    }
+
+    function clearElementAnimationState(element) {
+      var node = getDomNode(element);
+      node.removeAttribute(NG_ANIMATE_ATTR_NAME);
+      activeAnimationsLookup.remove(node);
+    }
+
+    function isMatchingElement(nodeOrElmA, nodeOrElmB) {
+      return getDomNode(nodeOrElmA) === getDomNode(nodeOrElmB);
+    }
+
+    function areAnimationsAllowed(element, parentElement, event) {
+      var bodyElement = jqLite($document[0].body);
+      var bodyElementDetected = isMatchingElement(element, bodyElement) || element[0].nodeName === 'HTML';
+      var rootElementDetected = isMatchingElement(element, $rootElement);
+      var parentAnimationDetected = false;
+      var animateChildren;
+
+      var parentHost = element.data(NG_ANIMATE_PIN_DATA);
+      if (parentHost) {
+        parentElement = parentHost;
+      }
+
+      while (parentElement && parentElement.length) {
+        if (!rootElementDetected) {
+          // angular doesn't want to attempt to animate elements outside of the application
+          // therefore we need to ensure that the rootElement is an ancestor of the current element
+          rootElementDetected = isMatchingElement(parentElement, $rootElement);
+        }
+
+        var parentNode = parentElement[0];
+        if (parentNode.nodeType !== ELEMENT_NODE) {
+          // no point in inspecting the #document element
+          break;
+        }
+
+        var details = activeAnimationsLookup.get(parentNode) || {};
+        // either an enter, leave or move animation will commence
+        // therefore we can't allow any animations to take place
+        // but if a parent animation is class-based then that's ok
+        if (!parentAnimationDetected) {
+          parentAnimationDetected = details.structural || disabledElementsLookup.get(parentNode);
+        }
+
+        if (isUndefined(animateChildren) || animateChildren === true) {
+          var value = parentElement.data(NG_ANIMATE_CHILDREN_DATA);
+          if (isDefined(value)) {
+            animateChildren = value;
+          }
+        }
+
+        // there is no need to continue traversing at this point
+        if (parentAnimationDetected && animateChildren === false) break;
+
+        if (!rootElementDetected) {
+          // angular doesn't want to attempt to animate elements outside of the application
+          // therefore we need to ensure that the rootElement is an ancestor of the current element
+          rootElementDetected = isMatchingElement(parentElement, $rootElement);
+          if (!rootElementDetected) {
+            parentHost = parentElement.data(NG_ANIMATE_PIN_DATA);
+            if (parentHost) {
+              parentElement = parentHost;
+            }
+          }
+        }
+
+        if (!bodyElementDetected) {
+          // we also need to ensure that the element is or will be apart of the body element
+          // otherwise it is pointless to even issue an animation to be rendered
+          bodyElementDetected = isMatchingElement(parentElement, bodyElement);
+        }
+
+        parentElement = parentElement.parent();
+      }
+
+      var allowAnimation = !parentAnimationDetected || animateChildren;
+      return allowAnimation && rootElementDetected && bodyElementDetected;
+    }
+
+    function markElementAnimationState(element, state, details) {
+      details = details || {};
+      details.state = state;
+
+      var node = getDomNode(element);
+      node.setAttribute(NG_ANIMATE_ATTR_NAME, state);
+
+      var oldValue = activeAnimationsLookup.get(node);
+      var newValue = oldValue
+          ? extend(oldValue, details)
+          : details;
+      activeAnimationsLookup.put(node, newValue);
+    }
+  }];
+}];
+
+var $$AnimateAsyncRunFactory = ['$$rAF', function($$rAF) {
+  var waitQueue = [];
+
+  function waitForTick(fn) {
+    waitQueue.push(fn);
+    if (waitQueue.length > 1) return;
+    $$rAF(function() {
+      for (var i = 0; i < waitQueue.length; i++) {
+        waitQueue[i]();
+      }
+      waitQueue = [];
+    });
+  }
+
+  return function() {
+    var passed = false;
+    waitForTick(function() {
+      passed = true;
+    });
+    return function(callback) {
+      passed ? callback() : waitForTick(callback);
+    };
+  };
+}];
+
+var $$AnimateRunnerFactory = ['$q', '$sniffer', '$$animateAsyncRun',
+                      function($q,   $sniffer,   $$animateAsyncRun) {
+
+  var INITIAL_STATE = 0;
+  var DONE_PENDING_STATE = 1;
+  var DONE_COMPLETE_STATE = 2;
+
+  AnimateRunner.chain = function(chain, callback) {
+    var index = 0;
+
+    next();
+    function next() {
+      if (index === chain.length) {
+        callback(true);
+        return;
+      }
+
+      chain[index](function(response) {
+        if (response === false) {
+          callback(false);
+          return;
+        }
+        index++;
+        next();
+      });
+    }
+  };
+
+  AnimateRunner.all = function(runners, callback) {
+    var count = 0;
+    var status = true;
+    forEach(runners, function(runner) {
+      runner.done(onProgress);
+    });
+
+    function onProgress(response) {
+      status = status && response;
+      if (++count === runners.length) {
+        callback(status);
+      }
+    }
+  };
+
+  function AnimateRunner(host) {
+    this.setHost(host);
+
+    this._doneCallbacks = [];
+    this._runInAnimationFrame = $$animateAsyncRun();
+    this._state = 0;
+  }
+
+  AnimateRunner.prototype = {
+    setHost: function(host) {
+      this.host = host || {};
+    },
+
+    done: function(fn) {
+      if (this._state === DONE_COMPLETE_STATE) {
+        fn();
+      } else {
+        this._doneCallbacks.push(fn);
+      }
+    },
+
+    progress: noop,
+
+    getPromise: function() {
+      if (!this.promise) {
+        var self = this;
+        this.promise = $q(function(resolve, reject) {
+          self.done(function(status) {
+            status === false ? reject() : resolve();
+          });
+        });
+      }
+      return this.promise;
+    },
+
+    then: function(resolveHandler, rejectHandler) {
+      return this.getPromise().then(resolveHandler, rejectHandler);
+    },
+
+    'catch': function(handler) {
+      return this.getPromise()['catch'](handler);
+    },
+
+    'finally': function(handler) {
+      return this.getPromise()['finally'](handler);
+    },
+
+    pause: function() {
+      if (this.host.pause) {
+        this.host.pause();
+      }
+    },
+
+    resume: function() {
+      if (this.host.resume) {
+        this.host.resume();
+      }
+    },
+
+    end: function() {
+      if (this.host.end) {
+        this.host.end();
+      }
+      this._resolve(true);
+    },
+
+    cancel: function() {
+      if (this.host.cancel) {
+        this.host.cancel();
+      }
+      this._resolve(false);
+    },
+
+    complete: function(response) {
+      var self = this;
+      if (self._state === INITIAL_STATE) {
+        self._state = DONE_PENDING_STATE;
+        self._runInAnimationFrame(function() {
+          self._resolve(response);
+        });
+      }
+    },
+
+    _resolve: function(response) {
+      if (this._state !== DONE_COMPLETE_STATE) {
+        forEach(this._doneCallbacks, function(fn) {
+          fn(response);
+        });
+        this._doneCallbacks.length = 0;
+        this._state = DONE_COMPLETE_STATE;
+      }
+    }
+  };
+
+  return AnimateRunner;
+}];
+
+var $$AnimationProvider = ['$animateProvider', function($animateProvider) {
+  var NG_ANIMATE_REF_ATTR = 'ng-animate-ref';
+
+  var drivers = this.drivers = [];
+
+  var RUNNER_STORAGE_KEY = '$$animationRunner';
+
+  function setRunner(element, runner) {
+    element.data(RUNNER_STORAGE_KEY, runner);
+  }
+
+  function removeRunner(element) {
+    element.removeData(RUNNER_STORAGE_KEY);
+  }
+
+  function getRunner(element) {
+    return element.data(RUNNER_STORAGE_KEY);
+  }
+
+  this.$get = ['$$jqLite', '$rootScope', '$injector', '$$AnimateRunner', '$$HashMap', '$$rAFScheduler',
+       function($$jqLite,   $rootScope,   $injector,   $$AnimateRunner,   $$HashMap,   $$rAFScheduler) {
+
+    var animationQueue = [];
+    var applyAnimationClasses = applyAnimationClassesFactory($$jqLite);
+
+    function sortAnimations(animations) {
+      var tree = { children: [] };
+      var i, lookup = new $$HashMap();
+
+      // this is done first beforehand so that the hashmap
+      // is filled with a list of the elements that will be animated
+      for (i = 0; i < animations.length; i++) {
+        var animation = animations[i];
+        lookup.put(animation.domNode, animations[i] = {
+          domNode: animation.domNode,
+          fn: animation.fn,
+          children: []
+        });
+      }
+
+      for (i = 0; i < animations.length; i++) {
+        processNode(animations[i]);
+      }
+
+      return flatten(tree);
+
+      function processNode(entry) {
+        if (entry.processed) return entry;
+        entry.processed = true;
+
+        var elementNode = entry.domNode;
+        var parentNode = elementNode.parentNode;
+        lookup.put(elementNode, entry);
+
+        var parentEntry;
+        while (parentNode) {
+          parentEntry = lookup.get(parentNode);
+          if (parentEntry) {
+            if (!parentEntry.processed) {
+              parentEntry = processNode(parentEntry);
+            }
+            break;
+          }
+          parentNode = parentNode.parentNode;
+        }
+
+        (parentEntry || tree).children.push(entry);
+        return entry;
+      }
+
+      function flatten(tree) {
+        var result = [];
+        var queue = [];
+        var i;
+
+        for (i = 0; i < tree.children.length; i++) {
+          queue.push(tree.children[i]);
+        }
+
+        var remainingLevelEntries = queue.length;
+        var nextLevelEntries = 0;
+        var row = [];
+
+        for (i = 0; i < queue.length; i++) {
+          var entry = queue[i];
+          if (remainingLevelEntries <= 0) {
+            remainingLevelEntries = nextLevelEntries;
+            nextLevelEntries = 0;
+            result.push(row);
+            row = [];
+          }
+          row.push(entry.fn);
+          entry.children.forEach(function(childEntry) {
+            nextLevelEntries++;
+            queue.push(childEntry);
+          });
+          remainingLevelEntries--;
+        }
+
+        if (row.length) {
+          result.push(row);
+        }
+
+        return result;
+      }
+    }
+
+    // TODO(matsko): document the signature in a better way
+    return function(element, event, options) {
+      options = prepareAnimationOptions(options);
+      var isStructural = ['enter', 'move', 'leave'].indexOf(event) >= 0;
+
+      // there is no animation at the current moment, however
+      // these runner methods will get later updated with the
+      // methods leading into the driver's end/cancel methods
+      // for now they just stop the animation from starting
+      var runner = new $$AnimateRunner({
+        end: function() { close(); },
+        cancel: function() { close(true); }
+      });
+
+      if (!drivers.length) {
+        close();
+        return runner;
+      }
+
+      setRunner(element, runner);
+
+      var classes = mergeClasses(element.attr('class'), mergeClasses(options.addClass, options.removeClass));
+      var tempClasses = options.tempClasses;
+      if (tempClasses) {
+        classes += ' ' + tempClasses;
+        options.tempClasses = null;
+      }
+
+      animationQueue.push({
+        // this data is used by the postDigest code and passed into
+        // the driver step function
+        element: element,
+        classes: classes,
+        event: event,
+        structural: isStructural,
+        options: options,
+        beforeStart: beforeStart,
+        close: close
+      });
+
+      element.on('$destroy', handleDestroyedElement);
+
+      // we only want there to be one function called within the post digest
+      // block. This way we can group animations for all the animations that
+      // were apart of the same postDigest flush call.
+      if (animationQueue.length > 1) return runner;
+
+      $rootScope.$$postDigest(function() {
+        var animations = [];
+        forEach(animationQueue, function(entry) {
+          // the element was destroyed early on which removed the runner
+          // form its storage. This means we can't animate this element
+          // at all and it already has been closed due to destruction.
+          if (getRunner(entry.element)) {
+            animations.push(entry);
+          } else {
+            entry.close();
+          }
+        });
+
+        // now any future animations will be in another postDigest
+        animationQueue.length = 0;
+
+        var groupedAnimations = groupAnimations(animations);
+        var toBeSortedAnimations = [];
+
+        forEach(groupedAnimations, function(animationEntry) {
+          toBeSortedAnimations.push({
+            domNode: getDomNode(animationEntry.from ? animationEntry.from.element : animationEntry.element),
+            fn: function triggerAnimationStart() {
+              // it's important that we apply the `ng-animate` CSS class and the
+              // temporary classes before we do any driver invoking since these
+              // CSS classes may be required for proper CSS detection.
+              animationEntry.beforeStart();
+
+              var startAnimationFn, closeFn = animationEntry.close;
+
+              // in the event that the element was removed before the digest runs or
+              // during the RAF sequencing then we should not trigger the animation.
+              var targetElement = animationEntry.anchors
+                  ? (animationEntry.from.element || animationEntry.to.element)
+                  : animationEntry.element;
+
+              if (getRunner(targetElement)) {
+                var operation = invokeFirstDriver(animationEntry);
+                if (operation) {
+                  startAnimationFn = operation.start;
+                }
+              }
+
+              if (!startAnimationFn) {
+                closeFn();
+              } else {
+                var animationRunner = startAnimationFn();
+                animationRunner.done(function(status) {
+                  closeFn(!status);
+                });
+                updateAnimationRunners(animationEntry, animationRunner);
+              }
+            }
+          });
+        });
+
+        // we need to sort each of the animations in order of parent to child
+        // relationships. This ensures that the child classes are applied at the
+        // right time.
+        $$rAFScheduler(sortAnimations(toBeSortedAnimations));
+      });
+
+      return runner;
+
+      // TODO(matsko): change to reference nodes
+      function getAnchorNodes(node) {
+        var SELECTOR = '[' + NG_ANIMATE_REF_ATTR + ']';
+        var items = node.hasAttribute(NG_ANIMATE_REF_ATTR)
+              ? [node]
+              : node.querySelectorAll(SELECTOR);
+        var anchors = [];
+        forEach(items, function(node) {
+          var attr = node.getAttribute(NG_ANIMATE_REF_ATTR);
+          if (attr && attr.length) {
+            anchors.push(node);
+          }
+        });
+        return anchors;
+      }
+
+      function groupAnimations(animations) {
+        var preparedAnimations = [];
+        var refLookup = {};
+        forEach(animations, function(animation, index) {
+          var element = animation.element;
+          var node = getDomNode(element);
+          var event = animation.event;
+          var enterOrMove = ['enter', 'move'].indexOf(event) >= 0;
+          var anchorNodes = animation.structural ? getAnchorNodes(node) : [];
+
+          if (anchorNodes.length) {
+            var direction = enterOrMove ? 'to' : 'from';
+
+            forEach(anchorNodes, function(anchor) {
+              var key = anchor.getAttribute(NG_ANIMATE_REF_ATTR);
+              refLookup[key] = refLookup[key] || {};
+              refLookup[key][direction] = {
+                animationID: index,
+                element: jqLite(anchor)
+              };
+            });
+          } else {
+            preparedAnimations.push(animation);
+          }
+        });
+
+        var usedIndicesLookup = {};
+        var anchorGroups = {};
+        forEach(refLookup, function(operations, key) {
+          var from = operations.from;
+          var to = operations.to;
+
+          if (!from || !to) {
+            // only one of these is set therefore we can't have an
+            // anchor animation since all three pieces are required
+            var index = from ? from.animationID : to.animationID;
+            var indexKey = index.toString();
+            if (!usedIndicesLookup[indexKey]) {
+              usedIndicesLookup[indexKey] = true;
+              preparedAnimations.push(animations[index]);
+            }
+            return;
+          }
+
+          var fromAnimation = animations[from.animationID];
+          var toAnimation = animations[to.animationID];
+          var lookupKey = from.animationID.toString();
+          if (!anchorGroups[lookupKey]) {
+            var group = anchorGroups[lookupKey] = {
+              structural: true,
+              beforeStart: function() {
+                fromAnimation.beforeStart();
+                toAnimation.beforeStart();
+              },
+              close: function() {
+                fromAnimation.close();
+                toAnimation.close();
+              },
+              classes: cssClassesIntersection(fromAnimation.classes, toAnimation.classes),
+              from: fromAnimation,
+              to: toAnimation,
+              anchors: [] // TODO(matsko): change to reference nodes
+            };
+
+            // the anchor animations require that the from and to elements both have at least
+            // one shared CSS class which effictively marries the two elements together to use
+            // the same animation driver and to properly sequence the anchor animation.
+            if (group.classes.length) {
+              preparedAnimations.push(group);
+            } else {
+              preparedAnimations.push(fromAnimation);
+              preparedAnimations.push(toAnimation);
+            }
+          }
+
+          anchorGroups[lookupKey].anchors.push({
+            'out': from.element, 'in': to.element
+          });
+        });
+
+        return preparedAnimations;
+      }
+
+      function cssClassesIntersection(a,b) {
+        a = a.split(' ');
+        b = b.split(' ');
+        var matches = [];
+
+        for (var i = 0; i < a.length; i++) {
+          var aa = a[i];
+          if (aa.substring(0,3) === 'ng-') continue;
+
+          for (var j = 0; j < b.length; j++) {
+            if (aa === b[j]) {
+              matches.push(aa);
+              break;
+            }
+          }
+        }
+
+        return matches.join(' ');
+      }
+
+      function invokeFirstDriver(animationDetails) {
+        // we loop in reverse order since the more general drivers (like CSS and JS)
+        // may attempt more elements, but custom drivers are more particular
+        for (var i = drivers.length - 1; i >= 0; i--) {
+          var driverName = drivers[i];
+          if (!$injector.has(driverName)) continue; // TODO(matsko): remove this check
+
+          var factory = $injector.get(driverName);
+          var driver = factory(animationDetails);
+          if (driver) {
+            return driver;
+          }
+        }
+      }
+
+      function beforeStart() {
+        element.addClass(NG_ANIMATE_CLASSNAME);
+        if (tempClasses) {
+          $$jqLite.addClass(element, tempClasses);
+        }
+      }
+
+      function updateAnimationRunners(animation, newRunner) {
+        if (animation.from && animation.to) {
+          update(animation.from.element);
+          update(animation.to.element);
+        } else {
+          update(animation.element);
+        }
+
+        function update(element) {
+          getRunner(element).setHost(newRunner);
+        }
+      }
+
+      function handleDestroyedElement() {
+        var runner = getRunner(element);
+        if (runner && (event !== 'leave' || !options.$$domOperationFired)) {
+          runner.end();
+        }
+      }
+
+      function close(rejected) { // jshint ignore:line
+        element.off('$destroy', handleDestroyedElement);
+        removeRunner(element);
+
+        applyAnimationClasses(element, options);
+        applyAnimationStyles(element, options);
+        options.domOperation();
+
+        if (tempClasses) {
+          $$jqLite.removeClass(element, tempClasses);
+        }
+
+        element.removeClass(NG_ANIMATE_CLASSNAME);
+        runner.complete(!rejected);
+      }
+    };
+  }];
+}];
+
+/* global angularAnimateModule: true,
+
+   $$AnimateAsyncRunFactory,
+   $$rAFSchedulerFactory,
+   $$AnimateChildrenDirective,
+   $$AnimateRunnerFactory,
+   $$AnimateQueueProvider,
+   $$AnimationProvider,
+   $AnimateCssProvider,
+   $$AnimateCssDriverProvider,
+   $$AnimateJsProvider,
+   $$AnimateJsDriverProvider,
+*/
+
+/**
+ * @ngdoc module
+ * @name ngAnimate
+ * @description
+ *
+ * The `ngAnimate` module provides support for CSS-based animations (keyframes and transitions) as well as JavaScript-based animations via
+ * callback hooks. Animations are not enabled by default, however, by including `ngAnimate` the animation hooks are enabled for an Angular app.
+ *
+ * <div doc-module-components="ngAnimate"></div>
+ *
+ * # Usage
+ * Simply put, there are two ways to make use of animations when ngAnimate is used: by using **CSS** and **JavaScript**. The former works purely based
+ * using CSS (by using matching CSS selectors/styles) and the latter triggers animations that are registered via `module.animation()`. For
+ * both CSS and JS animations the sole requirement is to have a matching `CSS class` that exists both in the registered animation and within
+ * the HTML element that the animation will be triggered on.
+ *
+ * ## Directive Support
+ * The following directives are "animation aware":
+ *
+ * | Directive                                                                                                | Supported Animations                                                     |
+ * |----------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------|
+ * | {@link ng.directive:ngRepeat#animations ngRepeat}                                                        | enter, leave and move                                                    |
+ * | {@link ngRoute.directive:ngView#animations ngView}                                                       | enter and leave                                                          |
+ * | {@link ng.directive:ngInclude#animations ngInclude}                                                      | enter and leave                                                          |
+ * | {@link ng.directive:ngSwitch#animations ngSwitch}                                                        | enter and leave                                                          |
+ * | {@link ng.directive:ngIf#animations ngIf}                                                                | enter and leave                                                          |
+ * | {@link ng.directive:ngClass#animations ngClass}                                                          | add and remove (the CSS class(es) present)                               |
+ * | {@link ng.directive:ngShow#animations ngShow} & {@link ng.directive:ngHide#animations ngHide}            | add and remove (the ng-hide class value)                                 |
+ * | {@link ng.directive:form#animation-hooks form} & {@link ng.directive:ngModel#animation-hooks ngModel}    | add and remove (dirty, pristine, valid, invalid & all other validations) |
+ * | {@link module:ngMessages#animations ngMessages}                                                          | add and remove (ng-active & ng-inactive)                                 |
+ * | {@link module:ngMessages#animations ngMessage}                                                           | enter and leave                                                          |
+ *
+ * (More information can be found by visiting each the documentation associated with each directive.)
+ *
+ * ## CSS-based Animations
+ *
+ * CSS-based animations with ngAnimate are unique since they require no JavaScript code at all. By using a CSS class that we reference between our HTML
+ * and CSS code we can create an animation that will be picked up by Angular when an the underlying directive performs an operation.
+ *
+ * The example below shows how an `enter` animation can be made possible on an element using `ng-if`:
+ *
+ * ```html
+ * <div ng-if="bool" class="fade">
+ *    Fade me in out
+ * </div>
+ * <button ng-click="bool=true">Fade In!</button>
+ * <button ng-click="bool=false">Fade Out!</button>
+ * ```
+ *
+ * Notice the CSS class **fade**? We can now create the CSS transition code that references this class:
+ *
+ * ```css
+ * /&#42; The starting CSS styles for the enter animation &#42;/
+ * .fade.ng-enter {
+ *   transition:0.5s linear all;
+ *   opacity:0;
+ * }
+ *
+ * /&#42; The finishing CSS styles for the enter animation &#42;/
+ * .fade.ng-enter.ng-enter-active {
+ *   opacity:1;
+ * }
+ * ```
+ *
+ * The key thing to remember here is that, depending on the animation event (which each of the directives above trigger depending on what's going on) two
+ * generated CSS classes will be applied to the element; in the example above we have `.ng-enter` and `.ng-enter-active`. For CSS transitions, the transition
+ * code **must** be defined within the starting CSS class (in this case `.ng-enter`). The destination class is what the transition will animate towards.
+ *
+ * If for example we wanted to create animations for `leave` and `move` (ngRepeat triggers move) then we can do so using the same CSS naming conventions:
+ *
+ * ```css
+ * /&#42; now the element will fade out before it is removed from the DOM &#42;/
+ * .fade.ng-leave {
+ *   transition:0.5s linear all;
+ *   opacity:1;
+ * }
+ * .fade.ng-leave.ng-leave-active {
+ *   opacity:0;
+ * }
+ * ```
+ *
+ * We can also make use of **CSS Keyframes** by referencing the keyframe animation within the starting CSS class:
+ *
+ * ```css
+ * /&#42; there is no need to define anything inside of the destination
+ * CSS class since the keyframe will take charge of the animation &#42;/
+ * .fade.ng-leave {
+ *   animation: my_fade_animation 0.5s linear;
+ *   -webkit-animation: my_fade_animation 0.5s linear;
+ * }
+ *
+ * @keyframes my_fade_animation {
+ *   from { opacity:1; }
+ *   to { opacity:0; }
+ * }
+ *
+ * @-webkit-keyframes my_fade_animation {
+ *   from { opacity:1; }
+ *   to { opacity:0; }
+ * }
+ * ```
+ *
+ * Feel free also mix transitions and keyframes together as well as any other CSS classes on the same element.
+ *
+ * ### CSS Class-based Animations
+ *
+ * Class-based animations (animations that are triggered via `ngClass`, `ngShow`, `ngHide` and some other directives) have a slightly different
+ * naming convention. Class-based animations are basic enough that a standard transition or keyframe can be referenced on the class being added
+ * and removed.
+ *
+ * For example if we wanted to do a CSS animation for `ngHide` then we place an animation on the `.ng-hide` CSS class:
+ *
+ * ```html
+ * <div ng-show="bool" class="fade">
+ *   Show and hide me
+ * </div>
+ * <button ng-click="bool=true">Toggle</button>
+ *
+ * <style>
+ * .fade.ng-hide {
+ *   transition:0.5s linear all;
+ *   opacity:0;
+ * }
+ * </style>
+ * ```
+ *
+ * All that is going on here with ngShow/ngHide behind the scenes is the `.ng-hide` class is added/removed (when the hidden state is valid). Since
+ * ngShow and ngHide are animation aware then we can match up a transition and ngAnimate handles the rest.
+ *
+ * In addition the addition and removal of the CSS class, ngAnimate also provides two helper methods that we can use to further decorate the animation
+ * with CSS styles.
+ *
+ * ```html
+ * <div ng-class="{on:onOff}" class="highlight">
+ *   Highlight this box
+ * </div>
+ * <button ng-click="onOff=!onOff">Toggle</button>
+ *
+ * <style>
+ * .highlight {
+ *   transition:0.5s linear all;
+ * }
+ * .highlight.on-add {
+ *   background:white;
+ * }
+ * .highlight.on {
+ *   background:yellow;
+ * }
+ * .highlight.on-remove {
+ *   background:black;
+ * }
+ * </style>
+ * ```
+ *
+ * We can also make use of CSS keyframes by placing them within the CSS classes.
+ *
+ *
+ * ### CSS Staggering Animations
+ * A Staggering animation is a collection of animations that are issued with a slight delay in between each successive operation resulting in a
+ * curtain-like effect. The ngAnimate module (versions >=1.2) supports staggering animations and the stagger effect can be
+ * performed by creating a **ng-EVENT-stagger** CSS class and attaching that class to the base CSS class used for
+ * the animation. The style property expected within the stagger class can either be a **transition-delay** or an
+ * **animation-delay** property (or both if your animation contains both transitions and keyframe animations).
+ *
+ * ```css
+ * .my-animation.ng-enter {
+ *   /&#42; standard transition code &#42;/
+ *   transition: 1s linear all;
+ *   opacity:0;
+ * }
+ * .my-animation.ng-enter-stagger {
+ *   /&#42; this will have a 100ms delay between each successive leave animation &#42;/
+ *   transition-delay: 0.1s;
+ *
+ *   /&#42; As of 1.4.4, this must always be set: it signals ngAnimate
+ *     to not accidentally inherit a delay property from another CSS class &#42;/
+ *   transition-duration: 0s;
+ * }
+ * .my-animation.ng-enter.ng-enter-active {
+ *   /&#42; standard transition styles &#42;/
+ *   opacity:1;
+ * }
+ * ```
+ *
+ * Staggering animations work by default in ngRepeat (so long as the CSS class is defined). Outside of ngRepeat, to use staggering animations
+ * on your own, they can be triggered by firing multiple calls to the same event on $animate. However, the restrictions surrounding this
+ * are that each of the elements must have the same CSS className value as well as the same parent element. A stagger operation
+ * will also be reset if one or more animation frames have passed since the multiple calls to `$animate` were fired.
+ *
+ * The following code will issue the **ng-leave-stagger** event on the element provided:
+ *
+ * ```js
+ * var kids = parent.children();
+ *
+ * $animate.leave(kids[0]); //stagger index=0
+ * $animate.leave(kids[1]); //stagger index=1
+ * $animate.leave(kids[2]); //stagger index=2
+ * $animate.leave(kids[3]); //stagger index=3
+ * $animate.leave(kids[4]); //stagger index=4
+ *
+ * window.requestAnimationFrame(function() {
+ *   //stagger has reset itself
+ *   $animate.leave(kids[5]); //stagger index=0
+ *   $animate.leave(kids[6]); //stagger index=1
+ *
+ *   $scope.$digest();
+ * });
+ * ```
+ *
+ * Stagger animations are currently only supported within CSS-defined animations.
+ *
+ * ### The `ng-animate` CSS class
+ *
+ * When ngAnimate is animating an element it will apply the `ng-animate` CSS class to the element for the duration of the animation.
+ * This is a temporary CSS class and it will be removed once the animation is over (for both JavaScript and CSS-based animations).
+ *
+ * Therefore, animations can be applied to an element using this temporary class directly via CSS.
+ *
+ * ```css
+ * .zipper.ng-animate {
+ *   transition:0.5s linear all;
+ * }
+ * .zipper.ng-enter {
+ *   opacity:0;
+ * }
+ * .zipper.ng-enter.ng-enter-active {
+ *   opacity:1;
+ * }
+ * .zipper.ng-leave {
+ *   opacity:1;
+ * }
+ * .zipper.ng-leave.ng-leave-active {
+ *   opacity:0;
+ * }
+ * ```
+ *
+ * (Note that the `ng-animate` CSS class is reserved and it cannot be applied on an element directly since ngAnimate will always remove
+ * the CSS class once an animation has completed.)
+ *
+ *
+ * ## JavaScript-based Animations
+ *
+ * ngAnimate also allows for animations to be consumed by JavaScript code. The approach is similar to CSS-based animations (where there is a shared
+ * CSS class that is referenced in our HTML code) but in addition we need to register the JavaScript animation on the module. By making use of the
+ * `module.animation()` module function we can register the ainmation.
+ *
+ * Let's see an example of a enter/leave animation using `ngRepeat`:
+ *
+ * ```html
+ * <div ng-repeat="item in items" class="slide">
+ *   {{ item }}
+ * </div>
+ * ```
+ *
+ * See the **slide** CSS class? Let's use that class to define an animation that we'll structure in our module code by using `module.animation`:
+ *
+ * ```js
+ * myModule.animation('.slide', [function() {
+ *   return {
+ *     // make note that other events (like addClass/removeClass)
+ *     // have different function input parameters
+ *     enter: function(element, doneFn) {
+ *       jQuery(element).fadeIn(1000, doneFn);
+ *
+ *       // remember to call doneFn so that angular
+ *       // knows that the animation has concluded
+ *     },
+ *
+ *     move: function(element, doneFn) {
+ *       jQuery(element).fadeIn(1000, doneFn);
+ *     },
+ *
+ *     leave: function(element, doneFn) {
+ *       jQuery(element).fadeOut(1000, doneFn);
+ *     }
+ *   }
+ * }]
+ * ```
+ *
+ * The nice thing about JS-based animations is that we can inject other services and make use of advanced animation libraries such as
+ * greensock.js and velocity.js.
+ *
+ * If our animation code class-based (meaning that something like `ngClass`, `ngHide` and `ngShow` triggers it) then we can still define
+ * our animations inside of the same registered animation, however, the function input arguments are a bit different:
+ *
+ * ```html
+ * <div ng-class="color" class="colorful">
+ *   this box is moody
+ * </div>
+ * <button ng-click="color='red'">Change to red</button>
+ * <button ng-click="color='blue'">Change to blue</button>
+ * <button ng-click="color='green'">Change to green</button>
+ * ```
+ *
+ * ```js
+ * myModule.animation('.colorful', [function() {
+ *   return {
+ *     addClass: function(element, className, doneFn) {
+ *       // do some cool animation and call the doneFn
+ *     },
+ *     removeClass: function(element, className, doneFn) {
+ *       // do some cool animation and call the doneFn
+ *     },
+ *     setClass: function(element, addedClass, removedClass, doneFn) {
+ *       // do some cool animation and call the doneFn
+ *     }
+ *   }
+ * }]
+ * ```
+ *
+ * ## CSS + JS Animations Together
+ *
+ * AngularJS 1.4 and higher has taken steps to make the amalgamation of CSS and JS animations more flexible. However, unlike earlier versions of Angular,
+ * defining CSS and JS animations to work off of the same CSS class will not work anymore. Therefore the example below will only result in **JS animations taking
+ * charge of the animation**:
+ *
+ * ```html
+ * <div ng-if="bool" class="slide">
+ *   Slide in and out
+ * </div>
+ * ```
+ *
+ * ```js
+ * myModule.animation('.slide', [function() {
+ *   return {
+ *     enter: function(element, doneFn) {
+ *       jQuery(element).slideIn(1000, doneFn);
+ *     }
+ *   }
+ * }]
+ * ```
+ *
+ * ```css
+ * .slide.ng-enter {
+ *   transition:0.5s linear all;
+ *   transform:translateY(-100px);
+ * }
+ * .slide.ng-enter.ng-enter-active {
+ *   transform:translateY(0);
+ * }
+ * ```
+ *
+ * Does this mean that CSS and JS animations cannot be used together? Do JS-based animations always have higher priority? We can make up for the
+ * lack of CSS animations by using the `$animateCss` service to trigger our own tweaked-out, CSS-based animations directly from
+ * our own JS-based animation code:
+ *
+ * ```js
+ * myModule.animation('.slide', ['$animateCss', function($animateCss) {
+ *   return {
+ *     enter: function(element, doneFn) {
+*        // this will trigger `.slide.ng-enter` and `.slide.ng-enter-active`.
+ *       var runner = $animateCss(element, {
+ *         event: 'enter',
+ *         structural: true
+ *       }).start();
+*        runner.done(doneFn);
+ *     }
+ *   }
+ * }]
+ * ```
+ *
+ * The nice thing here is that we can save bandwidth by sticking to our CSS-based animation code and we don't need to rely on a 3rd-party animation framework.
+ *
+ * The `$animateCss` service is very powerful since we can feed in all kinds of extra properties that will be evaluated and fed into a CSS transition or
+ * keyframe animation. For example if we wanted to animate the height of an element while adding and removing classes then we can do so by providing that
+ * data into `$animateCss` directly:
+ *
+ * ```js
+ * myModule.animation('.slide', ['$animateCss', function($animateCss) {
+ *   return {
+ *     enter: function(element, doneFn) {
+ *       var runner = $animateCss(element, {
+ *         event: 'enter',
+ *         structural: true,
+ *         addClass: 'maroon-setting',
+ *         from: { height:0 },
+ *         to: { height: 200 }
+ *       }).start();
+ *
+ *       runner.done(doneFn);
+ *     }
+ *   }
+ * }]
+ * ```
+ *
+ * Now we can fill in the rest via our transition CSS code:
+ *
+ * ```css
+ * /&#42; the transition tells ngAnimate to make the animation happen &#42;/
+ * .slide.ng-enter { transition:0.5s linear all; }
+ *
+ * /&#42; this extra CSS class will be absorbed into the transition
+ * since the $animateCss code is adding the class &#42;/
+ * .maroon-setting { background:red; }
+ * ```
+ *
+ * And `$animateCss` will figure out the rest. Just make sure to have the `done()` callback fire the `doneFn` function to signal when the animation is over.
+ *
+ * To learn more about what's possible be sure to visit the {@link ngAnimate.$animateCss $animateCss service}.
+ *
+ * ## Animation Anchoring (via `ng-animate-ref`)
+ *
+ * ngAnimate in AngularJS 1.4 comes packed with the ability to cross-animate elements between
+ * structural areas of an application (like views) by pairing up elements using an attribute
+ * called `ng-animate-ref`.
+ *
+ * Let's say for example we have two views that are managed by `ng-view` and we want to show
+ * that there is a relationship between two components situated in within these views. By using the
+ * `ng-animate-ref` attribute we can identify that the two components are paired together and we
+ * can then attach an animation, which is triggered when the view changes.
+ *
+ * Say for example we have the following template code:
+ *
+ * ```html
+ * <!-- index.html -->
+ * <div ng-view class="view-animation">
+ * </div>
+ *
+ * <!-- home.html -->
+ * <a href="#/banner-page">
+ *   <img src="./banner.jpg" class="banner" ng-animate-ref="banner">
+ * </a>
+ *
+ * <!-- banner-page.html -->
+ * <img src="./banner.jpg" class="banner" ng-animate-ref="banner">
+ * ```
+ *
+ * Now, when the view changes (once the link is clicked), ngAnimate will examine the
+ * HTML contents to see if there is a match reference between any components in the view
+ * that is leaving and the view that is entering. It will scan both the view which is being
+ * removed (leave) and inserted (enter) to see if there are any paired DOM elements that
+ * contain a matching ref value.
+ *
+ * The two images match since they share the same ref value. ngAnimate will now create a
+ * transport element (which is a clone of the first image element) and it will then attempt
+ * to animate to the position of the second image element in the next view. For the animation to
+ * work a special CSS class called `ng-anchor` will be added to the transported element.
+ *
+ * We can now attach a transition onto the `.banner.ng-anchor` CSS class and then
+ * ngAnimate will handle the entire transition for us as well as the addition and removal of
+ * any changes of CSS classes between the elements:
+ *
+ * ```css
+ * .banner.ng-anchor {
+ *   /&#42; this animation will last for 1 second since there are
+ *          two phases to the animation (an `in` and an `out` phase) &#42;/
+ *   transition:0.5s linear all;
+ * }
+ * ```
+ *
+ * We also **must** include animations for the views that are being entered and removed
+ * (otherwise anchoring wouldn't be possible since the new view would be inserted right away).
+ *
+ * ```css
+ * .view-animation.ng-enter, .view-animation.ng-leave {
+ *   transition:0.5s linear all;
+ *   position:fixed;
+ *   left:0;
+ *   top:0;
+ *   width:100%;
+ * }
+ * .view-animation.ng-enter {
+ *   transform:translateX(100%);
+ * }
+ * .view-animation.ng-leave,
+ * .view-animation.ng-enter.ng-enter-active {
+ *   transform:translateX(0%);
+ * }
+ * .view-animation.ng-leave.ng-leave-active {
+ *   transform:translateX(-100%);
+ * }
+ * ```
+ *
+ * Now we can jump back to the anchor animation. When the animation happens, there are two stages that occur:
+ * an `out` and an `in` stage. The `out` stage happens first and that is when the element is animated away
+ * from its origin. Once that animation is over then the `in` stage occurs which animates the
+ * element to its destination. The reason why there are two animations is to give enough time
+ * for the enter animation on the new element to be ready.
+ *
+ * The example above sets up a transition for both the in and out phases, but we can also target the out or
+ * in phases directly via `ng-anchor-out` and `ng-anchor-in`.
+ *
+ * ```css
+ * .banner.ng-anchor-out {
+ *   transition: 0.5s linear all;
+ *
+ *   /&#42; the scale will be applied during the out animation,
+ *          but will be animated away when the in animation runs &#42;/
+ *   transform: scale(1.2);
+ * }
+ *
+ * .banner.ng-anchor-in {
+ *   transition: 1s linear all;
+ * }
+ * ```
+ *
+ *
+ *
+ *
+ * ### Anchoring Demo
+ *
+  <example module="anchoringExample"
+           name="anchoringExample"
+           id="anchoringExample"
+           deps="angular-animate.js;angular-route.js"
+           animations="true">
+    <file name="index.html">
+      <a href="#/">Home</a>
+      <hr />
+      <div class="view-container">
+        <div ng-view class="view"></div>
+      </div>
+    </file>
+    <file name="script.js">
+      angular.module('anchoringExample', ['ngAnimate', 'ngRoute'])
+        .config(['$routeProvider', function($routeProvider) {
+          $routeProvider.when('/', {
+            templateUrl: 'home.html',
+            controller: 'HomeController as home'
+          });
+          $routeProvider.when('/profile/:id', {
+            templateUrl: 'profile.html',
+            controller: 'ProfileController as profile'
+          });
+        }])
+        .run(['$rootScope', function($rootScope) {
+          $rootScope.records = [
+            { id:1, title: "Miss Beulah Roob" },
+            { id:2, title: "Trent Morissette" },
+            { id:3, title: "Miss Ava Pouros" },
+            { id:4, title: "Rod Pouros" },
+            { id:5, title: "Abdul Rice" },
+            { id:6, title: "Laurie Rutherford Sr." },
+            { id:7, title: "Nakia McLaughlin" },
+            { id:8, title: "Jordon Blanda DVM" },
+            { id:9, title: "Rhoda Hand" },
+            { id:10, title: "Alexandrea Sauer" }
+          ];
+        }])
+        .controller('HomeController', [function() {
+          //empty
+        }])
+        .controller('ProfileController', ['$rootScope', '$routeParams', function($rootScope, $routeParams) {
+          var index = parseInt($routeParams.id, 10);
+          var record = $rootScope.records[index - 1];
+
+          this.title = record.title;
+          this.id = record.id;
+        }]);
+    </file>
+    <file name="home.html">
+      <h2>Welcome to the home page</h1>
+      <p>Please click on an element</p>
+      <a class="record"
+         ng-href="#/profile/{{ record.id }}"
+         ng-animate-ref="{{ record.id }}"
+         ng-repeat="record in records">
+        {{ record.title }}
+      </a>
+    </file>
+    <file name="profile.html">
+      <div class="profile record" ng-animate-ref="{{ profile.id }}">
+        {{ profile.title }}
+      </div>
+    </file>
+    <file name="animations.css">
+      .record {
+        display:block;
+        font-size:20px;
+      }
+      .profile {
+        background:black;
+        color:white;
+        font-size:100px;
+      }
+      .view-container {
+        position:relative;
+      }
+      .view-container > .view.ng-animate {
+        position:absolute;
+        top:0;
+        left:0;
+        width:100%;
+        min-height:500px;
+      }
+      .view.ng-enter, .view.ng-leave,
+      .record.ng-anchor {
+        transition:0.5s linear all;
+      }
+      .view.ng-enter {
+        transform:translateX(100%);
+      }
+      .view.ng-enter.ng-enter-active, .view.ng-leave {
+        transform:translateX(0%);
+      }
+      .view.ng-leave.ng-leave-active {
+        transform:translateX(-100%);
+      }
+      .record.ng-anchor-out {
+        background:red;
+      }
+    </file>
+  </example>
+ *
+ * ### How is the element transported?
+ *
+ * When an anchor animation occurs, ngAnimate will clone the starting element and position it exactly where the starting
+ * element is located on screen via absolute positioning. The cloned element will be placed inside of the root element
+ * of the application (where ng-app was defined) and all of the CSS classes of the starting element will be applied. The
+ * element will then animate into the `out` and `in` animations and will eventually reach the coordinates and match
+ * the dimensions of the destination element. During the entire animation a CSS class of `.ng-animate-shim` will be applied
+ * to both the starting and destination elements in order to hide them from being visible (the CSS styling for the class
+ * is: `visibility:hidden`). Once the anchor reaches its destination then it will be removed and the destination element
+ * will become visible since the shim class will be removed.
+ *
+ * ### How is the morphing handled?
+ *
+ * CSS Anchoring relies on transitions and keyframes and the internal code is intelligent enough to figure out
+ * what CSS classes differ between the starting element and the destination element. These different CSS classes
+ * will be added/removed on the anchor element and a transition will be applied (the transition that is provided
+ * in the anchor class). Long story short, ngAnimate will figure out what classes to add and remove which will
+ * make the transition of the element as smooth and automatic as possible. Be sure to use simple CSS classes that
+ * do not rely on DOM nesting structure so that the anchor element appears the same as the starting element (since
+ * the cloned element is placed inside of root element which is likely close to the body element).
+ *
+ * Note that if the root element is on the `<html>` element then the cloned node will be placed inside of body.
+ *
+ *
+ * ## Using $animate in your directive code
+ *
+ * So far we've explored how to feed in animations into an Angular application, but how do we trigger animations within our own directives in our application?
+ * By injecting the `$animate` service into our directive code, we can trigger structural and class-based hooks which can then be consumed by animations. Let's
+ * imagine we have a greeting box that shows and hides itself when the data changes
+ *
+ * ```html
+ * <greeting-box active="onOrOff">Hi there</greeting-box>
+ * ```
+ *
+ * ```js
+ * ngModule.directive('greetingBox', ['$animate', function($animate) {
+ *   return function(scope, element, attrs) {
+ *     attrs.$observe('active', function(value) {
+ *       value ? $animate.addClass(element, 'on') : $animate.removeClass(element, 'on');
+ *     });
+ *   });
+ * }]);
+ * ```
+ *
+ * Now the `on` CSS class is added and removed on the greeting box component. Now if we add a CSS class on top of the greeting box element
+ * in our HTML code then we can trigger a CSS or JS animation to happen.
+ *
+ * ```css
+ * /&#42; normally we would create a CSS class to reference on the element &#42;/
+ * greeting-box.on { transition:0.5s linear all; background:green; color:white; }
+ * ```
+ *
+ * The `$animate` service contains a variety of other methods like `enter`, `leave`, `animate` and `setClass`. To learn more about what's
+ * possible be sure to visit the {@link ng.$animate $animate service API page}.
+ *
+ *
+ * ### Preventing Collisions With Third Party Libraries
+ *
+ * Some third-party frameworks place animation duration defaults across many element or className
+ * selectors in order to make their code small and reuseable. This can lead to issues with ngAnimate, which
+ * is expecting actual animations on these elements and has to wait for their completion.
+ *
+ * You can prevent this unwanted behavior by using a prefix on all your animation classes:
+ *
+ * ```css
+ * /&#42; prefixed with animate- &#42;/
+ * .animate-fade-add.animate-fade-add-active {
+ *   transition:1s linear all;
+ *   opacity:0;
+ * }
+ * ```
+ *
+ * You then configure `$animate` to enforce this prefix:
+ *
+ * ```js
+ * $animateProvider.classNameFilter(/animate-/);
+ * ```
+ *
+ * This also may provide your application with a speed boost since only specific elements containing CSS class prefix
+ * will be evaluated for animation when any DOM changes occur in the application.
+ *
+ * ## Callbacks and Promises
+ *
+ * When `$animate` is called it returns a promise that can be used to capture when the animation has ended. Therefore if we were to trigger
+ * an animation (within our directive code) then we can continue performing directive and scope related activities after the animation has
+ * ended by chaining onto the returned promise that animation method returns.
+ *
+ * ```js
+ * // somewhere within the depths of the directive
+ * $animate.enter(element, parent).then(function() {
+ *   //the animation has completed
+ * });
+ * ```
+ *
+ * (Note that earlier versions of Angular prior to v1.4 required the promise code to be wrapped using `$scope.$apply(...)`. This is not the case
+ * anymore.)
+ *
+ * In addition to the animation promise, we can also make use of animation-related callbacks within our directives and controller code by registering
+ * an event listener using the `$animate` service. Let's say for example that an animation was triggered on our view
+ * routing controller to hook into that:
+ *
+ * ```js
+ * ngModule.controller('HomePageController', ['$animate', function($animate) {
+ *   $animate.on('enter', ngViewElement, function(element) {
+ *     // the animation for this route has completed
+ *   }]);
+ * }])
+ * ```
+ *
+ * (Note that you will need to trigger a digest within the callback to get angular to notice any scope-related changes.)
+ */
+
+/**
+ * @ngdoc service
+ * @name $animate
+ * @kind object
+ *
+ * @description
+ * The ngAnimate `$animate` service documentation is the same for the core `$animate` service.
+ *
+ * Click here {@link ng.$animate to learn more about animations with `$animate`}.
+ */
+angular.module('ngAnimate', [])
+  .directive('ngAnimateChildren', $$AnimateChildrenDirective)
+  .factory('$$rAFScheduler', $$rAFSchedulerFactory)
+
+  .factory('$$AnimateRunner', $$AnimateRunnerFactory)
+  .factory('$$animateAsyncRun', $$AnimateAsyncRunFactory)
+
+  .provider('$$animateQueue', $$AnimateQueueProvider)
+  .provider('$$animation', $$AnimationProvider)
+
+  .provider('$animateCss', $AnimateCssProvider)
+  .provider('$$animateCssDriver', $$AnimateCssDriverProvider)
+
+  .provider('$$animateJs', $$AnimateJsProvider)
+  .provider('$$animateJsDriver', $$AnimateJsDriverProvider);
+
+
+})(window, window.angular);
diff --git a/xos/core/xoslib/static/js/xosApi.js b/xos/core/xoslib/static/js/xosApi.js
deleted file mode 100644
index 65d8820..0000000
--- a/xos/core/xoslib/static/js/xosApi.js
+++ /dev/null
@@ -1,51569 +0,0 @@
-/*jshint -W069 */
-/*global angular:false */
-angular.module('xos.hpcapi', [])
-    .factory('hpcapi', ['$q', '$http', '$rootScope', function($q, $http, $rootScope) {
-        'use strict';
-
-        /**
-         * 
-         * @class hpcapi
-         * @param {(string|object)} [domainOrOptions] - The project domain or options object. If object, see the object's optional properties.
-         * @param {string} [domainOrOptions.domain] - The project domain
-         * @param {string} [domainOrOptions.cache] - An angularjs cache implementation
-         * @param {object} [domainOrOptions.token] - auth token - object with value property and optional headerOrQueryName and isQuery properties
-         * @param {string} [cache] - An angularjs cache implementation
-         */
-        var hpcapi = (function() {
-            function hpcapi(options, cache) {
-                var domain = (typeof options === 'object') ? options.domain : options;
-                this.domain = typeof(domain) === 'string' ? domain : 'http://localhost:9999';
-                cache = cache || ((typeof options === 'object') ? options.cache : cache);
-                this.cache = cache;
-            }
-
-            hpcapi.prototype.$on = function($scope, path, handler) {
-                var url = domain + path;
-                $scope.$on(url, function() {
-                    handler();
-                });
-                return this;
-            };
-
-            hpcapi.prototype.$broadcast = function(path) {
-                var url = domain + path;
-                //cache.remove(url);
-                $rootScope.$broadcast(url);
-                return this;
-            };
-
-            hpcapi.transformRequest = function(obj) {
-                var str = [];
-                for (var p in obj) {
-                    var val = obj[p];
-                    if (angular.isArray(val)) {
-                        val.forEach(function(val) {
-                            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(val));
-                        });
-                    } else {
-                        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(val));
-                    }
-                }
-                return str.join("&");
-            };
-
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Api_Root_GET
-             * 
-             */
-            hpcapi.prototype.Hpc_Api_Root_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Health_Check_List_GET
-             * 
-             */
-            hpcapi.prototype.Hpc_Health_Check_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/hpchealthchecks/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Health_Check_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} hpcService - 
-             * @param {choice} kind - 
-             * @param {string} resource_name - 
-             * @param {string} result_contains - 
-             * @param {integer} result_min_size - 
-             * @param {integer} result_max_size - 
-             * 
-             */
-            hpcapi.prototype.Hpc_Health_Check_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/hpchealthchecks/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['hpcService'] !== undefined) {
-                    form['hpcService'] = parameters['hpcService'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['resource_name'] !== undefined) {
-                    form['resource_name'] = parameters['resource_name'];
-                }
-
-                if (parameters['resource_name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: resource_name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['result_contains'] !== undefined) {
-                    form['result_contains'] = parameters['result_contains'];
-                }
-
-                if (parameters['result_min_size'] !== undefined) {
-                    form['result_min_size'] = parameters['result_min_size'];
-                }
-
-                if (parameters['result_max_size'] !== undefined) {
-                    form['result_max_size'] = parameters['result_max_size'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Health_Check_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Hpc_Health_Check_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/hpchealthchecks/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Health_Check_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} hpcService - 
-             * @param {choice} kind - 
-             * @param {string} resource_name - 
-             * @param {string} result_contains - 
-             * @param {integer} result_min_size - 
-             * @param {integer} result_max_size - 
-             * 
-             */
-            hpcapi.prototype.Hpc_Health_Check_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/hpchealthchecks/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['hpcService'] !== undefined) {
-                    form['hpcService'] = parameters['hpcService'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['resource_name'] !== undefined) {
-                    form['resource_name'] = parameters['resource_name'];
-                }
-
-                if (parameters['resource_name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: resource_name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['result_contains'] !== undefined) {
-                    form['result_contains'] = parameters['result_contains'];
-                }
-
-                if (parameters['result_min_size'] !== undefined) {
-                    form['result_min_size'] = parameters['result_min_size'];
-                }
-
-                if (parameters['result_max_size'] !== undefined) {
-                    form['result_max_size'] = parameters['result_max_size'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Health_Check_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} hpcService - 
-             * @param {choice} kind - 
-             * @param {string} resource_name - 
-             * @param {string} result_contains - 
-             * @param {integer} result_min_size - 
-             * @param {integer} result_max_size - 
-             * 
-             */
-            hpcapi.prototype.Hpc_Health_Check_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/hpchealthchecks/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['hpcService'] !== undefined) {
-                    form['hpcService'] = parameters['hpcService'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['resource_name'] !== undefined) {
-                    form['resource_name'] = parameters['resource_name'];
-                }
-
-                if (parameters['result_contains'] !== undefined) {
-                    form['result_contains'] = parameters['result_contains'];
-                }
-
-                if (parameters['result_min_size'] !== undefined) {
-                    form['result_min_size'] = parameters['result_min_size'];
-                }
-
-                if (parameters['result_max_size'] !== undefined) {
-                    form['result_max_size'] = parameters['result_max_size'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Health_Check_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Hpc_Health_Check_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/hpchealthchecks/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Service_List_GET
-             * 
-             */
-            hpcapi.prototype.Hpc_Service_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/hpcservices/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Service_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} description - Description of Service
-             * @param {boolean} enabled - 
-             * @param {string} kind - Kind of service
-             * @param {string} name - Service Name
-             * @param {string} versionNumber - Version of Service Definition
-             * @param {boolean} published - 
-             * @param {string} view_url - 
-             * @param {string} icon_url - 
-             * @param {string} public_key - Public key string
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * @param {string} cmi_hostname - 
-             * @param {boolean} hpc_port80 - Enable port 80 for HPC
-             * @param {string} watcher_hpc_network - Network for hpc_watcher to contact hpc instance
-             * @param {string} watcher_dnsdemux_network - Network for hpc_watcher to contact dnsdemux instance
-             * @param {string} watcher_dnsredir_network - Network for hpc_watcher to contact dnsredir instance
-             * 
-             */
-            hpcapi.prototype.Hpc_Service_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/hpcservices/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['versionNumber'] !== undefined) {
-                    form['versionNumber'] = parameters['versionNumber'];
-                }
-
-                if (parameters['versionNumber'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: versionNumber'));
-                    return deferred.promise;
-                }
-
-                if (parameters['published'] !== undefined) {
-                    form['published'] = parameters['published'];
-                }
-
-                if (parameters['view_url'] !== undefined) {
-                    form['view_url'] = parameters['view_url'];
-                }
-
-                if (parameters['icon_url'] !== undefined) {
-                    form['icon_url'] = parameters['icon_url'];
-                }
-
-                if (parameters['public_key'] !== undefined) {
-                    form['public_key'] = parameters['public_key'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['cmi_hostname'] !== undefined) {
-                    form['cmi_hostname'] = parameters['cmi_hostname'];
-                }
-
-                if (parameters['hpc_port80'] !== undefined) {
-                    form['hpc_port80'] = parameters['hpc_port80'];
-                }
-
-                if (parameters['watcher_hpc_network'] !== undefined) {
-                    form['watcher_hpc_network'] = parameters['watcher_hpc_network'];
-                }
-
-                if (parameters['watcher_dnsdemux_network'] !== undefined) {
-                    form['watcher_dnsdemux_network'] = parameters['watcher_dnsdemux_network'];
-                }
-
-                if (parameters['watcher_dnsredir_network'] !== undefined) {
-                    form['watcher_dnsredir_network'] = parameters['watcher_dnsredir_network'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Service_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Hpc_Service_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/hpcservices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Service_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} description - Description of Service
-             * @param {boolean} enabled - 
-             * @param {string} kind - Kind of service
-             * @param {string} name - Service Name
-             * @param {string} versionNumber - Version of Service Definition
-             * @param {boolean} published - 
-             * @param {string} view_url - 
-             * @param {string} icon_url - 
-             * @param {string} public_key - Public key string
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * @param {string} cmi_hostname - 
-             * @param {boolean} hpc_port80 - Enable port 80 for HPC
-             * @param {string} watcher_hpc_network - Network for hpc_watcher to contact hpc instance
-             * @param {string} watcher_dnsdemux_network - Network for hpc_watcher to contact dnsdemux instance
-             * @param {string} watcher_dnsredir_network - Network for hpc_watcher to contact dnsredir instance
-             * 
-             */
-            hpcapi.prototype.Hpc_Service_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/hpcservices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['versionNumber'] !== undefined) {
-                    form['versionNumber'] = parameters['versionNumber'];
-                }
-
-                if (parameters['versionNumber'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: versionNumber'));
-                    return deferred.promise;
-                }
-
-                if (parameters['published'] !== undefined) {
-                    form['published'] = parameters['published'];
-                }
-
-                if (parameters['view_url'] !== undefined) {
-                    form['view_url'] = parameters['view_url'];
-                }
-
-                if (parameters['icon_url'] !== undefined) {
-                    form['icon_url'] = parameters['icon_url'];
-                }
-
-                if (parameters['public_key'] !== undefined) {
-                    form['public_key'] = parameters['public_key'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['cmi_hostname'] !== undefined) {
-                    form['cmi_hostname'] = parameters['cmi_hostname'];
-                }
-
-                if (parameters['hpc_port80'] !== undefined) {
-                    form['hpc_port80'] = parameters['hpc_port80'];
-                }
-
-                if (parameters['watcher_hpc_network'] !== undefined) {
-                    form['watcher_hpc_network'] = parameters['watcher_hpc_network'];
-                }
-
-                if (parameters['watcher_dnsdemux_network'] !== undefined) {
-                    form['watcher_dnsdemux_network'] = parameters['watcher_dnsdemux_network'];
-                }
-
-                if (parameters['watcher_dnsredir_network'] !== undefined) {
-                    form['watcher_dnsredir_network'] = parameters['watcher_dnsredir_network'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Service_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} description - Description of Service
-             * @param {boolean} enabled - 
-             * @param {string} kind - Kind of service
-             * @param {string} name - Service Name
-             * @param {string} versionNumber - Version of Service Definition
-             * @param {boolean} published - 
-             * @param {string} view_url - 
-             * @param {string} icon_url - 
-             * @param {string} public_key - Public key string
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * @param {string} cmi_hostname - 
-             * @param {boolean} hpc_port80 - Enable port 80 for HPC
-             * @param {string} watcher_hpc_network - Network for hpc_watcher to contact hpc instance
-             * @param {string} watcher_dnsdemux_network - Network for hpc_watcher to contact dnsdemux instance
-             * @param {string} watcher_dnsredir_network - Network for hpc_watcher to contact dnsredir instance
-             * 
-             */
-            hpcapi.prototype.Hpc_Service_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/hpcservices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['versionNumber'] !== undefined) {
-                    form['versionNumber'] = parameters['versionNumber'];
-                }
-
-                if (parameters['published'] !== undefined) {
-                    form['published'] = parameters['published'];
-                }
-
-                if (parameters['view_url'] !== undefined) {
-                    form['view_url'] = parameters['view_url'];
-                }
-
-                if (parameters['icon_url'] !== undefined) {
-                    form['icon_url'] = parameters['icon_url'];
-                }
-
-                if (parameters['public_key'] !== undefined) {
-                    form['public_key'] = parameters['public_key'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['cmi_hostname'] !== undefined) {
-                    form['cmi_hostname'] = parameters['cmi_hostname'];
-                }
-
-                if (parameters['hpc_port80'] !== undefined) {
-                    form['hpc_port80'] = parameters['hpc_port80'];
-                }
-
-                if (parameters['watcher_hpc_network'] !== undefined) {
-                    form['watcher_hpc_network'] = parameters['watcher_hpc_network'];
-                }
-
-                if (parameters['watcher_dnsdemux_network'] !== undefined) {
-                    form['watcher_dnsdemux_network'] = parameters['watcher_dnsdemux_network'];
-                }
-
-                if (parameters['watcher_dnsredir_network'] !== undefined) {
-                    form['watcher_dnsredir_network'] = parameters['watcher_dnsredir_network'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Hpc_Service_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Hpc_Service_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/hpcservices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Origin_Server_List_GET
-             * 
-             */
-            hpcapi.prototype.Origin_Server_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/originservers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Origin_Server_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {integer} origin_server_id - 
-             * @param {string} url - 
-             * @param {string} contentProvider - 
-             * @param {boolean} authenticated - Status for this Site
-             * @param {boolean} enabled - Status for this Site
-             * @param {choice} protocol - 
-             * @param {boolean} redirects - Indicates whether Origin Server redirects should be used for this Origin Server
-             * @param {string} description - 
-             * 
-             */
-            hpcapi.prototype.Origin_Server_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/originservers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['origin_server_id'] !== undefined) {
-                    form['origin_server_id'] = parameters['origin_server_id'];
-                }
-
-                if (parameters['url'] !== undefined) {
-                    form['url'] = parameters['url'];
-                }
-
-                if (parameters['url'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url'));
-                    return deferred.promise;
-                }
-
-                if (parameters['contentProvider'] !== undefined) {
-                    form['contentProvider'] = parameters['contentProvider'];
-                }
-
-                if (parameters['contentProvider'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: contentProvider'));
-                    return deferred.promise;
-                }
-
-                if (parameters['authenticated'] !== undefined) {
-                    form['authenticated'] = parameters['authenticated'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['protocol'] !== undefined) {
-                    form['protocol'] = parameters['protocol'];
-                }
-
-                if (parameters['protocol'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: protocol'));
-                    return deferred.promise;
-                }
-
-                if (parameters['redirects'] !== undefined) {
-                    form['redirects'] = parameters['redirects'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Origin_Server_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Origin_Server_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/originservers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Origin_Server_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {integer} origin_server_id - 
-             * @param {string} url - 
-             * @param {string} contentProvider - 
-             * @param {boolean} authenticated - Status for this Site
-             * @param {boolean} enabled - Status for this Site
-             * @param {choice} protocol - 
-             * @param {boolean} redirects - Indicates whether Origin Server redirects should be used for this Origin Server
-             * @param {string} description - 
-             * 
-             */
-            hpcapi.prototype.Origin_Server_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/originservers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['origin_server_id'] !== undefined) {
-                    form['origin_server_id'] = parameters['origin_server_id'];
-                }
-
-                if (parameters['url'] !== undefined) {
-                    form['url'] = parameters['url'];
-                }
-
-                if (parameters['url'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url'));
-                    return deferred.promise;
-                }
-
-                if (parameters['contentProvider'] !== undefined) {
-                    form['contentProvider'] = parameters['contentProvider'];
-                }
-
-                if (parameters['contentProvider'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: contentProvider'));
-                    return deferred.promise;
-                }
-
-                if (parameters['authenticated'] !== undefined) {
-                    form['authenticated'] = parameters['authenticated'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['protocol'] !== undefined) {
-                    form['protocol'] = parameters['protocol'];
-                }
-
-                if (parameters['protocol'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: protocol'));
-                    return deferred.promise;
-                }
-
-                if (parameters['redirects'] !== undefined) {
-                    form['redirects'] = parameters['redirects'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Origin_Server_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {integer} origin_server_id - 
-             * @param {string} url - 
-             * @param {string} contentProvider - 
-             * @param {boolean} authenticated - Status for this Site
-             * @param {boolean} enabled - Status for this Site
-             * @param {choice} protocol - 
-             * @param {boolean} redirects - Indicates whether Origin Server redirects should be used for this Origin Server
-             * @param {string} description - 
-             * 
-             */
-            hpcapi.prototype.Origin_Server_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/originservers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['origin_server_id'] !== undefined) {
-                    form['origin_server_id'] = parameters['origin_server_id'];
-                }
-
-                if (parameters['url'] !== undefined) {
-                    form['url'] = parameters['url'];
-                }
-
-                if (parameters['contentProvider'] !== undefined) {
-                    form['contentProvider'] = parameters['contentProvider'];
-                }
-
-                if (parameters['authenticated'] !== undefined) {
-                    form['authenticated'] = parameters['authenticated'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['protocol'] !== undefined) {
-                    form['protocol'] = parameters['protocol'];
-                }
-
-                if (parameters['redirects'] !== undefined) {
-                    form['redirects'] = parameters['redirects'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Origin_Server_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Origin_Server_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/originservers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Cdn_Prefix_List_GET
-             * 
-             */
-            hpcapi.prototype.Cdn_Prefix_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/cdnprefixs/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Cdn_Prefix_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {integer} cdn_prefix_id - 
-             * @param {string} prefix - Registered Prefix for Domain
-             * @param {string} contentProvider - 
-             * @param {string} description - Description of Content Provider
-             * @param {string} defaultOriginServer - 
-             * @param {boolean} enabled - 
-             * 
-             */
-            hpcapi.prototype.Cdn_Prefix_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/cdnprefixs/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['cdn_prefix_id'] !== undefined) {
-                    form['cdn_prefix_id'] = parameters['cdn_prefix_id'];
-                }
-
-                if (parameters['prefix'] !== undefined) {
-                    form['prefix'] = parameters['prefix'];
-                }
-
-                if (parameters['prefix'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: prefix'));
-                    return deferred.promise;
-                }
-
-                if (parameters['contentProvider'] !== undefined) {
-                    form['contentProvider'] = parameters['contentProvider'];
-                }
-
-                if (parameters['contentProvider'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: contentProvider'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['defaultOriginServer'] !== undefined) {
-                    form['defaultOriginServer'] = parameters['defaultOriginServer'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Cdn_Prefix_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Cdn_Prefix_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/cdnprefixs/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Cdn_Prefix_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {integer} cdn_prefix_id - 
-             * @param {string} prefix - Registered Prefix for Domain
-             * @param {string} contentProvider - 
-             * @param {string} description - Description of Content Provider
-             * @param {string} defaultOriginServer - 
-             * @param {boolean} enabled - 
-             * 
-             */
-            hpcapi.prototype.Cdn_Prefix_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/cdnprefixs/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['cdn_prefix_id'] !== undefined) {
-                    form['cdn_prefix_id'] = parameters['cdn_prefix_id'];
-                }
-
-                if (parameters['prefix'] !== undefined) {
-                    form['prefix'] = parameters['prefix'];
-                }
-
-                if (parameters['prefix'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: prefix'));
-                    return deferred.promise;
-                }
-
-                if (parameters['contentProvider'] !== undefined) {
-                    form['contentProvider'] = parameters['contentProvider'];
-                }
-
-                if (parameters['contentProvider'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: contentProvider'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['defaultOriginServer'] !== undefined) {
-                    form['defaultOriginServer'] = parameters['defaultOriginServer'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Cdn_Prefix_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {integer} cdn_prefix_id - 
-             * @param {string} prefix - Registered Prefix for Domain
-             * @param {string} contentProvider - 
-             * @param {string} description - Description of Content Provider
-             * @param {string} defaultOriginServer - 
-             * @param {boolean} enabled - 
-             * 
-             */
-            hpcapi.prototype.Cdn_Prefix_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/cdnprefixs/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['cdn_prefix_id'] !== undefined) {
-                    form['cdn_prefix_id'] = parameters['cdn_prefix_id'];
-                }
-
-                if (parameters['prefix'] !== undefined) {
-                    form['prefix'] = parameters['prefix'];
-                }
-
-                if (parameters['contentProvider'] !== undefined) {
-                    form['contentProvider'] = parameters['contentProvider'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['defaultOriginServer'] !== undefined) {
-                    form['defaultOriginServer'] = parameters['defaultOriginServer'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Cdn_Prefix_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Cdn_Prefix_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/cdnprefixs/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Service_Provider_List_GET
-             * 
-             */
-            hpcapi.prototype.Service_Provider_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/serviceproviders/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Service_Provider_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} hpcService - 
-             * @param {integer} service_provider_id - 
-             * @param {string} name - Service Provider Name
-             * @param {string} description - Description of Service Provider
-             * @param {boolean} enabled - 
-             * 
-             */
-            hpcapi.prototype.Service_Provider_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/serviceproviders/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['hpcService'] !== undefined) {
-                    form['hpcService'] = parameters['hpcService'];
-                }
-
-                if (parameters['hpcService'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: hpcService'));
-                    return deferred.promise;
-                }
-
-                if (parameters['service_provider_id'] !== undefined) {
-                    form['service_provider_id'] = parameters['service_provider_id'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Service_Provider_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Service_Provider_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/serviceproviders/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Service_Provider_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} hpcService - 
-             * @param {integer} service_provider_id - 
-             * @param {string} name - Service Provider Name
-             * @param {string} description - Description of Service Provider
-             * @param {boolean} enabled - 
-             * 
-             */
-            hpcapi.prototype.Service_Provider_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/serviceproviders/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['hpcService'] !== undefined) {
-                    form['hpcService'] = parameters['hpcService'];
-                }
-
-                if (parameters['hpcService'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: hpcService'));
-                    return deferred.promise;
-                }
-
-                if (parameters['service_provider_id'] !== undefined) {
-                    form['service_provider_id'] = parameters['service_provider_id'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Service_Provider_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} hpcService - 
-             * @param {integer} service_provider_id - 
-             * @param {string} name - Service Provider Name
-             * @param {string} description - Description of Service Provider
-             * @param {boolean} enabled - 
-             * 
-             */
-            hpcapi.prototype.Service_Provider_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/serviceproviders/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['hpcService'] !== undefined) {
-                    form['hpcService'] = parameters['hpcService'];
-                }
-
-                if (parameters['service_provider_id'] !== undefined) {
-                    form['service_provider_id'] = parameters['service_provider_id'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Service_Provider_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Service_Provider_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/serviceproviders/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Content_Provider_List_GET
-             * 
-             */
-            hpcapi.prototype.Content_Provider_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/contentproviders/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Content_Provider_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {integer} content_provider_id - 
-             * @param {string} name - 
-             * @param {boolean} enabled - 
-             * @param {string} description - Description of Content Provider
-             * @param {string} serviceProvider - 
-             * 
-             */
-            hpcapi.prototype.Content_Provider_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/contentproviders/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['content_provider_id'] !== undefined) {
-                    form['content_provider_id'] = parameters['content_provider_id'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['serviceProvider'] !== undefined) {
-                    form['serviceProvider'] = parameters['serviceProvider'];
-                }
-
-                if (parameters['serviceProvider'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: serviceProvider'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Content_Provider_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Content_Provider_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/contentproviders/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Content_Provider_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {integer} content_provider_id - 
-             * @param {string} name - 
-             * @param {boolean} enabled - 
-             * @param {string} description - Description of Content Provider
-             * @param {string} serviceProvider - 
-             * 
-             */
-            hpcapi.prototype.Content_Provider_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/contentproviders/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['content_provider_id'] !== undefined) {
-                    form['content_provider_id'] = parameters['content_provider_id'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['serviceProvider'] !== undefined) {
-                    form['serviceProvider'] = parameters['serviceProvider'];
-                }
-
-                if (parameters['serviceProvider'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: serviceProvider'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Content_Provider_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {integer} content_provider_id - 
-             * @param {string} name - 
-             * @param {boolean} enabled - 
-             * @param {string} description - Description of Content Provider
-             * @param {string} serviceProvider - 
-             * 
-             */
-            hpcapi.prototype.Content_Provider_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/contentproviders/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['content_provider_id'] !== undefined) {
-                    form['content_provider_id'] = parameters['content_provider_id'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['serviceProvider'] !== undefined) {
-                    form['serviceProvider'] = parameters['serviceProvider'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Content_Provider_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Content_Provider_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/contentproviders/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Access_Map_List_GET
-             * 
-             */
-            hpcapi.prototype.Access_Map_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/accessmaps/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Access_Map_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} contentProvider - 
-             * @param {string} name - Name of the Access Map
-             * @param {string} description - 
-             * @param {string} map - specifies which client requests are allowed
-             * 
-             */
-            hpcapi.prototype.Access_Map_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/accessmaps/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['contentProvider'] !== undefined) {
-                    form['contentProvider'] = parameters['contentProvider'];
-                }
-
-                if (parameters['contentProvider'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: contentProvider'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['map'] !== undefined) {
-                    form['map'] = parameters['map'];
-                }
-
-                if (parameters['map'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: map'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Access_Map_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Access_Map_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/accessmaps/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Access_Map_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} contentProvider - 
-             * @param {string} name - Name of the Access Map
-             * @param {string} description - 
-             * @param {string} map - specifies which client requests are allowed
-             * 
-             */
-            hpcapi.prototype.Access_Map_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/accessmaps/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['contentProvider'] !== undefined) {
-                    form['contentProvider'] = parameters['contentProvider'];
-                }
-
-                if (parameters['contentProvider'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: contentProvider'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['map'] !== undefined) {
-                    form['map'] = parameters['map'];
-                }
-
-                if (parameters['map'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: map'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Access_Map_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} contentProvider - 
-             * @param {string} name - Name of the Access Map
-             * @param {string} description - 
-             * @param {string} map - specifies which client requests are allowed
-             * 
-             */
-            hpcapi.prototype.Access_Map_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/accessmaps/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['contentProvider'] !== undefined) {
-                    form['contentProvider'] = parameters['contentProvider'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['map'] !== undefined) {
-                    form['map'] = parameters['map'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Access_Map_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Access_Map_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/accessmaps/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Site_Map_List_GET
-             * 
-             */
-            hpcapi.prototype.Site_Map_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/sitemaps/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Site_Map_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} contentProvider - 
-             * @param {string} serviceProvider - 
-             * @param {string} cdnPrefix - 
-             * @param {string} hpcService - 
-             * @param {string} name - Name of the Site Map
-             * @param {string} description - 
-             * @param {string} map - specifies how to map requests to hpc instances
-             * @param {integer} map_id - 
-             * 
-             */
-            hpcapi.prototype.Site_Map_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/sitemaps/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['contentProvider'] !== undefined) {
-                    form['contentProvider'] = parameters['contentProvider'];
-                }
-
-                if (parameters['serviceProvider'] !== undefined) {
-                    form['serviceProvider'] = parameters['serviceProvider'];
-                }
-
-                if (parameters['cdnPrefix'] !== undefined) {
-                    form['cdnPrefix'] = parameters['cdnPrefix'];
-                }
-
-                if (parameters['hpcService'] !== undefined) {
-                    form['hpcService'] = parameters['hpcService'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['map'] !== undefined) {
-                    form['map'] = parameters['map'];
-                }
-
-                if (parameters['map'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: map'));
-                    return deferred.promise;
-                }
-
-                if (parameters['map_id'] !== undefined) {
-                    form['map_id'] = parameters['map_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Site_Map_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Site_Map_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/sitemaps/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Site_Map_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} contentProvider - 
-             * @param {string} serviceProvider - 
-             * @param {string} cdnPrefix - 
-             * @param {string} hpcService - 
-             * @param {string} name - Name of the Site Map
-             * @param {string} description - 
-             * @param {string} map - specifies how to map requests to hpc instances
-             * @param {integer} map_id - 
-             * 
-             */
-            hpcapi.prototype.Site_Map_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/sitemaps/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['contentProvider'] !== undefined) {
-                    form['contentProvider'] = parameters['contentProvider'];
-                }
-
-                if (parameters['serviceProvider'] !== undefined) {
-                    form['serviceProvider'] = parameters['serviceProvider'];
-                }
-
-                if (parameters['cdnPrefix'] !== undefined) {
-                    form['cdnPrefix'] = parameters['cdnPrefix'];
-                }
-
-                if (parameters['hpcService'] !== undefined) {
-                    form['hpcService'] = parameters['hpcService'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['map'] !== undefined) {
-                    form['map'] = parameters['map'];
-                }
-
-                if (parameters['map'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: map'));
-                    return deferred.promise;
-                }
-
-                if (parameters['map_id'] !== undefined) {
-                    form['map_id'] = parameters['map_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Site_Map_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} contentProvider - 
-             * @param {string} serviceProvider - 
-             * @param {string} cdnPrefix - 
-             * @param {string} hpcService - 
-             * @param {string} name - Name of the Site Map
-             * @param {string} description - 
-             * @param {string} map - specifies how to map requests to hpc instances
-             * @param {integer} map_id - 
-             * 
-             */
-            hpcapi.prototype.Site_Map_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/sitemaps/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['contentProvider'] !== undefined) {
-                    form['contentProvider'] = parameters['contentProvider'];
-                }
-
-                if (parameters['serviceProvider'] !== undefined) {
-                    form['serviceProvider'] = parameters['serviceProvider'];
-                }
-
-                if (parameters['cdnPrefix'] !== undefined) {
-                    form['cdnPrefix'] = parameters['cdnPrefix'];
-                }
-
-                if (parameters['hpcService'] !== undefined) {
-                    form['hpcService'] = parameters['hpcService'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['map'] !== undefined) {
-                    form['map'] = parameters['map'];
-                }
-
-                if (parameters['map_id'] !== undefined) {
-                    form['map_id'] = parameters['map_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name hpcapi#Site_Map_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            hpcapi.prototype.Site_Map_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/hpcapi/sitemaps/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = hpcapi.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-
-            return hpcapi;
-        })();
-
-        return hpcapi;
-    }]);/*jshint -W069 */
-/*global angular:false */
-angular.module('xos.xos', [])
-    .factory('xos', ['$q', '$http', '$rootScope', function($q, $http, $rootScope) {
-        'use strict';
-
-        /**
-         * 
-         * @class xos
-         * @param {(string|object)} [domainOrOptions] - The project domain or options object. If object, see the object's optional properties.
-         * @param {string} [domainOrOptions.domain] - The project domain
-         * @param {string} [domainOrOptions.cache] - An angularjs cache implementation
-         * @param {object} [domainOrOptions.token] - auth token - object with value property and optional headerOrQueryName and isQuery properties
-         * @param {string} [cache] - An angularjs cache implementation
-         */
-        var xos = (function() {
-            function xos(options, cache) {
-                var domain = (typeof options === 'object') ? options.domain : options;
-                this.domain = typeof(domain) === 'string' ? domain : 'http://localhost:9999';
-                cache = cache || ((typeof options === 'object') ? options.cache : cache);
-                this.cache = cache;
-            }
-
-            xos.prototype.$on = function($scope, path, handler) {
-                var url = domain + path;
-                $scope.$on(url, function() {
-                    handler();
-                });
-                return this;
-            };
-
-            xos.prototype.$broadcast = function(path) {
-                var url = domain + path;
-                //cache.remove(url);
-                $rootScope.$broadcast(url);
-                return this;
-            };
-
-            xos.transformRequest = function(obj) {
-                var str = [];
-                for (var p in obj) {
-                    var val = obj[p];
-                    if (angular.isArray(val)) {
-                        val.forEach(function(val) {
-                            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(val));
-                        });
-                    } else {
-                        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(val));
-                    }
-                }
-                return str.join("&");
-            };
-
-            /**
-             * 
-             * @method
-             * @name xos#Api_Root_GET
-             * 
-             */
-            xos.prototype.Api_Root_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Role_List_GET
-             * 
-             */
-            xos.prototype.Site_Role_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/site_roles/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Role_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {choice} role - 
-             * 
-             */
-            xos.prototype.Site_Role_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/site_roles/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Role_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Site_Role_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/site_roles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Role_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {choice} role - 
-             * 
-             */
-            xos.prototype.Site_Role_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/site_roles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Role_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {choice} role - 
-             * 
-             */
-            xos.prototype.Site_Role_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/site_roles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Role_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Site_Role_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/site_roles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Attribute_List_GET
-             * 
-             */
-            xos.prototype.Service_Attribute_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceattributes/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Attribute_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Attribute Name
-             * @param {string} value - Attribute Value
-             * @param {string} service - The Service this attribute is associated with
-             * 
-             */
-            xos.prototype.Service_Attribute_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceattributes/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['value'] !== undefined) {
-                    form['value'] = parameters['value'];
-                }
-
-                if (parameters['value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: value'));
-                    return deferred.promise;
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: service'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Attribute_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Service_Attribute_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceattributes/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Attribute_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Attribute Name
-             * @param {string} value - Attribute Value
-             * @param {string} service - The Service this attribute is associated with
-             * 
-             */
-            xos.prototype.Service_Attribute_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceattributes/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['value'] !== undefined) {
-                    form['value'] = parameters['value'];
-                }
-
-                if (parameters['value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: value'));
-                    return deferred.promise;
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: service'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Attribute_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Attribute Name
-             * @param {string} value - Attribute Value
-             * @param {string} service - The Service this attribute is associated with
-             * 
-             */
-            xos.prototype.Service_Attribute_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceattributes/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['value'] !== undefined) {
-                    form['value'] = parameters['value'];
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Attribute_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Service_Attribute_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceattributes/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Images_List_GET
-             * 
-             */
-            xos.prototype.Controller_Images_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerimages/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Images_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} image - 
-             * @param {string} controller - 
-             * @param {string} glance_image_id - Glance image id
-             * 
-             */
-            xos.prototype.Controller_Images_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerimages/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['image'] !== undefined) {
-                    form['image'] = parameters['image'];
-                }
-
-                if (parameters['image'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: image'));
-                    return deferred.promise;
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['glance_image_id'] !== undefined) {
-                    form['glance_image_id'] = parameters['glance_image_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Images_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Images_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerimages/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Images_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} image - 
-             * @param {string} controller - 
-             * @param {string} glance_image_id - Glance image id
-             * 
-             */
-            xos.prototype.Controller_Images_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerimages/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['image'] !== undefined) {
-                    form['image'] = parameters['image'];
-                }
-
-                if (parameters['image'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: image'));
-                    return deferred.promise;
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['glance_image_id'] !== undefined) {
-                    form['glance_image_id'] = parameters['glance_image_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Images_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} image - 
-             * @param {string} controller - 
-             * @param {string} glance_image_id - Glance image id
-             * 
-             */
-            xos.prototype.Controller_Images_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerimages/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['image'] !== undefined) {
-                    form['image'] = parameters['image'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['glance_image_id'] !== undefined) {
-                    form['glance_image_id'] = parameters['glance_image_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Images_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Images_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerimages/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Site_Privilege_List_GET
-             * 
-             */
-            xos.prototype.Controller_Site_Privilege_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersiteprivileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Site_Privilege_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} controller - 
-             * @param {string} site_privilege - 
-             * @param {string} role_id - Keystone id
-             * 
-             */
-            xos.prototype.Controller_Site_Privilege_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersiteprivileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['site_privilege'] !== undefined) {
-                    form['site_privilege'] = parameters['site_privilege'];
-                }
-
-                if (parameters['site_privilege'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site_privilege'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role_id'] !== undefined) {
-                    form['role_id'] = parameters['role_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Site_Privilege_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Site_Privilege_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersiteprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Site_Privilege_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} controller - 
-             * @param {string} site_privilege - 
-             * @param {string} role_id - Keystone id
-             * 
-             */
-            xos.prototype.Controller_Site_Privilege_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersiteprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['site_privilege'] !== undefined) {
-                    form['site_privilege'] = parameters['site_privilege'];
-                }
-
-                if (parameters['site_privilege'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site_privilege'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role_id'] !== undefined) {
-                    form['role_id'] = parameters['role_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Site_Privilege_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} controller - 
-             * @param {string} site_privilege - 
-             * @param {string} role_id - Keystone id
-             * 
-             */
-            xos.prototype.Controller_Site_Privilege_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersiteprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['site_privilege'] !== undefined) {
-                    form['site_privilege'] = parameters['site_privilege'];
-                }
-
-                if (parameters['role_id'] !== undefined) {
-                    form['role_id'] = parameters['role_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Site_Privilege_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Site_Privilege_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersiteprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Image_List_GET
-             * 
-             */
-            xos.prototype.Image_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/images/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Image_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} disk_format - 
-             * @param {string} container_format - 
-             * @param {string} path - Path to image on local disk
-             * 
-             */
-            xos.prototype.Image_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/images/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['disk_format'] !== undefined) {
-                    form['disk_format'] = parameters['disk_format'];
-                }
-
-                if (parameters['disk_format'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: disk_format'));
-                    return deferred.promise;
-                }
-
-                if (parameters['container_format'] !== undefined) {
-                    form['container_format'] = parameters['container_format'];
-                }
-
-                if (parameters['container_format'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: container_format'));
-                    return deferred.promise;
-                }
-
-                if (parameters['path'] !== undefined) {
-                    form['path'] = parameters['path'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Image_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Image_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/images/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Image_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} disk_format - 
-             * @param {string} container_format - 
-             * @param {string} path - Path to image on local disk
-             * 
-             */
-            xos.prototype.Image_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/images/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['disk_format'] !== undefined) {
-                    form['disk_format'] = parameters['disk_format'];
-                }
-
-                if (parameters['disk_format'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: disk_format'));
-                    return deferred.promise;
-                }
-
-                if (parameters['container_format'] !== undefined) {
-                    form['container_format'] = parameters['container_format'];
-                }
-
-                if (parameters['container_format'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: container_format'));
-                    return deferred.promise;
-                }
-
-                if (parameters['path'] !== undefined) {
-                    form['path'] = parameters['path'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Image_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} disk_format - 
-             * @param {string} container_format - 
-             * @param {string} path - Path to image on local disk
-             * 
-             */
-            xos.prototype.Image_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/images/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['disk_format'] !== undefined) {
-                    form['disk_format'] = parameters['disk_format'];
-                }
-
-                if (parameters['container_format'] !== undefined) {
-                    form['container_format'] = parameters['container_format'];
-                }
-
-                if (parameters['path'] !== undefined) {
-                    form['path'] = parameters['path'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Image_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Image_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/images/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Parameter_List_GET
-             * 
-             */
-            xos.prototype.Network_Parameter_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkparameters/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Parameter_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} parameter - The type of the parameter
-             * @param {string} value - The value of this parameter
-             * @param {string} content_type - 
-             * @param {integer} object_id - 
-             * 
-             */
-            xos.prototype.Network_Parameter_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkparameters/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['parameter'] !== undefined) {
-                    form['parameter'] = parameters['parameter'];
-                }
-
-                if (parameters['parameter'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: parameter'));
-                    return deferred.promise;
-                }
-
-                if (parameters['value'] !== undefined) {
-                    form['value'] = parameters['value'];
-                }
-
-                if (parameters['value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: value'));
-                    return deferred.promise;
-                }
-
-                if (parameters['content_type'] !== undefined) {
-                    form['content_type'] = parameters['content_type'];
-                }
-
-                if (parameters['content_type'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: content_type'));
-                    return deferred.promise;
-                }
-
-                if (parameters['object_id'] !== undefined) {
-                    form['object_id'] = parameters['object_id'];
-                }
-
-                if (parameters['object_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: object_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Parameter_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Network_Parameter_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkparameters/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Parameter_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} parameter - The type of the parameter
-             * @param {string} value - The value of this parameter
-             * @param {string} content_type - 
-             * @param {integer} object_id - 
-             * 
-             */
-            xos.prototype.Network_Parameter_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkparameters/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['parameter'] !== undefined) {
-                    form['parameter'] = parameters['parameter'];
-                }
-
-                if (parameters['parameter'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: parameter'));
-                    return deferred.promise;
-                }
-
-                if (parameters['value'] !== undefined) {
-                    form['value'] = parameters['value'];
-                }
-
-                if (parameters['value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: value'));
-                    return deferred.promise;
-                }
-
-                if (parameters['content_type'] !== undefined) {
-                    form['content_type'] = parameters['content_type'];
-                }
-
-                if (parameters['content_type'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: content_type'));
-                    return deferred.promise;
-                }
-
-                if (parameters['object_id'] !== undefined) {
-                    form['object_id'] = parameters['object_id'];
-                }
-
-                if (parameters['object_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: object_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Parameter_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} parameter - The type of the parameter
-             * @param {string} value - The value of this parameter
-             * @param {string} content_type - 
-             * @param {integer} object_id - 
-             * 
-             */
-            xos.prototype.Network_Parameter_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkparameters/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['parameter'] !== undefined) {
-                    form['parameter'] = parameters['parameter'];
-                }
-
-                if (parameters['value'] !== undefined) {
-                    form['value'] = parameters['value'];
-                }
-
-                if (parameters['content_type'] !== undefined) {
-                    form['content_type'] = parameters['content_type'];
-                }
-
-                if (parameters['object_id'] !== undefined) {
-                    form['object_id'] = parameters['object_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Parameter_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Network_Parameter_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkparameters/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_List_GET
-             * 
-             */
-            xos.prototype.Site_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sites/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name for this Site
-             * @param {string} site_url - Site's Home URL Page
-             * @param {boolean} enabled - Status for this Site
-             * @param {boolean} hosts_nodes - Indicates whether or not the site host nodes
-             * @param {boolean} hosts_users - Indicates whether or not the site manages user accounts
-             * @param {string} location - 
-             * @param {number} longitude - 
-             * @param {number} latitude - 
-             * @param {string} login_base - Prefix for Slices associated with this Site
-             * @param {boolean} is_public - Indicates the visibility of this site to other members
-             * @param {string} abbreviated_name - 
-             * 
-             */
-            xos.prototype.Site_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sites/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['site_url'] !== undefined) {
-                    form['site_url'] = parameters['site_url'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['hosts_nodes'] !== undefined) {
-                    form['hosts_nodes'] = parameters['hosts_nodes'];
-                }
-
-                if (parameters['hosts_users'] !== undefined) {
-                    form['hosts_users'] = parameters['hosts_users'];
-                }
-
-                if (parameters['location'] !== undefined) {
-                    form['location'] = parameters['location'];
-                }
-
-                if (parameters['location'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: location'));
-                    return deferred.promise;
-                }
-
-                if (parameters['longitude'] !== undefined) {
-                    form['longitude'] = parameters['longitude'];
-                }
-
-                if (parameters['latitude'] !== undefined) {
-                    form['latitude'] = parameters['latitude'];
-                }
-
-                if (parameters['login_base'] !== undefined) {
-                    form['login_base'] = parameters['login_base'];
-                }
-
-                if (parameters['login_base'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: login_base'));
-                    return deferred.promise;
-                }
-
-                if (parameters['is_public'] !== undefined) {
-                    form['is_public'] = parameters['is_public'];
-                }
-
-                if (parameters['abbreviated_name'] !== undefined) {
-                    form['abbreviated_name'] = parameters['abbreviated_name'];
-                }
-
-                if (parameters['abbreviated_name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: abbreviated_name'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Site_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sites/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name for this Site
-             * @param {string} site_url - Site's Home URL Page
-             * @param {boolean} enabled - Status for this Site
-             * @param {boolean} hosts_nodes - Indicates whether or not the site host nodes
-             * @param {boolean} hosts_users - Indicates whether or not the site manages user accounts
-             * @param {string} location - 
-             * @param {number} longitude - 
-             * @param {number} latitude - 
-             * @param {string} login_base - Prefix for Slices associated with this Site
-             * @param {boolean} is_public - Indicates the visibility of this site to other members
-             * @param {string} abbreviated_name - 
-             * 
-             */
-            xos.prototype.Site_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sites/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['site_url'] !== undefined) {
-                    form['site_url'] = parameters['site_url'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['hosts_nodes'] !== undefined) {
-                    form['hosts_nodes'] = parameters['hosts_nodes'];
-                }
-
-                if (parameters['hosts_users'] !== undefined) {
-                    form['hosts_users'] = parameters['hosts_users'];
-                }
-
-                if (parameters['location'] !== undefined) {
-                    form['location'] = parameters['location'];
-                }
-
-                if (parameters['location'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: location'));
-                    return deferred.promise;
-                }
-
-                if (parameters['longitude'] !== undefined) {
-                    form['longitude'] = parameters['longitude'];
-                }
-
-                if (parameters['latitude'] !== undefined) {
-                    form['latitude'] = parameters['latitude'];
-                }
-
-                if (parameters['login_base'] !== undefined) {
-                    form['login_base'] = parameters['login_base'];
-                }
-
-                if (parameters['login_base'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: login_base'));
-                    return deferred.promise;
-                }
-
-                if (parameters['is_public'] !== undefined) {
-                    form['is_public'] = parameters['is_public'];
-                }
-
-                if (parameters['abbreviated_name'] !== undefined) {
-                    form['abbreviated_name'] = parameters['abbreviated_name'];
-                }
-
-                if (parameters['abbreviated_name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: abbreviated_name'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name for this Site
-             * @param {string} site_url - Site's Home URL Page
-             * @param {boolean} enabled - Status for this Site
-             * @param {boolean} hosts_nodes - Indicates whether or not the site host nodes
-             * @param {boolean} hosts_users - Indicates whether or not the site manages user accounts
-             * @param {string} location - 
-             * @param {number} longitude - 
-             * @param {number} latitude - 
-             * @param {string} login_base - Prefix for Slices associated with this Site
-             * @param {boolean} is_public - Indicates the visibility of this site to other members
-             * @param {string} abbreviated_name - 
-             * 
-             */
-            xos.prototype.Site_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sites/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['site_url'] !== undefined) {
-                    form['site_url'] = parameters['site_url'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['hosts_nodes'] !== undefined) {
-                    form['hosts_nodes'] = parameters['hosts_nodes'];
-                }
-
-                if (parameters['hosts_users'] !== undefined) {
-                    form['hosts_users'] = parameters['hosts_users'];
-                }
-
-                if (parameters['location'] !== undefined) {
-                    form['location'] = parameters['location'];
-                }
-
-                if (parameters['longitude'] !== undefined) {
-                    form['longitude'] = parameters['longitude'];
-                }
-
-                if (parameters['latitude'] !== undefined) {
-                    form['latitude'] = parameters['latitude'];
-                }
-
-                if (parameters['login_base'] !== undefined) {
-                    form['login_base'] = parameters['login_base'];
-                }
-
-                if (parameters['is_public'] !== undefined) {
-                    form['is_public'] = parameters['is_public'];
-                }
-
-                if (parameters['abbreviated_name'] !== undefined) {
-                    form['abbreviated_name'] = parameters['abbreviated_name'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Site_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sites/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Role_List_GET
-             * 
-             */
-            xos.prototype.Tenant_Root_Role_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantrootroles/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Role_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {choice} role - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Role_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantrootroles/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Role_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Role_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantrootroles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Role_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {choice} role - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Role_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantrootroles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Role_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {choice} role - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Role_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantrootroles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Role_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Role_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantrootroles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Role_List_GET
-             * 
-             */
-            xos.prototype.Slice_Role_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slice_roles/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Role_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {choice} role - 
-             * 
-             */
-            xos.prototype.Slice_Role_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slice_roles/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Role_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Slice_Role_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slice_roles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Role_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {choice} role - 
-             * 
-             */
-            xos.prototype.Slice_Role_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slice_roles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Role_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {choice} role - 
-             * 
-             */
-            xos.prototype.Slice_Role_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slice_roles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Role_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Slice_Role_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slice_roles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tag_List_GET
-             * 
-             */
-            xos.prototype.Tag_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tags/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tag_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} service - The Service this Tag is associated with
-             * @param {string} name - The name of this tag
-             * @param {string} value - The value of this tag
-             * @param {string} content_type - 
-             * @param {integer} object_id - 
-             * 
-             */
-            xos.prototype.Tag_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tags/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['value'] !== undefined) {
-                    form['value'] = parameters['value'];
-                }
-
-                if (parameters['value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: value'));
-                    return deferred.promise;
-                }
-
-                if (parameters['content_type'] !== undefined) {
-                    form['content_type'] = parameters['content_type'];
-                }
-
-                if (parameters['content_type'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: content_type'));
-                    return deferred.promise;
-                }
-
-                if (parameters['object_id'] !== undefined) {
-                    form['object_id'] = parameters['object_id'];
-                }
-
-                if (parameters['object_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: object_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tag_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Tag_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tags/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tag_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} service - The Service this Tag is associated with
-             * @param {string} name - The name of this tag
-             * @param {string} value - The value of this tag
-             * @param {string} content_type - 
-             * @param {integer} object_id - 
-             * 
-             */
-            xos.prototype.Tag_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tags/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['value'] !== undefined) {
-                    form['value'] = parameters['value'];
-                }
-
-                if (parameters['value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: value'));
-                    return deferred.promise;
-                }
-
-                if (parameters['content_type'] !== undefined) {
-                    form['content_type'] = parameters['content_type'];
-                }
-
-                if (parameters['content_type'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: content_type'));
-                    return deferred.promise;
-                }
-
-                if (parameters['object_id'] !== undefined) {
-                    form['object_id'] = parameters['object_id'];
-                }
-
-                if (parameters['object_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: object_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tag_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} service - The Service this Tag is associated with
-             * @param {string} name - The name of this tag
-             * @param {string} value - The value of this tag
-             * @param {string} content_type - 
-             * @param {integer} object_id - 
-             * 
-             */
-            xos.prototype.Tag_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tags/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['value'] !== undefined) {
-                    form['value'] = parameters['value'];
-                }
-
-                if (parameters['content_type'] !== undefined) {
-                    form['content_type'] = parameters['content_type'];
-                }
-
-                if (parameters['object_id'] !== undefined) {
-                    form['object_id'] = parameters['object_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tag_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Tag_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tags/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Invoice_List_GET
-             * 
-             */
-            xos.prototype.Invoice_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/invoices/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Invoice_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} date - 
-             * @param {string} account - 
-             * 
-             */
-            xos.prototype.Invoice_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/invoices/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['date'] !== undefined) {
-                    form['date'] = parameters['date'];
-                }
-
-                if (parameters['date'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: date'));
-                    return deferred.promise;
-                }
-
-                if (parameters['account'] !== undefined) {
-                    form['account'] = parameters['account'];
-                }
-
-                if (parameters['account'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: account'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Invoice_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Invoice_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/invoices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Invoice_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} date - 
-             * @param {string} account - 
-             * 
-             */
-            xos.prototype.Invoice_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/invoices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['date'] !== undefined) {
-                    form['date'] = parameters['date'];
-                }
-
-                if (parameters['date'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: date'));
-                    return deferred.promise;
-                }
-
-                if (parameters['account'] !== undefined) {
-                    form['account'] = parameters['account'];
-                }
-
-                if (parameters['account'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: account'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Invoice_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} date - 
-             * @param {string} account - 
-             * 
-             */
-            xos.prototype.Invoice_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/invoices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['date'] !== undefined) {
-                    form['date'] = parameters['date'];
-                }
-
-                if (parameters['account'] !== undefined) {
-                    form['account'] = parameters['account'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Invoice_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Invoice_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/invoices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Privilege_List_GET
-             * 
-             */
-            xos.prototype.Slice_Privilege_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slice_privileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Privilege_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} slice - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Slice_Privilege_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slice_privileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Privilege_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Slice_Privilege_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slice_privileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Privilege_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} slice - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Slice_Privilege_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slice_privileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Privilege_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} slice - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Slice_Privilege_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slice_privileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Privilege_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Slice_Privilege_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slice_privileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Flavor_List_GET
-             * 
-             */
-            xos.prototype.Flavor_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/flavors/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Flavor_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - name of this flavor, as displayed to users
-             * @param {string} description - 
-             * @param {string} flavor - flavor string used to configure deployments
-             * @param {integer} order - used to order flavors when displayed in a list
-             * @param {boolean} default - make this a default flavor to use when creating new instances
-             * 
-             */
-            xos.prototype.Flavor_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/flavors/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['flavor'] !== undefined) {
-                    form['flavor'] = parameters['flavor'];
-                }
-
-                if (parameters['flavor'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: flavor'));
-                    return deferred.promise;
-                }
-
-                if (parameters['order'] !== undefined) {
-                    form['order'] = parameters['order'];
-                }
-
-                if (parameters['order'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: order'));
-                    return deferred.promise;
-                }
-
-                if (parameters['default'] !== undefined) {
-                    form['default'] = parameters['default'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Flavor_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Flavor_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/flavors/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Flavor_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - name of this flavor, as displayed to users
-             * @param {string} description - 
-             * @param {string} flavor - flavor string used to configure deployments
-             * @param {integer} order - used to order flavors when displayed in a list
-             * @param {boolean} default - make this a default flavor to use when creating new instances
-             * 
-             */
-            xos.prototype.Flavor_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/flavors/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['flavor'] !== undefined) {
-                    form['flavor'] = parameters['flavor'];
-                }
-
-                if (parameters['flavor'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: flavor'));
-                    return deferred.promise;
-                }
-
-                if (parameters['order'] !== undefined) {
-                    form['order'] = parameters['order'];
-                }
-
-                if (parameters['order'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: order'));
-                    return deferred.promise;
-                }
-
-                if (parameters['default'] !== undefined) {
-                    form['default'] = parameters['default'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Flavor_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - name of this flavor, as displayed to users
-             * @param {string} description - 
-             * @param {string} flavor - flavor string used to configure deployments
-             * @param {integer} order - used to order flavors when displayed in a list
-             * @param {boolean} default - make this a default flavor to use when creating new instances
-             * 
-             */
-            xos.prototype.Flavor_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/flavors/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['flavor'] !== undefined) {
-                    form['flavor'] = parameters['flavor'];
-                }
-
-                if (parameters['order'] !== undefined) {
-                    form['order'] = parameters['order'];
-                }
-
-                if (parameters['default'] !== undefined) {
-                    form['default'] = parameters['default'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Flavor_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Flavor_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/flavors/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Port_List_GET
-             * 
-             */
-            xos.prototype.Port_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/ports/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Port_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} network - 
-             * @param {string} instance - 
-             * @param {string} ip - Instance ip address
-             * @param {string} port_id - Quantum port id
-             * @param {string} mac - MAC address associated with this port
-             * 
-             */
-            xos.prototype.Port_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/ports/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['network'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: network'));
-                    return deferred.promise;
-                }
-
-                if (parameters['instance'] !== undefined) {
-                    form['instance'] = parameters['instance'];
-                }
-
-                if (parameters['ip'] !== undefined) {
-                    form['ip'] = parameters['ip'];
-                }
-
-                if (parameters['port_id'] !== undefined) {
-                    form['port_id'] = parameters['port_id'];
-                }
-
-                if (parameters['mac'] !== undefined) {
-                    form['mac'] = parameters['mac'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Port_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Port_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/ports/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Port_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} network - 
-             * @param {string} instance - 
-             * @param {string} ip - Instance ip address
-             * @param {string} port_id - Quantum port id
-             * @param {string} mac - MAC address associated with this port
-             * 
-             */
-            xos.prototype.Port_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/ports/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['network'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: network'));
-                    return deferred.promise;
-                }
-
-                if (parameters['instance'] !== undefined) {
-                    form['instance'] = parameters['instance'];
-                }
-
-                if (parameters['ip'] !== undefined) {
-                    form['ip'] = parameters['ip'];
-                }
-
-                if (parameters['port_id'] !== undefined) {
-                    form['port_id'] = parameters['port_id'];
-                }
-
-                if (parameters['mac'] !== undefined) {
-                    form['mac'] = parameters['mac'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Port_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} network - 
-             * @param {string} instance - 
-             * @param {string} ip - Instance ip address
-             * @param {string} port_id - Quantum port id
-             * @param {string} mac - MAC address associated with this port
-             * 
-             */
-            xos.prototype.Port_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/ports/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['instance'] !== undefined) {
-                    form['instance'] = parameters['instance'];
-                }
-
-                if (parameters['ip'] !== undefined) {
-                    form['ip'] = parameters['ip'];
-                }
-
-                if (parameters['port_id'] !== undefined) {
-                    form['port_id'] = parameters['port_id'];
-                }
-
-                if (parameters['mac'] !== undefined) {
-                    form['mac'] = parameters['mac'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Port_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Port_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/ports/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Site_List_GET
-             * 
-             */
-            xos.prototype.Controller_Site_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersites/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Site_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} site - 
-             * @param {string} controller - 
-             * @param {string} tenant_id - Keystone tenant id
-             * 
-             */
-            xos.prototype.Controller_Site_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersites/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['tenant_id'] !== undefined) {
-                    form['tenant_id'] = parameters['tenant_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Site_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Site_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersites/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Site_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} site - 
-             * @param {string} controller - 
-             * @param {string} tenant_id - Keystone tenant id
-             * 
-             */
-            xos.prototype.Controller_Site_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersites/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['tenant_id'] !== undefined) {
-                    form['tenant_id'] = parameters['tenant_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Site_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} site - 
-             * @param {string} controller - 
-             * @param {string} tenant_id - Keystone tenant id
-             * 
-             */
-            xos.prototype.Controller_Site_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersites/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['tenant_id'] !== undefined) {
-                    form['tenant_id'] = parameters['tenant_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Site_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Site_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersites/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Project_List_GET
-             * 
-             */
-            xos.prototype.Project_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/projects/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Project_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of Project
-             * 
-             */
-            xos.prototype.Project_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/projects/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Project_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Project_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/projects/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Project_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of Project
-             * 
-             */
-            xos.prototype.Project_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/projects/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Project_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of Project
-             * 
-             */
-            xos.prototype.Project_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/projects/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Project_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Project_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/projects/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_List_GET
-             * 
-             */
-            xos.prototype.Slice_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slices/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - The Name of the Slice
-             * @param {boolean} enabled - Status for this Slice
-             * @param {boolean} omf_friendly - 
-             * @param {string} description - High level description of the slice and expected activities
-             * @param {string} slice_url - 
-             * @param {string} site - The Site this Slice belongs to
-             * @param {integer} max_instances - 
-             * @param {string} service - 
-             * @param {string} network - 
-             * @param {string} serviceClass - 
-             * @param {string} creator - 
-             * @param {string} default_flavor - 
-             * @param {string} default_image - 
-             * @param {string} mount_data_sets - 
-             * 
-             */
-            xos.prototype.Slice_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slices/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['omf_friendly'] !== undefined) {
-                    form['omf_friendly'] = parameters['omf_friendly'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['slice_url'] !== undefined) {
-                    form['slice_url'] = parameters['slice_url'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['max_instances'] !== undefined) {
-                    form['max_instances'] = parameters['max_instances'];
-                }
-
-                if (parameters['max_instances'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: max_instances'));
-                    return deferred.promise;
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['serviceClass'] !== undefined) {
-                    form['serviceClass'] = parameters['serviceClass'];
-                }
-
-                if (parameters['creator'] !== undefined) {
-                    form['creator'] = parameters['creator'];
-                }
-
-                if (parameters['default_flavor'] !== undefined) {
-                    form['default_flavor'] = parameters['default_flavor'];
-                }
-
-                if (parameters['default_image'] !== undefined) {
-                    form['default_image'] = parameters['default_image'];
-                }
-
-                if (parameters['mount_data_sets'] !== undefined) {
-                    form['mount_data_sets'] = parameters['mount_data_sets'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Slice_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - The Name of the Slice
-             * @param {boolean} enabled - Status for this Slice
-             * @param {boolean} omf_friendly - 
-             * @param {string} description - High level description of the slice and expected activities
-             * @param {string} slice_url - 
-             * @param {string} site - The Site this Slice belongs to
-             * @param {integer} max_instances - 
-             * @param {string} service - 
-             * @param {string} network - 
-             * @param {string} serviceClass - 
-             * @param {string} creator - 
-             * @param {string} default_flavor - 
-             * @param {string} default_image - 
-             * @param {string} mount_data_sets - 
-             * 
-             */
-            xos.prototype.Slice_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['omf_friendly'] !== undefined) {
-                    form['omf_friendly'] = parameters['omf_friendly'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['slice_url'] !== undefined) {
-                    form['slice_url'] = parameters['slice_url'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['max_instances'] !== undefined) {
-                    form['max_instances'] = parameters['max_instances'];
-                }
-
-                if (parameters['max_instances'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: max_instances'));
-                    return deferred.promise;
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['serviceClass'] !== undefined) {
-                    form['serviceClass'] = parameters['serviceClass'];
-                }
-
-                if (parameters['creator'] !== undefined) {
-                    form['creator'] = parameters['creator'];
-                }
-
-                if (parameters['default_flavor'] !== undefined) {
-                    form['default_flavor'] = parameters['default_flavor'];
-                }
-
-                if (parameters['default_image'] !== undefined) {
-                    form['default_image'] = parameters['default_image'];
-                }
-
-                if (parameters['mount_data_sets'] !== undefined) {
-                    form['mount_data_sets'] = parameters['mount_data_sets'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - The Name of the Slice
-             * @param {boolean} enabled - Status for this Slice
-             * @param {boolean} omf_friendly - 
-             * @param {string} description - High level description of the slice and expected activities
-             * @param {string} slice_url - 
-             * @param {string} site - The Site this Slice belongs to
-             * @param {integer} max_instances - 
-             * @param {string} service - 
-             * @param {string} network - 
-             * @param {string} serviceClass - 
-             * @param {string} creator - 
-             * @param {string} default_flavor - 
-             * @param {string} default_image - 
-             * @param {string} mount_data_sets - 
-             * 
-             */
-            xos.prototype.Slice_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['omf_friendly'] !== undefined) {
-                    form['omf_friendly'] = parameters['omf_friendly'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['slice_url'] !== undefined) {
-                    form['slice_url'] = parameters['slice_url'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['max_instances'] !== undefined) {
-                    form['max_instances'] = parameters['max_instances'];
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['serviceClass'] !== undefined) {
-                    form['serviceClass'] = parameters['serviceClass'];
-                }
-
-                if (parameters['creator'] !== undefined) {
-                    form['creator'] = parameters['creator'];
-                }
-
-                if (parameters['default_flavor'] !== undefined) {
-                    form['default_flavor'] = parameters['default_flavor'];
-                }
-
-                if (parameters['default_image'] !== undefined) {
-                    form['default_image'] = parameters['default_image'];
-                }
-
-                if (parameters['mount_data_sets'] !== undefined) {
-                    form['mount_data_sets'] = parameters['mount_data_sets'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Slice_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_List_GET
-             * 
-             */
-            xos.prototype.Network_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networks/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} template - 
-             * @param {string} subnet - 
-             * @param {string} ports - 
-             * @param {string} labels - 
-             * @param {string} owner - Slice that owns control of this Network
-             * @param {integer} guaranteed_bandwidth - 
-             * @param {boolean} permit_all_slices - 
-             * @param {string} topology_parameters - 
-             * @param {string} controller_url - 
-             * @param {string} controller_parameters - 
-             * @param {string} network_id - Quantum network
-             * @param {string} router_id - Quantum router id
-             * @param {string} subnet_id - Quantum subnet id
-             * @param {boolean} autoconnect - This network can be autoconnected to the slice that owns it
-             * 
-             */
-            xos.prototype.Network_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networks/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['template'] !== undefined) {
-                    form['template'] = parameters['template'];
-                }
-
-                if (parameters['template'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: template'));
-                    return deferred.promise;
-                }
-
-                if (parameters['subnet'] !== undefined) {
-                    form['subnet'] = parameters['subnet'];
-                }
-
-                if (parameters['ports'] !== undefined) {
-                    form['ports'] = parameters['ports'];
-                }
-
-                if (parameters['labels'] !== undefined) {
-                    form['labels'] = parameters['labels'];
-                }
-
-                if (parameters['owner'] !== undefined) {
-                    form['owner'] = parameters['owner'];
-                }
-
-                if (parameters['owner'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: owner'));
-                    return deferred.promise;
-                }
-
-                if (parameters['guaranteed_bandwidth'] !== undefined) {
-                    form['guaranteed_bandwidth'] = parameters['guaranteed_bandwidth'];
-                }
-
-                if (parameters['guaranteed_bandwidth'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: guaranteed_bandwidth'));
-                    return deferred.promise;
-                }
-
-                if (parameters['permit_all_slices'] !== undefined) {
-                    form['permit_all_slices'] = parameters['permit_all_slices'];
-                }
-
-                if (parameters['topology_parameters'] !== undefined) {
-                    form['topology_parameters'] = parameters['topology_parameters'];
-                }
-
-                if (parameters['controller_url'] !== undefined) {
-                    form['controller_url'] = parameters['controller_url'];
-                }
-
-                if (parameters['controller_parameters'] !== undefined) {
-                    form['controller_parameters'] = parameters['controller_parameters'];
-                }
-
-                if (parameters['network_id'] !== undefined) {
-                    form['network_id'] = parameters['network_id'];
-                }
-
-                if (parameters['router_id'] !== undefined) {
-                    form['router_id'] = parameters['router_id'];
-                }
-
-                if (parameters['subnet_id'] !== undefined) {
-                    form['subnet_id'] = parameters['subnet_id'];
-                }
-
-                if (parameters['autoconnect'] !== undefined) {
-                    form['autoconnect'] = parameters['autoconnect'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Network_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networks/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} template - 
-             * @param {string} subnet - 
-             * @param {string} ports - 
-             * @param {string} labels - 
-             * @param {string} owner - Slice that owns control of this Network
-             * @param {integer} guaranteed_bandwidth - 
-             * @param {boolean} permit_all_slices - 
-             * @param {string} topology_parameters - 
-             * @param {string} controller_url - 
-             * @param {string} controller_parameters - 
-             * @param {string} network_id - Quantum network
-             * @param {string} router_id - Quantum router id
-             * @param {string} subnet_id - Quantum subnet id
-             * @param {boolean} autoconnect - This network can be autoconnected to the slice that owns it
-             * 
-             */
-            xos.prototype.Network_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networks/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['template'] !== undefined) {
-                    form['template'] = parameters['template'];
-                }
-
-                if (parameters['template'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: template'));
-                    return deferred.promise;
-                }
-
-                if (parameters['subnet'] !== undefined) {
-                    form['subnet'] = parameters['subnet'];
-                }
-
-                if (parameters['ports'] !== undefined) {
-                    form['ports'] = parameters['ports'];
-                }
-
-                if (parameters['labels'] !== undefined) {
-                    form['labels'] = parameters['labels'];
-                }
-
-                if (parameters['owner'] !== undefined) {
-                    form['owner'] = parameters['owner'];
-                }
-
-                if (parameters['owner'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: owner'));
-                    return deferred.promise;
-                }
-
-                if (parameters['guaranteed_bandwidth'] !== undefined) {
-                    form['guaranteed_bandwidth'] = parameters['guaranteed_bandwidth'];
-                }
-
-                if (parameters['guaranteed_bandwidth'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: guaranteed_bandwidth'));
-                    return deferred.promise;
-                }
-
-                if (parameters['permit_all_slices'] !== undefined) {
-                    form['permit_all_slices'] = parameters['permit_all_slices'];
-                }
-
-                if (parameters['topology_parameters'] !== undefined) {
-                    form['topology_parameters'] = parameters['topology_parameters'];
-                }
-
-                if (parameters['controller_url'] !== undefined) {
-                    form['controller_url'] = parameters['controller_url'];
-                }
-
-                if (parameters['controller_parameters'] !== undefined) {
-                    form['controller_parameters'] = parameters['controller_parameters'];
-                }
-
-                if (parameters['network_id'] !== undefined) {
-                    form['network_id'] = parameters['network_id'];
-                }
-
-                if (parameters['router_id'] !== undefined) {
-                    form['router_id'] = parameters['router_id'];
-                }
-
-                if (parameters['subnet_id'] !== undefined) {
-                    form['subnet_id'] = parameters['subnet_id'];
-                }
-
-                if (parameters['autoconnect'] !== undefined) {
-                    form['autoconnect'] = parameters['autoconnect'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} template - 
-             * @param {string} subnet - 
-             * @param {string} ports - 
-             * @param {string} labels - 
-             * @param {string} owner - Slice that owns control of this Network
-             * @param {integer} guaranteed_bandwidth - 
-             * @param {boolean} permit_all_slices - 
-             * @param {string} topology_parameters - 
-             * @param {string} controller_url - 
-             * @param {string} controller_parameters - 
-             * @param {string} network_id - Quantum network
-             * @param {string} router_id - Quantum router id
-             * @param {string} subnet_id - Quantum subnet id
-             * @param {boolean} autoconnect - This network can be autoconnected to the slice that owns it
-             * 
-             */
-            xos.prototype.Network_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networks/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['template'] !== undefined) {
-                    form['template'] = parameters['template'];
-                }
-
-                if (parameters['subnet'] !== undefined) {
-                    form['subnet'] = parameters['subnet'];
-                }
-
-                if (parameters['ports'] !== undefined) {
-                    form['ports'] = parameters['ports'];
-                }
-
-                if (parameters['labels'] !== undefined) {
-                    form['labels'] = parameters['labels'];
-                }
-
-                if (parameters['owner'] !== undefined) {
-                    form['owner'] = parameters['owner'];
-                }
-
-                if (parameters['guaranteed_bandwidth'] !== undefined) {
-                    form['guaranteed_bandwidth'] = parameters['guaranteed_bandwidth'];
-                }
-
-                if (parameters['permit_all_slices'] !== undefined) {
-                    form['permit_all_slices'] = parameters['permit_all_slices'];
-                }
-
-                if (parameters['topology_parameters'] !== undefined) {
-                    form['topology_parameters'] = parameters['topology_parameters'];
-                }
-
-                if (parameters['controller_url'] !== undefined) {
-                    form['controller_url'] = parameters['controller_url'];
-                }
-
-                if (parameters['controller_parameters'] !== undefined) {
-                    form['controller_parameters'] = parameters['controller_parameters'];
-                }
-
-                if (parameters['network_id'] !== undefined) {
-                    form['network_id'] = parameters['network_id'];
-                }
-
-                if (parameters['router_id'] !== undefined) {
-                    form['router_id'] = parameters['router_id'];
-                }
-
-                if (parameters['subnet_id'] !== undefined) {
-                    form['subnet_id'] = parameters['subnet_id'];
-                }
-
-                if (parameters['autoconnect'] !== undefined) {
-                    form['autoconnect'] = parameters['autoconnect'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Network_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networks/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_List_GET
-             * 
-             */
-            xos.prototype.Service_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/services/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} description - Description of Service
-             * @param {boolean} enabled - 
-             * @param {string} kind - Kind of service
-             * @param {string} name - Service Name
-             * @param {string} versionNumber - Version of Service Definition
-             * @param {boolean} published - 
-             * @param {string} view_url - 
-             * @param {string} icon_url - 
-             * @param {string} public_key - Public key string
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * 
-             */
-            xos.prototype.Service_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/services/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['versionNumber'] !== undefined) {
-                    form['versionNumber'] = parameters['versionNumber'];
-                }
-
-                if (parameters['versionNumber'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: versionNumber'));
-                    return deferred.promise;
-                }
-
-                if (parameters['published'] !== undefined) {
-                    form['published'] = parameters['published'];
-                }
-
-                if (parameters['view_url'] !== undefined) {
-                    form['view_url'] = parameters['view_url'];
-                }
-
-                if (parameters['icon_url'] !== undefined) {
-                    form['icon_url'] = parameters['icon_url'];
-                }
-
-                if (parameters['public_key'] !== undefined) {
-                    form['public_key'] = parameters['public_key'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Service_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/services/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} description - Description of Service
-             * @param {boolean} enabled - 
-             * @param {string} kind - Kind of service
-             * @param {string} name - Service Name
-             * @param {string} versionNumber - Version of Service Definition
-             * @param {boolean} published - 
-             * @param {string} view_url - 
-             * @param {string} icon_url - 
-             * @param {string} public_key - Public key string
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * 
-             */
-            xos.prototype.Service_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/services/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['versionNumber'] !== undefined) {
-                    form['versionNumber'] = parameters['versionNumber'];
-                }
-
-                if (parameters['versionNumber'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: versionNumber'));
-                    return deferred.promise;
-                }
-
-                if (parameters['published'] !== undefined) {
-                    form['published'] = parameters['published'];
-                }
-
-                if (parameters['view_url'] !== undefined) {
-                    form['view_url'] = parameters['view_url'];
-                }
-
-                if (parameters['icon_url'] !== undefined) {
-                    form['icon_url'] = parameters['icon_url'];
-                }
-
-                if (parameters['public_key'] !== undefined) {
-                    form['public_key'] = parameters['public_key'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} description - Description of Service
-             * @param {boolean} enabled - 
-             * @param {string} kind - Kind of service
-             * @param {string} name - Service Name
-             * @param {string} versionNumber - Version of Service Definition
-             * @param {boolean} published - 
-             * @param {string} view_url - 
-             * @param {string} icon_url - 
-             * @param {string} public_key - Public key string
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * 
-             */
-            xos.prototype.Service_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/services/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['versionNumber'] !== undefined) {
-                    form['versionNumber'] = parameters['versionNumber'];
-                }
-
-                if (parameters['published'] !== undefined) {
-                    form['published'] = parameters['published'];
-                }
-
-                if (parameters['view_url'] !== undefined) {
-                    form['view_url'] = parameters['view_url'];
-                }
-
-                if (parameters['icon_url'] !== undefined) {
-                    form['icon_url'] = parameters['icon_url'];
-                }
-
-                if (parameters['public_key'] !== undefined) {
-                    form['public_key'] = parameters['public_key'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Service_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/services/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Class_List_GET
-             * 
-             */
-            xos.prototype.Service_Class_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceclasses/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Class_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} description - 
-             * @param {integer} commitment - 
-             * @param {integer} membershipFee - 
-             * @param {integer} membershipFeeMonths - 
-             * @param {boolean} upgradeRequiresApproval - 
-             * 
-             */
-            xos.prototype.Service_Class_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceclasses/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['description'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: description'));
-                    return deferred.promise;
-                }
-
-                if (parameters['commitment'] !== undefined) {
-                    form['commitment'] = parameters['commitment'];
-                }
-
-                if (parameters['commitment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: commitment'));
-                    return deferred.promise;
-                }
-
-                if (parameters['membershipFee'] !== undefined) {
-                    form['membershipFee'] = parameters['membershipFee'];
-                }
-
-                if (parameters['membershipFee'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: membershipFee'));
-                    return deferred.promise;
-                }
-
-                if (parameters['membershipFeeMonths'] !== undefined) {
-                    form['membershipFeeMonths'] = parameters['membershipFeeMonths'];
-                }
-
-                if (parameters['membershipFeeMonths'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: membershipFeeMonths'));
-                    return deferred.promise;
-                }
-
-                if (parameters['upgradeRequiresApproval'] !== undefined) {
-                    form['upgradeRequiresApproval'] = parameters['upgradeRequiresApproval'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Class_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Service_Class_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceclasses/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Class_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} description - 
-             * @param {integer} commitment - 
-             * @param {integer} membershipFee - 
-             * @param {integer} membershipFeeMonths - 
-             * @param {boolean} upgradeRequiresApproval - 
-             * 
-             */
-            xos.prototype.Service_Class_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceclasses/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['description'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: description'));
-                    return deferred.promise;
-                }
-
-                if (parameters['commitment'] !== undefined) {
-                    form['commitment'] = parameters['commitment'];
-                }
-
-                if (parameters['commitment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: commitment'));
-                    return deferred.promise;
-                }
-
-                if (parameters['membershipFee'] !== undefined) {
-                    form['membershipFee'] = parameters['membershipFee'];
-                }
-
-                if (parameters['membershipFee'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: membershipFee'));
-                    return deferred.promise;
-                }
-
-                if (parameters['membershipFeeMonths'] !== undefined) {
-                    form['membershipFeeMonths'] = parameters['membershipFeeMonths'];
-                }
-
-                if (parameters['membershipFeeMonths'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: membershipFeeMonths'));
-                    return deferred.promise;
-                }
-
-                if (parameters['upgradeRequiresApproval'] !== undefined) {
-                    form['upgradeRequiresApproval'] = parameters['upgradeRequiresApproval'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Class_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} description - 
-             * @param {integer} commitment - 
-             * @param {integer} membershipFee - 
-             * @param {integer} membershipFeeMonths - 
-             * @param {boolean} upgradeRequiresApproval - 
-             * 
-             */
-            xos.prototype.Service_Class_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceclasses/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['commitment'] !== undefined) {
-                    form['commitment'] = parameters['commitment'];
-                }
-
-                if (parameters['membershipFee'] !== undefined) {
-                    form['membershipFee'] = parameters['membershipFee'];
-                }
-
-                if (parameters['membershipFeeMonths'] !== undefined) {
-                    form['membershipFeeMonths'] = parameters['membershipFeeMonths'];
-                }
-
-                if (parameters['upgradeRequiresApproval'] !== undefined) {
-                    form['upgradeRequiresApproval'] = parameters['upgradeRequiresApproval'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Class_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Service_Class_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceclasses/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Payment_List_GET
-             * 
-             */
-            xos.prototype.Payment_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/payments/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Payment_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} account - 
-             * @param {number} amount - 
-             * @param {string} date - 
-             * 
-             */
-            xos.prototype.Payment_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/payments/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['account'] !== undefined) {
-                    form['account'] = parameters['account'];
-                }
-
-                if (parameters['account'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: account'));
-                    return deferred.promise;
-                }
-
-                if (parameters['amount'] !== undefined) {
-                    form['amount'] = parameters['amount'];
-                }
-
-                if (parameters['amount'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: amount'));
-                    return deferred.promise;
-                }
-
-                if (parameters['date'] !== undefined) {
-                    form['date'] = parameters['date'];
-                }
-
-                if (parameters['date'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: date'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Payment_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Payment_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/payments/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Payment_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} account - 
-             * @param {number} amount - 
-             * @param {string} date - 
-             * 
-             */
-            xos.prototype.Payment_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/payments/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['account'] !== undefined) {
-                    form['account'] = parameters['account'];
-                }
-
-                if (parameters['account'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: account'));
-                    return deferred.promise;
-                }
-
-                if (parameters['amount'] !== undefined) {
-                    form['amount'] = parameters['amount'];
-                }
-
-                if (parameters['amount'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: amount'));
-                    return deferred.promise;
-                }
-
-                if (parameters['date'] !== undefined) {
-                    form['date'] = parameters['date'];
-                }
-
-                if (parameters['date'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: date'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Payment_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} account - 
-             * @param {number} amount - 
-             * @param {string} date - 
-             * 
-             */
-            xos.prototype.Payment_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/payments/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['account'] !== undefined) {
-                    form['account'] = parameters['account'];
-                }
-
-                if (parameters['amount'] !== undefined) {
-                    form['amount'] = parameters['amount'];
-                }
-
-                if (parameters['date'] !== undefined) {
-                    form['date'] = parameters['date'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Payment_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Payment_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/payments/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Subscriber_List_GET
-             * 
-             */
-            xos.prototype.Subscriber_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/subscribers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Subscriber_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} name - name
-             * @param {string} service_specific_attribute - 
-             * @param {string} service_specific_id - 
-             * 
-             */
-            xos.prototype.Subscriber_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/subscribers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Subscriber_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Subscriber_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/subscribers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Subscriber_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} name - name
-             * @param {string} service_specific_attribute - 
-             * @param {string} service_specific_id - 
-             * 
-             */
-            xos.prototype.Subscriber_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/subscribers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Subscriber_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} name - name
-             * @param {string} service_specific_attribute - 
-             * @param {string} service_specific_id - 
-             * 
-             */
-            xos.prototype.Subscriber_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/subscribers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Subscriber_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Subscriber_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/subscribers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Instance_List_GET
-             * 
-             */
-            xos.prototype.Instance_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/instances/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Instance_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} instance_id - Nova instance id
-             * @param {string} instance_uuid - Nova instance uuid
-             * @param {string} name - Instance name
-             * @param {string} instance_name - OpenStack generated name
-             * @param {string} ip - Instance ip address
-             * @param {string} image - 
-             * @param {string} creator - 
-             * @param {string} slice - 
-             * @param {string} deployment - 
-             * @param {string} node - 
-             * @param {integer} numberCores - Number of cores for instance
-             * @param {string} flavor - Flavor of this instance
-             * @param {string} userData - user_data passed to instance during creation
-             * 
-             */
-            xos.prototype.Instance_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/instances/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['instance_id'] !== undefined) {
-                    form['instance_id'] = parameters['instance_id'];
-                }
-
-                if (parameters['instance_uuid'] !== undefined) {
-                    form['instance_uuid'] = parameters['instance_uuid'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['instance_name'] !== undefined) {
-                    form['instance_name'] = parameters['instance_name'];
-                }
-
-                if (parameters['ip'] !== undefined) {
-                    form['ip'] = parameters['ip'];
-                }
-
-                if (parameters['image'] !== undefined) {
-                    form['image'] = parameters['image'];
-                }
-
-                if (parameters['image'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: image'));
-                    return deferred.promise;
-                }
-
-                if (parameters['creator'] !== undefined) {
-                    form['creator'] = parameters['creator'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['deployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: deployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters['node'] !== undefined) {
-                    form['node'] = parameters['node'];
-                }
-
-                if (parameters['node'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: node'));
-                    return deferred.promise;
-                }
-
-                if (parameters['numberCores'] !== undefined) {
-                    form['numberCores'] = parameters['numberCores'];
-                }
-
-                if (parameters['numberCores'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: numberCores'));
-                    return deferred.promise;
-                }
-
-                if (parameters['flavor'] !== undefined) {
-                    form['flavor'] = parameters['flavor'];
-                }
-
-                if (parameters['flavor'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: flavor'));
-                    return deferred.promise;
-                }
-
-                if (parameters['userData'] !== undefined) {
-                    form['userData'] = parameters['userData'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Instance_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Instance_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/instances/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Instance_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} instance_id - Nova instance id
-             * @param {string} instance_uuid - Nova instance uuid
-             * @param {string} name - Instance name
-             * @param {string} instance_name - OpenStack generated name
-             * @param {string} ip - Instance ip address
-             * @param {string} image - 
-             * @param {string} creator - 
-             * @param {string} slice - 
-             * @param {string} deployment - 
-             * @param {string} node - 
-             * @param {integer} numberCores - Number of cores for instance
-             * @param {string} flavor - Flavor of this instance
-             * @param {string} userData - user_data passed to instance during creation
-             * 
-             */
-            xos.prototype.Instance_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/instances/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['instance_id'] !== undefined) {
-                    form['instance_id'] = parameters['instance_id'];
-                }
-
-                if (parameters['instance_uuid'] !== undefined) {
-                    form['instance_uuid'] = parameters['instance_uuid'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['instance_name'] !== undefined) {
-                    form['instance_name'] = parameters['instance_name'];
-                }
-
-                if (parameters['ip'] !== undefined) {
-                    form['ip'] = parameters['ip'];
-                }
-
-                if (parameters['image'] !== undefined) {
-                    form['image'] = parameters['image'];
-                }
-
-                if (parameters['image'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: image'));
-                    return deferred.promise;
-                }
-
-                if (parameters['creator'] !== undefined) {
-                    form['creator'] = parameters['creator'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['deployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: deployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters['node'] !== undefined) {
-                    form['node'] = parameters['node'];
-                }
-
-                if (parameters['node'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: node'));
-                    return deferred.promise;
-                }
-
-                if (parameters['numberCores'] !== undefined) {
-                    form['numberCores'] = parameters['numberCores'];
-                }
-
-                if (parameters['numberCores'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: numberCores'));
-                    return deferred.promise;
-                }
-
-                if (parameters['flavor'] !== undefined) {
-                    form['flavor'] = parameters['flavor'];
-                }
-
-                if (parameters['flavor'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: flavor'));
-                    return deferred.promise;
-                }
-
-                if (parameters['userData'] !== undefined) {
-                    form['userData'] = parameters['userData'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Instance_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} instance_id - Nova instance id
-             * @param {string} instance_uuid - Nova instance uuid
-             * @param {string} name - Instance name
-             * @param {string} instance_name - OpenStack generated name
-             * @param {string} ip - Instance ip address
-             * @param {string} image - 
-             * @param {string} creator - 
-             * @param {string} slice - 
-             * @param {string} deployment - 
-             * @param {string} node - 
-             * @param {integer} numberCores - Number of cores for instance
-             * @param {string} flavor - Flavor of this instance
-             * @param {string} userData - user_data passed to instance during creation
-             * 
-             */
-            xos.prototype.Instance_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/instances/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['instance_id'] !== undefined) {
-                    form['instance_id'] = parameters['instance_id'];
-                }
-
-                if (parameters['instance_uuid'] !== undefined) {
-                    form['instance_uuid'] = parameters['instance_uuid'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['instance_name'] !== undefined) {
-                    form['instance_name'] = parameters['instance_name'];
-                }
-
-                if (parameters['ip'] !== undefined) {
-                    form['ip'] = parameters['ip'];
-                }
-
-                if (parameters['image'] !== undefined) {
-                    form['image'] = parameters['image'];
-                }
-
-                if (parameters['creator'] !== undefined) {
-                    form['creator'] = parameters['creator'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['node'] !== undefined) {
-                    form['node'] = parameters['node'];
-                }
-
-                if (parameters['numberCores'] !== undefined) {
-                    form['numberCores'] = parameters['numberCores'];
-                }
-
-                if (parameters['flavor'] !== undefined) {
-                    form['flavor'] = parameters['flavor'];
-                }
-
-                if (parameters['userData'] !== undefined) {
-                    form['userData'] = parameters['userData'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Instance_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Instance_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/instances/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Charge_List_GET
-             * 
-             */
-            xos.prototype.Charge_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/charges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Charge_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} account - 
-             * @param {string} slice - 
-             * @param {choice} kind - 
-             * @param {choice} state - 
-             * @param {string} date - 
-             * @param {string} object - 
-             * @param {number} amount - 
-             * @param {number} coreHours - 
-             * @param {string} invoice - 
-             * 
-             */
-            xos.prototype.Charge_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/charges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['account'] !== undefined) {
-                    form['account'] = parameters['account'];
-                }
-
-                if (parameters['account'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: account'));
-                    return deferred.promise;
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['state'] !== undefined) {
-                    form['state'] = parameters['state'];
-                }
-
-                if (parameters['state'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: state'));
-                    return deferred.promise;
-                }
-
-                if (parameters['date'] !== undefined) {
-                    form['date'] = parameters['date'];
-                }
-
-                if (parameters['date'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: date'));
-                    return deferred.promise;
-                }
-
-                if (parameters['object'] !== undefined) {
-                    form['object'] = parameters['object'];
-                }
-
-                if (parameters['object'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: object'));
-                    return deferred.promise;
-                }
-
-                if (parameters['amount'] !== undefined) {
-                    form['amount'] = parameters['amount'];
-                }
-
-                if (parameters['amount'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: amount'));
-                    return deferred.promise;
-                }
-
-                if (parameters['coreHours'] !== undefined) {
-                    form['coreHours'] = parameters['coreHours'];
-                }
-
-                if (parameters['coreHours'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: coreHours'));
-                    return deferred.promise;
-                }
-
-                if (parameters['invoice'] !== undefined) {
-                    form['invoice'] = parameters['invoice'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Charge_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Charge_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/charges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Charge_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} account - 
-             * @param {string} slice - 
-             * @param {choice} kind - 
-             * @param {choice} state - 
-             * @param {string} date - 
-             * @param {string} object - 
-             * @param {number} amount - 
-             * @param {number} coreHours - 
-             * @param {string} invoice - 
-             * 
-             */
-            xos.prototype.Charge_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/charges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['account'] !== undefined) {
-                    form['account'] = parameters['account'];
-                }
-
-                if (parameters['account'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: account'));
-                    return deferred.promise;
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['state'] !== undefined) {
-                    form['state'] = parameters['state'];
-                }
-
-                if (parameters['state'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: state'));
-                    return deferred.promise;
-                }
-
-                if (parameters['date'] !== undefined) {
-                    form['date'] = parameters['date'];
-                }
-
-                if (parameters['date'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: date'));
-                    return deferred.promise;
-                }
-
-                if (parameters['object'] !== undefined) {
-                    form['object'] = parameters['object'];
-                }
-
-                if (parameters['object'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: object'));
-                    return deferred.promise;
-                }
-
-                if (parameters['amount'] !== undefined) {
-                    form['amount'] = parameters['amount'];
-                }
-
-                if (parameters['amount'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: amount'));
-                    return deferred.promise;
-                }
-
-                if (parameters['coreHours'] !== undefined) {
-                    form['coreHours'] = parameters['coreHours'];
-                }
-
-                if (parameters['coreHours'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: coreHours'));
-                    return deferred.promise;
-                }
-
-                if (parameters['invoice'] !== undefined) {
-                    form['invoice'] = parameters['invoice'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Charge_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} account - 
-             * @param {string} slice - 
-             * @param {choice} kind - 
-             * @param {choice} state - 
-             * @param {string} date - 
-             * @param {string} object - 
-             * @param {number} amount - 
-             * @param {number} coreHours - 
-             * @param {string} invoice - 
-             * 
-             */
-            xos.prototype.Charge_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/charges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['account'] !== undefined) {
-                    form['account'] = parameters['account'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['state'] !== undefined) {
-                    form['state'] = parameters['state'];
-                }
-
-                if (parameters['date'] !== undefined) {
-                    form['date'] = parameters['date'];
-                }
-
-                if (parameters['object'] !== undefined) {
-                    form['object'] = parameters['object'];
-                }
-
-                if (parameters['amount'] !== undefined) {
-                    form['amount'] = parameters['amount'];
-                }
-
-                if (parameters['coreHours'] !== undefined) {
-                    form['coreHours'] = parameters['coreHours'];
-                }
-
-                if (parameters['invoice'] !== undefined) {
-                    form['invoice'] = parameters['invoice'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Charge_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Charge_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/charges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Program_List_GET
-             * 
-             */
-            xos.prototype.Program_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/programs/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Program_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Service Name
-             * @param {string} description - Description of Service
-
-             * @param {choice} command - Command to run
-             * @param {string} owner - 
-             * @param {string} contents - Contents of Program
-             * @param {string} output - Output of Program
-             * @param {string} messages - Debug messages
-             * @param {string} status - Status of program
-             * 
-             */
-            xos.prototype.Program_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/programs/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                form['kind'] = 'tosca';
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['command'] !== undefined) {
-                    form['command'] = parameters['command'];
-                }
-
-                if (parameters['owner'] !== undefined) {
-                    form['owner'] = parameters['owner'];
-                }
-
-                if (parameters['contents'] !== undefined) {
-                    form['contents'] = parameters['contents'];
-                }
-
-                if (parameters['output'] !== undefined) {
-                    form['output'] = parameters['output'];
-                }
-
-                if (parameters['messages'] !== undefined) {
-                    form['messages'] = parameters['messages'];
-                }
-
-                if (parameters['status'] !== undefined) {
-                    form['status'] = parameters['status'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Program_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Program_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/programs/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Program_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Service Name
-             * @param {string} description - Description of Service
-
-             * @param {choice} command - Command to run
-             * @param {string} owner - 
-             * @param {string} contents - Contents of Program
-             * @param {string} output - Output of Program
-             * @param {string} messages - Debug messages
-             * @param {string} status - Status of program
-             * 
-             */
-            xos.prototype.Program_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/programs/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                form['kind'] = 'tosca';
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['command'] !== undefined) {
-                    form['command'] = parameters['command'];
-                }
-
-                if (parameters['owner'] !== undefined) {
-                    form['owner'] = parameters['owner'];
-                }
-
-                if (parameters['contents'] !== undefined) {
-                    form['contents'] = parameters['contents'];
-                }
-
-                if (parameters['output'] !== undefined) {
-                    form['output'] = parameters['output'];
-                }
-
-                if (parameters['messages'] !== undefined) {
-                    form['messages'] = parameters['messages'];
-                }
-
-                if (parameters['status'] !== undefined) {
-                    form['status'] = parameters['status'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Program_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Service Name
-             * @param {string} description - Description of Service
-
-             * @param {choice} command - Command to run
-             * @param {string} owner - 
-             * @param {string} contents - Contents of Program
-             * @param {string} output - Output of Program
-             * @param {string} messages - Debug messages
-             * @param {string} status - Status of program
-             * 
-             */
-            xos.prototype.Program_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/programs/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                form['kind'] = 'tosca';
-
-                if (parameters['command'] !== undefined) {
-                    form['command'] = parameters['command'];
-                }
-
-                if (parameters['owner'] !== undefined) {
-                    form['owner'] = parameters['owner'];
-                }
-
-                if (parameters['contents'] !== undefined) {
-                    form['contents'] = parameters['contents'];
-                }
-
-                if (parameters['output'] !== undefined) {
-                    form['output'] = parameters['output'];
-                }
-
-                if (parameters['messages'] !== undefined) {
-                    form['messages'] = parameters['messages'];
-                }
-
-                if (parameters['status'] !== undefined) {
-                    form['status'] = parameters['status'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Program_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Program_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/programs/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Role_List_GET
-             * 
-             */
-            xos.prototype.Role_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/roles/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Role_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} role_type - 
-             * @param {string} role - 
-             * @param {string} description - 
-             * @param {string} content_type - 
-             * 
-             */
-            xos.prototype.Role_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/roles/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['role_type'] !== undefined) {
-                    form['role_type'] = parameters['role_type'];
-                }
-
-                if (parameters['role_type'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role_type'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['description'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: description'));
-                    return deferred.promise;
-                }
-
-                if (parameters['content_type'] !== undefined) {
-                    form['content_type'] = parameters['content_type'];
-                }
-
-                if (parameters['content_type'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: content_type'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Role_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Role_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/roles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Role_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} role_type - 
-             * @param {string} role - 
-             * @param {string} description - 
-             * @param {string} content_type - 
-             * 
-             */
-            xos.prototype.Role_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/roles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['role_type'] !== undefined) {
-                    form['role_type'] = parameters['role_type'];
-                }
-
-                if (parameters['role_type'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role_type'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['description'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: description'));
-                    return deferred.promise;
-                }
-
-                if (parameters['content_type'] !== undefined) {
-                    form['content_type'] = parameters['content_type'];
-                }
-
-                if (parameters['content_type'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: content_type'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Role_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} role_type - 
-             * @param {string} role - 
-             * @param {string} description - 
-             * @param {string} content_type - 
-             * 
-             */
-            xos.prototype.Role_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/roles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['role_type'] !== undefined) {
-                    form['role_type'] = parameters['role_type'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['content_type'] !== undefined) {
-                    form['content_type'] = parameters['content_type'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Role_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Role_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/roles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Usable_Object_List_GET
-             * 
-             */
-            xos.prototype.Usable_Object_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/usableobjects/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Usable_Object_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * 
-             */
-            xos.prototype.Usable_Object_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/usableobjects/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Usable_Object_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Usable_Object_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/usableobjects/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Usable_Object_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * 
-             */
-            xos.prototype.Usable_Object_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/usableobjects/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Usable_Object_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * 
-             */
-            xos.prototype.Usable_Object_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/usableobjects/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Usable_Object_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Usable_Object_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/usableobjects/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Provider_List_GET
-             * 
-             */
-            xos.prototype.Provider_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/providers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Provider_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} name - name
-             * @param {string} service_specific_attribute - 
-             * @param {string} service_specific_id - 
-             * 
-             */
-            xos.prototype.Provider_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/providers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Provider_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Provider_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/providers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Provider_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} name - name
-             * @param {string} service_specific_attribute - 
-             * @param {string} service_specific_id - 
-             * 
-             */
-            xos.prototype.Provider_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/providers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Provider_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} name - name
-             * @param {string} service_specific_attribute - 
-             * @param {string} service_specific_id - 
-             * 
-             */
-            xos.prototype.Provider_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/providers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Provider_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Provider_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/providers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Credential_List_GET
-             * 
-             */
-            xos.prototype.Slice_Credential_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slicecredentials/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Credential_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} slice - The User this credential is associated with
-             * @param {string} name - The credential type, e.g. ec2
-             * @param {string} key_id - The backend id of this credential
-             * @param {string} enc_value - The key value of this credential
-             * 
-             */
-            xos.prototype.Slice_Credential_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slicecredentials/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['key_id'] !== undefined) {
-                    form['key_id'] = parameters['key_id'];
-                }
-
-                if (parameters['key_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: key_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enc_value'] !== undefined) {
-                    form['enc_value'] = parameters['enc_value'];
-                }
-
-                if (parameters['enc_value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: enc_value'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Credential_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Slice_Credential_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slicecredentials/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Credential_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} slice - The User this credential is associated with
-             * @param {string} name - The credential type, e.g. ec2
-             * @param {string} key_id - The backend id of this credential
-             * @param {string} enc_value - The key value of this credential
-             * 
-             */
-            xos.prototype.Slice_Credential_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slicecredentials/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['key_id'] !== undefined) {
-                    form['key_id'] = parameters['key_id'];
-                }
-
-                if (parameters['key_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: key_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enc_value'] !== undefined) {
-                    form['enc_value'] = parameters['enc_value'];
-                }
-
-                if (parameters['enc_value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: enc_value'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Credential_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} slice - The User this credential is associated with
-             * @param {string} name - The credential type, e.g. ec2
-             * @param {string} key_id - The backend id of this credential
-             * @param {string} enc_value - The key value of this credential
-             * 
-             */
-            xos.prototype.Slice_Credential_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slicecredentials/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['key_id'] !== undefined) {
-                    form['key_id'] = parameters['key_id'];
-                }
-
-                if (parameters['enc_value'] !== undefined) {
-                    form['enc_value'] = parameters['enc_value'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Credential_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Slice_Credential_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slicecredentials/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Node_List_GET
-             * 
-             */
-            xos.prototype.Node_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/nodes/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Node_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of the Node
-             * @param {string} site_deployment - 
-             * @param {string} site - 
-             * 
-             */
-            xos.prototype.Node_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/nodes/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['site_deployment'] !== undefined) {
-                    form['site_deployment'] = parameters['site_deployment'];
-                }
-
-                if (parameters['site_deployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site_deployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Node_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Node_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/nodes/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Node_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of the Node
-             * @param {string} site_deployment - 
-             * @param {string} site - 
-             * 
-             */
-            xos.prototype.Node_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/nodes/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['site_deployment'] !== undefined) {
-                    form['site_deployment'] = parameters['site_deployment'];
-                }
-
-                if (parameters['site_deployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site_deployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Node_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of the Node
-             * @param {string} site_deployment - 
-             * @param {string} site - 
-             * 
-             */
-            xos.prototype.Node_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/nodes/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['site_deployment'] !== undefined) {
-                    form['site_deployment'] = parameters['site_deployment'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Node_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Node_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/nodes/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Dashboard_View_List_GET
-             * 
-             */
-            xos.prototype.Dashboard_View_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/dashboardviews/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Dashboard_View_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of the View
-             * @param {string} url - URL of Dashboard
-             * @param {boolean} enabled - 
-             * 
-             */
-            xos.prototype.Dashboard_View_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/dashboardviews/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url'] !== undefined) {
-                    form['url'] = parameters['url'];
-                }
-
-                if (parameters['url'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Dashboard_View_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Dashboard_View_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/dashboardviews/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Dashboard_View_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of the View
-             * @param {string} url - URL of Dashboard
-             * @param {boolean} enabled - 
-             * 
-             */
-            xos.prototype.Dashboard_View_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/dashboardviews/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url'] !== undefined) {
-                    form['url'] = parameters['url'];
-                }
-
-                if (parameters['url'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Dashboard_View_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of the View
-             * @param {string} url - URL of Dashboard
-             * @param {boolean} enabled - 
-             * 
-             */
-            xos.prototype.Dashboard_View_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/dashboardviews/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['url'] !== undefined) {
-                    form['url'] = parameters['url'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Dashboard_View_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Dashboard_View_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/dashboardviews/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Network_List_GET
-             * 
-             */
-            xos.prototype.Controller_Network_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllernetworks/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Network_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} network - 
-             * @param {string} controller - 
-             * @param {string} net_id - Quantum network
-             * @param {string} router_id - Quantum router id
-             * @param {string} subnet_id - Quantum subnet id
-             * @param {string} subnet - 
-             * 
-             */
-            xos.prototype.Controller_Network_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllernetworks/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['network'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: network'));
-                    return deferred.promise;
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['net_id'] !== undefined) {
-                    form['net_id'] = parameters['net_id'];
-                }
-
-                if (parameters['router_id'] !== undefined) {
-                    form['router_id'] = parameters['router_id'];
-                }
-
-                if (parameters['subnet_id'] !== undefined) {
-                    form['subnet_id'] = parameters['subnet_id'];
-                }
-
-                if (parameters['subnet'] !== undefined) {
-                    form['subnet'] = parameters['subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Network_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Network_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllernetworks/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Network_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} network - 
-             * @param {string} controller - 
-             * @param {string} net_id - Quantum network
-             * @param {string} router_id - Quantum router id
-             * @param {string} subnet_id - Quantum subnet id
-             * @param {string} subnet - 
-             * 
-             */
-            xos.prototype.Controller_Network_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllernetworks/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['network'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: network'));
-                    return deferred.promise;
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['net_id'] !== undefined) {
-                    form['net_id'] = parameters['net_id'];
-                }
-
-                if (parameters['router_id'] !== undefined) {
-                    form['router_id'] = parameters['router_id'];
-                }
-
-                if (parameters['subnet_id'] !== undefined) {
-                    form['subnet_id'] = parameters['subnet_id'];
-                }
-
-                if (parameters['subnet'] !== undefined) {
-                    form['subnet'] = parameters['subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Network_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} network - 
-             * @param {string} controller - 
-             * @param {string} net_id - Quantum network
-             * @param {string} router_id - Quantum router id
-             * @param {string} subnet_id - Quantum subnet id
-             * @param {string} subnet - 
-             * 
-             */
-            xos.prototype.Controller_Network_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllernetworks/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['net_id'] !== undefined) {
-                    form['net_id'] = parameters['net_id'];
-                }
-
-                if (parameters['router_id'] !== undefined) {
-                    form['router_id'] = parameters['router_id'];
-                }
-
-                if (parameters['subnet_id'] !== undefined) {
-                    form['subnet_id'] = parameters['subnet_id'];
-                }
-
-                if (parameters['subnet'] !== undefined) {
-                    form['subnet'] = parameters['subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Network_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Network_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllernetworks/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Image_Deployments_List_GET
-             * 
-             */
-            xos.prototype.Image_Deployments_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/imagedeploymentses/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Image_Deployments_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} image - 
-             * @param {string} deployment - 
-             * 
-             */
-            xos.prototype.Image_Deployments_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/imagedeploymentses/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['image'] !== undefined) {
-                    form['image'] = parameters['image'];
-                }
-
-                if (parameters['image'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: image'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['deployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: deployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Image_Deployments_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Image_Deployments_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/imagedeploymentses/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Image_Deployments_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} image - 
-             * @param {string} deployment - 
-             * 
-             */
-            xos.prototype.Image_Deployments_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/imagedeploymentses/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['image'] !== undefined) {
-                    form['image'] = parameters['image'];
-                }
-
-                if (parameters['image'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: image'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['deployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: deployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Image_Deployments_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} image - 
-             * @param {string} deployment - 
-             * 
-             */
-            xos.prototype.Image_Deployments_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/imagedeploymentses/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['image'] !== undefined) {
-                    form['image'] = parameters['image'];
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Image_Deployments_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Image_Deployments_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/imagedeploymentses/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_User_List_GET
-             * 
-             */
-            xos.prototype.Controller_User_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerusers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_User_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} controller - 
-             * @param {string} kuser_id - Keystone user id
-             * 
-             */
-            xos.prototype.Controller_User_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerusers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['kuser_id'] !== undefined) {
-                    form['kuser_id'] = parameters['kuser_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_User_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_User_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerusers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_User_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} controller - 
-             * @param {string} kuser_id - Keystone user id
-             * 
-             */
-            xos.prototype.Controller_User_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerusers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['kuser_id'] !== undefined) {
-                    form['kuser_id'] = parameters['kuser_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_User_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} controller - 
-             * @param {string} kuser_id - Keystone user id
-             * 
-             */
-            xos.prototype.Controller_User_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerusers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['kuser_id'] !== undefined) {
-                    form['kuser_id'] = parameters['kuser_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_User_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_User_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerusers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Reserved_Resource_List_GET
-             * 
-             */
-            xos.prototype.Reserved_Resource_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/reservedresources/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Reserved_Resource_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} instance - 
-             * @param {string} resource - 
-             * @param {integer} quantity - 
-             * @param {string} reservationSet - 
-             * 
-             */
-            xos.prototype.Reserved_Resource_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/reservedresources/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['instance'] !== undefined) {
-                    form['instance'] = parameters['instance'];
-                }
-
-                if (parameters['instance'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: instance'));
-                    return deferred.promise;
-                }
-
-                if (parameters['resource'] !== undefined) {
-                    form['resource'] = parameters['resource'];
-                }
-
-                if (parameters['resource'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: resource'));
-                    return deferred.promise;
-                }
-
-                if (parameters['quantity'] !== undefined) {
-                    form['quantity'] = parameters['quantity'];
-                }
-
-                if (parameters['quantity'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: quantity'));
-                    return deferred.promise;
-                }
-
-                if (parameters['reservationSet'] !== undefined) {
-                    form['reservationSet'] = parameters['reservationSet'];
-                }
-
-                if (parameters['reservationSet'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: reservationSet'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Reserved_Resource_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Reserved_Resource_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/reservedresources/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Reserved_Resource_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} instance - 
-             * @param {string} resource - 
-             * @param {integer} quantity - 
-             * @param {string} reservationSet - 
-             * 
-             */
-            xos.prototype.Reserved_Resource_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/reservedresources/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['instance'] !== undefined) {
-                    form['instance'] = parameters['instance'];
-                }
-
-                if (parameters['instance'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: instance'));
-                    return deferred.promise;
-                }
-
-                if (parameters['resource'] !== undefined) {
-                    form['resource'] = parameters['resource'];
-                }
-
-                if (parameters['resource'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: resource'));
-                    return deferred.promise;
-                }
-
-                if (parameters['quantity'] !== undefined) {
-                    form['quantity'] = parameters['quantity'];
-                }
-
-                if (parameters['quantity'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: quantity'));
-                    return deferred.promise;
-                }
-
-                if (parameters['reservationSet'] !== undefined) {
-                    form['reservationSet'] = parameters['reservationSet'];
-                }
-
-                if (parameters['reservationSet'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: reservationSet'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Reserved_Resource_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} instance - 
-             * @param {string} resource - 
-             * @param {integer} quantity - 
-             * @param {string} reservationSet - 
-             * 
-             */
-            xos.prototype.Reserved_Resource_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/reservedresources/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['instance'] !== undefined) {
-                    form['instance'] = parameters['instance'];
-                }
-
-                if (parameters['resource'] !== undefined) {
-                    form['resource'] = parameters['resource'];
-                }
-
-                if (parameters['quantity'] !== undefined) {
-                    form['quantity'] = parameters['quantity'];
-                }
-
-                if (parameters['reservationSet'] !== undefined) {
-                    form['reservationSet'] = parameters['reservationSet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Reserved_Resource_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Reserved_Resource_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/reservedresources/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Template_List_GET
-             * 
-             */
-            xos.prototype.Network_Template_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networktemplates/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Template_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} description - 
-             * @param {integer} guaranteed_bandwidth - 
-             * @param {choice} visibility - 
-             * @param {choice} translation - 
-             * @param {string} shared_network_name - 
-             * @param {string} shared_network_id - Quantum network
-             * @param {choice} topology_kind - 
-             * @param {choice} controller_kind - 
-             * 
-             */
-            xos.prototype.Network_Template_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networktemplates/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['guaranteed_bandwidth'] !== undefined) {
-                    form['guaranteed_bandwidth'] = parameters['guaranteed_bandwidth'];
-                }
-
-                if (parameters['guaranteed_bandwidth'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: guaranteed_bandwidth'));
-                    return deferred.promise;
-                }
-
-                if (parameters['visibility'] !== undefined) {
-                    form['visibility'] = parameters['visibility'];
-                }
-
-                if (parameters['visibility'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: visibility'));
-                    return deferred.promise;
-                }
-
-                if (parameters['translation'] !== undefined) {
-                    form['translation'] = parameters['translation'];
-                }
-
-                if (parameters['translation'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: translation'));
-                    return deferred.promise;
-                }
-
-                if (parameters['shared_network_name'] !== undefined) {
-                    form['shared_network_name'] = parameters['shared_network_name'];
-                }
-
-                if (parameters['shared_network_id'] !== undefined) {
-                    form['shared_network_id'] = parameters['shared_network_id'];
-                }
-
-                if (parameters['topology_kind'] !== undefined) {
-                    form['topology_kind'] = parameters['topology_kind'];
-                }
-
-                if (parameters['topology_kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: topology_kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['controller_kind'] !== undefined) {
-                    form['controller_kind'] = parameters['controller_kind'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Template_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Network_Template_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networktemplates/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Template_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} description - 
-             * @param {integer} guaranteed_bandwidth - 
-             * @param {choice} visibility - 
-             * @param {choice} translation - 
-             * @param {string} shared_network_name - 
-             * @param {string} shared_network_id - Quantum network
-             * @param {choice} topology_kind - 
-             * @param {choice} controller_kind - 
-             * 
-             */
-            xos.prototype.Network_Template_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networktemplates/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['guaranteed_bandwidth'] !== undefined) {
-                    form['guaranteed_bandwidth'] = parameters['guaranteed_bandwidth'];
-                }
-
-                if (parameters['guaranteed_bandwidth'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: guaranteed_bandwidth'));
-                    return deferred.promise;
-                }
-
-                if (parameters['visibility'] !== undefined) {
-                    form['visibility'] = parameters['visibility'];
-                }
-
-                if (parameters['visibility'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: visibility'));
-                    return deferred.promise;
-                }
-
-                if (parameters['translation'] !== undefined) {
-                    form['translation'] = parameters['translation'];
-                }
-
-                if (parameters['translation'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: translation'));
-                    return deferred.promise;
-                }
-
-                if (parameters['shared_network_name'] !== undefined) {
-                    form['shared_network_name'] = parameters['shared_network_name'];
-                }
-
-                if (parameters['shared_network_id'] !== undefined) {
-                    form['shared_network_id'] = parameters['shared_network_id'];
-                }
-
-                if (parameters['topology_kind'] !== undefined) {
-                    form['topology_kind'] = parameters['topology_kind'];
-                }
-
-                if (parameters['topology_kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: topology_kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['controller_kind'] !== undefined) {
-                    form['controller_kind'] = parameters['controller_kind'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Template_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} description - 
-             * @param {integer} guaranteed_bandwidth - 
-             * @param {choice} visibility - 
-             * @param {choice} translation - 
-             * @param {string} shared_network_name - 
-             * @param {string} shared_network_id - Quantum network
-             * @param {choice} topology_kind - 
-             * @param {choice} controller_kind - 
-             * 
-             */
-            xos.prototype.Network_Template_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networktemplates/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['guaranteed_bandwidth'] !== undefined) {
-                    form['guaranteed_bandwidth'] = parameters['guaranteed_bandwidth'];
-                }
-
-                if (parameters['visibility'] !== undefined) {
-                    form['visibility'] = parameters['visibility'];
-                }
-
-                if (parameters['translation'] !== undefined) {
-                    form['translation'] = parameters['translation'];
-                }
-
-                if (parameters['shared_network_name'] !== undefined) {
-                    form['shared_network_name'] = parameters['shared_network_name'];
-                }
-
-                if (parameters['shared_network_id'] !== undefined) {
-                    form['shared_network_id'] = parameters['shared_network_id'];
-                }
-
-                if (parameters['topology_kind'] !== undefined) {
-                    form['topology_kind'] = parameters['topology_kind'];
-                }
-
-                if (parameters['controller_kind'] !== undefined) {
-                    form['controller_kind'] = parameters['controller_kind'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Template_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Network_Template_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networktemplates/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Slice_List_GET
-             * 
-             */
-            xos.prototype.Network_Slice_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkslices/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Slice_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} network - 
-             * @param {string} slice - 
-             * 
-             */
-            xos.prototype.Network_Slice_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkslices/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['network'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: network'));
-                    return deferred.promise;
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Slice_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Network_Slice_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkslices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Slice_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} network - 
-             * @param {string} slice - 
-             * 
-             */
-            xos.prototype.Network_Slice_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkslices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['network'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: network'));
-                    return deferred.promise;
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Slice_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} network - 
-             * @param {string} slice - 
-             * 
-             */
-            xos.prototype.Network_Slice_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkslices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Slice_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Network_Slice_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkslices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Dashboard_View_List_GET
-             * 
-             */
-            xos.prototype.User_Dashboard_View_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/userdashboardviews/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Dashboard_View_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} dashboardView - 
-             * @param {integer} order - 
-             * 
-             */
-            xos.prototype.User_Dashboard_View_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/userdashboardviews/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['dashboardView'] !== undefined) {
-                    form['dashboardView'] = parameters['dashboardView'];
-                }
-
-                if (parameters['dashboardView'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: dashboardView'));
-                    return deferred.promise;
-                }
-
-                if (parameters['order'] !== undefined) {
-                    form['order'] = parameters['order'];
-                }
-
-                if (parameters['order'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: order'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Dashboard_View_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.User_Dashboard_View_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/userdashboardviews/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Dashboard_View_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} dashboardView - 
-             * @param {integer} order - 
-             * 
-             */
-            xos.prototype.User_Dashboard_View_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/userdashboardviews/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['dashboardView'] !== undefined) {
-                    form['dashboardView'] = parameters['dashboardView'];
-                }
-
-                if (parameters['dashboardView'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: dashboardView'));
-                    return deferred.promise;
-                }
-
-                if (parameters['order'] !== undefined) {
-                    form['order'] = parameters['order'];
-                }
-
-                if (parameters['order'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: order'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Dashboard_View_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} dashboardView - 
-             * @param {integer} order - 
-             * 
-             */
-            xos.prototype.User_Dashboard_View_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/userdashboardviews/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['dashboardView'] !== undefined) {
-                    form['dashboardView'] = parameters['dashboardView'];
-                }
-
-                if (parameters['order'] !== undefined) {
-                    form['order'] = parameters['order'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Dashboard_View_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.User_Dashboard_View_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/userdashboardviews/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_List_GET
-             * 
-             */
-            xos.prototype.Controller_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of the Controller
-             * @param {string} backend_type - Type of compute controller, e.g. EC2, OpenStack, or OpenStack version
-             * @param {string} version - Controller version
-             * @param {string} auth_url - Auth url for the compute controller
-             * @param {string} admin_user - Username of an admin user at this controller
-             * @param {string} admin_password - Password of theadmin user at this controller
-             * @param {string} admin_tenant - Name of the tenant the admin user belongs to
-             * @param {string} domain - Name of the domain this controller belongs to
-             * @param {string} rabbit_host - IP address of rabbitmq server at this controller
-             * @param {string} rabbit_user - Username of rabbitmq server at this controller
-             * @param {string} rabbit_password - Password of rabbitmq server at this controller
-             * @param {string} deployment - 
-             * 
-             */
-            xos.prototype.Controller_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['backend_type'] !== undefined) {
-                    form['backend_type'] = parameters['backend_type'];
-                }
-
-                if (parameters['backend_type'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_type'));
-                    return deferred.promise;
-                }
-
-                if (parameters['version'] !== undefined) {
-                    form['version'] = parameters['version'];
-                }
-
-                if (parameters['version'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: version'));
-                    return deferred.promise;
-                }
-
-                if (parameters['auth_url'] !== undefined) {
-                    form['auth_url'] = parameters['auth_url'];
-                }
-
-                if (parameters['admin_user'] !== undefined) {
-                    form['admin_user'] = parameters['admin_user'];
-                }
-
-                if (parameters['admin_password'] !== undefined) {
-                    form['admin_password'] = parameters['admin_password'];
-                }
-
-                if (parameters['admin_tenant'] !== undefined) {
-                    form['admin_tenant'] = parameters['admin_tenant'];
-                }
-
-                if (parameters['domain'] !== undefined) {
-                    form['domain'] = parameters['domain'];
-                }
-
-                if (parameters['rabbit_host'] !== undefined) {
-                    form['rabbit_host'] = parameters['rabbit_host'];
-                }
-
-                if (parameters['rabbit_user'] !== undefined) {
-                    form['rabbit_user'] = parameters['rabbit_user'];
-                }
-
-                if (parameters['rabbit_password'] !== undefined) {
-                    form['rabbit_password'] = parameters['rabbit_password'];
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['deployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: deployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of the Controller
-             * @param {string} backend_type - Type of compute controller, e.g. EC2, OpenStack, or OpenStack version
-             * @param {string} version - Controller version
-             * @param {string} auth_url - Auth url for the compute controller
-             * @param {string} admin_user - Username of an admin user at this controller
-             * @param {string} admin_password - Password of theadmin user at this controller
-             * @param {string} admin_tenant - Name of the tenant the admin user belongs to
-             * @param {string} domain - Name of the domain this controller belongs to
-             * @param {string} rabbit_host - IP address of rabbitmq server at this controller
-             * @param {string} rabbit_user - Username of rabbitmq server at this controller
-             * @param {string} rabbit_password - Password of rabbitmq server at this controller
-             * @param {string} deployment - 
-             * 
-             */
-            xos.prototype.Controller_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['backend_type'] !== undefined) {
-                    form['backend_type'] = parameters['backend_type'];
-                }
-
-                if (parameters['backend_type'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_type'));
-                    return deferred.promise;
-                }
-
-                if (parameters['version'] !== undefined) {
-                    form['version'] = parameters['version'];
-                }
-
-                if (parameters['version'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: version'));
-                    return deferred.promise;
-                }
-
-                if (parameters['auth_url'] !== undefined) {
-                    form['auth_url'] = parameters['auth_url'];
-                }
-
-                if (parameters['admin_user'] !== undefined) {
-                    form['admin_user'] = parameters['admin_user'];
-                }
-
-                if (parameters['admin_password'] !== undefined) {
-                    form['admin_password'] = parameters['admin_password'];
-                }
-
-                if (parameters['admin_tenant'] !== undefined) {
-                    form['admin_tenant'] = parameters['admin_tenant'];
-                }
-
-                if (parameters['domain'] !== undefined) {
-                    form['domain'] = parameters['domain'];
-                }
-
-                if (parameters['rabbit_host'] !== undefined) {
-                    form['rabbit_host'] = parameters['rabbit_host'];
-                }
-
-                if (parameters['rabbit_user'] !== undefined) {
-                    form['rabbit_user'] = parameters['rabbit_user'];
-                }
-
-                if (parameters['rabbit_password'] !== undefined) {
-                    form['rabbit_password'] = parameters['rabbit_password'];
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['deployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: deployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of the Controller
-             * @param {string} backend_type - Type of compute controller, e.g. EC2, OpenStack, or OpenStack version
-             * @param {string} version - Controller version
-             * @param {string} auth_url - Auth url for the compute controller
-             * @param {string} admin_user - Username of an admin user at this controller
-             * @param {string} admin_password - Password of theadmin user at this controller
-             * @param {string} admin_tenant - Name of the tenant the admin user belongs to
-             * @param {string} domain - Name of the domain this controller belongs to
-             * @param {string} rabbit_host - IP address of rabbitmq server at this controller
-             * @param {string} rabbit_user - Username of rabbitmq server at this controller
-             * @param {string} rabbit_password - Password of rabbitmq server at this controller
-             * @param {string} deployment - 
-             * 
-             */
-            xos.prototype.Controller_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['backend_type'] !== undefined) {
-                    form['backend_type'] = parameters['backend_type'];
-                }
-
-                if (parameters['version'] !== undefined) {
-                    form['version'] = parameters['version'];
-                }
-
-                if (parameters['auth_url'] !== undefined) {
-                    form['auth_url'] = parameters['auth_url'];
-                }
-
-                if (parameters['admin_user'] !== undefined) {
-                    form['admin_user'] = parameters['admin_user'];
-                }
-
-                if (parameters['admin_password'] !== undefined) {
-                    form['admin_password'] = parameters['admin_password'];
-                }
-
-                if (parameters['admin_tenant'] !== undefined) {
-                    form['admin_tenant'] = parameters['admin_tenant'];
-                }
-
-                if (parameters['domain'] !== undefined) {
-                    form['domain'] = parameters['domain'];
-                }
-
-                if (parameters['rabbit_host'] !== undefined) {
-                    form['rabbit_host'] = parameters['rabbit_host'];
-                }
-
-                if (parameters['rabbit_user'] !== undefined) {
-                    form['rabbit_user'] = parameters['rabbit_user'];
-                }
-
-                if (parameters['rabbit_password'] !== undefined) {
-                    form['rabbit_password'] = parameters['rabbit_password'];
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_List_GET
-             * 
-             */
-            xos.prototype.User_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/users/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_List_POST
-             * @param {string} password - 
-             * @param {string} last_login - 
-             * @param {string} email - 
-             * @param {string} username - 
-             * @param {string} firstname - person's given name
-             * @param {string} lastname - person's surname
-             * @param {string} phone - phone number contact
-             * @param {string} user_url - 
-             * @param {string} site - Site this user will be homed too
-             * @param {string} public_key - Public key string
-             * @param {boolean} is_active - 
-             * @param {boolean} is_admin - 
-             * @param {boolean} is_staff - 
-             * @param {boolean} is_readonly - 
-             * @param {boolean} is_registering - 
-             * @param {boolean} is_appuser - 
-             * @param {string} login_page - send this user to a specific page on login
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {choice} timezone - 
-             * 
-             */
-            xos.prototype.User_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/users/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['password'] !== undefined) {
-                    form['password'] = parameters['password'];
-                }
-
-                if (parameters['password'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: password'));
-                    return deferred.promise;
-                }
-
-                if (parameters['last_login'] !== undefined) {
-                    form['last_login'] = parameters['last_login'];
-                }
-
-                if (parameters['last_login'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: last_login'));
-                    return deferred.promise;
-                }
-
-                if (parameters['email'] !== undefined) {
-                    form['email'] = parameters['email'];
-                }
-
-                if (parameters['email'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: email'));
-                    return deferred.promise;
-                }
-
-                if (parameters['username'] !== undefined) {
-                    form['username'] = parameters['username'];
-                }
-
-                if (parameters['username'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: username'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firstname'] !== undefined) {
-                    form['firstname'] = parameters['firstname'];
-                }
-
-                if (parameters['firstname'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firstname'));
-                    return deferred.promise;
-                }
-
-                if (parameters['lastname'] !== undefined) {
-                    form['lastname'] = parameters['lastname'];
-                }
-
-                if (parameters['lastname'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: lastname'));
-                    return deferred.promise;
-                }
-
-                if (parameters['phone'] !== undefined) {
-                    form['phone'] = parameters['phone'];
-                }
-
-                if (parameters['user_url'] !== undefined) {
-                    form['user_url'] = parameters['user_url'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['public_key'] !== undefined) {
-                    form['public_key'] = parameters['public_key'];
-                }
-
-                if (parameters['is_active'] !== undefined) {
-                    form['is_active'] = parameters['is_active'];
-                }
-
-                if (parameters['is_admin'] !== undefined) {
-                    form['is_admin'] = parameters['is_admin'];
-                }
-
-                if (parameters['is_staff'] !== undefined) {
-                    form['is_staff'] = parameters['is_staff'];
-                }
-
-                if (parameters['is_readonly'] !== undefined) {
-                    form['is_readonly'] = parameters['is_readonly'];
-                }
-
-                if (parameters['is_registering'] !== undefined) {
-                    form['is_registering'] = parameters['is_registering'];
-                }
-
-                if (parameters['is_appuser'] !== undefined) {
-                    form['is_appuser'] = parameters['is_appuser'];
-                }
-
-                if (parameters['login_page'] !== undefined) {
-                    form['login_page'] = parameters['login_page'];
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['timezone'] !== undefined) {
-                    form['timezone'] = parameters['timezone'];
-                }
-
-                if (parameters['timezone'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: timezone'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.User_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/users/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} password - 
-             * @param {string} last_login - 
-             * @param {string} email - 
-             * @param {string} username - 
-             * @param {string} firstname - person's given name
-             * @param {string} lastname - person's surname
-             * @param {string} phone - phone number contact
-             * @param {string} user_url - 
-             * @param {string} site - Site this user will be homed too
-             * @param {string} public_key - Public key string
-             * @param {boolean} is_active - 
-             * @param {boolean} is_admin - 
-             * @param {boolean} is_staff - 
-             * @param {boolean} is_readonly - 
-             * @param {boolean} is_registering - 
-             * @param {boolean} is_appuser - 
-             * @param {string} login_page - send this user to a specific page on login
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {choice} timezone - 
-             * 
-             */
-            xos.prototype.User_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/users/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['password'] !== undefined) {
-                    form['password'] = parameters['password'];
-                }
-
-                if (parameters['password'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: password'));
-                    return deferred.promise;
-                }
-
-                if (parameters['last_login'] !== undefined) {
-                    form['last_login'] = parameters['last_login'];
-                }
-
-                if (parameters['last_login'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: last_login'));
-                    return deferred.promise;
-                }
-
-                if (parameters['email'] !== undefined) {
-                    form['email'] = parameters['email'];
-                }
-
-                if (parameters['email'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: email'));
-                    return deferred.promise;
-                }
-
-                if (parameters['username'] !== undefined) {
-                    form['username'] = parameters['username'];
-                }
-
-                if (parameters['username'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: username'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firstname'] !== undefined) {
-                    form['firstname'] = parameters['firstname'];
-                }
-
-                if (parameters['firstname'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firstname'));
-                    return deferred.promise;
-                }
-
-                if (parameters['lastname'] !== undefined) {
-                    form['lastname'] = parameters['lastname'];
-                }
-
-                if (parameters['lastname'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: lastname'));
-                    return deferred.promise;
-                }
-
-                if (parameters['phone'] !== undefined) {
-                    form['phone'] = parameters['phone'];
-                }
-
-                if (parameters['user_url'] !== undefined) {
-                    form['user_url'] = parameters['user_url'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['public_key'] !== undefined) {
-                    form['public_key'] = parameters['public_key'];
-                }
-
-                if (parameters['is_active'] !== undefined) {
-                    form['is_active'] = parameters['is_active'];
-                }
-
-                if (parameters['is_admin'] !== undefined) {
-                    form['is_admin'] = parameters['is_admin'];
-                }
-
-                if (parameters['is_staff'] !== undefined) {
-                    form['is_staff'] = parameters['is_staff'];
-                }
-
-                if (parameters['is_readonly'] !== undefined) {
-                    form['is_readonly'] = parameters['is_readonly'];
-                }
-
-                if (parameters['is_registering'] !== undefined) {
-                    form['is_registering'] = parameters['is_registering'];
-                }
-
-                if (parameters['is_appuser'] !== undefined) {
-                    form['is_appuser'] = parameters['is_appuser'];
-                }
-
-                if (parameters['login_page'] !== undefined) {
-                    form['login_page'] = parameters['login_page'];
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['timezone'] !== undefined) {
-                    form['timezone'] = parameters['timezone'];
-                }
-
-                if (parameters['timezone'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: timezone'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} password - 
-             * @param {string} last_login - 
-             * @param {string} email - 
-             * @param {string} username - 
-             * @param {string} firstname - person's given name
-             * @param {string} lastname - person's surname
-             * @param {string} phone - phone number contact
-             * @param {string} user_url - 
-             * @param {string} site - Site this user will be homed too
-             * @param {string} public_key - Public key string
-             * @param {boolean} is_active - 
-             * @param {boolean} is_admin - 
-             * @param {boolean} is_staff - 
-             * @param {boolean} is_readonly - 
-             * @param {boolean} is_registering - 
-             * @param {boolean} is_appuser - 
-             * @param {string} login_page - send this user to a specific page on login
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {choice} timezone - 
-             * 
-             */
-            xos.prototype.User_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/users/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['password'] !== undefined) {
-                    form['password'] = parameters['password'];
-                }
-
-                if (parameters['last_login'] !== undefined) {
-                    form['last_login'] = parameters['last_login'];
-                }
-
-                if (parameters['email'] !== undefined) {
-                    form['email'] = parameters['email'];
-                }
-
-                if (parameters['username'] !== undefined) {
-                    form['username'] = parameters['username'];
-                }
-
-                if (parameters['firstname'] !== undefined) {
-                    form['firstname'] = parameters['firstname'];
-                }
-
-                if (parameters['lastname'] !== undefined) {
-                    form['lastname'] = parameters['lastname'];
-                }
-
-                if (parameters['phone'] !== undefined) {
-                    form['phone'] = parameters['phone'];
-                }
-
-                if (parameters['user_url'] !== undefined) {
-                    form['user_url'] = parameters['user_url'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['public_key'] !== undefined) {
-                    form['public_key'] = parameters['public_key'];
-                }
-
-                if (parameters['is_active'] !== undefined) {
-                    form['is_active'] = parameters['is_active'];
-                }
-
-                if (parameters['is_admin'] !== undefined) {
-                    form['is_admin'] = parameters['is_admin'];
-                }
-
-                if (parameters['is_staff'] !== undefined) {
-                    form['is_staff'] = parameters['is_staff'];
-                }
-
-                if (parameters['is_readonly'] !== undefined) {
-                    form['is_readonly'] = parameters['is_readonly'];
-                }
-
-                if (parameters['is_registering'] !== undefined) {
-                    form['is_registering'] = parameters['is_registering'];
-                }
-
-                if (parameters['is_appuser'] !== undefined) {
-                    form['is_appuser'] = parameters['is_appuser'];
-                }
-
-                if (parameters['login_page'] !== undefined) {
-                    form['login_page'] = parameters['login_page'];
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['timezone'] !== undefined) {
-                    form['timezone'] = parameters['timezone'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.User_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/users/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_List_GET
-             * 
-             */
-            xos.prototype.Deployment_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deployments/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of the Deployment
-             * @param {string} accessControl - Access control list that specifies which sites/users may use nodes in this deployment
-             * 
-             */
-            xos.prototype.Deployment_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deployments/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['accessControl'] !== undefined) {
-                    form['accessControl'] = parameters['accessControl'];
-                }
-
-                if (parameters['accessControl'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: accessControl'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Deployment_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deployments/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of the Deployment
-             * @param {string} accessControl - Access control list that specifies which sites/users may use nodes in this deployment
-             * 
-             */
-            xos.prototype.Deployment_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deployments/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['accessControl'] !== undefined) {
-                    form['accessControl'] = parameters['accessControl'];
-                }
-
-                if (parameters['accessControl'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: accessControl'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - Name of the Deployment
-             * @param {string} accessControl - Access control list that specifies which sites/users may use nodes in this deployment
-             * 
-             */
-            xos.prototype.Deployment_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deployments/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['accessControl'] !== undefined) {
-                    form['accessControl'] = parameters['accessControl'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Deployment_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deployments/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Reservation_List_GET
-             * 
-             */
-            xos.prototype.Reservation_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/reservations/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Reservation_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} startTime - 
-             * @param {string} slice - 
-             * @param {integer} duration - 
-             * 
-             */
-            xos.prototype.Reservation_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/reservations/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['startTime'] !== undefined) {
-                    form['startTime'] = parameters['startTime'];
-                }
-
-                if (parameters['startTime'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: startTime'));
-                    return deferred.promise;
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters['duration'] !== undefined) {
-                    form['duration'] = parameters['duration'];
-                }
-
-                if (parameters['duration'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: duration'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Reservation_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Reservation_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/reservations/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Reservation_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} startTime - 
-             * @param {string} slice - 
-             * @param {integer} duration - 
-             * 
-             */
-            xos.prototype.Reservation_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/reservations/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['startTime'] !== undefined) {
-                    form['startTime'] = parameters['startTime'];
-                }
-
-                if (parameters['startTime'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: startTime'));
-                    return deferred.promise;
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters['duration'] !== undefined) {
-                    form['duration'] = parameters['duration'];
-                }
-
-                if (parameters['duration'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: duration'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Reservation_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} startTime - 
-             * @param {string} slice - 
-             * @param {integer} duration - 
-             * 
-             */
-            xos.prototype.Reservation_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/reservations/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['startTime'] !== undefined) {
-                    form['startTime'] = parameters['startTime'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['duration'] !== undefined) {
-                    form['duration'] = parameters['duration'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Reservation_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Reservation_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/reservations/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Privilege_List_GET
-             * 
-             */
-            xos.prototype.Site_Privilege_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/siteprivileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Privilege_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} site - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Site_Privilege_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/siteprivileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Privilege_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Site_Privilege_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/siteprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Privilege_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} site - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Site_Privilege_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/siteprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Privilege_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} site - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Site_Privilege_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/siteprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Privilege_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Site_Privilege_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/siteprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Slice_List_GET
-             * 
-             */
-            xos.prototype.Controller_Slice_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerslices/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Slice_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} controller - 
-             * @param {string} slice - 
-             * @param {string} tenant_id - Keystone tenant id
-             * 
-             */
-            xos.prototype.Controller_Slice_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerslices/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters['tenant_id'] !== undefined) {
-                    form['tenant_id'] = parameters['tenant_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Slice_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Slice_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerslices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Slice_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} controller - 
-             * @param {string} slice - 
-             * @param {string} tenant_id - Keystone tenant id
-             * 
-             */
-            xos.prototype.Controller_Slice_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerslices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters['tenant_id'] !== undefined) {
-                    form['tenant_id'] = parameters['tenant_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Slice_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} controller - 
-             * @param {string} slice - 
-             * @param {string} tenant_id - Keystone tenant id
-             * 
-             */
-            xos.prototype.Controller_Slice_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerslices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['tenant_id'] !== undefined) {
-                    form['tenant_id'] = parameters['tenant_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Slice_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Slice_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerslices/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_List_GET
-             * 
-             */
-            xos.prototype.Tenant_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenants/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} provider_service - 
-             * @param {string} subscriber_service - 
-             * @param {string} subscriber_tenant - 
-             * @param {string} subscriber_user - 
-             * @param {string} subscriber_root - 
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * @param {choice} connect_method - 
-             * 
-             */
-            xos.prototype.Tenant_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenants/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['provider_service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: provider_service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['subscriber_service'] !== undefined) {
-                    form['subscriber_service'] = parameters['subscriber_service'];
-                }
-
-                if (parameters['subscriber_tenant'] !== undefined) {
-                    form['subscriber_tenant'] = parameters['subscriber_tenant'];
-                }
-
-                if (parameters['subscriber_user'] !== undefined) {
-                    form['subscriber_user'] = parameters['subscriber_user'];
-                }
-
-                if (parameters['subscriber_root'] !== undefined) {
-                    form['subscriber_root'] = parameters['subscriber_root'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['connect_method'] !== undefined) {
-                    form['connect_method'] = parameters['connect_method'];
-                }
-
-                if (parameters['connect_method'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: connect_method'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Tenant_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenants/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} provider_service - 
-             * @param {string} subscriber_service - 
-             * @param {string} subscriber_tenant - 
-             * @param {string} subscriber_user - 
-             * @param {string} subscriber_root - 
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * @param {choice} connect_method - 
-             * 
-             */
-            xos.prototype.Tenant_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenants/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['provider_service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: provider_service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['subscriber_service'] !== undefined) {
-                    form['subscriber_service'] = parameters['subscriber_service'];
-                }
-
-                if (parameters['subscriber_tenant'] !== undefined) {
-                    form['subscriber_tenant'] = parameters['subscriber_tenant'];
-                }
-
-                if (parameters['subscriber_user'] !== undefined) {
-                    form['subscriber_user'] = parameters['subscriber_user'];
-                }
-
-                if (parameters['subscriber_root'] !== undefined) {
-                    form['subscriber_root'] = parameters['subscriber_root'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['connect_method'] !== undefined) {
-                    form['connect_method'] = parameters['connect_method'];
-                }
-
-                if (parameters['connect_method'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: connect_method'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} provider_service - 
-             * @param {string} subscriber_service - 
-             * @param {string} subscriber_tenant - 
-             * @param {string} subscriber_user - 
-             * @param {string} subscriber_root - 
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * @param {choice} connect_method - 
-             * 
-             */
-            xos.prototype.Tenant_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenants/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['subscriber_service'] !== undefined) {
-                    form['subscriber_service'] = parameters['subscriber_service'];
-                }
-
-                if (parameters['subscriber_tenant'] !== undefined) {
-                    form['subscriber_tenant'] = parameters['subscriber_tenant'];
-                }
-
-                if (parameters['subscriber_user'] !== undefined) {
-                    form['subscriber_user'] = parameters['subscriber_user'];
-                }
-
-                if (parameters['subscriber_root'] !== undefined) {
-                    form['subscriber_root'] = parameters['subscriber_root'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['connect_method'] !== undefined) {
-                    form['connect_method'] = parameters['connect_method'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Tenant_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenants/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Dashboard_View_List_GET
-             * 
-             */
-            xos.prototype.Controller_Dashboard_View_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerdashboardviews/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Dashboard_View_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} controller - 
-             * @param {string} dashboardView - 
-             * @param {boolean} enabled - 
-             * @param {string} url - URL of Dashboard
-             * 
-             */
-            xos.prototype.Controller_Dashboard_View_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerdashboardviews/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['dashboardView'] !== undefined) {
-                    form['dashboardView'] = parameters['dashboardView'];
-                }
-
-                if (parameters['dashboardView'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: dashboardView'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['url'] !== undefined) {
-                    form['url'] = parameters['url'];
-                }
-
-                if (parameters['url'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Dashboard_View_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Dashboard_View_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerdashboardviews/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Dashboard_View_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} controller - 
-             * @param {string} dashboardView - 
-             * @param {boolean} enabled - 
-             * @param {string} url - URL of Dashboard
-             * 
-             */
-            xos.prototype.Controller_Dashboard_View_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerdashboardviews/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['dashboardView'] !== undefined) {
-                    form['dashboardView'] = parameters['dashboardView'];
-                }
-
-                if (parameters['dashboardView'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: dashboardView'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['url'] !== undefined) {
-                    form['url'] = parameters['url'];
-                }
-
-                if (parameters['url'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Dashboard_View_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} controller - 
-             * @param {string} dashboardView - 
-             * @param {boolean} enabled - 
-             * @param {string} url - URL of Dashboard
-             * 
-             */
-            xos.prototype.Controller_Dashboard_View_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerdashboardviews/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['dashboardView'] !== undefined) {
-                    form['dashboardView'] = parameters['dashboardView'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['url'] !== undefined) {
-                    form['url'] = parameters['url'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Dashboard_View_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Dashboard_View_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerdashboardviews/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Account_List_GET
-             * 
-             */
-            xos.prototype.Account_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/accounts/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Account_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} site - Site for this account
-             * 
-             */
-            xos.prototype.Account_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/accounts/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Account_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Account_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/accounts/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Account_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} site - Site for this account
-             * 
-             */
-            xos.prototype.Account_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/accounts/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Account_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} site - Site for this account
-             * 
-             */
-            xos.prototype.Account_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/accounts/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Account_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Account_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/accounts/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_List_GET
-             * 
-             */
-            xos.prototype.Tenant_Root_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantroots/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} name - name
-             * @param {string} service_specific_attribute - 
-             * @param {string} service_specific_id - 
-             * 
-             */
-            xos.prototype.Tenant_Root_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantroots/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantroots/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} name - name
-             * @param {string} service_specific_attribute - 
-             * @param {string} service_specific_id - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantroots/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} name - name
-             * @param {string} service_specific_attribute - 
-             * @param {string} service_specific_id - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantroots/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantroots/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Role_List_GET
-             * 
-             */
-            xos.prototype.Controller_Role_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerroles/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Role_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-
-             * 
-             */
-            xos.prototype.Controller_Role_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerroles/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                form['role'] = 'admin';
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Role_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Role_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerroles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Role_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-
-             * 
-             */
-            xos.prototype.Controller_Role_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerroles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                form['role'] = 'admin';
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Role_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-
-             * 
-             */
-            xos.prototype.Controller_Role_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerroles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                form['role'] = 'admin';
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Role_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Role_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllerroles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Parameter_Type_List_GET
-             * 
-             */
-            xos.prototype.Network_Parameter_Type_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkparametertypes/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Parameter_Type_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - The name of this parameter
-             * @param {string} description - 
-             * 
-             */
-            xos.prototype.Network_Parameter_Type_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkparametertypes/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['description'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: description'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Parameter_Type_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Network_Parameter_Type_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkparametertypes/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Parameter_Type_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - The name of this parameter
-             * @param {string} description - 
-             * 
-             */
-            xos.prototype.Network_Parameter_Type_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkparametertypes/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['description'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: description'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Parameter_Type_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - The name of this parameter
-             * @param {string} description - 
-             * 
-             */
-            xos.prototype.Network_Parameter_Type_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkparametertypes/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Network_Parameter_Type_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Network_Parameter_Type_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/networkparametertypes/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Credential_List_GET
-             * 
-             */
-            xos.prototype.Site_Credential_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sitecredentials/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Credential_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} site - The User this credential is associated with
-             * @param {string} name - The credential type, e.g. ec2
-             * @param {string} key_id - The backend id of this credential
-             * @param {string} enc_value - The key value of this credential
-             * 
-             */
-            xos.prototype.Site_Credential_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sitecredentials/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['key_id'] !== undefined) {
-                    form['key_id'] = parameters['key_id'];
-                }
-
-                if (parameters['key_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: key_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enc_value'] !== undefined) {
-                    form['enc_value'] = parameters['enc_value'];
-                }
-
-                if (parameters['enc_value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: enc_value'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Credential_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Site_Credential_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sitecredentials/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Credential_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} site - The User this credential is associated with
-             * @param {string} name - The credential type, e.g. ec2
-             * @param {string} key_id - The backend id of this credential
-             * @param {string} enc_value - The key value of this credential
-             * 
-             */
-            xos.prototype.Site_Credential_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sitecredentials/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['key_id'] !== undefined) {
-                    form['key_id'] = parameters['key_id'];
-                }
-
-                if (parameters['key_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: key_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enc_value'] !== undefined) {
-                    form['enc_value'] = parameters['enc_value'];
-                }
-
-                if (parameters['enc_value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: enc_value'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Credential_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} site - The User this credential is associated with
-             * @param {string} name - The credential type, e.g. ec2
-             * @param {string} key_id - The backend id of this credential
-             * @param {string} enc_value - The key value of this credential
-             * 
-             */
-            xos.prototype.Site_Credential_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sitecredentials/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['key_id'] !== undefined) {
-                    form['key_id'] = parameters['key_id'];
-                }
-
-                if (parameters['enc_value'] !== undefined) {
-                    form['enc_value'] = parameters['enc_value'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Credential_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Site_Credential_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sitecredentials/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Privilege_List_GET
-             * 
-             */
-            xos.prototype.Deployment_Privilege_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deploymentprivileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Privilege_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} deployment - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Deployment_Privilege_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deploymentprivileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['deployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: deployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Privilege_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Deployment_Privilege_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deploymentprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Privilege_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} deployment - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Deployment_Privilege_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deploymentprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['deployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: deployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Privilege_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} deployment - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Deployment_Privilege_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deploymentprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Privilege_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Deployment_Privilege_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deploymentprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Slice_Privilege_List_GET
-             * 
-             */
-            xos.prototype.Controller_Slice_Privilege_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersliceprivileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Slice_Privilege_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} controller - 
-             * @param {string} slice_privilege - 
-             * @param {string} role_id - Keystone id
-             * 
-             */
-            xos.prototype.Controller_Slice_Privilege_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersliceprivileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['slice_privilege'] !== undefined) {
-                    form['slice_privilege'] = parameters['slice_privilege'];
-                }
-
-                if (parameters['slice_privilege'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice_privilege'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role_id'] !== undefined) {
-                    form['role_id'] = parameters['role_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Slice_Privilege_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Slice_Privilege_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersliceprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Slice_Privilege_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} controller - 
-             * @param {string} slice_privilege - 
-             * @param {string} role_id - Keystone id
-             * 
-             */
-            xos.prototype.Controller_Slice_Privilege_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersliceprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['controller'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: controller'));
-                    return deferred.promise;
-                }
-
-                if (parameters['slice_privilege'] !== undefined) {
-                    form['slice_privilege'] = parameters['slice_privilege'];
-                }
-
-                if (parameters['slice_privilege'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice_privilege'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role_id'] !== undefined) {
-                    form['role_id'] = parameters['role_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Slice_Privilege_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} controller - 
-             * @param {string} slice_privilege - 
-             * @param {string} role_id - Keystone id
-             * 
-             */
-            xos.prototype.Controller_Slice_Privilege_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersliceprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['slice_privilege'] !== undefined) {
-                    form['slice_privilege'] = parameters['slice_privilege'];
-                }
-
-                if (parameters['role_id'] !== undefined) {
-                    form['role_id'] = parameters['role_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Controller_Slice_Privilege_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Controller_Slice_Privilege_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/controllersliceprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Deployment_List_GET
-             * 
-             */
-            xos.prototype.Site_Deployment_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sitedeployments/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Deployment_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} site - 
-             * @param {string} deployment - 
-             * @param {string} controller - 
-             * @param {string} availability_zone - OpenStack availability zone
-             * 
-             */
-            xos.prototype.Site_Deployment_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sitedeployments/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['deployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: deployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['availability_zone'] !== undefined) {
-                    form['availability_zone'] = parameters['availability_zone'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Deployment_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Site_Deployment_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sitedeployments/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Deployment_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} site - 
-             * @param {string} deployment - 
-             * @param {string} controller - 
-             * @param {string} availability_zone - OpenStack availability zone
-             * 
-             */
-            xos.prototype.Site_Deployment_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sitedeployments/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['deployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: deployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['availability_zone'] !== undefined) {
-                    form['availability_zone'] = parameters['availability_zone'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Deployment_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} site - 
-             * @param {string} deployment - 
-             * @param {string} controller - 
-             * @param {string} availability_zone - OpenStack availability zone
-             * 
-             */
-            xos.prototype.Site_Deployment_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sitedeployments/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['deployment'] !== undefined) {
-                    form['deployment'] = parameters['deployment'];
-                }
-
-                if (parameters['controller'] !== undefined) {
-                    form['controller'] = parameters['controller'];
-                }
-
-                if (parameters['availability_zone'] !== undefined) {
-                    form['availability_zone'] = parameters['availability_zone'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Site_Deployment_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Site_Deployment_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/sitedeployments/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_With_Container_List_GET
-             * 
-             */
-            xos.prototype.Tenant_With_Container_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantwithcontainers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_With_Container_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} provider_service - 
-             * @param {string} subscriber_service - 
-             * @param {string} subscriber_tenant - 
-             * @param {string} subscriber_user - 
-             * @param {string} subscriber_root - 
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * @param {choice} connect_method - 
-             * 
-             */
-            xos.prototype.Tenant_With_Container_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantwithcontainers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['provider_service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: provider_service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['subscriber_service'] !== undefined) {
-                    form['subscriber_service'] = parameters['subscriber_service'];
-                }
-
-                if (parameters['subscriber_tenant'] !== undefined) {
-                    form['subscriber_tenant'] = parameters['subscriber_tenant'];
-                }
-
-                if (parameters['subscriber_user'] !== undefined) {
-                    form['subscriber_user'] = parameters['subscriber_user'];
-                }
-
-                if (parameters['subscriber_root'] !== undefined) {
-                    form['subscriber_root'] = parameters['subscriber_root'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['connect_method'] !== undefined) {
-                    form['connect_method'] = parameters['connect_method'];
-                }
-
-                if (parameters['connect_method'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: connect_method'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_With_Container_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Tenant_With_Container_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantwithcontainers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_With_Container_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} provider_service - 
-             * @param {string} subscriber_service - 
-             * @param {string} subscriber_tenant - 
-             * @param {string} subscriber_user - 
-             * @param {string} subscriber_root - 
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * @param {choice} connect_method - 
-             * 
-             */
-            xos.prototype.Tenant_With_Container_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantwithcontainers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['provider_service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: provider_service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['subscriber_service'] !== undefined) {
-                    form['subscriber_service'] = parameters['subscriber_service'];
-                }
-
-                if (parameters['subscriber_tenant'] !== undefined) {
-                    form['subscriber_tenant'] = parameters['subscriber_tenant'];
-                }
-
-                if (parameters['subscriber_user'] !== undefined) {
-                    form['subscriber_user'] = parameters['subscriber_user'];
-                }
-
-                if (parameters['subscriber_root'] !== undefined) {
-                    form['subscriber_root'] = parameters['subscriber_root'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['connect_method'] !== undefined) {
-                    form['connect_method'] = parameters['connect_method'];
-                }
-
-                if (parameters['connect_method'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: connect_method'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_With_Container_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} provider_service - 
-             * @param {string} subscriber_service - 
-             * @param {string} subscriber_tenant - 
-             * @param {string} subscriber_user - 
-             * @param {string} subscriber_root - 
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * @param {choice} connect_method - 
-             * 
-             */
-            xos.prototype.Tenant_With_Container_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantwithcontainers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['subscriber_service'] !== undefined) {
-                    form['subscriber_service'] = parameters['subscriber_service'];
-                }
-
-                if (parameters['subscriber_tenant'] !== undefined) {
-                    form['subscriber_tenant'] = parameters['subscriber_tenant'];
-                }
-
-                if (parameters['subscriber_user'] !== undefined) {
-                    form['subscriber_user'] = parameters['subscriber_user'];
-                }
-
-                if (parameters['subscriber_root'] !== undefined) {
-                    form['subscriber_root'] = parameters['subscriber_root'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['connect_method'] !== undefined) {
-                    form['connect_method'] = parameters['connect_method'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_With_Container_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Tenant_With_Container_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantwithcontainers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Role_List_GET
-             * 
-             */
-            xos.prototype.Deployment_Role_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deploymentroles/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Role_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-
-             * 
-             */
-            xos.prototype.Deployment_Role_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deploymentroles/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                form['role'] = 'admin';
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Role_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Deployment_Role_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deploymentroles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Role_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-
-             * 
-             */
-            xos.prototype.Deployment_Role_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deploymentroles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                form['role'] = 'admin';
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Role_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-
-             * 
-             */
-            xos.prototype.Deployment_Role_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deploymentroles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                form['role'] = 'admin';
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Deployment_Role_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Deployment_Role_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/deploymentroles/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Credential_List_GET
-             * 
-             */
-            xos.prototype.User_Credential_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/usercredentials/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Credential_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - The User this credential is associated with
-             * @param {string} name - The credential type, e.g. ec2
-             * @param {string} key_id - The backend id of this credential
-             * @param {string} enc_value - The key value of this credential
-             * 
-             */
-            xos.prototype.User_Credential_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/usercredentials/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['key_id'] !== undefined) {
-                    form['key_id'] = parameters['key_id'];
-                }
-
-                if (parameters['key_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: key_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enc_value'] !== undefined) {
-                    form['enc_value'] = parameters['enc_value'];
-                }
-
-                if (parameters['enc_value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: enc_value'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Credential_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.User_Credential_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/usercredentials/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Credential_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - The User this credential is associated with
-             * @param {string} name - The credential type, e.g. ec2
-             * @param {string} key_id - The backend id of this credential
-             * @param {string} enc_value - The key value of this credential
-             * 
-             */
-            xos.prototype.User_Credential_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/usercredentials/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['key_id'] !== undefined) {
-                    form['key_id'] = parameters['key_id'];
-                }
-
-                if (parameters['key_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: key_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enc_value'] !== undefined) {
-                    form['enc_value'] = parameters['enc_value'];
-                }
-
-                if (parameters['enc_value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: enc_value'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Credential_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - The User this credential is associated with
-             * @param {string} name - The credential type, e.g. ec2
-             * @param {string} key_id - The backend id of this credential
-             * @param {string} enc_value - The key value of this credential
-             * 
-             */
-            xos.prototype.User_Credential_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/usercredentials/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['key_id'] !== undefined) {
-                    form['key_id'] = parameters['key_id'];
-                }
-
-                if (parameters['enc_value'] !== undefined) {
-                    form['enc_value'] = parameters['enc_value'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#User_Credential_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.User_Credential_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/usercredentials/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Privilege_List_GET
-             * 
-             */
-            xos.prototype.Tenant_Root_Privilege_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantrootprivileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Privilege_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} tenant_root - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Privilege_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantrootprivileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['tenant_root'] !== undefined) {
-                    form['tenant_root'] = parameters['tenant_root'];
-                }
-
-                if (parameters['tenant_root'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: tenant_root'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Privilege_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Privilege_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantrootprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Privilege_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} tenant_root - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Privilege_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantrootprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['tenant_root'] !== undefined) {
-                    form['tenant_root'] = parameters['tenant_root'];
-                }
-
-                if (parameters['tenant_root'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: tenant_root'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Privilege_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} tenant_root - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Privilege_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantrootprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['tenant_root'] !== undefined) {
-                    form['tenant_root'] = parameters['tenant_root'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Tenant_Root_Privilege_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Tenant_Root_Privilege_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/tenantrootprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Tag_List_GET
-             * 
-             */
-            xos.prototype.Slice_Tag_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slicetags/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Tag_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} slice - 
-             * @param {choice} name - The name of this tag
-             * @param {string} value - The value of this tag
-             * 
-             */
-            xos.prototype.Slice_Tag_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slicetags/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['value'] !== undefined) {
-                    form['value'] = parameters['value'];
-                }
-
-                if (parameters['value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: value'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Tag_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Slice_Tag_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slicetags/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Tag_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} slice - 
-             * @param {choice} name - The name of this tag
-             * @param {string} value - The value of this tag
-             * 
-             */
-            xos.prototype.Slice_Tag_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slicetags/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['slice'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: slice'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['value'] !== undefined) {
-                    form['value'] = parameters['value'];
-                }
-
-                if (parameters['value'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: value'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Tag_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} slice - 
-             * @param {choice} name - The name of this tag
-             * @param {string} value - The value of this tag
-             * 
-             */
-            xos.prototype.Slice_Tag_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slicetags/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['slice'] !== undefined) {
-                    form['slice'] = parameters['slice'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['value'] !== undefined) {
-                    form['value'] = parameters['value'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Slice_Tag_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Slice_Tag_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/slicetags/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Coarse_Tenant_List_GET
-             * 
-             */
-            xos.prototype.Coarse_Tenant_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/coarsetenants/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Coarse_Tenant_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} provider_service - 
-             * @param {string} subscriber_service - 
-             * @param {string} subscriber_tenant - 
-             * @param {string} subscriber_user - 
-             * @param {string} subscriber_root - 
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * @param {choice} connect_method - 
-             * 
-             */
-            xos.prototype.Coarse_Tenant_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/coarsetenants/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['provider_service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: provider_service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['subscriber_service'] !== undefined) {
-                    form['subscriber_service'] = parameters['subscriber_service'];
-                }
-
-                if (parameters['subscriber_tenant'] !== undefined) {
-                    form['subscriber_tenant'] = parameters['subscriber_tenant'];
-                }
-
-                if (parameters['subscriber_user'] !== undefined) {
-                    form['subscriber_user'] = parameters['subscriber_user'];
-                }
-
-                if (parameters['subscriber_root'] !== undefined) {
-                    form['subscriber_root'] = parameters['subscriber_root'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['connect_method'] !== undefined) {
-                    form['connect_method'] = parameters['connect_method'];
-                }
-
-                if (parameters['connect_method'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: connect_method'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Coarse_Tenant_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Coarse_Tenant_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/coarsetenants/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Coarse_Tenant_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} provider_service - 
-             * @param {string} subscriber_service - 
-             * @param {string} subscriber_tenant - 
-             * @param {string} subscriber_user - 
-             * @param {string} subscriber_root - 
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * @param {choice} connect_method - 
-             * 
-             */
-            xos.prototype.Coarse_Tenant_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/coarsetenants/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['kind'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: kind'));
-                    return deferred.promise;
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['provider_service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: provider_service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['subscriber_service'] !== undefined) {
-                    form['subscriber_service'] = parameters['subscriber_service'];
-                }
-
-                if (parameters['subscriber_tenant'] !== undefined) {
-                    form['subscriber_tenant'] = parameters['subscriber_tenant'];
-                }
-
-                if (parameters['subscriber_user'] !== undefined) {
-                    form['subscriber_user'] = parameters['subscriber_user'];
-                }
-
-                if (parameters['subscriber_root'] !== undefined) {
-                    form['subscriber_root'] = parameters['subscriber_root'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['connect_method'] !== undefined) {
-                    form['connect_method'] = parameters['connect_method'];
-                }
-
-                if (parameters['connect_method'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: connect_method'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Coarse_Tenant_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} kind - 
-             * @param {string} provider_service - 
-             * @param {string} subscriber_service - 
-             * @param {string} subscriber_tenant - 
-             * @param {string} subscriber_user - 
-             * @param {string} subscriber_root - 
-             * @param {string} service_specific_id - 
-             * @param {string} service_specific_attribute - 
-             * @param {choice} connect_method - 
-             * 
-             */
-            xos.prototype.Coarse_Tenant_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/coarsetenants/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['kind'] !== undefined) {
-                    form['kind'] = parameters['kind'];
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['subscriber_service'] !== undefined) {
-                    form['subscriber_service'] = parameters['subscriber_service'];
-                }
-
-                if (parameters['subscriber_tenant'] !== undefined) {
-                    form['subscriber_tenant'] = parameters['subscriber_tenant'];
-                }
-
-                if (parameters['subscriber_user'] !== undefined) {
-                    form['subscriber_user'] = parameters['subscriber_user'];
-                }
-
-                if (parameters['subscriber_root'] !== undefined) {
-                    form['subscriber_root'] = parameters['subscriber_root'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_attribute'] !== undefined) {
-                    form['service_specific_attribute'] = parameters['service_specific_attribute'];
-                }
-
-                if (parameters['connect_method'] !== undefined) {
-                    form['connect_method'] = parameters['connect_method'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Coarse_Tenant_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Coarse_Tenant_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/coarsetenants/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Router_List_GET
-             * 
-             */
-            xos.prototype.Router_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/routers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Router_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} owner - 
-             * 
-             */
-            xos.prototype.Router_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/routers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['owner'] !== undefined) {
-                    form['owner'] = parameters['owner'];
-                }
-
-                if (parameters['owner'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: owner'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Router_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Router_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/routers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Router_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} owner - 
-             * 
-             */
-            xos.prototype.Router_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/routers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['owner'] !== undefined) {
-                    form['owner'] = parameters['owner'];
-                }
-
-                if (parameters['owner'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: owner'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Router_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} name - 
-             * @param {string} owner - 
-             * 
-             */
-            xos.prototype.Router_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/routers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['owner'] !== undefined) {
-                    form['owner'] = parameters['owner'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Router_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Router_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/routers/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Resource_List_GET
-             * 
-             */
-            xos.prototype.Service_Resource_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceresources/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Resource_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} serviceClass - 
-             * @param {string} name - 
-             * @param {integer} maxUnitsDeployment - 
-             * @param {integer} maxUnitsNode - 
-             * @param {integer} maxDuration - 
-             * @param {integer} bucketInRate - 
-             * @param {integer} bucketMaxSize - 
-             * @param {integer} cost - 
-             * @param {boolean} calendarReservable - 
-             * 
-             */
-            xos.prototype.Service_Resource_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceresources/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['serviceClass'] !== undefined) {
-                    form['serviceClass'] = parameters['serviceClass'];
-                }
-
-                if (parameters['serviceClass'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: serviceClass'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['maxUnitsDeployment'] !== undefined) {
-                    form['maxUnitsDeployment'] = parameters['maxUnitsDeployment'];
-                }
-
-                if (parameters['maxUnitsDeployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: maxUnitsDeployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters['maxUnitsNode'] !== undefined) {
-                    form['maxUnitsNode'] = parameters['maxUnitsNode'];
-                }
-
-                if (parameters['maxUnitsNode'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: maxUnitsNode'));
-                    return deferred.promise;
-                }
-
-                if (parameters['maxDuration'] !== undefined) {
-                    form['maxDuration'] = parameters['maxDuration'];
-                }
-
-                if (parameters['maxDuration'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: maxDuration'));
-                    return deferred.promise;
-                }
-
-                if (parameters['bucketInRate'] !== undefined) {
-                    form['bucketInRate'] = parameters['bucketInRate'];
-                }
-
-                if (parameters['bucketInRate'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: bucketInRate'));
-                    return deferred.promise;
-                }
-
-                if (parameters['bucketMaxSize'] !== undefined) {
-                    form['bucketMaxSize'] = parameters['bucketMaxSize'];
-                }
-
-                if (parameters['bucketMaxSize'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: bucketMaxSize'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cost'] !== undefined) {
-                    form['cost'] = parameters['cost'];
-                }
-
-                if (parameters['cost'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cost'));
-                    return deferred.promise;
-                }
-
-                if (parameters['calendarReservable'] !== undefined) {
-                    form['calendarReservable'] = parameters['calendarReservable'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Resource_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Service_Resource_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceresources/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Resource_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} serviceClass - 
-             * @param {string} name - 
-             * @param {integer} maxUnitsDeployment - 
-             * @param {integer} maxUnitsNode - 
-             * @param {integer} maxDuration - 
-             * @param {integer} bucketInRate - 
-             * @param {integer} bucketMaxSize - 
-             * @param {integer} cost - 
-             * @param {boolean} calendarReservable - 
-             * 
-             */
-            xos.prototype.Service_Resource_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceresources/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['serviceClass'] !== undefined) {
-                    form['serviceClass'] = parameters['serviceClass'];
-                }
-
-                if (parameters['serviceClass'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: serviceClass'));
-                    return deferred.promise;
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['maxUnitsDeployment'] !== undefined) {
-                    form['maxUnitsDeployment'] = parameters['maxUnitsDeployment'];
-                }
-
-                if (parameters['maxUnitsDeployment'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: maxUnitsDeployment'));
-                    return deferred.promise;
-                }
-
-                if (parameters['maxUnitsNode'] !== undefined) {
-                    form['maxUnitsNode'] = parameters['maxUnitsNode'];
-                }
-
-                if (parameters['maxUnitsNode'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: maxUnitsNode'));
-                    return deferred.promise;
-                }
-
-                if (parameters['maxDuration'] !== undefined) {
-                    form['maxDuration'] = parameters['maxDuration'];
-                }
-
-                if (parameters['maxDuration'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: maxDuration'));
-                    return deferred.promise;
-                }
-
-                if (parameters['bucketInRate'] !== undefined) {
-                    form['bucketInRate'] = parameters['bucketInRate'];
-                }
-
-                if (parameters['bucketInRate'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: bucketInRate'));
-                    return deferred.promise;
-                }
-
-                if (parameters['bucketMaxSize'] !== undefined) {
-                    form['bucketMaxSize'] = parameters['bucketMaxSize'];
-                }
-
-                if (parameters['bucketMaxSize'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: bucketMaxSize'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cost'] !== undefined) {
-                    form['cost'] = parameters['cost'];
-                }
-
-                if (parameters['cost'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cost'));
-                    return deferred.promise;
-                }
-
-                if (parameters['calendarReservable'] !== undefined) {
-                    form['calendarReservable'] = parameters['calendarReservable'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Resource_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} serviceClass - 
-             * @param {string} name - 
-             * @param {integer} maxUnitsDeployment - 
-             * @param {integer} maxUnitsNode - 
-             * @param {integer} maxDuration - 
-             * @param {integer} bucketInRate - 
-             * @param {integer} bucketMaxSize - 
-             * @param {integer} cost - 
-             * @param {boolean} calendarReservable - 
-             * 
-             */
-            xos.prototype.Service_Resource_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceresources/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['serviceClass'] !== undefined) {
-                    form['serviceClass'] = parameters['serviceClass'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['maxUnitsDeployment'] !== undefined) {
-                    form['maxUnitsDeployment'] = parameters['maxUnitsDeployment'];
-                }
-
-                if (parameters['maxUnitsNode'] !== undefined) {
-                    form['maxUnitsNode'] = parameters['maxUnitsNode'];
-                }
-
-                if (parameters['maxDuration'] !== undefined) {
-                    form['maxDuration'] = parameters['maxDuration'];
-                }
-
-                if (parameters['bucketInRate'] !== undefined) {
-                    form['bucketInRate'] = parameters['bucketInRate'];
-                }
-
-                if (parameters['bucketMaxSize'] !== undefined) {
-                    form['bucketMaxSize'] = parameters['bucketMaxSize'];
-                }
-
-                if (parameters['cost'] !== undefined) {
-                    form['cost'] = parameters['cost'];
-                }
-
-                if (parameters['calendarReservable'] !== undefined) {
-                    form['calendarReservable'] = parameters['calendarReservable'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Resource_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Service_Resource_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceresources/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Privilege_List_GET
-             * 
-             */
-            xos.prototype.Service_Privilege_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceprivileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Privilege_List_POST
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} service - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Service_Privilege_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceprivileges/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Privilege_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Service_Privilege_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Privilege_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} service - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Service_Privilege_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['backend_status'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: backend_status'));
-                    return deferred.promise;
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['user'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: user'));
-                    return deferred.promise;
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters['role'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: role'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Privilege_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} policed - 
-             * @param {string} backend_register - 
-             * @param {string} backend_status - 
-             * @param {boolean} deleted - 
-             * @param {boolean} write_protect - 
-             * @param {boolean} lazy_blocked - 
-             * @param {boolean} no_sync - 
-             * @param {string} user - 
-             * @param {string} service - 
-             * @param {string} role - 
-             * 
-             */
-            xos.prototype.Service_Privilege_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['policed'] !== undefined) {
-                    form['policed'] = parameters['policed'];
-                }
-
-                if (parameters['backend_register'] !== undefined) {
-                    form['backend_register'] = parameters['backend_register'];
-                }
-
-                if (parameters['backend_status'] !== undefined) {
-                    form['backend_status'] = parameters['backend_status'];
-                }
-
-                if (parameters['deleted'] !== undefined) {
-                    form['deleted'] = parameters['deleted'];
-                }
-
-                if (parameters['write_protect'] !== undefined) {
-                    form['write_protect'] = parameters['write_protect'];
-                }
-
-                if (parameters['lazy_blocked'] !== undefined) {
-                    form['lazy_blocked'] = parameters['lazy_blocked'];
-                }
-
-                if (parameters['no_sync'] !== undefined) {
-                    form['no_sync'] = parameters['no_sync'];
-                }
-
-                if (parameters['user'] !== undefined) {
-                    form['user'] = parameters['user'];
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['role'] !== undefined) {
-                    form['role'] = parameters['role'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xos#Service_Privilege_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xos.prototype.Service_Privilege_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xos/serviceprivileges/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xos.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-
-            return xos;
-        })();
-
-        return xos;
-    }]);/*jshint -W069 */
-/*global angular:false */
-angular.module('xos.xoslib', [])
-    .factory('xoslib', ['$q', '$http', '$rootScope', function($q, $http, $rootScope) {
-        'use strict';
-
-        /**
-         * 
-         * @class xoslib
-         * @param {(string|object)} [domainOrOptions] - The project domain or options object. If object, see the object's optional properties.
-         * @param {string} [domainOrOptions.domain] - The project domain
-         * @param {string} [domainOrOptions.cache] - An angularjs cache implementation
-         * @param {object} [domainOrOptions.token] - auth token - object with value property and optional headerOrQueryName and isQuery properties
-         * @param {string} [cache] - An angularjs cache implementation
-         */
-        var xoslib = (function() {
-            function xoslib(options, cache) {
-                var domain = (typeof options === 'object') ? options.domain : options;
-                this.domain = typeof(domain) === 'string' ? domain : 'http://localhost:9999';
-                cache = cache || ((typeof options === 'object') ? options.cache : cache);
-                this.cache = cache;
-            }
-
-            xoslib.prototype.$on = function($scope, path, handler) {
-                var url = domain + path;
-                $scope.$on(url, function() {
-                    handler();
-                });
-                return this;
-            };
-
-            xoslib.prototype.$broadcast = function(path) {
-                var url = domain + path;
-                //cache.remove(url);
-                $rootScope.$broadcast(url);
-                return this;
-            };
-
-            xoslib.transformRequest = function(obj) {
-                var str = [];
-                for (var p in obj) {
-                    var val = obj[p];
-                    if (angular.isArray(val)) {
-                        val.forEach(function(val) {
-                            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(val));
-                        });
-                    } else {
-                        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(val));
-                    }
-                }
-                return str.join("&");
-            };
-
-            /**
-             * 
-             * @method
-             * @name xoslib#Monitoring_Channel_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Monitoring_Channel_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/monitoringchannel/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Monitoring_Channel_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} provider_service - 
-             * 
-             */
-            xoslib.prototype.Monitoring_Channel_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/monitoringchannel/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['provider_service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: provider_service'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Monitoring_Channel_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} provider_service - 
-             * 
-             */
-            xoslib.prototype.Monitoring_Channel_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/monitoringchannel/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Monitoring_Channel_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Monitoring_Channel_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/monitoringchannel/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Monitoring_Channel_List_GET
-             * 
-             */
-            xoslib.prototype.Monitoring_Channel_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/monitoringchannel/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Monitoring_Channel_List_POST
-             * @param {string} provider_service - 
-             * 
-             */
-            xoslib.prototype.Monitoring_Channel_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/monitoringchannel/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['provider_service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: provider_service'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Hpc_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Hpc_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/hpcview/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Hpc_List_GET
-             * 
-             */
-            xoslib.prototype.Hpc_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/hpcview/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Tenant_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Tenant_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/tenantview/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Tenant_List_GET
-             * 
-             */
-            xoslib.prototype.Tenant_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/tenantview/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Port_Forwarding_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Port_Forwarding_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/portforwarding/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Port_Forwarding_List_GET
-             * 
-             */
-            xoslib.prototype.Port_Forwarding_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/portforwarding/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Ssh_Key_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Ssh_Key_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/sshkeys/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Ssh_Key_List_GET
-             * 
-             */
-            xoslib.prototype.Ssh_Key_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/sshkeys/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Debug_get_vbng_dump
-             * 
-             */
-            xoslib.prototype.Cord_Debug_get_vbng_dump = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/cord_debug/vbng_dump/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/cordsubscriber/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_Detail_PUT
-             * @param {string} pk - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/cordsubscriber/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_Detail_PATCH
-             * @param {string} pk - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/cordsubscriber/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/cordsubscriber/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_List_GET
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/cordsubscriber/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_List_POST
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/cordsubscriber/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_list
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_list = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_update
-             * @param {string} pk - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_update = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_partial_update
-             * @param {string} pk - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_partial_update = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_update
-             * @param {string} pk - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_update = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_destroy
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_destroy = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_retrieve
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_retrieve = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_get_vcpe_synced
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_get_vcpe_synced = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/vcpe_synced/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_get_url_filter
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_get_url_filter = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/url_filter/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_set_url_filter
-             * @param {string} pk - 
-             * @param {string} level - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_set_url_filter = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/url_filter/{level}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                path = path.replace('{level}', parameters['level']);
-
-                if (parameters['level'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: level'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_get_services
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_get_services = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/services/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_get_service
-             * @param {string} pk - 
-             * @param {string} service - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_get_service = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/services/{service}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                path = path.replace('{service}', parameters['service']);
-
-                if (parameters['service'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: service'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_enable_service
-             * @param {string} pk - 
-             * @param {string} service - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_enable_service = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/services/{service}/true/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                path = path.replace('{service}', parameters['service']);
-
-                if (parameters['service'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_disable_service
-             * @param {string} pk - 
-             * @param {string} service - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_disable_service = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/services/{service}/false/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                path = path.replace('{service}', parameters['service']);
-
-                if (parameters['service'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_create_user
-             * @param {string} pk - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_create_user = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/users/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_get_users
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_get_users = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/users/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_clear_users
-             * @param {string} pk - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_clear_users = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/users/clearusers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_clear_users
-             * @param {string} pk - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_clear_users = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/users/clearusers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_clear_users
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_clear_users = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/users/clearusers/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_create_user
-             * @param {string} pk - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_create_user = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/users/newuser/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_create_user
-             * @param {string} pk - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_create_user = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/users/newuser/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_delete_user
-             * @param {string} pk - 
-             * @param {string} uid - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_delete_user = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/users/{uid}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                path = path.replace('{uid}', parameters['uid']);
-
-                if (parameters['uid'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: uid'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_get_user_level
-             * @param {string} pk - 
-             * @param {string} uid - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_get_user_level = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/users/{uid}/url_filter/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                path = path.replace('{uid}', parameters['uid']);
-
-                if (parameters['uid'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: uid'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_set_user_level
-             * @param {string} pk - 
-             * @param {string} uid - 
-             * @param {string} level - 
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_set_user_level = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/users/{uid}/url_filter/{level}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                path = path.replace('{uid}', parameters['uid']);
-
-                if (parameters['uid'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: uid'));
-                    return deferred.promise;
-                }
-
-                path = path.replace('{level}', parameters['level']);
-
-                if (parameters['level'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: level'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_get_bbsdump
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_get_bbsdump = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subscriber/{pk}/bbsdump/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_initdemo
-             * @param {boolean} firewall_enable - 
-             * @param {string} firewall_rules - 
-             * @param {boolean} url_filter_enable - 
-             * @param {string} url_filter_rules - 
-             * @param {string} url_filter_level - 
-             * @param {boolean} vcpe_synced - 
-             * @param {boolean} cdn_enable - 
-             * @param {string} routeable_subnet - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_initdemo = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/initdemo/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['firewall_enable'] !== undefined) {
-                    form['firewall_enable'] = parameters['firewall_enable'];
-                }
-
-                if (parameters['firewall_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['firewall_rules'] !== undefined) {
-                    form['firewall_rules'] = parameters['firewall_rules'];
-                }
-
-                if (parameters['firewall_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: firewall_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_enable'] !== undefined) {
-                    form['url_filter_enable'] = parameters['url_filter_enable'];
-                }
-
-                if (parameters['url_filter_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_rules'] !== undefined) {
-                    form['url_filter_rules'] = parameters['url_filter_rules'];
-                }
-
-                if (parameters['url_filter_rules'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: url_filter_rules'));
-                    return deferred.promise;
-                }
-
-                if (parameters['url_filter_level'] !== undefined) {
-                    form['url_filter_level'] = parameters['url_filter_level'];
-                }
-
-                if (parameters['vcpe_synced'] !== undefined) {
-                    form['vcpe_synced'] = parameters['vcpe_synced'];
-                }
-
-                if (parameters['vcpe_synced'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vcpe_synced'));
-                    return deferred.promise;
-                }
-
-                if (parameters['cdn_enable'] !== undefined) {
-                    form['cdn_enable'] = parameters['cdn_enable'];
-                }
-
-                if (parameters['cdn_enable'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: cdn_enable'));
-                    return deferred.promise;
-                }
-
-                if (parameters['routeable_subnet'] !== undefined) {
-                    form['routeable_subnet'] = parameters['routeable_subnet'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_initdemo
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_initdemo = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/initdemo/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_ssiddetail
-             * @param {string} ssid - 
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_ssiddetail = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subidlookup/{ssid}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{ssid}', parameters['ssid']);
-
-                if (parameters['ssid'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: ssid'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_ssidlist
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_ssidlist = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/subidlookup/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_Subscriber_get_vbng_mapping
-             * 
-             */
-            xoslib.prototype.Cord_Subscriber_get_vbng_mapping = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/rs/vbng_mapping/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_User_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_User_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/corduser/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_User_Detail_PUT
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_User_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/corduser/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_User_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Cord_User_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/corduser/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_User_List_GET
-             * 
-             */
-            xoslib.prototype.Cord_User_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/corduser/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Cord_User_List_POST
-             * 
-             */
-            xoslib.prototype.Cord_User_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/corduser/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Volt_Tenant_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Volt_Tenant_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/volttenant/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Volt_Tenant_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} provider_service - 
-             * @param {string} service_specific_id - 
-             * @param {string} vlan_id - 
-             * 
-             */
-            xoslib.prototype.Volt_Tenant_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/volttenant/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['provider_service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: provider_service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: service_specific_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters['vlan_id'] !== undefined) {
-                    form['vlan_id'] = parameters['vlan_id'];
-                }
-
-                if (parameters['vlan_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vlan_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Volt_Tenant_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} provider_service - 
-             * @param {string} service_specific_id - 
-             * @param {string} vlan_id - 
-             * 
-             */
-            xoslib.prototype.Volt_Tenant_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/volttenant/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['vlan_id'] !== undefined) {
-                    form['vlan_id'] = parameters['vlan_id'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Volt_Tenant_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Volt_Tenant_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/volttenant/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Volt_Tenant_List_GET
-             * 
-             */
-            xoslib.prototype.Volt_Tenant_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/volttenant/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Volt_Tenant_List_POST
-             * @param {string} provider_service - 
-             * @param {string} service_specific_id - 
-             * @param {string} vlan_id - 
-             * 
-             */
-            xoslib.prototype.Volt_Tenant_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/volttenant/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['provider_service'] !== undefined) {
-                    form['provider_service'] = parameters['provider_service'];
-                }
-
-                if (parameters['provider_service'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: provider_service'));
-                    return deferred.promise;
-                }
-
-                if (parameters['service_specific_id'] !== undefined) {
-                    form['service_specific_id'] = parameters['service_specific_id'];
-                }
-
-                if (parameters['service_specific_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: service_specific_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters['vlan_id'] !== undefined) {
-                    form['vlan_id'] = parameters['vlan_id'];
-                }
-
-                if (parameters['vlan_id'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: vlan_id'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Slice_Plus_Detail_GET
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Slice_Plus_Detail_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/slicesplus/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Slice_Plus_Detail_PUT
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} name - The Name of the Slice
-             * @param {boolean} enabled - Status for this Slice
-             * @param {boolean} omf_friendly - 
-             * @param {string} description - High level description of the slice and expected activities
-             * @param {string} slice_url - 
-             * @param {string} site - The Site this Slice belongs to
-             * @param {integer} max_instances - 
-             * @param {string} service - 
-             * @param {string} network - 
-             * @param {string} mount_data_sets - 
-             * @param {string} default_image - 
-             * @param {string} default_flavor - 
-             * @param {string} serviceClass - 
-             * @param {string} creator - 
-             * @param {string} network_ports - 
-             * @param {string} site_allocation - 
-             * @param {string} site_ready - 
-             * @param {string} users - 
-             * @param {string} user_names - 
-             * 
-             */
-            xoslib.prototype.Slice_Plus_Detail_PUT = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/slicesplus/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['omf_friendly'] !== undefined) {
-                    form['omf_friendly'] = parameters['omf_friendly'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['slice_url'] !== undefined) {
-                    form['slice_url'] = parameters['slice_url'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['max_instances'] !== undefined) {
-                    form['max_instances'] = parameters['max_instances'];
-                }
-
-                if (parameters['max_instances'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: max_instances'));
-                    return deferred.promise;
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['mount_data_sets'] !== undefined) {
-                    form['mount_data_sets'] = parameters['mount_data_sets'];
-                }
-
-                if (parameters['default_image'] !== undefined) {
-                    form['default_image'] = parameters['default_image'];
-                }
-
-                if (parameters['default_flavor'] !== undefined) {
-                    form['default_flavor'] = parameters['default_flavor'];
-                }
-
-                if (parameters['serviceClass'] !== undefined) {
-                    form['serviceClass'] = parameters['serviceClass'];
-                }
-
-                if (parameters['creator'] !== undefined) {
-                    form['creator'] = parameters['creator'];
-                }
-
-                if (parameters['network_ports'] !== undefined) {
-                    form['network_ports'] = parameters['network_ports'];
-                }
-
-                if (parameters['site_allocation'] !== undefined) {
-                    form['site_allocation'] = parameters['site_allocation'];
-                }
-
-                if (parameters['site_ready'] !== undefined) {
-                    form['site_ready'] = parameters['site_ready'];
-                }
-
-                if (parameters['users'] !== undefined) {
-                    form['users'] = parameters['users'];
-                }
-
-                if (parameters['user_names'] !== undefined) {
-                    form['user_names'] = parameters['user_names'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PUT',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Slice_Plus_Detail_PATCH
-             * @param {string} pk - 
-             * @param {string} enacted - 
-             * @param {string} name - The Name of the Slice
-             * @param {boolean} enabled - Status for this Slice
-             * @param {boolean} omf_friendly - 
-             * @param {string} description - High level description of the slice and expected activities
-             * @param {string} slice_url - 
-             * @param {string} site - The Site this Slice belongs to
-             * @param {integer} max_instances - 
-             * @param {string} service - 
-             * @param {string} network - 
-             * @param {string} mount_data_sets - 
-             * @param {string} default_image - 
-             * @param {string} default_flavor - 
-             * @param {string} serviceClass - 
-             * @param {string} creator - 
-             * @param {string} network_ports - 
-             * @param {string} site_allocation - 
-             * @param {string} site_ready - 
-             * @param {string} users - 
-             * @param {string} user_names - 
-             * 
-             */
-            xoslib.prototype.Slice_Plus_Detail_PATCH = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/slicesplus/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['omf_friendly'] !== undefined) {
-                    form['omf_friendly'] = parameters['omf_friendly'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['slice_url'] !== undefined) {
-                    form['slice_url'] = parameters['slice_url'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['max_instances'] !== undefined) {
-                    form['max_instances'] = parameters['max_instances'];
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['mount_data_sets'] !== undefined) {
-                    form['mount_data_sets'] = parameters['mount_data_sets'];
-                }
-
-                if (parameters['default_image'] !== undefined) {
-                    form['default_image'] = parameters['default_image'];
-                }
-
-                if (parameters['default_flavor'] !== undefined) {
-                    form['default_flavor'] = parameters['default_flavor'];
-                }
-
-                if (parameters['serviceClass'] !== undefined) {
-                    form['serviceClass'] = parameters['serviceClass'];
-                }
-
-                if (parameters['creator'] !== undefined) {
-                    form['creator'] = parameters['creator'];
-                }
-
-                if (parameters['network_ports'] !== undefined) {
-                    form['network_ports'] = parameters['network_ports'];
-                }
-
-                if (parameters['site_allocation'] !== undefined) {
-                    form['site_allocation'] = parameters['site_allocation'];
-                }
-
-                if (parameters['site_ready'] !== undefined) {
-                    form['site_ready'] = parameters['site_ready'];
-                }
-
-                if (parameters['users'] !== undefined) {
-                    form['users'] = parameters['users'];
-                }
-
-                if (parameters['user_names'] !== undefined) {
-                    form['user_names'] = parameters['user_names'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'PATCH',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Slice_Plus_Detail_DELETE
-             * @param {string} pk - 
-             * 
-             */
-            xoslib.prototype.Slice_Plus_Detail_DELETE = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/slicesplus/{pk}/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                path = path.replace('{pk}', parameters['pk']);
-
-                if (parameters['pk'] === undefined) {
-                    deferred.reject(new Error('Missing required path parameter: pk'));
-                    return deferred.promise;
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'DELETE',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Slice_Plus_List_GET
-             * 
-             */
-            xoslib.prototype.Slice_Plus_List_GET = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/slicesplus/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var cached = parameters.$cache && parameters.$cache.get(url);
-                if (cached !== undefined && parameters.$refresh !== true) {
-                    deferred.resolve(cached);
-                    return deferred.promise;
-                }
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'GET',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-            /**
-             * 
-             * @method
-             * @name xoslib#Slice_Plus_List_POST
-             * @param {string} enacted - 
-             * @param {string} name - The Name of the Slice
-             * @param {boolean} enabled - Status for this Slice
-             * @param {boolean} omf_friendly - 
-             * @param {string} description - High level description of the slice and expected activities
-             * @param {string} slice_url - 
-             * @param {string} site - The Site this Slice belongs to
-             * @param {integer} max_instances - 
-             * @param {string} service - 
-             * @param {string} network - 
-             * @param {string} mount_data_sets - 
-             * @param {string} default_image - 
-             * @param {string} default_flavor - 
-             * @param {string} serviceClass - 
-             * @param {string} creator - 
-             * @param {string} network_ports - 
-             * @param {string} site_allocation - 
-             * @param {string} site_ready - 
-             * @param {string} users - 
-             * @param {string} user_names - 
-             * 
-             */
-            xoslib.prototype.Slice_Plus_List_POST = function(parameters) {
-                if (parameters === undefined) {
-                    parameters = {};
-                }
-                var deferred = $q.defer();
-
-                var domain = this.domain;
-                var path = '/xoslib/slicesplus/';
-
-                var body;
-                var queryParameters = {};
-                var headers = {};
-                var form = {};
-
-                if (parameters['enacted'] !== undefined) {
-                    form['enacted'] = parameters['enacted'];
-                }
-
-                if (parameters['name'] !== undefined) {
-                    form['name'] = parameters['name'];
-                }
-
-                if (parameters['name'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: name'));
-                    return deferred.promise;
-                }
-
-                if (parameters['enabled'] !== undefined) {
-                    form['enabled'] = parameters['enabled'];
-                }
-
-                if (parameters['omf_friendly'] !== undefined) {
-                    form['omf_friendly'] = parameters['omf_friendly'];
-                }
-
-                if (parameters['description'] !== undefined) {
-                    form['description'] = parameters['description'];
-                }
-
-                if (parameters['slice_url'] !== undefined) {
-                    form['slice_url'] = parameters['slice_url'];
-                }
-
-                if (parameters['site'] !== undefined) {
-                    form['site'] = parameters['site'];
-                }
-
-                if (parameters['site'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: site'));
-                    return deferred.promise;
-                }
-
-                if (parameters['max_instances'] !== undefined) {
-                    form['max_instances'] = parameters['max_instances'];
-                }
-
-                if (parameters['max_instances'] === undefined) {
-                    deferred.reject(new Error('Missing required form parameter: max_instances'));
-                    return deferred.promise;
-                }
-
-                if (parameters['service'] !== undefined) {
-                    form['service'] = parameters['service'];
-                }
-
-                if (parameters['network'] !== undefined) {
-                    form['network'] = parameters['network'];
-                }
-
-                if (parameters['mount_data_sets'] !== undefined) {
-                    form['mount_data_sets'] = parameters['mount_data_sets'];
-                }
-
-                if (parameters['default_image'] !== undefined) {
-                    form['default_image'] = parameters['default_image'];
-                }
-
-                if (parameters['default_flavor'] !== undefined) {
-                    form['default_flavor'] = parameters['default_flavor'];
-                }
-
-                if (parameters['serviceClass'] !== undefined) {
-                    form['serviceClass'] = parameters['serviceClass'];
-                }
-
-                if (parameters['creator'] !== undefined) {
-                    form['creator'] = parameters['creator'];
-                }
-
-                if (parameters['network_ports'] !== undefined) {
-                    form['network_ports'] = parameters['network_ports'];
-                }
-
-                if (parameters['site_allocation'] !== undefined) {
-                    form['site_allocation'] = parameters['site_allocation'];
-                }
-
-                if (parameters['site_ready'] !== undefined) {
-                    form['site_ready'] = parameters['site_ready'];
-                }
-
-                if (parameters['users'] !== undefined) {
-                    form['users'] = parameters['users'];
-                }
-
-                if (parameters['user_names'] !== undefined) {
-                    form['user_names'] = parameters['user_names'];
-                }
-
-                if (parameters.$queryParameters) {
-                    Object.keys(parameters.$queryParameters)
-                        .forEach(function(parameterName) {
-                            var parameter = parameters.$queryParameters[parameterName];
-                            queryParameters[parameterName] = parameter;
-                        });
-                }
-
-                var url = domain + path;
-                var options = {
-                    timeout: parameters.$timeout,
-                    method: 'POST',
-                    url: url,
-                    params: queryParameters,
-                    data: body,
-                    headers: headers
-                };
-                if (Object.keys(form).length > 0) {
-                    options.data = form;
-                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
-                    options.transformRequest = xoslib.transformRequest;
-                }
-                $http(options)
-                    .success(function(data, status, headers, config) {
-                        deferred.resolve(data);
-                        if (parameters.$cache !== undefined) {
-                            parameters.$cache.put(url, data, parameters.$cacheItemOpts ? parameters.$cacheItemOpts : {});
-                        }
-                    })
-                    .error(function(data, status, headers, config) {
-                        deferred.reject({
-                            status: status,
-                            headers: headers,
-                            config: config,
-                            body: data
-                        });
-                    });
-
-                return deferred.promise;
-            };
-
-            return xoslib;
-        })();
-
-        return xoslib;
-    }]);
\ No newline at end of file
diff --git a/xos/core/xoslib/static/js/xosDiagnostic.js b/xos/core/xoslib/static/js/xosDiagnostic.js
index 4e4def0..6214de7 100644
--- a/xos/core/xoslib/static/js/xosDiagnostic.js
+++ b/xos/core/xoslib/static/js/xosDiagnostic.js
@@ -1,2018 +1,2 @@
-//Autogenerated, do not edit!!!
-'use strict';
-
-(function () {
-  'use strict';
-
-  angular.module('xos.diagnostic', ['ngResource', 'ngCookies', 'ngLodash', 'ngAnimate', 'ui.router', 'xos.helpers']).config(["$stateProvider", function ($stateProvider) {
-    $stateProvider.state('home', {
-      url: '/',
-      template: '<diagnostic-container></diagnostic-container>'
-    });
-  }]).config(["$httpProvider", function ($httpProvider) {
-    $httpProvider.interceptors.push('NoHyperlinks');
-  }]).run(["$log", function ($log) {
-    $log.info('Diagnostic Started');
-  }]);
-})();
-angular.module("xos.diagnostic").run(["$templateCache", function($templateCache) {$templateCache.put("templates/diagnostic.tpl.html","<div class=\"container-fluid\">\n  <div ng-hide=\"vm.error && vm.loader\" style=\"height: 900px\">\n    <div class=\"onethird-height\">\n      <div class=\"well\">\n        Services Graph\n      </div>\n      <div class=\"well pull-right\" ng-click=\"vm.reloadGlobalScope()\" ng-show=\"vm.selectedSubscriber\">\n        Reset subscriber\n      </div>\n      <service-topology service-chain=\"vm.serviceChain\"></service-topology>\n    </div>\n    <div class=\"twothird-height\">\n      <div class=\"well\">\n        Logical Resources\n      </div>\n      <logic-topology ng-if=\"vm.subscribers\" subscribers=\"vm.subscribers\" selected=\"vm.selectedSubscriber\"></logic-topology>\n    </div>\n  </div>\n  <div class=\"row\" ng-if=\"vm.error\">\n    <div class=\"col-xs-12\">\n      <div class=\"alert alert-danger\">\n        {{vm.error}}\n      </div>\n    </div>\n  </div>\n  <div class=\"row\" ng-if=\"vm.loader\">\n    <div class=\"col-xs-12\">\n      <div class=\"loader\">Loading</div>\n    </div>\n  </div>\n</div>");
-$templateCache.put("templates/logicTopology.tpl.html","<select-subscriber-modal open=\"vm.openSelectSubscriberModal\" subscribers=\"vm.subscribers\"></select-subscriber-modal>\n<subscriber-status-modal open=\"vm.openSubscriberStatusModal\" subscriber=\"vm.currentSubscriber\"></subscriber-status-modal>\n<div class=\"alert alert-danger animate\" ng-hide=\"!vm.error\">\n  {{vm.error}}\n</div>\n<!-- <div class=\"instances-stats animate\" ng-hide=\"vm.hideInstanceStats\">\n  <div class=\"row\">\n    <div class=\"col-sm-3 col-sm-offset-8\">\n      <div class=\"panel panel-primary\" ng-repeat=\"instance in vm.selectedInstances\">\n        <div class=\"panel-heading\">\n          {{instance.humanReadableName}}\n        </div>\n          <ul class=\"list-group\">\n            <li class=\"list-group-item\">Backend Status: {{instance.backend_status}}</li>\n            <li class=\"list-group-item\">IP Address: {{instance.ip}}</li>\n          </ul>\n          <ul class=\"list-group\">\n            <li class=\"list-group-item\" ng-repeat=\"stat in instance.stats\">\n              <span class=\"badge\">{{stat.value}}</span>\n              {{stat.meter}}\n            </li>\n          </ul>\n        </div>\n      </div>  \n    </div>\n  </div>\n</div> -->");
-$templateCache.put("templates/select-subscriber-modal.tpl.html","<div class=\"modal fade\" ng-class=\"{in: vm.open}\" tabindex=\"-1\" role=\"dialog\">\n  <div class=\"modal-dialog modal-sm\">\n    <div class=\"modal-content\">\n      <div class=\"modal-header\">\n        <button ng-click=\"vm.close()\"  type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button>\n        <h4 class=\"modal-title\">Select a subscriber:</h4>\n      </div>\n      <div class=\"modal-body\">\n        <select class=\"form-control\" ng-options=\"s as s.humanReadableName for s in vm.subscribers\" ng-model=\"vm.selected\"></select>\n      </div>\n      <div class=\"modal-footer\">\n        <button ng-click=\"vm.close()\" type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">Close</button>\n        <button ng-click=\"vm.select(vm.selected)\" type=\"button\" class=\"btn btn-primary\">Select</button>\n      </div>\n    </div><!-- /.modal-content -->\n  </div><!-- /.modal-dialog -->\n</div><!-- /.modal -->");
-$templateCache.put("templates/subscriber-status-modal.tpl.html","<div class=\"modal fade\" ng-class=\"{in: vm.open}\" tabindex=\"-1\" role=\"dialog\">\n  <div class=\"modal-dialog modal-sm\">\n    <div class=\"modal-content\">\n      <div class=\"modal-header\">\n        <button ng-click=\"vm.close()\"  type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button>\n        <h4 class=\"modal-title\">Manage subscriber:</h4>\n      </div>\n      <form name=\"vm.subscriber-detail\">\n        <div class=\"modal-body\">\n          <div class=\"row\">\n            <div class=\"col-xs-12\">\n              <label>Status</label>\n            </div>\n            <div class=\"col-xs-6\">\n              <a ng-click=\"vm.subscriber.status = \'enabled\'\"\n                class=\"btn btn-block\"\n                ng-class=\"{\'btn-primary\': vm.subscriber.status === \'enabled\' ,\'btn-default\': vm.subscriber.status !== \'enabled\'}\"\n                >Enabled</a>\n            </div>\n            <div class=\"col-xs-6\">\n              <a ng-click=\"vm.subscriber.status = \'suspended\'\"\n                class=\"btn btn-block\"\n                ng-class=\"{\'btn-primary\': vm.subscriber.status === \'suspended\' ,\'btn-default\': vm.subscriber.status !== \'suspended\'}\"\n                >Suspended</a>\n            </div>\n          </div>\n          <div class=\"row\">\n            <div class=\"col-xs-6\">\n              <a ng-click=\"vm.subscriber.status = \'delinquent\'\"\n                class=\"btn btn-block\"\n                ng-class=\"{\'btn-primary\': vm.subscriber.status === \'delinquent\' ,\'btn-default\': vm.subscriber.status !== \'delinquent\'}\"\n                >Delinquent <br> payment</a>\n            </div>\n            <div class=\"col-xs-6\">\n              <a ng-click=\"vm.subscriber.status = \'copyrightviolation\'\"\n                class=\"btn btn-block\"\n                ng-class=\"{\'btn-primary\': vm.subscriber.status === \'copyrightviolation\' ,\'btn-default\': vm.subscriber.status !== \'copyrightviolation\'}\"\n                >Copyright <br> violation</a>\n            </div>\n          </div>\n          <div class=\"row\">\n            <div class=\"col-xs-6\">\n              <label>Uplink Speed</label>\n              <div class=\"input-group\">\n                <input type=\"number\" class=\"form-control small-padding\" ng-model=\"vm.subscriber.uplink_speed\"/>\n                <span class=\"input-group-addon\">Mbps</span>\n              </div>\n            </div>\n            <div class=\"col-xs-6\">\n              <label>Downlink Speed</label>\n              <div class=\"input-group\">\n                <input type=\"number\" class=\"form-control small-padding\" ng-model=\"vm.subscriber.downlink_speed\"/>\n                <span class=\"input-group-addon\">Mbps</span>\n              </div>\n            </div>\n          </div>\n          <div class=\"row\">\n            <div class=\"col-xs-6\">\n              <label>Enable Internet</label>\n            </div>\n            <div class=\"col-xs-6\">\n              <a \n                ng-click=\"vm.subscriber.enable_uverse = !vm.subscriber.enable_uverse\" \n                ng-class=\"{\'btn-success\': vm.subscriber.enable_uverse, \'btn-danger\': !vm.subscriber.enable_uverse}\"\n                class=\"btn btn-block\">\n                <span ng-show=\"vm.subscriber.enable_uverse === true\">Enabled</span>\n                <span ng-show=\"vm.subscriber.enable_uverse !== true\">Disabled</span>\n              </a>\n            </div>\n          </div>\n        </div>\n        <div class=\"modal-footer\" ng-show=\"vm.success || vm.formError\">\n          <div class=\"alert alert-success\" ng-show=\"vm.success\">\n            {{vm.success}}\n          </div>\n          <div class=\"alert alert-danger\" ng-show=\"vm.formError\">\n            {{vm.formError}}\n          </div>\n        </div>\n        <div class=\"modal-footer\">\n          <button ng-click=\"vm.close()\" type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">Close</button>\n          <button ng-click=\"vm.updateSubscriber(vm.subscriber)\" type=\"button\" class=\"btn btn-primary\">Save</button>\n        </div>\n      </form>\n    </div><!-- /.modal-content -->\n  </div><!-- /.modal-dialog -->\n</div><!-- /.modal -->");}]);
-'use strict';
-
-(function () {
-  'use strict';
-  angular.module('xos.diagnostic').directive('selectSubscriberModal', function () {
-    return {
-      scope: {
-        subscribers: '=',
-        open: '='
-      },
-      bindToController: true,
-      restrict: 'E',
-      templateUrl: 'templates/select-subscriber-modal.tpl.html',
-      controllerAs: 'vm',
-      controller: ["$rootScope", function controller($rootScope) {
-        var _this = this;
-
-        this.close = function () {
-          _this.open = false;
-        };
-
-        this.select = function (subscriber) {
-          $rootScope.$emit('subscriber.selected', subscriber);
-          _this.close();
-        };
-      }]
-    };
-  }).directive('subscriberStatusModal', function () {
-    return {
-      scope: {
-        open: '=',
-        subscriber: '='
-      },
-      bindToController: true,
-      restrict: 'E',
-      templateUrl: 'templates/subscriber-status-modal.tpl.html',
-      controllerAs: 'vm',
-      controller: ["$log", "$timeout", "$scope", "Subscribers", function controller($log, $timeout, $scope, Subscribers) {
-        var _this2 = this;
-
-        var mb = 1000000;
-
-        $scope.$watch(function () {
-          return _this2.open;
-        }, function () {
-          _this2.success = null;
-          _this2.formError = null;
-        });
-
-        $scope.$watch(function () {
-          return _this2.subscriber;
-        }, function (newVal, oldVal) {
-          if (!_this2.subscriber) {
-            return;
-          }
-          console.log(newVal, oldVal);
-          console.log('subscriber change', newVal === oldVal);
-          _this2.subscriber.uplink_speed = parseInt(_this2.subscriber.uplink_speed, 10) / mb;
-          _this2.subscriber.downlink_speed = parseInt(_this2.subscriber.downlink_speed, 10) / mb;
-        });
-
-        this.close = function () {
-          _this2.open = false;
-        };
-
-        this.updateSubscriber = function (subscriber) {
-
-          // TODO Copy the subscriber, this will update the GUI also and we don't want
-          // TODO Change GBps to MBps
-
-          var body = angular.copy(subscriber, body);
-
-          body.uplink_speed = body.uplink_speed * mb;
-          body.downlink_speed = body.downlink_speed * mb;
-
-          Subscribers.update(body).$promise.then(function (res) {
-            _this2.success = 'Subscriber successfully updated!';
-          })['catch'](function (e) {
-            _this2.formError = e;
-          })['finally'](function () {
-            $timeout(function () {
-              _this2.close();
-            }, 1500);
-          });
-        };
-      }]
-    };
-  });
-})();
-'use strict';
-
-(function () {
-  'use strict';
-
-  angular.module('xos.diagnostic').service('ServiceTopologyHelper', ["$rootScope", "$window", "$log", "lodash", "ServiceRelation", "serviceTopologyConfig", "d3", function ($rootScope, $window, $log, lodash, ServiceRelation, serviceTopologyConfig, d3) {
-
-    var _svg, _layout, _source, _el;
-
-    var i = 0;
-
-    // given a canvas, a layout and a data source, draw a tree layout
-    var updateTree = function updateTree(svg, layout, source) {
-      var el = arguments.length <= 3 || arguments[3] === undefined ? _el : arguments[3];
-
-      if (el) {
-        _el = el;
-      }
-
-      var targetWidth = _el.clientWidth - serviceTopologyConfig.widthMargin * 2;
-
-      //cache data
-      _svg = svg;
-      _layout = layout;
-      _source = source;
-
-      var maxDepth = ServiceRelation.depthOf(source);
-
-      var diagonal = d3.svg.diagonal().projection(function (d) {
-        return [d.y, d.x];
-      });
-
-      // Compute the new tree layout.
-      var nodes = layout.nodes(source).reverse(),
-          links = layout.links(nodes);
-
-      // Normalize for fixed-depth.
-      nodes.forEach(function (d) {
-        // position the child node horizontally
-        var step = (targetWidth - serviceTopologyConfig.widthMargin * 2) / (maxDepth - 1);
-        d.y = d.depth * step;
-      });
-
-      // Update the nodes…
-      var node = svg.selectAll('g.node').data(nodes, function (d) {
-        return d.id || (d.id = ++i);
-      });
-
-      // Enter any new nodes at the parent's previous position.
-      var nodeEnter = node.enter().append('g').attr({
-        'class': function _class(d) {
-          return 'node ' + d.type;
-        },
-        transform: function transform(d) {
-          return d.x && d.y ? 'translate(' + d.y + ', ' + d.x + ')' : 'translate(' + source.y0 + ', ' + source.x0 + ')';
-        }
-      });
-
-      var subscriberNodes = nodeEnter.filter('.subscriber');
-      var internetNodes = nodeEnter.filter('.router');
-      var serviceNodes = nodeEnter.filter('.service');
-
-      subscriberNodes.append('rect').attr(serviceTopologyConfig.square)
-      // add event listener to subscriber
-      .on('click', function () {
-        $rootScope.$emit('subscriber.modal.open');
-      });
-
-      internetNodes.append('rect').attr(serviceTopologyConfig.square);
-
-      serviceNodes.append('circle').attr('r', 1e-6).style('fill', function (d) {
-        return d._children ? 'lightsteelblue' : '#fff';
-      }).on('click', serviceClick);
-
-      nodeEnter.append('text').attr({
-        x: function x(d) {
-          return d.children ? -serviceTopologyConfig.circle.selectedRadius - 5 : serviceTopologyConfig.circle.selectedRadius + 5;
-        },
-        dy: '.35em',
-        y: function y(d) {
-          if (d.children && d.parent) {
-            return '-5';
-          }
-        },
-        transform: function transform(d) {
-          if (d.children && d.parent) {
-            if (d.parent.x < d.x) {
-              return 'rotate(-30)';
-            }
-            return 'rotate(30)';
-          }
-        },
-        'text-anchor': function textAnchor(d) {
-          return d.children ? 'end' : 'start';
-        }
-      }).text(function (d) {
-        return d.name;
-      }).style('fill-opacity', 1e-6);
-
-      // Transition nodes to their new position.
-      var nodeUpdate = node.transition().duration(serviceTopologyConfig.duration).attr({
-        'transform': function transform(d) {
-          return 'translate(' + d.y + ',' + d.x + ')';
-        }
-      });
-
-      nodeUpdate.select('circle').attr('r', function (d) {
-        return d.selected ? serviceTopologyConfig.circle.selectedRadius : serviceTopologyConfig.circle.radius;
-      }).style('fill', function (d) {
-        return d.selected ? 'lightsteelblue' : '#fff';
-      });
-
-      nodeUpdate.select('text').style('fill-opacity', 1);
-
-      // Transition exiting nodes to the parent's new position.
-      var nodeExit = node.exit().transition().duration(serviceTopologyConfig.duration).remove();
-
-      nodeExit.select('circle').attr('r', 1e-6);
-
-      nodeExit.select('text').style('fill-opacity', 1e-6);
-
-      // Update the links…
-      var link = svg.selectAll('path.link').data(links, function (d) {
-        return d.target.id;
-      });
-
-      // Enter any new links at the parent's previous position.
-      link.enter().insert('path', 'g').attr('class', function (d) {
-        return 'link ' + d.target.type + ' ' + (d.target.active ? '' : 'active');
-      }).attr('d', function (d) {
-        var o = { x: source.x0, y: source.y0 };
-        return diagonal({ source: o, target: o });
-      });
-
-      // Transition links to their new position.
-      link.transition().duration(serviceTopologyConfig.duration).attr('d', diagonal);
-
-      // Transition exiting nodes to the parent's new position.
-      link.exit().transition().duration(serviceTopologyConfig.duration).attr('d', function (d) {
-        var o = { x: source.x, y: source.y };
-        return diagonal({ source: o, target: o });
-      }).remove();
-
-      // Stash the old positions for transition.
-      nodes.forEach(function (d) {
-        d.x0 = d.x;
-        d.y0 = d.y;
-      });
-    };
-
-    var serviceClick = function serviceClick(d) {
-
-      // if was selected
-      if (d.selected) {
-        d.selected = !d.selected;
-        $rootScope.$emit('instance.detail.hide', {});
-        return updateTree(_svg, _layout, _source);
-      }
-
-      $rootScope.$emit('instance.detail', { name: d.name, service: d.service, tenant: d.tenant });
-
-      // unselect all
-      _svg.selectAll('circle').each(function (d) {
-        return d.selected = false;
-      });
-
-      // toggling selected status
-      d.selected = !d.selected;
-
-      updateTree(_svg, _layout, _source);
-    };
-
-    this.updateTree = updateTree;
-  }]);
-})();
-'use strict';
-
-(function () {
-  'use strict';
-
-  angular.module('xos.diagnostic').directive('serviceTopology', function () {
-    return {
-      restrict: 'E',
-      scope: {
-        serviceChain: '='
-      },
-      bindToController: true,
-      controllerAs: 'vm',
-      template: '',
-      controller: ["$element", "$window", "$scope", "d3", "serviceTopologyConfig", "ServiceRelation", "Slice", "Instances", "Subscribers", "ServiceTopologyHelper", function controller($element, $window, $scope, d3, serviceTopologyConfig, ServiceRelation, Slice, Instances, Subscribers, ServiceTopologyHelper) {
-        var _this = this;
-
-        var el = $element[0];
-
-        d3.select(window).on('resize.service', function () {
-          draw(_this.serviceChain);
-        });
-
-        var root, svg;
-
-        var draw = function draw(tree) {
-
-          if (!tree) {
-            console.error('Tree is missing');
-            return;
-          }
-
-          // TODO update instead clear and redraw
-
-          // clean
-          d3.select($element[0]).select('svg').remove();
-
-          var width = el.clientWidth - serviceTopologyConfig.widthMargin * 2;
-          var height = el.clientHeight - serviceTopologyConfig.heightMargin * 2;
-
-          var treeLayout = d3.layout.tree().size([height, width]);
-
-          svg = d3.select($element[0]).append('svg').style('width', el.clientWidth + 'px').style('height', el.clientHeight + 'px');
-
-          var treeContainer = svg.append('g').attr('transform', 'translate(' + serviceTopologyConfig.widthMargin * 2 + ',' + serviceTopologyConfig.heightMargin + ')');
-
-          root = tree;
-          root.x0 = height / 2;
-          root.y0 = width / 2;
-
-          // ServiceTopologyHelper.drawLegend(svg);
-          ServiceTopologyHelper.updateTree(treeContainer, treeLayout, root, el);
-        };
-
-        $scope.$watch(function () {
-          return _this.serviceChain;
-        }, function (chain) {
-          if (angular.isDefined(chain)) {
-            draw(chain);
-          }
-        });
-      }]
-    };
-  });
-})();
-'use strict';
-
-(function () {
-  'use strict';
-
-  angular.module('xos.diagnostic').service('Services', ["$resource", function ($resource) {
-    return $resource('/xos/services/:id', { id: '@id' });
-  }]).service('Tenant', ["$resource", function ($resource) {
-    return $resource('/xos/tenants', { id: '@id' }, {
-      queryVsgInstances: {
-        method: 'GET',
-        isArray: true,
-        interceptor: {
-          response: function response(res) {
-
-            // NOTE
-            // Note that VCPETenant is now VSGTenant.
-
-            var instances = [];
-
-            angular.forEach(res.data, function (tenant) {
-              var info = JSON.parse(tenant.service_specific_attribute);
-              if (info && info.instance_id) {
-                instances.push(info.instance_id);
-              }
-            });
-
-            return instances;
-          }
-        }
-      },
-      getSubscriberTag: {
-        method: 'GET',
-        isArray: true,
-        interceptor: {
-          response: function response(res) {
-            // NOTE we should receive only one vOLT tenant here
-            return JSON.parse(res.data[0].service_specific_attribute);
-          }
-        }
-      }
-    });
-  }]).service('Ceilometer', ["$http", "$q", "Instances", function ($http, $q, Instances) {
-    var _this = this;
-
-    /**
-    * Get stats for a single instance
-    */
-    this.getInstanceStats = function (instanceUuid) {
-      var deferred = $q.defer();
-
-      $http.get('/xoslib/xos-instance-statistics', { params: { 'instance-uuid': instanceUuid } }).then(function (res) {
-        deferred.resolve(res.data);
-      })['catch'](function (e) {
-        deferred.reject(e);
-      });
-
-      return deferred.promise;
-    };
-
-    /**
-    * Collect stats for an array of instances
-    */
-    this.getInstancesStats = function (instances) {
-      var deferred = $q.defer();
-      var instancePromises = [];
-      var instanceList = [];
-
-      // retrieve instance details
-      instances.forEach(function (instanceId) {
-        instancePromises.push(Instances.get({ id: instanceId }).$promise);
-      });
-
-      // get all instance data
-      $q.all(instancePromises).then(function (_instanceList) {
-        instanceList = _instanceList;
-        var promises = [];
-        // foreach instance query stats
-        instanceList.forEach(function (instance) {
-          promises.push(_this.getInstanceStats(instance.instance_uuid));
-        });
-        return $q.all(promises);
-      }).then(function (stats) {
-        // augment instance with stats information
-        instanceList.map(function (instance, i) {
-          instance.stats = stats[i];
-        });
-        deferred.resolve(instanceList);
-      })['catch'](deferred.reject);
-
-      return deferred.promise;
-    };
-
-    this.getContainerStats = function (containerName) {
-      var deferred = $q.defer();
-
-      var res = {};
-
-      $http.get('/xoslib/meterstatistics', { params: { 'resource': containerName } }).then(function (containerStats) {
-        res.stats = containerStats.data;
-        return $http.get('/xoslib/meterstatistics', { params: { 'resource': containerName + '-eth0' } });
-      }).then(function (portStats) {
-        res.port = {
-          eth0: portStats.data
-        };
-        return $http.get('/xoslib/meterstatistics', { params: { 'resource': containerName + '-eth1' } });
-      }).then(function (portStats) {
-        res.port.eth1 = portStats.data;
-        deferred.resolve(res);
-      })['catch'](function (e) {
-        deferred.reject(e);
-      });
-
-      return deferred.promise;
-    };
-  }]).service('Slice', ["$resource", function ($resource) {
-    return $resource('/xos/slices', { id: '@id' });
-  }]).service('Instances', ["$resource", function ($resource) {
-    return $resource('/xos/instances/:id', { id: '@id' });
-  }]).service('Node', ["$resource", "$q", "Instances", function ($resource, $q, Instances) {
-    return $resource('/xos/nodes', { id: '@id' }, {
-      queryWithInstances: {
-        method: 'GET',
-        isArray: true,
-        interceptor: {
-          response: function response(res) {
-
-            // TODO update the API to include instances in nodes
-            // http://stackoverflow.com/questions/14573102/how-do-i-include-related-model-fields-using-django-rest-framework
-
-            var deferred = $q.defer();
-
-            var requests = [];
-
-            angular.forEach(res.data, function (node) {
-              requests.push(Instances.query({ node: node.id }).$promise);
-            });
-
-            $q.all(requests).then(function (list) {
-              res.data.map(function (node, i) {
-                node.instances = list[i];
-                return node;
-              });
-              deferred.resolve(res.data);
-            });
-
-            return deferred.promise;
-          }
-        }
-      }
-    });
-  }]).service('Subscribers', ["$resource", "$q", "SubscriberDevice", function ($resource, $q, SubscriberDevice) {
-    return $resource('/xoslib/cordsubscriber/:id', { id: '@id' }, {
-      update: {
-        method: 'PUT',
-        isArray: false
-      },
-      queryWithDevices: {
-        method: 'GET',
-        isArray: true,
-        interceptor: {
-          response: function response(res) {
-
-            /**
-            * For each subscriber retrieve devices and append them
-            */
-
-            var deferred = $q.defer();
-
-            var requests = [];
-
-            angular.forEach(res.data, function (subscriber) {
-              requests.push(SubscriberDevice.query({ id: subscriber.id }).$promise);
-            });
-
-            $q.all(requests).then(function (list) {
-
-              // adding devices
-
-              res.data.map(function (subscriber, i) {
-                subscriber.devices = list[i];
-                subscriber.type = 'subscriber';
-
-                subscriber.devices.map(function (d) {
-                  return d.type = 'device';
-                });
-
-                return subscriber;
-              });
-
-              // faking to have 2 subscriber
-              // res.data.push(angular.copy(res.data[0]));
-
-              deferred.resolve(res.data);
-            });
-
-            return deferred.promise;
-          }
-        }
-      },
-      getWithDevices: {
-        method: 'GET',
-        isArray: false,
-        interceptor: {
-          response: function response(res) {
-            var d = $q.defer();
-
-            SubscriberDevice.query({ id: res.data.id }).$promise.then(function (devices) {
-              devices.map(function (d) {
-                return d.type = 'device';
-              });
-              res.data.devices = devices;
-              res.data.type = 'subscriber';
-              d.resolve(res.data);
-            })['catch'](function (err) {
-              d.reject(err);
-            });
-
-            return d.promise;
-          }
-        }
-      }
-    });
-  }]).service('SubscriberDevice', ["$resource", function ($resource) {
-    return $resource('/xoslib/rs/subscriber/:id/users/', { id: '@id' });
-  }]).service('ServiceRelation', ["$q", "lodash", "Services", "Tenant", "Slice", "Instances", function ($q, lodash, Services, Tenant, Slice, Instances) {
-
-    // count the mas depth of an object
-    var depthOf = function depthOf(obj) {
-      var depth = 0;
-      if (obj.children) {
-        obj.children.forEach(function (d) {
-          var tmpDepth = depthOf(d);
-          if (tmpDepth > depth) {
-            depth = tmpDepth;
-          }
-        });
-      }
-      return 1 + depth;
-    };
-
-    // find all the relation defined for a given root
-    var findLevelRelation = function findLevelRelation(tenants, rootId) {
-      return lodash.filter(tenants, function (service) {
-        return service.subscriber_service === rootId;
-      });
-    };
-
-    var findSpecificInformation = function findSpecificInformation(tenants, rootId) {
-      var tenants = lodash.filter(tenants, function (service) {
-        return service.provider_service === rootId && service.subscriber_tenant;
-      });
-
-      var info;
-
-      tenants.forEach(function (tenant) {
-        if (tenant.service_specific_attribute) {
-          info = JSON.parse(tenant.service_specific_attribute);
-        }
-      });
-
-      return info;
-    };
-
-    // find all the service defined by a given array of relations
-    var findLevelServices = function findLevelServices(relations, services) {
-      var levelServices = [];
-      lodash.forEach(relations, function (tenant) {
-        var service = lodash.find(services, { id: tenant.provider_service });
-        levelServices.push(service);
-      });
-      return levelServices;
-    };
-
-    var buildLevel = function buildLevel(tenants, services, rootService, rootTenant) {
-      var parentName = arguments.length <= 4 || arguments[4] === undefined ? null : arguments[4];
-
-      // build an array of unlinked services
-      // these are the services that should still placed in the tree
-      var unlinkedServices = lodash.difference(services, [rootService]);
-
-      // find all relations relative to this rootElement
-      var levelRelation = findLevelRelation(tenants, rootService.id);
-      // find all items related to rootElement
-      var levelServices = findLevelServices(levelRelation, services);
-
-      // remove this item from the list (performance
-      unlinkedServices = lodash.difference(unlinkedServices, levelServices);
-
-      rootService.service_specific_attribute = findSpecificInformation(tenants, rootService.id);
-
-      if (rootService.humanReadableName === 'service_vbng') {
-        rootService.humanReadableName = 'service_vrouter';
-      }
-
-      var tree = {
-        name: rootService.humanReadableName,
-        parent: parentName,
-        type: 'service',
-        service: rootService,
-        tenant: rootTenant,
-        children: []
-      };
-
-      lodash.forEach(levelServices, function (service) {
-        if (service.humanReadableName === 'service_ONOS_vBNG' || service.humanReadableName === 'service_ONOS_vOLT') {
-          // remove ONOSes from service chart
-          return;
-        }
-        var tenant = lodash.find(tenants, { subscriber_tenant: rootTenant.id, provider_service: service.id });
-        tree.children.push(buildLevel(tenants, unlinkedServices, service, tenant, rootService.humanReadableName));
-      });
-
-      // if it is the last element append internet
-      if (tree.children.length === 0) {
-        tree.children.push({
-          name: 'Router',
-          type: 'router',
-          children: []
-        });
-      }
-
-      return tree;
-    };
-
-    var buildSubscriberServiceTree = function buildSubscriberServiceTree(services, tenants) {
-      var subscriber = arguments.length <= 2 || arguments[2] === undefined ? { id: 1, name: 'fakeSubs' } : arguments[2];
-
-      // find the root service
-      // it is the one attached to subsriber_root
-      // as now we have only one root so this can work
-      var rootTenant = lodash.find(tenants, { subscriber_root: subscriber.id });
-      var rootService = lodash.find(services, { id: rootTenant.provider_service });
-
-      var serviceTree = buildLevel(tenants, services, rootService, rootTenant);
-
-      return {
-        name: subscriber.name || subscriber.humanReadableName,
-        parent: null,
-        type: 'subscriber',
-        children: [serviceTree]
-      };
-    };
-
-    // applying domain knowledge to build the global service tree
-    var buildServiceTree = function buildServiceTree(services, tenants) {
-
-      // TODO refactor
-      var buildChild = function buildChild(services, tenants, currentService) {
-
-        if (currentService.humanReadableName === 'service_vbng') {
-          currentService.humanReadableName = 'service_vrouter';
-        }
-
-        var response = {
-          type: 'service',
-          name: currentService.humanReadableName,
-          service: currentService
-        };
-
-        var tenant = lodash.find(tenants, { subscriber_service: currentService.id });
-        if (tenant) {
-          var next = lodash.find(services, { id: tenant.provider_service });
-          response.children = [buildChild(services, tenants, next)];
-        } else {
-          response.children = [{
-            name: 'Router',
-            type: 'router',
-            children: []
-          }];
-        }
-        delete currentService.id; // conflict with d3
-        return response;
-      };
-
-      var baseService = lodash.find(services, { id: 3 });
-
-      if (!angular.isDefined(baseService)) {
-        console.error('Missing Base service!');
-        return;
-      }
-
-      var baseData = {
-        name: 'Subscriber',
-        type: 'subscriber',
-        parent: null,
-        children: [buildChild(services, tenants, baseService)]
-      };
-      return baseData;
-    };
-
-    var getBySubscriber = function getBySubscriber(subscriber) {
-      var deferred = $q.defer();
-      var services, tenants;
-      Services.query().$promise.then(function (res) {
-        services = res;
-        return Tenant.query().$promise;
-      }).then(function (res) {
-        tenants = res;
-        deferred.resolve(buildSubscriberServiceTree(services, tenants, subscriber));
-      })['catch'](function (e) {
-        throw new Error(e);
-      });
-
-      return deferred.promise;
-    };
-
-    var get = function get() {
-      var deferred = $q.defer();
-      var services, tenants;
-      Services.query().$promise.then(function (res) {
-        services = res;
-        return Tenant.query({ kind: 'coarse' }).$promise;
-      }).then(function (res) {
-        tenants = res;
-        deferred.resolve(buildServiceTree(services, tenants));
-      })['catch'](function (e) {
-        throw new Error(e);
-      });
-
-      return deferred.promise;
-    };
-
-    // export APIs
-    return {
-      get: get,
-      buildServiceTree: buildServiceTree,
-      getBySubscriber: getBySubscriber,
-      buildLevel: buildLevel,
-      buildSubscriberServiceTree: buildSubscriberServiceTree,
-      findLevelRelation: findLevelRelation,
-      findLevelServices: findLevelServices,
-      depthOf: depthOf,
-      findSpecificInformation: findSpecificInformation
-    };
-  }]);
-})();
-'use strict';
-
-var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i['return']) _i['return'](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError('Invalid attempt to destructure non-iterable instance'); } }; })();
-
-(function () {
-  angular.module('xos.diagnostic').service('RackHelper', ["serviceTopologyConfig", "lodash", function (serviceTopologyConfig, lodash) {
-    var _this = this;
-
-    this.getComputeNodeLabelSize = function () {
-      return serviceTopologyConfig.computeNode.labelHeight + serviceTopologyConfig.instance.margin * 2;
-    };
-
-    /**
-    * Given a list of instance should get the Compute Node size.
-    * They are placed in rows of 2 with 5px margin on each side.
-    */
-
-    this.getComputeNodeSize = lodash.memoize(function (instances) {
-      var width = serviceTopologyConfig.instance.margin * 3 + serviceTopologyConfig.instance.width * 2;
-
-      var rows = Math.round(instances.length / 2);
-
-      var labelSpace = _this.getComputeNodeLabelSize();
-
-      var height = serviceTopologyConfig.instance.height * rows + serviceTopologyConfig.instance.margin * (rows + 1) + labelSpace;
-
-      return [width, height];
-    });
-
-    /**
-    * Give a list on Compute Node should calculate the Rack Size.
-    * Compute nodes are placed in a single column with 5px margin on each side.
-    */
-    this.getRackSize = function (nodes) {
-
-      var width = 0;
-      var height = serviceTopologyConfig.computeNode.margin;
-
-      lodash.forEach(nodes, function (node) {
-        var _getComputeNodeSize = _this.getComputeNodeSize(node.instances);
-
-        var _getComputeNodeSize2 = _slicedToArray(_getComputeNodeSize, 2);
-
-        var nodeWidth = _getComputeNodeSize2[0];
-        var nodeHeight = _getComputeNodeSize2[1];
-
-        width = nodeWidth + serviceTopologyConfig.computeNode.margin * 2;
-        height += nodeHeight + serviceTopologyConfig.computeNode.margin;
-      });
-
-      return [width, height];
-    };
-
-    /**
-    * Given an instance index, return the coordinates
-    */
-
-    this.getInstancePosition = function (position) {
-      var row = Math.floor(position / 2);
-      var column = position % 2 ? 1 : 0;
-
-      // add ComputeNode label size
-      var labelSpace = _this.getComputeNodeLabelSize();
-
-      // x = margin + (width * column) + ( maring * column)
-      var x = serviceTopologyConfig.instance.margin + serviceTopologyConfig.instance.width * column + serviceTopologyConfig.instance.margin * column;
-
-      // y = label + margin + (height * row) + ( maring * row)
-      var y = labelSpace + serviceTopologyConfig.instance.margin + serviceTopologyConfig.instance.height * row + serviceTopologyConfig.instance.margin * row;
-      return [x, y];
-    };
-
-    /**
-    * Given an Compute Node index, return the coordinates
-    */
-
-    this.getComputeNodePosition = function (nodes, position) {
-
-      var x = serviceTopologyConfig.computeNode.margin;
-
-      var previousElEight = lodash.reduce(nodes.slice(0, position), function (val, node) {
-        return val + _this.getComputeNodeSize(node.instances)[1];
-      }, 0);
-
-      var y = serviceTopologyConfig.computeNode.margin + serviceTopologyConfig.computeNode.margin * position + previousElEight;
-
-      return [x, y];
-    };
-  }]);
-})();
-'use strict';
-
-var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i['return']) _i['return'](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError('Invalid attempt to destructure non-iterable instance'); } }; })();
-
-(function () {
-  'use strict';
-
-  var shapes = {
-    cloud: ' M 79.72 49.60 C 86.00 37.29 98.57 29.01 111.96 26.42 C 124.27 24.11 137.53 26.15 148.18 32.90 C 158.08 38.78 165.39 48.87 167.65 60.20 C 176.20 57.90 185.14 56.01 194.00 57.73 C 206.08 59.59 217.92 66.01 224.37 76.66 C 227.51 81.54 228.85 87.33 229.23 93.06 C 237.59 93.33 246.22 95.10 253.04 100.19 C 256.69 103.13 259.87 107.67 258.91 112.59 C 257.95 118.43 252.78 122.38 247.78 124.82 C 235.27 130.43 220.23 130.09 207.98 123.93 C 199.33 127.88 189.76 129.43 180.30 128.57 C 173.70 139.92 161.70 147.65 148.86 149.93 C 133.10 153.26 116.06 148.15 104.42 137.08 C 92.98 143.04 78.96 143.87 66.97 139.04 C 57.75 135.41 49.70 128.00 46.60 118.43 C 43.87 109.95 45.81 100.29 51.30 93.32 C 57.38 85.18 67.10 80.44 76.99 78.89 C 74.38 69.20 74.87 58.52 79.72 49.60 Z'
-  };
-
-  var computeNodeId = 0;
-  var instanceId = 0;
-
-  angular.module('xos.diagnostic').service('NodeDrawer', ["d3", "serviceTopologyConfig", "RackHelper", "lodash", function (d3, serviceTopologyConfig, RackHelper, lodash) {
-    var _this2 = this;
-
-    var _this = this;
-
-    this.addNetworks = function (nodes) {
-
-      // clean childs
-      nodes.selectAll('*').remove();
-
-      nodes.append('path').attr({
-        d: shapes.cloud,
-        transform: 'translate(-100, -72), scale(0.7)',
-        'class': 'cloud'
-      });
-
-      nodes.append('text').attr({
-        'text-anchor': 'middle',
-        y: -5,
-        x: 5
-      }).text(function (d) {
-        return d.name;
-      });
-
-      nodes.append('text').attr({
-        'text-anchor': 'middle',
-        y: 8,
-        x: 5,
-        'class': 'small'
-      }).text(function (d) {
-        return d.subtitle;
-      });
-
-      nodes.each(function (n) {
-        var currentNode = d3.select(this);
-        // cicle trouch node to add Tags and Public IP
-        if (n.name === 'LAN-Side' && angular.isDefined(n.subscriberTag)) {
-          currentNode.append('text').attr({
-            'text-anchor': 'middle',
-            y: 50
-          }).text(function () {
-            return 'C-Tag: ' + n.subscriberTag.cTag;
-          });
-
-          currentNode.append('text').attr({
-            'text-anchor': 'middle',
-            y: 70
-          }).text(function () {
-            return 'S-Tag: ' + n.subscriberTag.sTag;
-          });
-        }
-
-        if (n.name === 'WAN-Side' && angular.isDefined(n.subscriberIP)) {
-          currentNode.append('text').attr({
-            'text-anchor': 'middle',
-            y: 50
-          }).text(function () {
-            return 'Public IP: ' + n.subscriberIP;
-          });
-        }
-      });
-    };
-
-    this.addRack = function (nodes) {
-
-      // loop because of D3
-      // rack will be only one
-      nodes.each(function (d) {
-        var _RackHelper$getRackSize = RackHelper.getRackSize(d.computeNodes);
-
-        var _RackHelper$getRackSize2 = _slicedToArray(_RackHelper$getRackSize, 2);
-
-        var w = _RackHelper$getRackSize2[0];
-        var h = _RackHelper$getRackSize2[1];
-
-        // TODO update instead of delete and redraw
-        nodes.select('g').remove();
-
-        var rack = nodes.append('g');
-
-        rack.attr({
-          transform: 'translate(0,0)'
-        }).transition().duration(serviceTopologyConfig.duration).attr({
-          transform: function transform() {
-            return 'translate(' + -(w / 2) + ', ' + -(h / 2) + ')';
-          }
-        });
-
-        rack.append('rect').attr({
-          width: 0,
-          height: 0
-        }).transition().duration(serviceTopologyConfig.duration).attr({
-          width: w,
-          height: h
-        });
-
-        rack.append('text').attr({
-          'text-anchor': 'middle',
-          y: -10,
-          x: w / 2,
-          opacity: 0
-        }).text(function (d) {
-          return d.name;
-        }).transition().duration(serviceTopologyConfig.duration).attr({
-          opacity: 1
-        });
-
-        _this2.drawComputeNodes(rack, d.computeNodes);
-      });
-    };
-
-    this.drawComputeNodes = function (container, nodes) {
-
-      var elements = container.selectAll('.compute-nodes').data(nodes, function (d) {
-        if (!angular.isString(d.d3Id)) {
-          d.d3Id = 'compute-node-' + ++computeNodeId;
-        }
-        return d.d3Id;
-      });
-
-      var _container$node$getBoundingClientRect = container.node().getBoundingClientRect();
-
-      var width = _container$node$getBoundingClientRect.width;
-      var height = _container$node$getBoundingClientRect.height;
-
-      var nodeContainer = elements.enter().append('g');
-
-      nodeContainer.attr({
-        transform: 'translate(' + width / 2 + ', ' + height / 2 + ')',
-        'class': 'compute-node'
-      }).transition().duration(serviceTopologyConfig.duration).attr({
-        transform: function transform(d) {
-          return 'translate(' + RackHelper.getComputeNodePosition(nodes, d.d3Id.replace('compute-node-', '') - 1) + ')';
-        }
-      });
-
-      nodeContainer.append('rect').attr({
-        width: 0,
-        height: 0
-      }).transition().duration(serviceTopologyConfig.duration).attr({
-        width: function width(d) {
-          return RackHelper.getComputeNodeSize(d.instances)[0];
-        },
-        height: function height(d) {
-          return RackHelper.getComputeNodeSize(d.instances)[1];
-        }
-      });
-
-      nodeContainer.append('text').attr({
-        'text-anchor': 'start',
-        y: 17, //FIXME
-        x: 10, //FIXME
-        opacity: 0
-      }).text(function (d) {
-        return d.humanReadableName.split('.')[0];
-      }).transition().duration(serviceTopologyConfig.duration).attr({
-        opacity: 1
-      });
-
-      // if there are Compute Nodes
-      if (nodeContainer.length > 0) {
-        // draw instances for each compute node
-        nodeContainer.each(function (a) {
-          _this.drawInstances(d3.select(this), a.instances);
-        });
-      }
-    };
-
-    // NOTE Stripping unuseful names to shorten labels.
-    // This is not elegant
-    var formatInstanceName = function formatInstanceName(name) {
-      return name.replace('app_', '').replace('service_', '')
-      // .replace('ovs_', '')
-      .replace('mysite_', '').replace('_instance', '');
-    };
-
-    var getInstanceStatusColor = function getInstanceStatusColor(instance) {
-      function startWith(val, string) {
-        return string.substring(0, val.length) === val;
-      }
-
-      if (startWith('0 - ', instance.backend_status)) {
-        return 'provisioning';
-      }
-      if (startWith('1 - ', instance.backend_status)) {
-        return 'good';
-      }
-      if (startWith('2 - ', instance.backend_status)) {
-        return 'bad';
-      } else {
-        return '';
-      }
-    };
-
-    var drawContainer = function drawContainer(container, docker) {
-
-      var containerBox = container.append('g').attr({
-        'class': 'container',
-        transform: 'translate(' + serviceTopologyConfig.instance.margin + ', 115)'
-      });
-
-      containerBox.append('rect').attr({
-        width: 250 - serviceTopologyConfig.container.margin * 2,
-        height: serviceTopologyConfig.container.height
-      });
-
-      containerBox.append('text').attr({
-        y: 20,
-        x: serviceTopologyConfig.instance.margin,
-        'class': 'name'
-      }).text(docker.name);
-
-      // add stats
-      var interestingMeters = ['memory', 'memory.usage', 'cpu_util'];
-
-      interestingMeters.forEach(function (m, i) {
-        var meter = lodash.find(docker.stats, { meter: m });
-        // if there is no meter stats skip rendering
-        if (!angular.isDefined(meter)) {
-          return;
-        }
-        containerBox.append('text').attr({
-          y: 40 + i * 15,
-          x: serviceTopologyConfig.instance.margin,
-          opacity: 0
-        }).text(meter.description + ': ' + Math.round(meter.value) + ' ' + meter.unit).transition().duration(serviceTopologyConfig.duration).attr({
-          opacity: 1
-        });
-      });
-
-      // add port stats
-      var ports = ['eth0', 'eth1'];
-      var interestingPortMeters = [{
-        meter: 'network.incoming.bytes.rate',
-        label: 'Incoming'
-      }, {
-        meter: 'network.outgoing.bytes.rate',
-        label: 'Outgoing'
-      }];
-
-      ports.forEach(function (p, j) {
-
-        // if there are no port stats skip rendering
-        if (docker.port[p].length === 0) {
-          return;
-        }
-
-        containerBox.append('text').attr({
-          y: 90,
-          x: serviceTopologyConfig.instance.margin + 120 * j,
-          'class': 'name'
-        }).text(docker.name + '-' + p);
-
-        interestingPortMeters.forEach(function (m, i) {
-
-          var meter = lodash.find(docker.port[p], { meter: m.meter });
-          // if there is no meter stats skip rendering
-          if (!angular.isDefined(meter)) {
-            return;
-          }
-          containerBox.append('text').attr({
-            y: 105 + i * 15,
-            x: serviceTopologyConfig.instance.margin + 120 * j,
-            opacity: 0
-          }).text(m.label + ': ' + Math.round(meter.value) + ' ' + meter.unit).transition().duration(serviceTopologyConfig.duration).attr({
-            opacity: 1
-          });
-        });
-      });
-    };
-
-    var showInstanceStats = function showInstanceStats(container, instance) {
-
-      // NOTE this should be dinamically positioned
-      // base on the number of element present
-
-      // fake the position
-      var translation = {
-        'mysite_vsg-1': '200, -120',
-        'mysite_vsg-2': '-300, 30',
-        'mysite_vsg-3': '-300, -250'
-      };
-
-      var statsContainer = container.append('g').attr({
-        transform: 'translate(' + (translation[instance.humanReadableName] || translation['mysite_vsg-1']) + ')',
-        'class': 'stats-container'
-      }).on('click', function (d) {
-        // toggling visisbility
-        d.fade = !d.fade;
-        var opacity = undefined;
-        if (d.fade) {
-          opacity = 0.1;
-        } else {
-          opacity = 1;
-        }
-
-        d3.select(this).transition().duration(serviceTopologyConfig.duration).attr({
-          opacity: opacity
-        });
-      });
-
-      var lines = {
-        'mysite_vsg-1': {
-          x1: -160,
-          y1: 120,
-          x2: 0,
-          y2: 50
-        },
-        'mysite_vsg-2': {
-          x1: 250,
-          y1: 50,
-          x2: 300,
-          y2: -10
-        },
-        'mysite_vsg-3': {
-          x1: 250,
-          y1: 50,
-          x2: 300,
-          y2: 270
-        }
-      };
-
-      statsContainer.append('line').attr({
-        x1: function x1(d) {
-          return lines[d.humanReadableName].x1 || lines['mysite_vsg-1'].x1;
-        },
-        y1: function y1(d) {
-          return lines[d.humanReadableName].y1 || lines['mysite_vsg-1'].y1;
-        },
-        x2: function x2(d) {
-          return lines[d.humanReadableName].x2 || lines['mysite_vsg-1'].x2;
-        },
-        y2: function y2(d) {
-          return lines[d.humanReadableName].y2 || lines['mysite_vsg-1'].y2;
-        },
-        stroke: 'black',
-        opacity: 0
-      }).transition().duration(serviceTopologyConfig.duration).attr({
-        opacity: 1
-      });
-
-      // NOTE rect should be dinamically sized base on the presence of a container
-      var statsHeight = 110;
-      var statsWidth = 250;
-
-      if (instance.container) {
-        statsHeight += serviceTopologyConfig.container.height + serviceTopologyConfig.container.margin * 2;
-      }
-
-      var statsVignette = statsContainer.append('rect').attr({
-        width: statsWidth,
-        height: statsHeight,
-        opacity: 0
-      }).transition().duration(serviceTopologyConfig.duration).attr({
-        opacity: 1
-      });
-
-      // add instance info
-      statsContainer.append('text').attr({
-        y: 15,
-        x: serviceTopologyConfig.instance.margin,
-        'class': 'name',
-        opacity: 0
-      }).text(instance.humanReadableName).transition().duration(serviceTopologyConfig.duration).attr({
-        opacity: 1
-      });
-
-      statsContainer.append('text').attr({
-        y: 30,
-        x: serviceTopologyConfig.instance.margin,
-        'class': 'ip',
-        opacity: 0
-      }).text(instance.ip).transition().duration(serviceTopologyConfig.duration).attr({
-        opacity: 1
-      });
-
-      // add stats
-      var interestingMeters = ['memory', 'memory.usage', 'cpu', 'cpu_util'];
-
-      interestingMeters.forEach(function (m, i) {
-        var meter = lodash.find(instance.stats, { meter: m });
-
-        if (meter) {
-
-          statsContainer.append('text').attr({
-            y: 55 + i * 15,
-            x: serviceTopologyConfig.instance.margin,
-            opacity: 0
-          }).text(meter.description + ': ' + Math.round(meter.value) + ' ' + meter.unit).transition().duration(serviceTopologyConfig.duration).attr({
-            opacity: 1
-          });
-        }
-      });
-
-      if (instance.container) {
-        // draw container
-        drawContainer(statsContainer, instance.container);
-      }
-    };
-
-    this.drawInstances = function (container, instances) {
-
-      // TODO check for stats field in instance and draw popup
-
-      var _container$node$getBoundingClientRect2 = container.node().getBoundingClientRect();
-
-      var width = _container$node$getBoundingClientRect2.width;
-      var height = _container$node$getBoundingClientRect2.height;
-
-      var elements = container.selectAll('.instances').data(instances, function (d) {
-        return angular.isString(d.d3Id) ? d.d3Id : d.d3Id = 'instance-' + ++instanceId;
-      });
-
-      var instanceContainer = elements.enter().append('g');
-
-      instanceContainer.attr({
-        transform: 'translate(' + width / 2 + ', ' + height / 2 + ')',
-        'class': function _class(d) {
-          return 'instance ' + (d.selected ? 'active' : '') + ' ' + getInstanceStatusColor(d);
-        }
-      }).transition().duration(serviceTopologyConfig.duration).attr({
-        transform: function transform(d, i) {
-          return 'translate(' + RackHelper.getInstancePosition(i) + ')';
-        }
-      });
-
-      instanceContainer.append('rect').attr({
-        width: 0,
-        height: 0
-      }).transition().duration(serviceTopologyConfig.duration).attr({
-        width: serviceTopologyConfig.instance.width,
-        height: serviceTopologyConfig.instance.height
-      });
-
-      instanceContainer.append('text').attr({
-        'text-anchor': 'middle',
-        y: 23, //FIXME
-        x: 40, //FIXME
-        opacity: 0
-      }).text(function (d) {
-        return formatInstanceName(d.humanReadableName);
-      }).transition().duration(serviceTopologyConfig.duration).attr({
-        opacity: 1
-      });
-
-      // if stats are attached and instance is active,
-      // draw stats
-      instanceContainer.each(function (instance, i) {
-
-        var container = d3.select(this);
-
-        if (angular.isDefined(instance.stats) && instance.selected) {
-          showInstanceStats(container, instance, i);
-        }
-      });
-
-      // instanceContainer
-      // .on('click', function(d){
-      //   console.log(`Draw vignette with stats for instance: ${d.name}`);
-      // });
-    };
-
-    this.addPhisical = function (nodes) {
-
-      nodes.select('rect').remove();
-      nodes.select('text').remove();
-
-      nodes.append('rect').attr(serviceTopologyConfig.square);
-
-      nodes.append('text').attr({
-        'text-anchor': 'middle',
-        y: serviceTopologyConfig.square.y - 10
-      }).text(function (d) {
-        return d.name || d.humanReadableName;
-      });
-    };
-
-    this.addDevice = function (nodes) {
-      nodes.append('circle').attr(serviceTopologyConfig.circle);
-
-      nodes.append('text').attr({
-        'text-anchor': 'end',
-        x: -serviceTopologyConfig.circle.r - 10,
-        y: serviceTopologyConfig.circle.r / 2
-      }).text(function (d) {
-        return d.name || d.mac;
-      });
-    };
-  }]);
-})();
-'use strict';
-
-var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i['return']) _i['return'](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError('Invalid attempt to destructure non-iterable instance'); } }; })();
-
-(function () {
-  'use strict';
-
-  angular.module('xos.diagnostic').service('LogicTopologyHelper', ["$window", "$log", "$rootScope", "lodash", "serviceTopologyConfig", "NodeDrawer", "ChartData", function ($window, $log, $rootScope, lodash, serviceTopologyConfig, NodeDrawer, ChartData) {
-    var _this = this;
-
-    var diagonal,
-        nodes,
-        links,
-        i = 0,
-        svgWidth,
-        svgHeight,
-        layout;
-
-    var baseData = ChartData.logicTopologyData;
-
-    /**
-     * Calculate the horizontal position for each element.
-     * subsrcribers, devices and routers have the same fixed width 20
-     * network have a fixed width 104
-     * rack have a fixed width 105
-     * build and array of 6 elements representing the position of each element in the svg
-     * to equally space them
-     */
-
-    this.computeElementPosition = function (svgWidth) {
-
-      var xPos = [];
-
-      var totalElWidth = lodash.reduce(serviceTopologyConfig.elWidths, function (el, val) {
-        return val + el;
-      }, 0);
-
-      var remainingSpace = svgWidth - totalElWidth - serviceTopologyConfig.widthMargin * 2;
-
-      var step = remainingSpace / (serviceTopologyConfig.elWidths.length - 1);
-
-      lodash.forEach(serviceTopologyConfig.elWidths, function (el, i) {
-
-        // get half of the previous elements width
-        var previousElWidth = 0;
-        if (i !== 0) {
-          previousElWidth = lodash.reduce(serviceTopologyConfig.elWidths.slice(0, i), function (el, val) {
-            return val + el;
-          }, 0);
-        }
-
-        var elPos = serviceTopologyConfig.widthMargin // right margin
-         + step * i // space between elements
-         + el / 2 // this el width
-         + previousElWidth; // previous elements width
-
-        xPos.push(svgWidth - elPos);
-      });
-
-      return xPos;
-    };
-
-    /**
-    * from a nested data structure,
-    * create nodes and links for a D3 Tree Layout
-    */
-    var computeLayout = function computeLayout(data) {
-      var nodes = layout.nodes(data);
-
-      // Normalize for fixed-depth.
-      nodes.forEach(function (d) {
-        // position the child node horizontally
-        d.y = _this.computeElementPosition(svgWidth)[d.depth];
-      });
-
-      var links = layout.links(nodes);
-
-      return [nodes, links];
-    };
-
-    /**
-    * Draw the containing group for any node or update the existing one
-    */
-    var drawNodes = function drawNodes(svg, nodes) {
-      // Update the nodes…
-      var node = svg.selectAll('g.node').data(nodes, function (d) {
-        if (!angular.isString(d.d3Id)) {
-          d.d3Id = 'tree-' + ++i;
-        }
-        return d.d3Id;
-      });
-
-      // Enter any new nodes
-      var nodeEnter = node.enter().append('g').attr({
-        'class': function _class(d) {
-          return 'node ' + d.type;
-        },
-        transform: 'translate(' + svgWidth / 2 + ', ' + svgHeight / 2 + ')'
-      });
-
-      // create Nodes
-      NodeDrawer.addNetworks(node.filter('.network'));
-      NodeDrawer.addRack(node.filter('.rack'));
-      NodeDrawer.addPhisical(node.filter('.router'));
-      NodeDrawer.addPhisical(node.filter('.subscriber'));
-      NodeDrawer.addDevice(node.filter('.device'));
-
-      // add event listener to subscriber
-      node.filter('.subscriber').on('click', function () {
-        $rootScope.$emit('subscriber.modal.open');
-      });
-
-      //update nodes
-      // TODO if data change, only update them
-      // NodeDrawer.updateRack(node.filter('.rack'));
-
-      // Transition nodes to their new position.
-      var nodeUpdate = node.transition().duration(serviceTopologyConfig.duration).attr({
-        'transform': function transform(d) {
-          return 'translate(' + d.y + ',' + d.x + ')';
-        }
-      });
-
-      // TODO handle node remove
-      var nodeExit = node.exit().remove();
-    };
-
-    /**
-    * Handle links in the tree layout
-    */
-    var drawLinks = function drawLinks(svg, links) {
-
-      diagonal = d3.svg.diagonal().projection(function (d) {
-        return [d.y, d.x];
-      });
-
-      // Update the links…
-      var link = svg.selectAll('path.link').data(links, function (d) {
-        return d.target.d3Id;
-      });
-
-      // Enter any new links at the parent's previous position.
-      link.enter().insert('path', 'g').attr('class', function (d) {
-        return 'link ' + d.target.type;
-      }).attr('d', function (d) {
-        var o = { x: svgHeight / 2, y: svgWidth / 2 };
-        return diagonal({ source: o, target: o });
-      });
-
-      // Transition links to their new position.
-      link.transition().duration(serviceTopologyConfig.duration).attr('d', diagonal);
-
-      link.exit().remove();
-    };
-
-    /**
-    * Calculate the svg size and setup tree layout
-    */
-    this.setupTree = function (svg) {
-
-      svgWidth = svg.node().getBoundingClientRect().width;
-      svgHeight = svg.node().getBoundingClientRect().height;
-
-      var width = svgWidth - serviceTopologyConfig.widthMargin * 2;
-      var height = svgHeight - serviceTopologyConfig.heightMargin * 2;
-
-      layout = d3.layout.tree().size([height, width]);
-    };
-
-    /**
-    * Update the tree layout
-    */
-
-    this.updateTree = function (svg) {
-
-      // console.log(baseData);
-
-      var _computeLayout = computeLayout(baseData);
-
-      // Compute the new tree layout.
-
-      var _computeLayout2 = _slicedToArray(_computeLayout, 2);
-
-      nodes = _computeLayout2[0];
-      links = _computeLayout2[1];
-      drawNodes(svg, nodes);
-      drawLinks(svg, links);
-    };
-  }]);
-})();
-'use strict';
-
-(function () {
-  'use strict';
-  angular.module('xos.diagnostic').directive('logicTopology', function () {
-    return {
-      restrict: 'E',
-      scope: {
-        subscribers: '=',
-        selected: '='
-      },
-      bindToController: true,
-      controllerAs: 'vm',
-      templateUrl: 'templates/logicTopology.tpl.html',
-      controller: ["$element", "$log", "$scope", "$rootScope", "$timeout", "d3", "LogicTopologyHelper", "Node", "Tenant", "Ceilometer", "serviceTopologyConfig", "ChartData", function controller($element, $log, $scope, $rootScope, $timeout, d3, LogicTopologyHelper, Node, Tenant, Ceilometer, serviceTopologyConfig, ChartData) {
-        var _this2 = this;
-
-        $log.info('Logic Plane');
-
-        var svg;
-        this.selectedInstances = [];
-        this.hideInstanceStats = true;
-        var _this = this;
-
-        var handleSvg = function handleSvg(el) {
-
-          d3.select($element[0]).select('svg').remove();
-
-          svg = d3.select(el).append('svg').style('width', el.clientWidth + 'px').style('height', el.clientHeight + 'px');
-        };
-
-        var loadGlobalScope = function loadGlobalScope() {
-          ChartData.getLogicTree().then(function (tree) {
-            LogicTopologyHelper.updateTree(svg);
-          });
-        };
-        loadGlobalScope();
-
-        $scope.$watch(function () {
-          return _this2.selected;
-        }, function (selected) {
-          if (selected) {
-            ChartData.selectSubscriber(selected);
-            LogicTopologyHelper.updateTree(svg);
-          } else {
-            ChartData.removeSubscriber();
-            LogicTopologyHelper.updateTree(svg);
-          }
-        });
-
-        $rootScope.$on('instance.detail.hide', function () {
-          _this2.hideInstanceStats = true;
-          $timeout(function () {
-            _this2.selectedInstances = [];
-            ChartData.highlightInstances([]);
-            LogicTopologyHelper.updateTree(svg);
-          }, 500);
-        });
-
-        $rootScope.$on('instance.detail', function (evt, service) {
-          ChartData.getInstanceStatus(service).then(function (instances) {
-            LogicTopologyHelper.updateTree(svg);
-          })['catch'](function (e) {
-            _this.error = 'Service statistics are not available at this time. Please try again later.';
-            $timeout(function () {
-              _this.error = null;
-            }, 2000);
-          });
-        });
-
-        d3.select(window).on('resize.logic', function () {
-          handleSvg($element[0]);
-          LogicTopologyHelper.setupTree(svg);
-          LogicTopologyHelper.updateTree(svg);
-        });
-
-        handleSvg($element[0]);
-        LogicTopologyHelper.setupTree(svg);
-
-        this.selectSubscriberModal = function () {
-          _this2.openSelectSubscriberModal = true;
-          $scope.$apply();
-        };
-
-        this.subscriberStatusModal = function () {
-          _this2.openSubscriberStatusModal = true;
-          $scope.$apply();
-        };
-
-        // listen for subscriber modal event
-        $rootScope.$on('subscriber.modal.open', function () {
-
-          if (ChartData.currentSubscriber) {
-            _this2.subscriberStatusModal();
-          } else {
-            _this2.selectSubscriberModal();
-          }
-        });
-
-        // listen for subscriber modal event
-        $rootScope.$on('subscriber.modal.open', function () {
-
-          if (ChartData.currentSubscriber) {
-            _this2.currentSubscriber = ChartData.currentSubscriber;
-            _this2.subscriberStatusModal();
-          } else {
-            _this2.selectSubscriberModal();
-          }
-        });
-      }]
-    };
-  });
-})();
-'use strict';
-
-(function () {
-  'use strict';
-  angular.module('xos.diagnostic').directive('diagnosticContainer', function () {
-    return {
-      restrict: 'E',
-      templateUrl: 'templates/diagnostic.tpl.html',
-      controllerAs: 'vm',
-      controller: ["ChartData", "Subscribers", "ServiceRelation", "$rootScope", "$log", function controller(ChartData, Subscribers, ServiceRelation, $rootScope, $log) {
-        var _this = this;
-
-        this.loader = true;
-        this.error = false;
-
-        var loadGlobalScope = function loadGlobalScope() {
-          Subscribers.query().$promise.then(function (subscribers) {
-            _this.subscribers = subscribers;
-            return ServiceRelation.get();
-          }).then(function (serviceChain) {
-            _this.serviceChain = serviceChain;
-            // debug helper
-            // loadSubscriber(this.subscribers[0]);
-          })['catch'](function (e) {
-            throw new Error(e);
-            _this.error = e;
-          })['finally'](function () {
-            _this.loader = false;
-          });
-        };
-
-        loadGlobalScope();
-
-        this.reloadGlobalScope = function () {
-          _this.selectedSubscriber = null;
-          loadGlobalScope();
-        };
-
-        var loadSubscriber = function loadSubscriber(subscriber) {
-          ServiceRelation.getBySubscriber(subscriber).then(function (serviceChain) {
-            _this.serviceChain = serviceChain;
-            ChartData.currentServiceChain = serviceChain;
-            return Subscribers.getWithDevices({ id: subscriber.id }).$promise;
-          }).then(function (subscriber) {
-            _this.selectedSubscriber = subscriber;
-            ChartData.currentSubscriber = subscriber;
-          });
-        };
-
-        $rootScope.$on('subscriber.selected', function (evt, subscriber) {
-          loadSubscriber(subscriber);
-        });
-      }]
-    };
-  });
-})();
-'use strict';
-
-(function () {
-  'use strict';
-
-  angular.module('xos.diagnostic').factory('d3', ["$window", function ($window) {
-    return $window.d3;
-  }]);
-})();
-'use strict';
-
-(function () {
-  'use strict';
-
-  angular.module('xos.diagnostic').constant('serviceTopologyConfig', {
-    widthMargin: 60,
-    heightMargin: 30,
-    duration: 750,
-    elWidths: [20, 104, 105, 104, 20], //this is not true
-    circle: {
-      radius: 10,
-      r: 10,
-      selectedRadius: 15
-    },
-    square: {
-      width: 20,
-      height: 20,
-      x: -10,
-      y: -10
-    },
-    rack: {
-      width: 105,
-      height: 50,
-      x: -30,
-      y: -25
-    },
-    computeNode: {
-      width: 50,
-      height: 20,
-      margin: 5,
-      labelHeight: 10,
-      x: -25,
-      y: -10
-    },
-    instance: {
-      width: 80,
-      height: 36,
-      margin: 5,
-      x: -40,
-      y: -18
-    },
-    container: {
-      width: 60,
-      height: 130,
-      margin: 5,
-      x: -30,
-      y: -15
-    }
-  });
-})();
-'use strict';
-
-(function () {
-  'use strict';
-
-  angular.module('xos.diagnostic').service('ChartData', ["$rootScope", "$q", "lodash", "Tenant", "Node", "serviceTopologyConfig", "Ceilometer", "Instances", function ($rootScope, $q, lodash, Tenant, Node, serviceTopologyConfig, Ceilometer, Instances) {
-    var _this = this;
-
-    this.currentSubscriber = null;
-    this.currentServiceChain = null;
-
-    this.logicTopologyData = {
-      name: 'Router',
-      type: 'router',
-      children: [{
-        name: 'WAN-Side',
-        subtitle: 'Virtual Network',
-        type: 'network',
-        children: [{
-          name: 'Compute Servers',
-          type: 'rack',
-          computeNodes: [],
-          children: [{
-            name: 'LAN-Side',
-            subtitle: 'Virtual Network',
-            type: 'network',
-            children: [{
-              name: 'Subscriber',
-              type: 'subscriber'
-            }] //subscribers goes here
-          }]
-        }]
-      }]
-    };
-
-    this.getLogicTree = function () {
-      var deferred = $q.defer();
-
-      Node.queryWithInstances().$promise.then(function (computeNodes) {
-        _this.logicTopologyData.children[0].children[0].computeNodes = computeNodes;
-        // LogicTopologyHelper.updateTree(svg);
-        deferred.resolve(_this.logicTopologyData);
-      });
-
-      return deferred.promise;
-    };
-
-    /**
-    * Add Subscriber tag to LAN Network
-    */
-    this.addSubscriberTag = function (tags) {
-      _this.logicTopologyData.children[0].children[0].children[0].subscriberTag = {
-        cTag: tags.cTag,
-        sTag: tags.sTag
-      };
-    };
-
-    /**
-    * Add Subscribers to the tree
-    */
-    this.addSubscriber = function (subscriber) {
-      subscriber.children = subscriber.devices;
-
-      // add subscriber to data tree
-      _this.logicTopologyData.children[0].children[0].children[0].children = [subscriber];
-      return _this.logicTopologyData;
-    };
-
-    /**
-    * Remove a subscriber from the tree
-    */
-
-    this.removeSubscriber = function () {
-      _this.logicTopologyData.children[0].children[0].children[0].children[0].humanReadableName = 'Subscriber';
-      _this.currentSubscriber = null;
-      if (serviceTopologyConfig.elWidths[serviceTopologyConfig.elWidths.length - 1] === 160) {
-        serviceTopologyConfig.elWidths.pop();
-      }
-
-      //remove tags and ip
-      delete _this.logicTopologyData.children[0].children[0].children[0].subscriberTag;
-      delete _this.logicTopologyData.children[0].subscriberIP;
-
-      _this.highlightInstances([]);
-      delete _this.logicTopologyData.children[0].children[0].children[0].children[0].children;
-    };
-
-    this.getSubscriberTag = function (subscriber) {
-      var tags = {
-        cTag: subscriber.c_tag,
-        sTag: subscriber.s_tag
-      };
-
-      _this.addSubscriberTag(tags);
-      // add tags info to current subscriber
-      _this.currentSubscriber.tags = tags;
-    };
-
-    this.getSubscriberIP = function (subscriber) {
-      // const ip = JSON.parse(this.currentServiceChain.children[0].children[0].tenant.service_specific_attribute).wan_container_ip;
-      // const ip = this.currentServiceChain.children[0].children[0].tenant.wan_container_ip;
-      _this.logicTopologyData.children[0].subscriberIP = subscriber.wan_container_ip;
-    };
-
-    this.selectSubscriber = function (subscriber) {
-      // append the device with to config settings
-      serviceTopologyConfig.elWidths.push(160);
-
-      _this.addSubscriber(angular.copy(subscriber));
-
-      //clean selected instances
-      _this.highlightInstances([]);
-
-      _this.getSubscriberTag(subscriber);
-      _this.getSubscriberIP(subscriber);
-    };
-
-    this.highlightInstances = function (instances) {
-
-      var computeNodes = _this.logicTopologyData.children[0].children[0].computeNodes;
-
-      // unselect all
-      computeNodes.map(function (node) {
-        node.instances.map(function (instance) {
-          instance.selected = false;
-          return instance;
-        });
-      });
-
-      lodash.forEach(instances, function (instance) {
-        computeNodes.map(function (node) {
-          node.instances.map(function (d3instance) {
-            if (d3instance.id === instance.id) {
-              // console.log(d3instance, instance);
-              d3instance.selected = true;
-              d3instance.stats = instance.stats; //add stats to d3 node
-              d3instance.container = instance.container; // container info to d3 node
-            }
-            return d3instance;
-          });
-        });
-      });
-    };
-
-    this.getInstanceStatus = function (service) {
-      var deferred = $q.defer();
-
-      var p = undefined;
-
-      // subscriber specific
-      if (_this.currentSubscriber) {
-
-        var attr = undefined;
-        try {
-          attr = JSON.parse(service.tenant.service_specific_attribute);
-        } catch (e) {
-          attr = null;
-        }
-
-        // if no instances are associated to the subscriber
-        if (!attr || !attr.instance_id) {
-          var d = $q.defer();
-          d.resolve([]);
-          p = d.promise;
-        }
-        // if ther is an instance
-        else {
-            (function () {
-              var instance = {};
-              p = Instances.get({ id: attr.instance_id }).$promise.then(function (_instance) {
-                instance = _instance;
-                return Ceilometer.getInstanceStats(instance.instance_uuid);
-              }).then(function (stats) {
-                instance.stats = stats;
-                var containerName = 'vcpe-' + _this.currentSubscriber.tags.sTag + '-' + _this.currentSubscriber.tags.cTag;
-                // append containers
-                instance.container = {
-                  name: containerName
-                };
-
-                // TODO fetch container stats
-                return Ceilometer.getContainerStats(containerName);
-              }).then(function (containerStats) {
-                instance.container.stats = containerStats.stats;
-                instance.container.port = containerStats.port;
-                return [instance];
-              });
-            })();
-          }
-      }
-      // global scope
-      else {
-          var param = {
-            'service_vsg': { kind: 'vCPE' },
-            'service_vbng': { kind: 'vBNG' },
-            'service_volt': { kind: 'vOLT' }
-          };
-
-          p = Tenant.queryVsgInstances(param[service.name]).$promise.then(function (instances) {
-            return Ceilometer.getInstancesStats(lodash.uniq(instances));
-          });
-        }
-
-      p.then(function (instances) {
-        _this.highlightInstances(instances);
-        deferred.resolve(instances);
-      })['catch'](function (e) {
-        deferred.reject(e);
-      });
-
-      return deferred.promise;
-    };
-  }]);
-})();
-angular.module('xos.diagnostic').run(function($location){
-  $location.path('/')
-});
-angular.bootstrap(angular.element('#xosDiagnostic'), ['xos.diagnostic']);
\ No newline at end of file
+"use strict";!function(){angular.module("xos.diagnostic",["ngResource","ngCookies","ngLodash","ngAnimate","ui.router","xos.helpers"]).config(["$stateProvider",function(e){e.state("home",{url:"/",template:"<diagnostic-container></diagnostic-container>"})}]).config(["$httpProvider",function(e){e.interceptors.push("NoHyperlinks")}]).run(["$log",function(e){e.info("Diagnostic Started")}])}(),angular.module("xos.diagnostic").run(["$templateCache",function(e){e.put("templates/diagnostic.tpl.html",'<div class="container-fluid">\n  <div ng-hide="vm.error && vm.loader" style="height: 900px">\n    <div class="onethird-height">\n      <div class="well">\n        Services Graph\n      </div>\n      <div class="well pull-right" ng-click="vm.reloadGlobalScope()" ng-show="vm.selectedSubscriber">\n        Reset subscriber\n      </div>\n      <service-topology service-chain="vm.serviceChain"></service-topology>\n    </div>\n    <div class="twothird-height">\n      <div class="well">\n        Logical Resources\n      </div>\n      <logic-topology ng-if="vm.subscribers" subscribers="vm.subscribers" selected="vm.selectedSubscriber"></logic-topology>\n    </div>\n  </div>\n  <div class="row" ng-if="vm.error">\n    <div class="col-xs-12">\n      <div class="alert alert-danger">\n        {{vm.error}}\n      </div>\n    </div>\n  </div>\n  <div class="row" ng-if="vm.loader">\n    <div class="col-xs-12">\n      <div class="loader">Loading</div>\n    </div>\n  </div>\n</div>'),e.put("templates/logicTopology.tpl.html",'<select-subscriber-modal open="vm.openSelectSubscriberModal" subscribers="vm.subscribers"></select-subscriber-modal>\n<subscriber-status-modal open="vm.openSubscriberStatusModal" subscriber="vm.currentSubscriber"></subscriber-status-modal>\n<div class="alert alert-danger animate" ng-hide="!vm.error">\n  {{vm.error}}\n</div>\n<!-- <div class="instances-stats animate" ng-hide="vm.hideInstanceStats">\n  <div class="row">\n    <div class="col-sm-3 col-sm-offset-8">\n      <div class="panel panel-primary" ng-repeat="instance in vm.selectedInstances">\n        <div class="panel-heading">\n          {{instance.humanReadableName}}\n        </div>\n          <ul class="list-group">\n            <li class="list-group-item">Backend Status: {{instance.backend_status}}</li>\n            <li class="list-group-item">IP Address: {{instance.ip}}</li>\n          </ul>\n          <ul class="list-group">\n            <li class="list-group-item" ng-repeat="stat in instance.stats">\n              <span class="badge">{{stat.value}}</span>\n              {{stat.meter}}\n            </li>\n          </ul>\n        </div>\n      </div>  \n    </div>\n  </div>\n</div> -->'),e.put("templates/select-subscriber-modal.tpl.html",'<div class="modal fade" ng-class="{in: vm.open}" tabindex="-1" role="dialog">\n  <div class="modal-dialog modal-sm">\n    <div class="modal-content">\n      <div class="modal-header">\n        <button ng-click="vm.close()"  type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>\n        <h4 class="modal-title">Select a subscriber:</h4>\n      </div>\n      <div class="modal-body">\n        <select class="form-control" ng-options="s as s.humanReadableName for s in vm.subscribers" ng-model="vm.selected"></select>\n      </div>\n      <div class="modal-footer">\n        <button ng-click="vm.close()" type="button" class="btn btn-default" data-dismiss="modal">Close</button>\n        <button ng-click="vm.select(vm.selected)" type="button" class="btn btn-primary">Select</button>\n      </div>\n    </div><!-- /.modal-content -->\n  </div><!-- /.modal-dialog -->\n</div><!-- /.modal -->'),e.put("templates/subscriber-status-modal.tpl.html",'<div class="modal fade" ng-class="{in: vm.open}" tabindex="-1" role="dialog">\n  <div class="modal-dialog modal-sm">\n    <div class="modal-content">\n      <div class="modal-header">\n        <button ng-click="vm.close()"  type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>\n        <h4 class="modal-title">Manage subscriber:</h4>\n      </div>\n      <form name="vm.subscriber-detail">\n        <div class="modal-body">\n          <div class="row">\n            <div class="col-xs-12">\n              <label>Status</label>\n            </div>\n            <div class="col-xs-6">\n              <a ng-click="vm.subscriber.status = \'enabled\'"\n                class="btn btn-block"\n                ng-class="{\'btn-primary\': vm.subscriber.status === \'enabled\' ,\'btn-default\': vm.subscriber.status !== \'enabled\'}"\n                >Enabled</a>\n            </div>\n            <div class="col-xs-6">\n              <a ng-click="vm.subscriber.status = \'suspended\'"\n                class="btn btn-block"\n                ng-class="{\'btn-primary\': vm.subscriber.status === \'suspended\' ,\'btn-default\': vm.subscriber.status !== \'suspended\'}"\n                >Suspended</a>\n            </div>\n          </div>\n          <div class="row">\n            <div class="col-xs-6">\n              <a ng-click="vm.subscriber.status = \'delinquent\'"\n                class="btn btn-block"\n                ng-class="{\'btn-primary\': vm.subscriber.status === \'delinquent\' ,\'btn-default\': vm.subscriber.status !== \'delinquent\'}"\n                >Delinquent <br> payment</a>\n            </div>\n            <div class="col-xs-6">\n              <a ng-click="vm.subscriber.status = \'copyrightviolation\'"\n                class="btn btn-block"\n                ng-class="{\'btn-primary\': vm.subscriber.status === \'copyrightviolation\' ,\'btn-default\': vm.subscriber.status !== \'copyrightviolation\'}"\n                >Copyright <br> violation</a>\n            </div>\n          </div>\n          <div class="row">\n            <div class="col-xs-6">\n              <label>Uplink Speed</label>\n              <div class="input-group">\n                <input type="number" class="form-control small-padding" ng-model="vm.subscriber.uplink_speed"/>\n                <span class="input-group-addon">Mbps</span>\n              </div>\n            </div>\n            <div class="col-xs-6">\n              <label>Downlink Speed</label>\n              <div class="input-group">\n                <input type="number" class="form-control small-padding" ng-model="vm.subscriber.downlink_speed"/>\n                <span class="input-group-addon">Mbps</span>\n              </div>\n            </div>\n          </div>\n          <div class="row">\n            <div class="col-xs-6">\n              <label>Enable Internet</label>\n            </div>\n            <div class="col-xs-6">\n              <a \n                ng-click="vm.subscriber.enable_uverse = !vm.subscriber.enable_uverse" \n                ng-class="{\'btn-success\': vm.subscriber.enable_uverse, \'btn-danger\': !vm.subscriber.enable_uverse}"\n                class="btn btn-block">\n                <span ng-show="vm.subscriber.enable_uverse === true">Enabled</span>\n                <span ng-show="vm.subscriber.enable_uverse !== true">Disabled</span>\n              </a>\n            </div>\n          </div>\n        </div>\n        <div class="modal-footer" ng-show="vm.success || vm.formError">\n          <div class="alert alert-success" ng-show="vm.success">\n            {{vm.success}}\n          </div>\n          <div class="alert alert-danger" ng-show="vm.formError">\n            {{vm.formError}}\n          </div>\n        </div>\n        <div class="modal-footer">\n          <button ng-click="vm.close()" type="button" class="btn btn-default" data-dismiss="modal">Close</button>\n          <button ng-click="vm.updateSubscriber(vm.subscriber)" type="button" class="btn btn-primary">Save</button>\n        </div>\n      </form>\n    </div><!-- /.modal-content -->\n  </div><!-- /.modal-dialog -->\n</div><!-- /.modal -->')}]),function(){angular.module("xos.diagnostic").directive("selectSubscriberModal",function(){return{scope:{subscribers:"=",open:"="},bindToController:!0,restrict:"E",templateUrl:"templates/select-subscriber-modal.tpl.html",controllerAs:"vm",controller:["$rootScope",function(e){var t=this;this.close=function(){t.open=!1},this.select=function(n){e.$emit("subscriber.selected",n),t.close()}}]}}).directive("subscriberStatusModal",function(){return{scope:{open:"=",subscriber:"="},bindToController:!0,restrict:"E",templateUrl:"templates/subscriber-status-modal.tpl.html",controllerAs:"vm",controller:["$log","$timeout","$scope","Subscribers",function(e,t,n,r){var i=this,a=1e6;n.$watch(function(){return i.open},function(){i.success=null,i.formError=null}),n.$watch(function(){return i.subscriber},function(e,t){i.subscriber&&(console.log(e,t),console.log("subscriber change",e===t),i.subscriber.uplink_speed=parseInt(i.subscriber.uplink_speed,10)/a,i.subscriber.downlink_speed=parseInt(i.subscriber.downlink_speed,10)/a)}),this.close=function(){i.open=!1},this.updateSubscriber=function(e){var n=angular.copy(e,n);n.uplink_speed=n.uplink_speed*a,n.downlink_speed=n.downlink_speed*a,r.update(n).$promise.then(function(e){i.success="Subscriber successfully updated!"})["catch"](function(e){i.formError=e})["finally"](function(){t(function(){i.close()},1500)})}}]}})}(),function(){angular.module("xos.diagnostic").service("ServiceTopologyHelper",["$rootScope","$window","$log","lodash","ServiceRelation","serviceTopologyConfig","d3",function(e,t,n,r,i,a,s){var c,o,u,l,d=0,p=function(t,n,r){var p=arguments.length<=3||void 0===arguments[3]?l:arguments[3];p&&(l=p);var h=l.clientWidth-2*a.widthMargin;c=t,o=n,u=r;var m=i.depthOf(r),b=s.svg.diagonal().projection(function(e){return[e.y,e.x]}),g=n.nodes(r).reverse(),f=n.links(g);g.forEach(function(e){var t=(h-2*a.widthMargin)/(m-1);e.y=e.depth*t});var y=t.selectAll("g.node").data(g,function(e){return e.id||(e.id=++d)}),x=y.enter().append("g").attr({"class":function(e){return"node "+e.type},transform:function(e){return e.x&&e.y?"translate("+e.y+", "+e.x+")":"translate("+r.y0+", "+r.x0+")"}}),S=x.filter(".subscriber"),w=x.filter(".router"),T=x.filter(".service");S.append("rect").attr(a.square).on("click",function(){e.$emit("subscriber.modal.open")}),w.append("rect").attr(a.square),T.append("circle").attr("r",1e-6).style("fill",function(e){return e._children?"lightsteelblue":"#fff"}).on("click",v),x.append("text").attr({x:function(e){return e.children?-a.circle.selectedRadius-5:a.circle.selectedRadius+5},dy:".35em",y:function(e){return e.children&&e.parent?"-5":void 0},transform:function(e){return e.children&&e.parent?e.parent.x<e.x?"rotate(-30)":"rotate(30)":void 0},"text-anchor":function(e){return e.children?"end":"start"}}).text(function(e){return e.name}).style("fill-opacity",1e-6);var _=y.transition().duration(a.duration).attr({transform:function(e){return"translate("+e.y+","+e.x+")"}});_.select("circle").attr("r",function(e){return e.selected?a.circle.selectedRadius:a.circle.radius}).style("fill",function(e){return e.selected?"lightsteelblue":"#fff"}),_.select("text").style("fill-opacity",1);var C=y.exit().transition().duration(a.duration).remove();C.select("circle").attr("r",1e-6),C.select("text").style("fill-opacity",1e-6);var k=t.selectAll("path.link").data(f,function(e){return e.target.id});k.enter().insert("path","g").attr("class",function(e){return"link "+e.target.type+" "+(e.target.active?"":"active")}).attr("d",function(e){var t={x:r.x0,y:r.y0};return b({source:t,target:t})}),k.transition().duration(a.duration).attr("d",b),k.exit().transition().duration(a.duration).attr("d",function(e){var t={x:r.x,y:r.y};return b({source:t,target:t})}).remove(),g.forEach(function(e){e.x0=e.x,e.y0=e.y})},v=function(t){return t.selected?(t.selected=!t.selected,e.$emit("instance.detail.hide",{}),p(c,o,u)):(e.$emit("instance.detail",{name:t.name,service:t.service,tenant:t.tenant}),c.selectAll("circle").each(function(e){return e.selected=!1}),t.selected=!t.selected,void p(c,o,u))};this.updateTree=p}])}(),function(){angular.module("xos.diagnostic").directive("serviceTopology",function(){return{restrict:"E",scope:{serviceChain:"="},bindToController:!0,controllerAs:"vm",template:"",controller:["$element","$window","$scope","d3","serviceTopologyConfig","ServiceRelation","Slice","Instances","Subscribers","ServiceTopologyHelper",function(e,t,n,r,i,a,s,c,o,u){var l=this,d=e[0];r.select(window).on("resize.service",function(){h(l.serviceChain)});var p,v,h=function(t){if(!t)return void console.error("Tree is missing");r.select(e[0]).select("svg").remove();var n=d.clientWidth-2*i.widthMargin,a=d.clientHeight-2*i.heightMargin,s=r.layout.tree().size([a,n]);v=r.select(e[0]).append("svg").style("width",d.clientWidth+"px").style("height",d.clientHeight+"px");var c=v.append("g").attr("transform","translate("+2*i.widthMargin+","+i.heightMargin+")");p=t,p.x0=a/2,p.y0=n/2,u.updateTree(c,s,p,d)};n.$watch(function(){return l.serviceChain},function(e){angular.isDefined(e)&&h(e)})}]}})}(),function(){angular.module("xos.diagnostic").service("Services",["$resource",function(e){return e("/xos/services/:id",{id:"@id"})}]).service("Tenant",["$resource",function(e){return e("/xos/tenants",{id:"@id"},{queryVsgInstances:{method:"GET",isArray:!0,interceptor:{response:function(e){var t=[];return angular.forEach(e.data,function(e){var n=JSON.parse(e.service_specific_attribute);n&&n.instance_id&&t.push(n.instance_id)}),t}}},getSubscriberTag:{method:"GET",isArray:!0,interceptor:{response:function(e){return JSON.parse(e.data[0].service_specific_attribute)}}}})}]).service("Ceilometer",["$http","$q","Instances",function(e,t,n){var r=this;this.getInstanceStats=function(n){var r=t.defer();return e.get("/xoslib/xos-instance-statistics",{params:{"instance-uuid":n}}).then(function(e){r.resolve(e.data)})["catch"](function(e){r.reject(e)}),r.promise},this.getInstancesStats=function(e){var i=t.defer(),a=[],s=[];return e.forEach(function(e){a.push(n.get({id:e}).$promise)}),t.all(a).then(function(e){s=e;var n=[];return s.forEach(function(e){n.push(r.getInstanceStats(e.instance_uuid))}),t.all(n)}).then(function(e){s.map(function(t,n){t.stats=e[n]}),i.resolve(s)})["catch"](i.reject),i.promise},this.getContainerStats=function(n){var r=t.defer(),i={};return e.get("/xoslib/meterstatistics",{params:{resource:n}}).then(function(t){return i.stats=t.data,e.get("/xoslib/meterstatistics",{params:{resource:n+"-eth0"}})}).then(function(t){return i.port={eth0:t.data},e.get("/xoslib/meterstatistics",{params:{resource:n+"-eth1"}})}).then(function(e){i.port.eth1=e.data,r.resolve(i)})["catch"](function(e){r.reject(e)}),r.promise}}]).service("Slice",["$resource",function(e){return e("/xos/slices",{id:"@id"})}]).service("Instances",["$resource",function(e){return e("/xos/instances/:id",{id:"@id"})}]).service("Node",["$resource","$q","Instances",function(e,t,n){return e("/xos/nodes",{id:"@id"},{queryWithInstances:{method:"GET",isArray:!0,interceptor:{response:function(e){var r=t.defer(),i=[];return angular.forEach(e.data,function(e){i.push(n.query({node:e.id}).$promise)}),t.all(i).then(function(t){e.data.map(function(e,n){return e.instances=t[n],e}),r.resolve(e.data)}),r.promise}}}})}]).service("Subscribers",["$resource","$q","SubscriberDevice",function(e,t,n){return e("/xoslib/cordsubscriber/:id",{id:"@id"},{update:{method:"PUT",isArray:!1},queryWithDevices:{method:"GET",isArray:!0,interceptor:{response:function(e){var r=t.defer(),i=[];return angular.forEach(e.data,function(e){i.push(n.query({id:e.id}).$promise)}),t.all(i).then(function(t){e.data.map(function(e,n){return e.devices=t[n],e.type="subscriber",e.devices.map(function(e){return e.type="device"}),e}),r.resolve(e.data)}),r.promise}}},getWithDevices:{method:"GET",isArray:!1,interceptor:{response:function(e){var r=t.defer();return n.query({id:e.data.id}).$promise.then(function(t){t.map(function(e){return e.type="device"}),e.data.devices=t,e.data.type="subscriber",r.resolve(e.data)})["catch"](function(e){r.reject(e)}),r.promise}}}})}]).service("SubscriberDevice",["$resource",function(e){return e("/xoslib/rs/subscriber/:id/users/",{id:"@id"})}]).service("ServiceRelation",["$q","lodash","Services","Tenant","Slice","Instances",function(e,t,n,r,i,a){var s=function m(e){var t=0;return e.children&&e.children.forEach(function(e){var n=m(e);n>t&&(t=n)}),1+t},c=function(e,n){return t.filter(e,function(e){return e.subscriber_service===n})},o=function(e,n){var r,e=t.filter(e,function(e){return e.provider_service===n&&e.subscriber_tenant});return e.forEach(function(e){e.service_specific_attribute&&(r=JSON.parse(e.service_specific_attribute))}),r},u=function(e,n){var r=[];return t.forEach(e,function(e){var i=t.find(n,{id:e.provider_service});r.push(i)}),r},l=function b(e,n,r,i){var a=arguments.length<=4||void 0===arguments[4]?null:arguments[4],s=t.difference(n,[r]),l=c(e,r.id),d=u(l,n);s=t.difference(s,d),r.service_specific_attribute=o(e,r.id),"service_vbng"===r.humanReadableName&&(r.humanReadableName="service_vrouter");var p={name:r.humanReadableName,parent:a,type:"service",service:r,tenant:i,children:[]};return t.forEach(d,function(n){if("service_ONOS_vBNG"!==n.humanReadableName&&"service_ONOS_vOLT"!==n.humanReadableName){var a=t.find(e,{subscriber_tenant:i.id,provider_service:n.id});p.children.push(b(e,s,n,a,r.humanReadableName))}}),0===p.children.length&&p.children.push({name:"Router",type:"router",children:[]}),p},d=function(e,n){var r=arguments.length<=2||void 0===arguments[2]?{id:1,name:"fakeSubs"}:arguments[2],i=t.find(n,{subscriber_root:r.id}),a=t.find(e,{id:i.provider_service}),s=l(n,e,a,i);return{name:r.name||r.humanReadableName,parent:null,type:"subscriber",children:[s]}},p=function(e,n){var r=function s(e,n,r){"service_vbng"===r.humanReadableName&&(r.humanReadableName="service_vrouter");var i={type:"service",name:r.humanReadableName,service:r},a=t.find(n,{subscriber_service:r.id});if(a){var c=t.find(e,{id:a.provider_service});i.children=[s(e,n,c)]}else i.children=[{name:"Router",type:"router",children:[]}];return delete r.id,i},i=t.find(e,{id:3});if(!angular.isDefined(i))return void console.error("Missing Base service!");var a={name:"Subscriber",type:"subscriber",parent:null,children:[r(e,n,i)]};return a},v=function(t){var i,a,s=e.defer();return n.query().$promise.then(function(e){return i=e,r.query().$promise}).then(function(e){a=e,s.resolve(d(i,a,t))})["catch"](function(e){throw new Error(e)}),s.promise},h=function(){var t,i,a=e.defer();return n.query().$promise.then(function(e){return t=e,r.query({kind:"coarse"}).$promise}).then(function(e){i=e,a.resolve(p(t,i))})["catch"](function(e){throw new Error(e)}),a.promise};return{get:h,buildServiceTree:p,getBySubscriber:v,buildLevel:l,buildSubscriberServiceTree:d,findLevelRelation:c,findLevelServices:u,depthOf:s,findSpecificInformation:o}}])}();var _slicedToArray=function(){function e(e,t){var n=[],r=!0,i=!1,a=void 0;try{for(var s,c=e[Symbol.iterator]();!(r=(s=c.next()).done)&&(n.push(s.value),!t||n.length!==t);r=!0);}catch(o){i=!0,a=o}finally{try{!r&&c["return"]&&c["return"]()}finally{if(i)throw a}}return n}return function(t,n){if(Array.isArray(t))return t;if(Symbol.iterator in Object(t))return e(t,n);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}();!function(){angular.module("xos.diagnostic").service("RackHelper",["serviceTopologyConfig","lodash",function(e,t){var n=this;this.getComputeNodeLabelSize=function(){return e.computeNode.labelHeight+2*e.instance.margin},this.getComputeNodeSize=t.memoize(function(t){var r=3*e.instance.margin+2*e.instance.width,i=Math.round(t.length/2),a=n.getComputeNodeLabelSize(),s=e.instance.height*i+e.instance.margin*(i+1)+a;return[r,s]}),this.getRackSize=function(r){var i=0,a=e.computeNode.margin;return t.forEach(r,function(t){var r=n.getComputeNodeSize(t.instances),s=_slicedToArray(r,2),c=s[0],o=s[1];i=c+2*e.computeNode.margin,a+=o+e.computeNode.margin}),[i,a]},this.getInstancePosition=function(t){var r=Math.floor(t/2),i=t%2?1:0,a=n.getComputeNodeLabelSize(),s=e.instance.margin+e.instance.width*i+e.instance.margin*i,c=a+e.instance.margin+e.instance.height*r+e.instance.margin*r;return[s,c]},this.getComputeNodePosition=function(r,i){var a=e.computeNode.margin,s=t.reduce(r.slice(0,i),function(e,t){return e+n.getComputeNodeSize(t.instances)[1]},0),c=e.computeNode.margin+e.computeNode.margin*i+s;return[a,c]}}])}();var _slicedToArray=function(){function e(e,t){var n=[],r=!0,i=!1,a=void 0;try{for(var s,c=e[Symbol.iterator]();!(r=(s=c.next()).done)&&(n.push(s.value),!t||n.length!==t);r=!0);}catch(o){i=!0,a=o}finally{try{!r&&c["return"]&&c["return"]()}finally{if(i)throw a}}return n}return function(t,n){if(Array.isArray(t))return t;if(Symbol.iterator in Object(t))return e(t,n);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}();!function(){var e={cloud:" M 79.72 49.60 C 86.00 37.29 98.57 29.01 111.96 26.42 C 124.27 24.11 137.53 26.15 148.18 32.90 C 158.08 38.78 165.39 48.87 167.65 60.20 C 176.20 57.90 185.14 56.01 194.00 57.73 C 206.08 59.59 217.92 66.01 224.37 76.66 C 227.51 81.54 228.85 87.33 229.23 93.06 C 237.59 93.33 246.22 95.10 253.04 100.19 C 256.69 103.13 259.87 107.67 258.91 112.59 C 257.95 118.43 252.78 122.38 247.78 124.82 C 235.27 130.43 220.23 130.09 207.98 123.93 C 199.33 127.88 189.76 129.43 180.30 128.57 C 173.70 139.92 161.70 147.65 148.86 149.93 C 133.10 153.26 116.06 148.15 104.42 137.08 C 92.98 143.04 78.96 143.87 66.97 139.04 C 57.75 135.41 49.70 128.00 46.60 118.43 C 43.87 109.95 45.81 100.29 51.30 93.32 C 57.38 85.18 67.10 80.44 76.99 78.89 C 74.38 69.20 74.87 58.52 79.72 49.60 Z"},t=0,n=0;angular.module("xos.diagnostic").service("NodeDrawer",["d3","serviceTopologyConfig","RackHelper","lodash",function(r,i,a,s){var c=this,o=this;this.addNetworks=function(t){t.selectAll("*").remove(),t.append("path").attr({d:e.cloud,transform:"translate(-100, -72), scale(0.7)","class":"cloud"}),t.append("text").attr({"text-anchor":"middle",y:-5,x:5}).text(function(e){return e.name}),t.append("text").attr({"text-anchor":"middle",y:8,x:5,"class":"small"}).text(function(e){return e.subtitle}),t.each(function(e){var t=r.select(this);"LAN-Side"===e.name&&angular.isDefined(e.subscriberTag)&&(t.append("text").attr({"text-anchor":"middle",y:50}).text(function(){return"C-Tag: "+e.subscriberTag.cTag}),t.append("text").attr({"text-anchor":"middle",y:70}).text(function(){return"S-Tag: "+e.subscriberTag.sTag})),"WAN-Side"===e.name&&angular.isDefined(e.subscriberIP)&&t.append("text").attr({"text-anchor":"middle",y:50}).text(function(){return"Public IP: "+e.subscriberIP})})},this.addRack=function(e){e.each(function(t){var n=a.getRackSize(t.computeNodes),r=_slicedToArray(n,2),s=r[0],o=r[1];e.select("g").remove();var u=e.append("g");u.attr({transform:"translate(0,0)"}).transition().duration(i.duration).attr({transform:function(){return"translate("+-(s/2)+", "+-(o/2)+")"}}),u.append("rect").attr({width:0,height:0}).transition().duration(i.duration).attr({width:s,height:o}),u.append("text").attr({"text-anchor":"middle",y:-10,x:s/2,opacity:0}).text(function(e){return e.name}).transition().duration(i.duration).attr({opacity:1}),c.drawComputeNodes(u,t.computeNodes)})},this.drawComputeNodes=function(e,n){var s=e.selectAll(".compute-nodes").data(n,function(e){return angular.isString(e.d3Id)||(e.d3Id="compute-node-"+ ++t),e.d3Id}),c=e.node().getBoundingClientRect(),u=c.width,l=c.height,d=s.enter().append("g");d.attr({transform:"translate("+u/2+", "+l/2+")","class":"compute-node"}).transition().duration(i.duration).attr({transform:function(e){return"translate("+a.getComputeNodePosition(n,e.d3Id.replace("compute-node-","")-1)+")"}}),d.append("rect").attr({width:0,height:0}).transition().duration(i.duration).attr({width:function(e){return a.getComputeNodeSize(e.instances)[0]},height:function(e){return a.getComputeNodeSize(e.instances)[1]}}),d.append("text").attr({"text-anchor":"start",y:17,x:10,opacity:0}).text(function(e){return e.humanReadableName.split(".")[0]}).transition().duration(i.duration).attr({opacity:1}),d.length>0&&d.each(function(e){o.drawInstances(r.select(this),e.instances)})};var u=function(e){return e.replace("app_","").replace("service_","").replace("mysite_","").replace("_instance","")},l=function(e){function t(e,t){return t.substring(0,e.length)===e}return t("0 - ",e.backend_status)?"provisioning":t("1 - ",e.backend_status)?"good":t("2 - ",e.backend_status)?"bad":""},d=function(e,t){var n=e.append("g").attr({"class":"container",transform:"translate("+i.instance.margin+", 115)"});n.append("rect").attr({width:250-2*i.container.margin,height:i.container.height}),n.append("text").attr({y:20,x:i.instance.margin,"class":"name"}).text(t.name);var r=["memory","memory.usage","cpu_util"];r.forEach(function(e,r){var a=s.find(t.stats,{meter:e});angular.isDefined(a)&&n.append("text").attr({y:40+15*r,x:i.instance.margin,opacity:0}).text(a.description+": "+Math.round(a.value)+" "+a.unit).transition().duration(i.duration).attr({opacity:1})});var a=["eth0","eth1"],c=[{meter:"network.incoming.bytes.rate",label:"Incoming"},{meter:"network.outgoing.bytes.rate",label:"Outgoing"}];a.forEach(function(e,r){0!==t.port[e].length&&(n.append("text").attr({y:90,x:i.instance.margin+120*r,"class":"name"}).text(t.name+"-"+e),c.forEach(function(a,c){var o=s.find(t.port[e],{meter:a.meter});angular.isDefined(o)&&n.append("text").attr({y:105+15*c,x:i.instance.margin+120*r,opacity:0}).text(a.label+": "+Math.round(o.value)+" "+o.unit).transition().duration(i.duration).attr({opacity:1})}))})},p=function(e,t){var n={"mysite_vsg-1":"200, -120","mysite_vsg-2":"-300, 30","mysite_vsg-3":"-300, -250"},a=e.append("g").attr({transform:"translate("+(n[t.humanReadableName]||n["mysite_vsg-1"])+")","class":"stats-container"}).on("click",function(e){e.fade=!e.fade;var t=void 0;t=e.fade?.1:1,r.select(this).transition().duration(i.duration).attr({opacity:t})}),c={"mysite_vsg-1":{x1:-160,y1:120,x2:0,y2:50},"mysite_vsg-2":{x1:250,y1:50,x2:300,y2:-10},"mysite_vsg-3":{x1:250,y1:50,x2:300,y2:270}};a.append("line").attr({x1:function(e){return c[e.humanReadableName].x1||c["mysite_vsg-1"].x1},y1:function(e){return c[e.humanReadableName].y1||c["mysite_vsg-1"].y1},x2:function(e){return c[e.humanReadableName].x2||c["mysite_vsg-1"].x2},y2:function(e){return c[e.humanReadableName].y2||c["mysite_vsg-1"].y2},stroke:"black",opacity:0}).transition().duration(i.duration).attr({opacity:1});var o=110,u=250;t.container&&(o+=i.container.height+2*i.container.margin);a.append("rect").attr({width:u,height:o,opacity:0}).transition().duration(i.duration).attr({opacity:1});a.append("text").attr({y:15,x:i.instance.margin,"class":"name",opacity:0}).text(t.humanReadableName).transition().duration(i.duration).attr({opacity:1}),a.append("text").attr({y:30,x:i.instance.margin,"class":"ip",opacity:0}).text(t.ip).transition().duration(i.duration).attr({opacity:1});var l=["memory","memory.usage","cpu","cpu_util"];l.forEach(function(e,n){var r=s.find(t.stats,{meter:e});r&&a.append("text").attr({y:55+15*n,x:i.instance.margin,opacity:0}).text(r.description+": "+Math.round(r.value)+" "+r.unit).transition().duration(i.duration).attr({opacity:1})}),t.container&&d(a,t.container)};this.drawInstances=function(e,t){var s=e.node().getBoundingClientRect(),c=s.width,o=s.height,d=e.selectAll(".instances").data(t,function(e){return angular.isString(e.d3Id)?e.d3Id:e.d3Id="instance-"+ ++n}),v=d.enter().append("g");v.attr({transform:"translate("+c/2+", "+o/2+")","class":function(e){return"instance "+(e.selected?"active":"")+" "+l(e)}}).transition().duration(i.duration).attr({transform:function(e,t){return"translate("+a.getInstancePosition(t)+")"}}),v.append("rect").attr({width:0,height:0}).transition().duration(i.duration).attr({width:i.instance.width,height:i.instance.height}),v.append("text").attr({"text-anchor":"middle",y:23,x:40,opacity:0}).text(function(e){return u(e.humanReadableName)}).transition().duration(i.duration).attr({opacity:1}),v.each(function(e,t){var n=r.select(this);angular.isDefined(e.stats)&&e.selected&&p(n,e,t)})},this.addPhisical=function(e){e.select("rect").remove(),e.select("text").remove(),e.append("rect").attr(i.square),e.append("text").attr({"text-anchor":"middle",y:i.square.y-10}).text(function(e){return e.name||e.humanReadableName})},this.addDevice=function(e){e.append("circle").attr(i.circle),e.append("text").attr({"text-anchor":"end",x:-i.circle.r-10,y:i.circle.r/2}).text(function(e){return e.name||e.mac})}}])}();var _slicedToArray=function(){function e(e,t){var n=[],r=!0,i=!1,a=void 0;try{for(var s,c=e[Symbol.iterator]();!(r=(s=c.next()).done)&&(n.push(s.value),!t||n.length!==t);r=!0);}catch(o){i=!0,a=o}finally{try{!r&&c["return"]&&c["return"]()}finally{if(i)throw a}}return n}return function(t,n){if(Array.isArray(t))return t;if(Symbol.iterator in Object(t))return e(t,n);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}();!function(){angular.module("xos.diagnostic").service("LogicTopologyHelper",["$window","$log","$rootScope","lodash","serviceTopologyConfig","NodeDrawer","ChartData",function(e,t,n,r,i,a,s){var c,o,u,l,d,p,v=this,h=0,m=s.logicTopologyData;this.computeElementPosition=function(e){var t=[],n=r.reduce(i.elWidths,function(e,t){return t+e},0),a=e-n-2*i.widthMargin,s=a/(i.elWidths.length-1);return r.forEach(i.elWidths,function(n,a){var c=0;0!==a&&(c=r.reduce(i.elWidths.slice(0,a),function(e,t){return t+e},0));var o=i.widthMargin+s*a+n/2+c;t.push(e-o)}),t};var b=function(e){var t=p.nodes(e);t.forEach(function(e){e.y=v.computeElementPosition(l)[e.depth]});var n=p.links(t);return[t,n]},g=function(e,t){var r=e.selectAll("g.node").data(t,function(e){return angular.isString(e.d3Id)||(e.d3Id="tree-"+ ++h),e.d3Id});r.enter().append("g").attr({"class":function(e){return"node "+e.type},transform:"translate("+l/2+", "+d/2+")"});a.addNetworks(r.filter(".network")),a.addRack(r.filter(".rack")),a.addPhisical(r.filter(".router")),a.addPhisical(r.filter(".subscriber")),a.addDevice(r.filter(".device")),r.filter(".subscriber").on("click",function(){n.$emit("subscriber.modal.open")});r.transition().duration(i.duration).attr({transform:function(e){return"translate("+e.y+","+e.x+")"}}),r.exit().remove()},f=function(e,t){c=d3.svg.diagonal().projection(function(e){return[e.y,e.x]});var n=e.selectAll("path.link").data(t,function(e){return e.target.d3Id});n.enter().insert("path","g").attr("class",function(e){return"link "+e.target.type}).attr("d",function(e){var t={x:d/2,y:l/2};return c({source:t,target:t})}),n.transition().duration(i.duration).attr("d",c),n.exit().remove()};this.setupTree=function(e){l=e.node().getBoundingClientRect().width,d=e.node().getBoundingClientRect().height;var t=l-2*i.widthMargin,n=d-2*i.heightMargin;p=d3.layout.tree().size([n,t])},this.updateTree=function(e){var t=b(m),n=_slicedToArray(t,2);o=n[0],u=n[1],g(e,o),f(e,u)}}])}(),function(){angular.module("xos.diagnostic").directive("logicTopology",function(){return{restrict:"E",scope:{subscribers:"=",selected:"="},bindToController:!0,controllerAs:"vm",templateUrl:"templates/logicTopology.tpl.html",controller:["$element","$log","$scope","$rootScope","$timeout","d3","LogicTopologyHelper","Node","Tenant","Ceilometer","serviceTopologyConfig","ChartData",function(e,t,n,r,i,a,s,c,o,u,l,d){var p=this;t.info("Logic Plane");var v;this.selectedInstances=[],this.hideInstanceStats=!0;var h=this,m=function(t){a.select(e[0]).select("svg").remove(),v=a.select(t).append("svg").style("width",t.clientWidth+"px").style("height",t.clientHeight+"px")},b=function(){d.getLogicTree().then(function(e){s.updateTree(v)})};b(),n.$watch(function(){return p.selected},function(e){e?(d.selectSubscriber(e),s.updateTree(v)):(d.removeSubscriber(),s.updateTree(v))}),r.$on("instance.detail.hide",function(){p.hideInstanceStats=!0,i(function(){p.selectedInstances=[],d.highlightInstances([]),s.updateTree(v)},500)}),r.$on("instance.detail",function(e,t){d.getInstanceStatus(t).then(function(e){s.updateTree(v)})["catch"](function(e){
+h.error="Service statistics are not available at this time. Please try again later.",i(function(){h.error=null},2e3)})}),a.select(window).on("resize.logic",function(){m(e[0]),s.setupTree(v),s.updateTree(v)}),m(e[0]),s.setupTree(v),this.selectSubscriberModal=function(){p.openSelectSubscriberModal=!0,n.$apply()},this.subscriberStatusModal=function(){p.openSubscriberStatusModal=!0,n.$apply()},r.$on("subscriber.modal.open",function(){d.currentSubscriber?p.subscriberStatusModal():p.selectSubscriberModal()}),r.$on("subscriber.modal.open",function(){d.currentSubscriber?(p.currentSubscriber=d.currentSubscriber,p.subscriberStatusModal()):p.selectSubscriberModal()})}]}})}(),function(){angular.module("xos.diagnostic").directive("diagnosticContainer",function(){return{restrict:"E",templateUrl:"templates/diagnostic.tpl.html",controllerAs:"vm",controller:["ChartData","Subscribers","ServiceRelation","$rootScope","$log",function(e,t,n,r,i){var a=this;this.loader=!0,this.error=!1;var s=function(){t.query().$promise.then(function(e){return a.subscribers=e,n.get()}).then(function(e){a.serviceChain=e})["catch"](function(e){throw new Error(e)})["finally"](function(){a.loader=!1})};s(),this.reloadGlobalScope=function(){a.selectedSubscriber=null,s()};var c=function(r){n.getBySubscriber(r).then(function(n){return a.serviceChain=n,e.currentServiceChain=n,t.getWithDevices({id:r.id}).$promise}).then(function(t){a.selectedSubscriber=t,e.currentSubscriber=t})};r.$on("subscriber.selected",function(e,t){c(t)})}]}})}(),function(){angular.module("xos.diagnostic").factory("d3",["$window",function(e){return e.d3}])}(),function(){angular.module("xos.diagnostic").constant("serviceTopologyConfig",{widthMargin:60,heightMargin:30,duration:750,elWidths:[20,104,105,104,20],circle:{radius:10,r:10,selectedRadius:15},square:{width:20,height:20,x:-10,y:-10},rack:{width:105,height:50,x:-30,y:-25},computeNode:{width:50,height:20,margin:5,labelHeight:10,x:-25,y:-10},instance:{width:80,height:36,margin:5,x:-40,y:-18},container:{width:60,height:130,margin:5,x:-30,y:-15}})}(),function(){angular.module("xos.diagnostic").service("ChartData",["$rootScope","$q","lodash","Tenant","Node","serviceTopologyConfig","Ceilometer","Instances",function(e,t,n,r,i,a,s,c){var o=this;this.currentSubscriber=null,this.currentServiceChain=null,this.logicTopologyData={name:"Router",type:"router",children:[{name:"WAN-Side",subtitle:"Virtual Network",type:"network",children:[{name:"Compute Servers",type:"rack",computeNodes:[],children:[{name:"LAN-Side",subtitle:"Virtual Network",type:"network",children:[{name:"Subscriber",type:"subscriber"}]}]}]}]},this.getLogicTree=function(){var e=t.defer();return i.queryWithInstances().$promise.then(function(t){o.logicTopologyData.children[0].children[0].computeNodes=t,e.resolve(o.logicTopologyData)}),e.promise},this.addSubscriberTag=function(e){o.logicTopologyData.children[0].children[0].children[0].subscriberTag={cTag:e.cTag,sTag:e.sTag}},this.addSubscriber=function(e){return e.children=e.devices,o.logicTopologyData.children[0].children[0].children[0].children=[e],o.logicTopologyData},this.removeSubscriber=function(){o.logicTopologyData.children[0].children[0].children[0].children[0].humanReadableName="Subscriber",o.currentSubscriber=null,160===a.elWidths[a.elWidths.length-1]&&a.elWidths.pop(),delete o.logicTopologyData.children[0].children[0].children[0].subscriberTag,delete o.logicTopologyData.children[0].subscriberIP,o.highlightInstances([]),delete o.logicTopologyData.children[0].children[0].children[0].children[0].children},this.getSubscriberTag=function(e){var t={cTag:e.c_tag,sTag:e.s_tag};o.addSubscriberTag(t),o.currentSubscriber.tags=t},this.getSubscriberIP=function(e){o.logicTopologyData.children[0].subscriberIP=e.wan_container_ip},this.selectSubscriber=function(e){a.elWidths.push(160),o.addSubscriber(angular.copy(e)),o.highlightInstances([]),o.getSubscriberTag(e),o.getSubscriberIP(e)},this.highlightInstances=function(e){var t=o.logicTopologyData.children[0].children[0].computeNodes;t.map(function(e){e.instances.map(function(e){return e.selected=!1,e})}),n.forEach(e,function(e){t.map(function(t){t.instances.map(function(t){return t.id===e.id&&(t.selected=!0,t.stats=e.stats,t.container=e.container),t})})})},this.getInstanceStatus=function(e){var i=t.defer(),a=void 0;if(o.currentSubscriber){var u=void 0;try{u=JSON.parse(e.tenant.service_specific_attribute)}catch(l){u=null}if(u&&u.instance_id)!function(){var e={};a=c.get({id:u.instance_id}).$promise.then(function(t){return e=t,s.getInstanceStats(e.instance_uuid)}).then(function(t){e.stats=t;var n="vcpe-"+o.currentSubscriber.tags.sTag+"-"+o.currentSubscriber.tags.cTag;return e.container={name:n},s.getContainerStats(n)}).then(function(t){return e.container.stats=t.stats,e.container.port=t.port,[e]})}();else{var d=t.defer();d.resolve([]),a=d.promise}}else{var p={service_vsg:{kind:"vCPE"},service_vbng:{kind:"vBNG"},service_volt:{kind:"vOLT"}};a=r.queryVsgInstances(p[e.name]).$promise.then(function(e){return s.getInstancesStats(n.uniq(e))})}return a.then(function(e){o.highlightInstances(e),i.resolve(e)})["catch"](function(e){i.reject(e)}),i.promise}}])}(),angular.module("xos.diagnostic").run(["$location",function(e){e.path("/")}]);
\ No newline at end of file
diff --git a/xos/core/xoslib/static/js/xosSampleView.js b/xos/core/xoslib/static/js/xosSampleView.js
index 69ec0bc..9c05349 100644
--- a/xos/core/xoslib/static/js/xosSampleView.js
+++ b/xos/core/xoslib/static/js/xosSampleView.js
@@ -1 +1 @@
-"use strict";angular.module("xos.sampleView",["ngResource","ngCookies","ngLodash","ui.router","xos.helpers"]).config(["$stateProvider",function(s){s.state("user-list",{url:"/",template:"<users-list></users-list>"})}]).config(["$httpProvider",function(s){s.interceptors.push("NoHyperlinks")}]).directive("usersList",function(){return{restrict:"E",scope:{},bindToController:!0,controllerAs:"vm",templateUrl:"templates/users-list.tpl.html",controller:["XosApi",function(s){var e=this;s.User_List_GET().then(function(s){e.users=s})["catch"](function(s){throw new Error(s)})}]}}),angular.module("xos.sampleView").run(["$templateCache",function(s){s.put("templates/users-list.tpl.html",'<div class="row">\n  <h1>Users List</h1>\n  <p>This is only an example view.</p>\n</div>\n<div class="row">\n  <div class="span4">Email</div>\n  <div class="span4">First Name</div>\n  <div class="span4">Last Name</div>\n</div>  \n<div class="row" ng-repeat="user in vm.users">\n  <div class="span4">{{user.email}}</div>\n  <div class="span4">{{user.firstname}}</div>\n  <div class="span4">{{user.lastname}}</div>\n</div>  ')}]),angular.bootstrap(angular.element("#xosSampleView"),["xos.sampleView"]);
\ No newline at end of file
+"use strict";angular.module("xos.sampleView",["ngResource","ngCookies","ngLodash","ui.router","xos.helpers"]).config(["$stateProvider",function(s){s.state("user-list",{url:"/",template:"<users-list></users-list>"})}]).config(["$httpProvider",function(s){s.interceptors.push("NoHyperlinks")}]).directive("usersList",function(){return{restrict:"E",scope:{},bindToController:!0,controllerAs:"vm",templateUrl:"templates/users-list.tpl.html",controller:["Users",function(s){var e=this;s.query().$promise.then(function(s){e.users=s})["catch"](function(s){throw new Error(s)})}]}}),angular.module("xos.sampleView").run(["$templateCache",function(s){s.put("templates/users-list.tpl.html",'<div class="row">\n  <h1>Users List</h1>\n  <p>This is only an example view.</p>\n</div>\n<div class="row">\n  <div class="col-xs-4">Email</div>\n  <div class="col-xs-4">First Name</div>\n  <div class="col-xs-4">Last Name</div>\n</div>  \n<div class="row" ng-repeat="user in vm.users">\n  <div class="col-xs-4">{{user.email}}</div>\n  <div class="col-xs-4">{{user.firstname}}</div>\n  <div class="col-xs-4">{{user.lastname}}</div>\n</div>  ')}]),angular.module("xos.sampleView").run(["$location",function(s){s.path("/")}]);
\ No newline at end of file
diff --git a/xos/templates/admin/base.html b/xos/templates/admin/base.html
index 3af6b33..b47bf74 100644
--- a/xos/templates/admin/base.html
+++ b/xos/templates/admin/base.html
@@ -39,7 +39,6 @@
   <!-- ngXosLib -->
   <script src="{% static 'js/vendor/ngXosVendor.js' %}"></script>
   <script src="{% static 'js/vendor/ngXosHelpers.js' %}"></script>
-  <script src="{% static 'js/xosApi.js' %}"></script>
 
   <script type="text/javascript">var Suit = { $: $.noConflict() }; if (!$) $ = Suit.$; </script>
   {% if 'SHOW_REQUIRED_ASTERISK'|suit_conf %}
diff --git a/xos/tests/api/apiary.apib b/xos/tests/api/apiary.apib
index 7db0509..f11d612 100644
--- a/xos/tests/api/apiary.apib
+++ b/xos/tests/api/apiary.apib
@@ -3,6 +3,49 @@
 # XOS
  
  
+# Group Users
+
+List of the XOS users
+
+## Users [/api/core/users/]
+
+### List all Users [GET]
+
++ Response 200 (application/json)
+
+        [
+            {
+                "id": 2,
+                "password": "pbkdf2_sha256$12000$9gn8DmZuIoz2$YPQkx3AOOV7jZNYr2ddrgUCkiuaPpvb8+aJR7RwLZNA=",
+                "last_login": "2016-04-12T18:50:45.880823Z",
+                "email": "johndoe@myhouse.com",
+                "username": "johndoe@myhouse.com",
+                "firstname": "john",
+                "lastname": "doe",
+                "phone": null,
+                "user_url": null,
+                "site": "http://xos.dev:9999/api/core/sites/1/",
+                "public_key": null,
+                "is_active": true,
+                "is_admin": false,
+                "is_staff": true,
+                "is_readonly": false,
+                "is_registering": false,
+                "is_appuser": false,
+                "login_page": null,
+                "created": "2016-04-12T18:50:45.912602Z",
+                "updated": "2016-04-12T18:50:45.912671Z",
+                "enacted": null,
+                "policed": null,
+                "backend_status": "Provisioning in progress",
+                "deleted": false,
+                "write_protect": false,
+                "timezone": "America/New_York"
+            }
+        ]
+        
+ 
+ 
 # Group Example
 
 ## Example Services Collection [/api/service/exampleservice/]
@@ -22,6 +65,8 @@
  
 # Group ONOS Services
 
+List of the active onos services
+
 ## ONOS Services Collection [/api/service/onos/]
 
 ### List all ONOS Services [GET]
@@ -63,7 +108,7 @@
 
 Resource related to the CORD Subscribers.
 
-## Subscribers Collection [/api/tenant/cord/subscriber/]
+## Subscribers [/api/tenant/cord/subscriber/{subscriber_id}/]
 
 ### List All Subscribers [GET]
 
@@ -97,13 +142,12 @@
             }
         ]
 
-## Subscriber Detail [/api/tenant/cord/subscriber/{subscriber_id}/]
+
+### View a Subscriber Detail [GET]
 
 + Parameters
     + subscriber_id: 1 (number) - ID of the Subscriber in the form of an integer
 
-### View a Subscriber Detail [GET]
-
 + Response 200 (application/json)
  
         {
@@ -134,6 +178,9 @@
 
 ### Delete a Subscriber [DELETE]
 
++ Parameters
+    + subscriber_id: 1 (number) - ID of the Subscriber in the form of an integer
+
 + Response 204
 
 ### Subscriber features [/api/tenant/cord/subscriber/{subscriber_id}/features/]
@@ -293,7 +340,7 @@
 
 Virtual Truckroll, enable to perform basic test on user connectivity such as ping, traceroute and tcpdump.
 
-## Truckroll Collection [/api/tenant/truckroll/]
+## Truckroll Collection [/api/tenant/truckroll/{truckroll_id}/]
 
 ### List all Truckroll [GET]
 
@@ -317,6 +364,8 @@
 
 ### Create a Truckroll [POST]
 
+A virtual truckroll is complete once is_synced equal true
+
 + Request (application/json)
 
         {
@@ -342,15 +391,12 @@
             "backend_status": "0 - Provisioning in progress"
         }
 
-## Truckroll Detail [/api/tenant/truckroll/{truckroll_id}/]
 
-A virtual truckroll is complete once is_synced equal true
+### View a Truckroll Detail [GET]
 
 + Parameters
     + truckroll_id: 1 (number) - ID of the Truckroll in the form of an integer
 
-### View a Truckroll Detail [GET]
-
 + Response 200 (application/json)
 
         {
@@ -369,6 +415,9 @@
 
 ### Delete a Truckroll Detail [DELETE]
 
++ Parameters
+    + truckroll_id: 1 (number) - ID of the Truckroll in the form of an integer
+
 + Response 204
 
  
@@ -377,7 +426,7 @@
 
 OLT devices aggregate a set of subscriber connections
 
-## vOLT Collection [/api/tenant/cord/volt/]
+## vOLT Collection [/api/tenant/cord/volt/{volt_id}/]
 
 ### List all vOLT [GET]
 
@@ -429,15 +478,11 @@
                 }
             }
 
-## vOLT Detail [/api/tenant/cord/volt/{volt_id}/]
-
-A virtual volt is complete once is_synced equal true
+### View a vOLT Detail [GET]
 
 + Parameters
     + volt_id: 1 (number) - ID of the vOLT in the form of an integer
 
-### View a vOLT Detail [GET]
-
 + Response 200 (application/json)
 
         {
@@ -460,7 +505,7 @@
  
 # Group ONOS Apps
 
-## app Collection [/api/tenant/onos/app/]
+## ONOS App Collection [/api/tenant/onos/app/]
 
 ### List all apps [GET]
 
diff --git a/xos/tests/api/hooks.py b/xos/tests/api/hooks.py
index e327852..ccf0c6c 100644
--- a/xos/tests/api/hooks.py
+++ b/xos/tests/api/hooks.py
@@ -196,13 +196,13 @@
     setUpTruckroll()
 
 
-@hooks.before("Truckroll > Truckroll Detail > View a Truckroll Detail")
+@hooks.before("Truckroll > Truckroll Collection > View a Truckroll Detail")
 def test2(transaction):
     deleteTruckrolls()
     createTruckroll()
 
 
-@hooks.before("Truckroll > Truckroll Detail > Delete a Truckroll Detail")
+@hooks.before("Truckroll > Truckroll Collection > Delete a Truckroll Detail")
 def test3(transaction):
     deleteTruckrolls()
     createTruckroll()
diff --git a/xos/tests/api/source/core/users.md b/xos/tests/api/source/core/users.md
new file mode 100644
index 0000000..c5dcf68
--- /dev/null
+++ b/xos/tests/api/source/core/users.md
@@ -0,0 +1,41 @@
+# Group Users
+
+List of the XOS users
+
+## Users [/api/core/users/]
+
+### List all Users [GET]
+
++ Response 200 (application/json)
+
+        [
+            {
+                "id": 2,
+                "password": "pbkdf2_sha256$12000$9gn8DmZuIoz2$YPQkx3AOOV7jZNYr2ddrgUCkiuaPpvb8+aJR7RwLZNA=",
+                "last_login": "2016-04-12T18:50:45.880823Z",
+                "email": "johndoe@myhouse.com",
+                "username": "johndoe@myhouse.com",
+                "firstname": "john",
+                "lastname": "doe",
+                "phone": null,
+                "user_url": null,
+                "site": "http://xos.dev:9999/api/core/sites/1/",
+                "public_key": null,
+                "is_active": true,
+                "is_admin": false,
+                "is_staff": true,
+                "is_readonly": false,
+                "is_registering": false,
+                "is_appuser": false,
+                "login_page": null,
+                "created": "2016-04-12T18:50:45.912602Z",
+                "updated": "2016-04-12T18:50:45.912671Z",
+                "enacted": null,
+                "policed": null,
+                "backend_status": "Provisioning in progress",
+                "deleted": false,
+                "write_protect": false,
+                "timezone": "America/New_York"
+            }
+        ]
+        
\ No newline at end of file
diff --git a/xos/tests/api/source/service/onos.md b/xos/tests/api/source/service/onos.md
index 3b5e623..0b82a86 100644
--- a/xos/tests/api/source/service/onos.md
+++ b/xos/tests/api/source/service/onos.md
@@ -1,5 +1,7 @@
 # Group ONOS Services
 
+List of the active onos services
+
 ## ONOS Services Collection [/api/service/onos/]
 
 ### List all ONOS Services [GET]
diff --git a/xos/tests/api/source/tenant/cord/subscribers.md b/xos/tests/api/source/tenant/cord/subscribers.md
index bd38d04..61126ce 100644
--- a/xos/tests/api/source/tenant/cord/subscribers.md
+++ b/xos/tests/api/source/tenant/cord/subscribers.md
@@ -2,7 +2,7 @@
 
 Resource related to the CORD Subscribers.
 
-## Subscribers Collection [/api/tenant/cord/subscriber/]
+## Subscribers [/api/tenant/cord/subscriber/{subscriber_id}/]
 
 ### List All Subscribers [GET]
 
@@ -36,13 +36,12 @@
             }
         ]
 
-## Subscriber Detail [/api/tenant/cord/subscriber/{subscriber_id}/]
+
+### View a Subscriber Detail [GET]
 
 + Parameters
     + subscriber_id: 1 (number) - ID of the Subscriber in the form of an integer
 
-### View a Subscriber Detail [GET]
-
 + Response 200 (application/json)
  
         {
@@ -73,6 +72,9 @@
 
 ### Delete a Subscriber [DELETE]
 
++ Parameters
+    + subscriber_id: 1 (number) - ID of the Subscriber in the form of an integer
+
 + Response 204
 
 ### Subscriber features [/api/tenant/cord/subscriber/{subscriber_id}/features/]
diff --git a/xos/tests/api/source/tenant/cord/truckroll.md b/xos/tests/api/source/tenant/cord/truckroll.md
index 33b67db..996c19c 100644
--- a/xos/tests/api/source/tenant/cord/truckroll.md
+++ b/xos/tests/api/source/tenant/cord/truckroll.md
@@ -2,7 +2,7 @@
 
 Virtual Truckroll, enable to perform basic test on user connectivity such as ping, traceroute and tcpdump.
 
-## Truckroll Collection [/api/tenant/truckroll/]
+## Truckroll Collection [/api/tenant/truckroll/{truckroll_id}/]
 
 ### List all Truckroll [GET]
 
@@ -26,6 +26,8 @@
 
 ### Create a Truckroll [POST]
 
+A virtual truckroll is complete once is_synced equal true
+
 + Request (application/json)
 
         {
@@ -51,15 +53,12 @@
             "backend_status": "0 - Provisioning in progress"
         }
 
-## Truckroll Detail [/api/tenant/truckroll/{truckroll_id}/]
 
-A virtual truckroll is complete once is_synced equal true
+### View a Truckroll Detail [GET]
 
 + Parameters
     + truckroll_id: 1 (number) - ID of the Truckroll in the form of an integer
 
-### View a Truckroll Detail [GET]
-
 + Response 200 (application/json)
 
         {
@@ -78,4 +77,7 @@
 
 ### Delete a Truckroll Detail [DELETE]
 
++ Parameters
+    + truckroll_id: 1 (number) - ID of the Truckroll in the form of an integer
+
 + Response 204
diff --git a/xos/tests/api/source/tenant/cord/volt.md b/xos/tests/api/source/tenant/cord/volt.md
index 09140fd..51cb679 100644
--- a/xos/tests/api/source/tenant/cord/volt.md
+++ b/xos/tests/api/source/tenant/cord/volt.md
@@ -2,7 +2,7 @@
 
 OLT devices aggregate a set of subscriber connections
 
-## vOLT Collection [/api/tenant/cord/volt/]
+## vOLT Collection [/api/tenant/cord/volt/{volt_id}/]
 
 ### List all vOLT [GET]
 
@@ -54,15 +54,11 @@
                 }
             }
 
-## vOLT Detail [/api/tenant/cord/volt/{volt_id}/]
-
-A virtual volt is complete once is_synced equal true
+### View a vOLT Detail [GET]
 
 + Parameters
     + volt_id: 1 (number) - ID of the vOLT in the form of an integer
 
-### View a vOLT Detail [GET]
-
 + Response 200 (application/json)
 
         {
diff --git a/xos/tests/api/source/tenant/onos/app.md b/xos/tests/api/source/tenant/onos/app.md
index a43aae9..5376c33 100644
--- a/xos/tests/api/source/tenant/onos/app.md
+++ b/xos/tests/api/source/tenant/onos/app.md
@@ -1,6 +1,6 @@
 # Group ONOS Apps
 
-## app Collection [/api/tenant/onos/app/]
+## ONOS App Collection [/api/tenant/onos/app/]
 
 ### List all apps [GET]
 
