Enabled CORS
Fetching info from L-XOS

Change-Id: I6293f15adeb7716cc31499b18d35bdedf8f7cde1
diff --git a/views/ngXosViews/globalXos/src/css/dev.css b/views/ngXosViews/globalXos/src/css/dev.css
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/views/ngXosViews/globalXos/src/css/dev.css
diff --git a/views/ngXosViews/globalXos/src/css/main.css b/views/ngXosViews/globalXos/src/css/main.css
new file mode 100644
index 0000000..be8d4bf
--- /dev/null
+++ b/views/ngXosViews/globalXos/src/css/main.css
@@ -0,0 +1,7 @@
+#xosGlobalXos ui-view,
+#xosGlobalXos xos-list {
+  display: block; }
+
+#xosGlobalXos .well.active {
+  background: #337ab7;
+  color: #fff; }
diff --git a/views/ngXosViews/globalXos/src/icons/README.md b/views/ngXosViews/globalXos/src/icons/README.md
new file mode 100644
index 0000000..0d5a787
--- /dev/null
+++ b/views/ngXosViews/globalXos/src/icons/README.md
@@ -0,0 +1,18 @@
+# Set icon for custom dashboard 
+
+## Path to add images : <dashboard-name>/src/icons
+
+### Add two images with name in following format 
+    <dashboard-name>-icon.png
+    <dashboard-name>-icon-active.png
+
+Examples: 
+    If your dashboard name is `slice`
+    then you will add the images with the name as
+        `slice-icon.png`
+        `slice-icon-active.png`
+        
+        
+        
+        
+Note: If you don't add the custom images, then dashboard will use the default images stored at `/xos/core/xoslib/static/images`
\ No newline at end of file
diff --git a/views/ngXosViews/globalXos/src/index.html b/views/ngXosViews/globalXos/src/index.html
new file mode 100644
index 0000000..4f2206e
--- /dev/null
+++ b/views/ngXosViews/globalXos/src/index.html
@@ -0,0 +1,38 @@
+<!-- browserSync -->
+<!-- bower:css -->
+<link rel="stylesheet" href="vendor/bootstrap-css/css/bootstrap.min.css" />
+<link rel="stylesheet" href="vendor/angular-chart.js/dist/angular-chart.css" />
+<!-- endbower -->
+<!-- endcss -->
+<!-- inject:css -->
+<link rel="stylesheet" href="/css/dev.css">
+<link rel="stylesheet" href="/css/main.css">
+<link rel="stylesheet" href="/../../../xos/core/static/xosNgLib.css">
+<!-- endinject -->
+
+<div ng-app="xos.globalXos" id="xosGlobalXos" class="container-fluid">
+  <div ui-view></div>
+</div>
+
+<!-- bower:js -->
+<script src="vendor/angular/angular.js"></script>
+<script src="vendor/angular-bootstrap/ui-bootstrap-tpls.js"></script>
+<script src="vendor/jquery/dist/jquery.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-animate/angular-animate.js"></script>
+<script src="vendor/angular-resource/angular-resource.js"></script>
+<script src="vendor/lodash/lodash.js"></script>
+<script src="vendor/bootstrap-css/js/bootstrap.min.js"></script>
+<script src="vendor/Chart.js/Chart.js"></script>
+<script src="vendor/angular-chart.js/dist/angular-chart.js"></script>
+<script src="vendor/d3/d3.js"></script>
+<script src="vendor/angular-recursion/angular-recursion.js"></script>
+<!-- endbower -->
+<!-- endjs -->
+<!-- inject:js -->
+<script src="/vendor/ng-xos-lib/dist/ngXosHelpers.min.js"></script>
+<script src="/.tmp/main.js"></script>
+<script src="/.tmp/rest.js"></script>
+<!-- endinject -->
\ No newline at end of file
diff --git a/views/ngXosViews/globalXos/src/js/main.js b/views/ngXosViews/globalXos/src/js/main.js
new file mode 100644
index 0000000..b512ad4
--- /dev/null
+++ b/views/ngXosViews/globalXos/src/js/main.js
@@ -0,0 +1,360 @@
+'use strict';
+
+angular.module('xos.globalXos', [
+  'ngResource',
+  'ngCookies',
+  'ui.router',
+  'xos.helpers',
+  'ui.bootstrap.modal',
+  'ui.bootstrap.tpls'
+])
+.config(($stateProvider) => {
+  $stateProvider
+  .state('xos-list', {
+    url: '/',
+    template: '<xos-list></xos-list>'
+  });
+})
+.config(function($httpProvider){
+  $httpProvider.interceptors.push('NoHyperlinks');
+})
+.value('LXOS', [])
+.directive('xosList', function(){
+  return {
+    restrict: 'E',
+    scope: {},
+    bindToController: true,
+    controllerAs: 'vm',
+    templateUrl: 'templates/xos-list.tpl.html',
+    controller: function($window, $q, _, Controllers, LXOS, LocalAuth, LocalSlices, LocalUsers, $uibModal, Slices){
+      const self = this;
+      $q.all([
+        Controllers.query({backend_type: 'XOS'}).$promise,
+        Slices.query().$promise // NOTE why this is queryFromAll??
+      ])
+      .then(res => {
+        [this.xoss, this.gSlices] = res;
+      });
+
+      this.openLocally = (itemKind) => {
+        return (item) => {
+          $window.open(`${item.xos.auth_url}admin/core/${itemKind}/${item.id}`, '_blank');
+        }
+      };
+
+      const baseSliceCols = [
+        {
+          label: 'Name',
+          prop: 'name',
+        },
+        {
+          label: 'Mount Data Sets',
+          prop: 'mount_data_sets'
+        }
+      ];
+
+      const lXosSliceCols = [
+        {
+          label: 'Max Instances',
+          prop: 'max_instances'
+        },
+        {
+          label: 'Instances',
+          prop: 'instance_total'
+        },
+        {
+          label: 'L-XOS',
+          type: 'custom',
+          formatter: item => item.xos.name
+        }
+      ];
+
+      this.gSliceTableCgf = {
+        columns: baseSliceCols,
+        filter: 'field',
+        order: true,
+        actions: [
+          {
+            label: 'Get Instances',
+            icon: 'search',
+            cb: (item) => {
+              $uibModal.open({
+                animation: true,
+                size: 'lg',
+                templateUrl: 'listInstances.html',
+                controllerAs: 'vm',
+                resolve: {
+                  slice: function () {
+                    return {
+                      name: item.name,
+                      xos: {
+                        name: 'G-XOS'
+                      }
+                    };
+                  }
+                },
+                controller: function($uibModalInstance, slice, LocalInstances, LocalSlices) {
+                  this.slice = slice;
+
+                  this.config = {
+                    columns: [
+                      {
+                        label: 'Name',
+                        prop: 'name',
+                      }
+                    ]
+                  };
+
+                  LocalSlices.queryFromAll(self.xoss).$promise
+                  .then(slices => {
+                    // keep only the slice that match the name
+                    this.slicesId = slices
+                      .filter(s => s.name.indexOf(this.slice.name) > -1)
+                      .reduce((o, s) => {
+                        o[s.xos.id] = s.id;
+                        return o;
+                      }, {});
+                    return LocalInstances.queryFromAll(self.xoss).$promise;
+                  })
+                  .then(instances => {
+                    this.instances = instances.filter(i => this.slicesId[i.xos.id] === i.slice);
+                  })
+                  .catch(e => {
+                    this.instances = [];
+                  });
+
+                  this.close = () => {
+                    $uibModalInstance.dismiss('cancel');
+                  }
+                }
+              })
+            }
+          },
+        ]
+      };
+
+      this.sliceTableCfg = {
+        columns: baseSliceCols.concat(lXosSliceCols),
+        actions: [
+          {
+            label: 'open locally',
+            icon: 'open',
+            cb: this.openLocally('slice')
+          },
+          {
+            label: 'Get Instances',
+            icon: 'search',
+            cb: (item) => {
+              $uibModal.open({
+                animation: true,
+                size: 'lg',
+                templateUrl: 'listInstances.html',
+                controllerAs: 'vm',
+                resolve: {
+                  slice: function () {
+                    return item;
+                  }
+                },
+                controller: function($uibModalInstance, slice, LocalInstances) {
+                  this.slice = slice;
+
+                  this.config = {
+                    columns: [
+                      {
+                        label: 'Name',
+                        prop: 'name',
+                      },
+                      {
+                        label: 'deployment',
+                        prop: 'deployment',
+                      },
+                    ]
+                  };
+
+                  LocalInstances.getFromLocal(slice.xos)
+                  .then(instances => {
+                    this.instances = instances.filter(i => i.slice === slice.id);
+                  });
+
+                  this.close = () => {
+                    $uibModalInstance.dismiss('cancel');
+                  }
+                }
+              })
+            }
+          },
+          {
+            label: 'Add Instance',
+            icon: 'plus',
+            cb: (item) => {
+              $uibModal.open({
+                animation: true,
+                size: 'lg',
+                templateUrl: 'addInstance.html',
+                controller: function($uibModalInstance, slice, LocalInstances){
+                  this.slice = slice;
+
+                  this.model = {};
+
+                  LocalInstances.getLocalInfo(slice.xos)
+                  .then((res) => {
+                    [
+                      this.config.fields['deployment'].options,
+                      this.config.fields['image'].options,
+                      this.config.fields['flavor'].options,
+                      this.config.fields['node'].options
+                    ] = res;
+                  });
+
+                  this.config = {
+                    formName: 'instanceForm',
+                    excludedFields: ['xos', 'slice'],
+                    actions: [
+                      {
+                        label: 'Save',
+                        icon: 'ok',
+                        cb: (instance) => {
+                          instance.xos = slice.xos;
+                          instance.slice = slice.id;
+                          instance.creator = instance.xos.user.id;
+                          LocalInstances.createOnLocal(instance)
+                          .then(res => {
+                            slice.instance_total = slice.instance_total + 1;
+                            $uibModalInstance.close();
+                          });
+                        },
+                        class: 'success'
+                      },
+                      {
+                        label: 'Cancel',
+                        icon: 'remove',
+                        cb: () => {
+                          $uibModalInstance.dismiss('cancel');
+                        },
+                        class: 'warning'
+                      }
+                    ],
+                    fields: {
+                      name: {
+                        type: 'text',
+                        validators: {
+                          required: true
+                        }
+                      },
+                      deployment: {
+                        type: 'select',
+                        validators: {
+                          required: true
+                        }
+                      },
+                      node: {
+                        type: 'select',
+                        validators: {
+                          required: true
+                        }
+                      },
+                      image: {
+                        type: 'select',
+                        validators: {
+                          required: true,
+                        }
+                      },
+                      flavor: {
+                        type: 'select',
+                        validators: {
+                          required: true,
+                        }
+                      },
+                      isolation: {
+                        type: 'select',
+                        options: [
+                          {id: 'vm', label: 'VM'},
+                          {id: 'container', label: 'Container'},
+                          {id: 'container_vm', label: 'Container in VM'}
+                        ],
+                        validators: {
+                          required: true,
+                        }
+                      },
+                    }
+                  };
+                },
+                controllerAs: 'vm',
+                resolve: {
+                  slice: function () {
+                    return item;
+                  }
+                }
+              });
+            }
+          }
+        ],
+        filter: 'field',
+        order: true
+      };
+
+      this.usersTableCfg = {
+        columns: [
+          {
+            label: 'Name',
+            type: 'custom',
+            formatter: item => `${item.firstname} ${item.lastname}`
+          },
+          {
+            label: 'E-Mail',
+            prop: 'email'
+          },
+          {
+            label: 'User Name',
+            prop: 'username'
+          },
+          {
+            label: 'Time Zone',
+            prop: 'timezone'
+          },
+          {
+            label: 'L-XOS',
+            type: 'custom',
+            formatter: item => item.xos.name
+          }
+        ],
+        actions: [
+          {
+            label: 'open locally',
+            icon: 'open',
+            cb: this.openLocally('user')
+          }
+        ],
+        filter: 'field',
+        order: true
+      };
+
+      this.toggleXos = (xos) => {
+        if(_.findIndex(LXOS, {id: xos.id}) > -1){
+          xos.active = false;
+          _.remove(LXOS, {id: xos.id});
+        }
+        else{
+          xos.active = true;
+          LXOS.push(xos);
+        }
+
+        // authenticate on L-XOS
+        LocalAuth.login()
+        .then(() => {
+          // fetch slices
+          return $q.all([
+            LocalSlices.queryFromAll().$promise,
+            LocalUsers.queryFromAll().$promise,
+          ]);
+        })
+        .then(res => {
+          [this.localSlices, this.localUsers] = res;
+        })
+        .catch(e => {
+          console.log(e);
+        });
+      }
+    }
+  };
+});
\ No newline at end of file
diff --git a/views/ngXosViews/globalXos/src/js/rest.js b/views/ngXosViews/globalXos/src/js/rest.js
new file mode 100644
index 0000000..012daa8
--- /dev/null
+++ b/views/ngXosViews/globalXos/src/js/rest.js
@@ -0,0 +1,268 @@
+/**
+ * © OpenCORD
+ *
+ * Visit http://guide.xosproject.org/devguide/addview/ for more information
+ *
+ * Created by teone on 10/20/16.
+ */
+
+(function () {
+  'use strict';
+  angular.module('xos.globalXos')
+  .service('Controllers', function($resource){
+    return $resource('/api/core/controllers/:id', {id: '@id'});
+  })
+  .service('GXOS', function($q, $resource, _, LXOS){
+    this.attachXosToItem = (items, target = LXOS) => {
+      return _.map(items, (lxos, i) => {
+        let currentXos = target[i];
+        return _.map(lxos, (item) => {
+          item.xos = currentXos;
+          return item;
+        });
+      })
+    };
+
+    this.buildQueryEndpoint = (baseUrl, target = LXOS) => {
+      return () => {
+        const d = $q.defer();
+
+        // store generated $resource for each L-XOS
+        let r = [];
+
+        let p = [];
+        _.forEach(target, (xos, i) => {
+          let resource = $resource(`${xos.auth_url}${baseUrl}`, {id: '@id'}, {
+            query: {
+              isArray: true,
+              headers: {
+                Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}`
+              }
+            }
+          });
+          r.push(resource);
+          p.push(r[i].query().$promise);
+        });
+
+        $q.all(p)
+        .then(res => {
+          res = this.attachXosToItem(res, target);
+          d.resolve(_.flatten(res));
+        })
+        .catch(d.reject);
+
+        return {$promise: d.promise};
+      };
+    };
+
+    // TODO evaluate
+    this.buildLocalResource = (baseUrl, xos) => {
+      const headers = {
+        Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}`
+      };
+      const resource = $resource(`${xos.auth_url}${baseUrl}`, {id: '@id'}, {
+        query: {
+          isArray: true,
+          headers: headers
+        }
+      });
+      return resource;
+    }
+  })
+  .service('LocalAuth', function ($q, $http, _, LXOS) {
+    const baseUrl = `api/utility/login/`;
+    this.login = () => {
+      const d = $q.defer();
+
+      let p = [];
+      _.forEach(LXOS, (xos, i) => {
+        let loginRequest = $http.post(`${xos.auth_url}${baseUrl}`, {username: xos.admin_user, password: xos.admin_password});
+        p.push(loginRequest);
+      });
+
+      $q.all(p)
+      .then(auths => {
+        _.forEach(auths, (auth, i) => {
+          LXOS[i].xoscsrftoken = auth.data.xoscsrftoken;
+          LXOS[i].xossessionid = auth.data.xossessionid;
+          LXOS[i].user = JSON.parse(auth.data.user);
+        });
+        d.resolve();
+      })
+      .catch(e => {
+        d.reject(e);
+      });
+
+      return d.promise;
+    }
+  })
+  .service('LocalSlices', function(GXOS){
+    const baseUrl = `api/utility/slicesplus/`;
+
+    // TODO build a global resource
+    this.queryFromAll = (targets) => {
+      return GXOS.buildQueryEndpoint(baseUrl, targets)();
+    };
+  })
+  .service('LocalUsers', function(GXOS){
+    const baseUrl = `api/core/users/`;
+
+    // TODO build a global resource
+    this.queryFromAll = GXOS.buildQueryEndpoint(baseUrl);
+  })
+  .service('LocalInstances', function($q, $http, GXOS, LocalDeployments, LocalImages, LocalFlavor, LocalNode){
+    const baseUrl = `api/core/instances/`;
+
+    // NOTE Evaluate to dinamically create a resource targeted to a L-XOS
+
+    this.queryFromAll = (targets) => {
+      return GXOS.buildQueryEndpoint(baseUrl, targets)();
+    };
+    this.createOnLocal = (instance) => {
+      const d = $q.defer();
+      const xos = instance.xos;
+      delete instance.xos;
+      $http.post(`${xos.auth_url}${baseUrl}`, instance, {
+        headers: {
+          Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}`
+        }
+      })
+      .then((inst) => {
+        d.resolve(inst);
+      })
+      .catch(e => {
+        d.reject(e);
+      });
+
+      return d.promise;
+    };
+
+    this.getFromLocal = (xos, params) => {
+      const d = $q.defer();
+      $http.get(`${xos.auth_url}${baseUrl}`, {
+        params: params,
+        headers: {
+          Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}`
+        }
+      })
+        .then((inst) => {
+          d.resolve(inst.data);
+        })
+        .catch(e => {
+          d.reject(e);
+        });
+
+      return d.promise;
+    };
+
+    this.getLocalInfo = (xos) => {
+      const d = $q.defer();
+      $q.all([
+        LocalDeployments.queryFromLocal(xos),
+        LocalImages.queryFromLocal(xos),
+        LocalFlavor.queryFromLocal(xos),
+        LocalNode.queryFromLocal(xos)
+      ])
+        .then((res) => {
+          res = _.map(res, collection => {
+            return _.map(collection, item => {
+              return {id: item.id, label: item.name}
+            });
+          });
+          d.resolve(res);
+        })
+        .catch(d.reject);
+      return d.promise;
+    };
+
+  })
+  .service('LocalDeployments', function($q, $http){
+
+    const baseUrl = `api/core/deployments/`;
+
+    this.queryFromLocal = (xos) => {
+      const d = $q.defer();
+
+      $http.get(`${xos.auth_url}${baseUrl}`, {
+        headers: {
+          Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}`
+        }
+      })
+      .then((res) => {
+        d.resolve(res.data);
+      })
+      .catch(e => {
+        d.reject(e);
+      });
+
+      return d.promise;
+    }
+  })
+  .service('LocalImages', function($q, $http){
+
+    const baseUrl = `api/core/images/`;
+
+    this.queryFromLocal = (xos) => {
+      const d = $q.defer();
+
+      $http.get(`${xos.auth_url}${baseUrl}`, {
+        headers: {
+          Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}`
+        }
+      })
+      .then((res) => {
+        d.resolve(res.data);
+      })
+      .catch(e => {
+        d.reject(e);
+      });
+
+      return d.promise;
+    }
+  })
+  .service('LocalFlavor', function($q, $http){
+
+    const baseUrl = `api/core/flavors/`;
+
+    this.queryFromLocal = (xos) => {
+      const d = $q.defer();
+
+      $http.get(`${xos.auth_url}${baseUrl}`, {
+        headers: {
+          Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}`
+        }
+      })
+        .then((res) => {
+          d.resolve(res.data);
+        })
+        .catch(e => {
+          d.reject(e);
+        });
+
+      return d.promise;
+    }
+  })
+  .service('LocalNode', function($q, $http){
+
+    const baseUrl = `api/core/nodes/`;
+
+    this.queryFromLocal = (xos) => {
+      const d = $q.defer();
+
+      $http.get(`${xos.auth_url}${baseUrl}`, {
+        headers: {
+          Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}`
+        }
+      })
+        .then((res) => {
+          d.resolve(res.data);
+        })
+        .catch(e => {
+          d.reject(e);
+        });
+
+      return d.promise;
+    }
+  });
+})();
+
diff --git a/views/ngXosViews/globalXos/src/sass/main.scss b/views/ngXosViews/globalXos/src/sass/main.scss
new file mode 100644
index 0000000..4ecf82d
--- /dev/null
+++ b/views/ngXosViews/globalXos/src/sass/main.scss
@@ -0,0 +1,13 @@
+@import '../../../../style/sass/lib/_variables.scss';
+
+#xosGlobalXos {
+  ui-view,
+  xos-list{
+    display: block;
+  }
+
+  .well.active{
+    background: $brand-primary;
+    color: #fff;
+  }
+}
\ No newline at end of file
diff --git a/views/ngXosViews/globalXos/src/templates/xos-list.tpl.html b/views/ngXosViews/globalXos/src/templates/xos-list.tpl.html
new file mode 100644
index 0000000..6299ba0
--- /dev/null
+++ b/views/ngXosViews/globalXos/src/templates/xos-list.tpl.html
@@ -0,0 +1,72 @@
+<div class="container">
+  <div class="row">
+    <h2>Select XOS to synch:</h2>
+  </div>
+  <div class="row">
+    <div class="col-xs-12">
+      <h2>G-XOS Slices:</h2>
+    </div>
+    <div class="col-xs-12">
+      <xos-table
+              config="vm.gSliceTableCgf"
+              data="vm.gSlices">
+      </xos-table>
+    </div>
+  </div>
+  <div class="row">
+    <div class="col-xs-12">
+      <h2>Get L-XOS details:</h2>
+    </div>
+    <div
+      ng-repeat="xos in vm.xoss"
+      class="col-sm-2">
+      <div
+        class="well"
+        ng-class="{active: xos.active}"
+        ng-click="vm.toggleXos(xos)">
+          {{xos.humanReadableName || xos.name}}
+      </div>
+    </div>
+  </div>
+  <div class="row" ng-if="vm.localSlices.length > 0">
+    <div class="col-xs-12">
+      <h2>L-XOS Slices:</h2>
+    </div>
+    <div class="col-xs-12">
+      <xos-table
+        config="vm.sliceTableCfg"
+        data="vm.localSlices">
+      </xos-table>
+    </div>
+  </div>
+  <div class="row" ng-if="vm.localSlices.length > 0">
+    <div class="col-xs-12">
+      <h2>L-XOS Users:</h2>
+    </div>
+    <div class="col-xs-12">
+      <xos-table
+        config="vm.usersTableCfg"
+        data="vm.localUsers">
+      </xos-table>
+    </div>
+  </div>
+</div>
+<script type="text/ng-template" id="addInstance.html">
+  <div class="modal-header">
+      <h3 class="modal-title" id="modal-title">Add Instance to {{vm.slice.name}} on {{vm.slice.xos.name}}</h3>
+  </div>
+  <div class="modal-body" id="modal-body">
+    <xos-form ng-model="vm.model" config="vm.config"></xos-form>
+  </div>
+</script>
+<script type="text/ng-template" id="listInstances.html">
+  <div class="modal-header">
+    <h3 class="modal-title" id="modal-title">Get Instances from {{vm.slice.name}} on {{vm.slice.xos.name}}</h3>
+  </div>
+  <div class="modal-body" id="modal-body">
+    <xos-table data="vm.instances" config="vm.config"></xos-table>
+  </div>
+  <div class="modal-footer">
+    <a ng-click="vm.close()" class="btn btn-warning">Close</a>
+  </div>
+</script>
\ No newline at end of file