Adding and removing instances from the Global Slice

Change-Id: Ib6658d643f2e468e43084b0fd8f11020f0975c40
diff --git a/views/ngXosViews/globalXos/src/js/main.js b/views/ngXosViews/globalXos/src/js/main.js
index 31effcc..16beca1 100644
--- a/views/ngXosViews/globalXos/src/js/main.js
+++ b/views/ngXosViews/globalXos/src/js/main.js
@@ -30,7 +30,7 @@
       const self = this;
       $q.all([
         Controllers.query({backend_type: 'CORD'}).$promise,
-        Slices.query().$promise // NOTE why this is queryFromAll??
+        Slices.query().$promise
       ])
       .then(res => {
         [this.xoss, this.gSlices] = res;
@@ -42,15 +42,218 @@
         }
       };
 
+      const getGlobalInstances = (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',
+                }
+              ],
+              actions: [
+                {
+                  label: 'Add Instance',
+                  icon: 'remove',
+                  color: 'red',
+                  cb: (item) => {
+                    console.log(item);
+                    LocalInstances.deleteFromLocal(item)
+                    .then(() => {
+                      _.remove(this.instances, i => i.id === item.id);
+                    });
+                  }
+                }
+              ]
+            };
+
+            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');
+            }
+          }
+        })
+      };
+
+      const createGlobalInstance = (item) => {
+        $uibModal.open({
+          animation: true,
+          size: 'lg',
+          templateUrl: 'addInstance.html',
+          controller: function($scope, $q, $uibModalInstance, slice, LocalInstances, LocalAuth){
+            this.slice = slice;
+
+            this.model = {
+              // isolation: 'vm'
+            };
+
+            let xos;
+
+            Controllers.query({backend_type: 'CORD'}).$promise
+            .then((xos) => {
+              this.xoss = xos;
+              this.config.fields['xos'].options = _.map(xos, item => {
+                return {id: item.id, label: item.name}
+              });
+            });
+
+            $scope.$watch(() => this.model.xos, () => {
+              if(!this.model.xos){
+                return;
+              }
+              xos = _.find(this.xoss, {id: this.model.xos});
+              LocalInstances.getLocalInfo(xos)
+              .then((res) => {
+                [
+                  this.config.fields['deployment'].options,
+                  this.config.fields['image'].options,
+                  this.config.fields['flavor'].options,
+                  this.config.fields['node'].options
+                ] = res;
+                return $q.all([
+                  LocalSlices.getLocalByName(xos, this.slice.name),
+                  LocalAuth.getUserByName(xos, xos.admin_user)
+                ]);
+              })
+              .then((res) => {
+                console.log('aaaa: ', res);
+                [this.localSlice, this.user] = res;
+              });
+            });
+
+
+            this.config = {
+              formName: 'instanceForm',
+              order: ['xos', 'name'],
+              excludedFields: ['xos', 'slice'],
+              actions: [
+                {
+                  label: 'Save',
+                  icon: 'ok',
+                  cb: (instance) => {
+                    instance.xos = xos;
+                    instance.slice = this.localSlice.id;
+                    instance.creator = this.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: {
+                xos: {
+                  type: 'select',
+                  validators: {
+                    required: true
+                  }
+                },
+                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;
+            }
+          }
+        });
+      };
+
       const baseSliceCols = [
         {
           label: 'Name',
           prop: 'name',
         },
-        {
-          label: 'Mount Data Sets',
-          prop: 'mount_data_sets'
-        }
+        // {
+        //   label: 'Mount Data Sets',
+        //   prop: 'mount_data_sets'
+        // }
       ];
 
       const lXosSliceCols = [
@@ -77,58 +280,12 @@
           {
             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');
-                  }
-                }
-              })
-            }
+            cb: getGlobalInstances
+          },
+          {
+            label: 'Add Instances',
+            icon: 'plus',
+            cb: createGlobalInstance
           },
         ]
       };
@@ -195,7 +352,7 @@
                   this.slice = slice;
 
                   this.model = {};
-
+                  console.log(slice);
                   LocalInstances.getLocalInfo(slice.xos)
                   .then((res) => {
                     [
@@ -216,7 +373,7 @@
                         cb: (instance) => {
                           instance.xos = slice.xos;
                           instance.slice = slice.id;
-                          instance.creator = instance.xos.user.id;
+                          instance.creator = this.user.id;
                           LocalInstances.createOnLocal(instance)
                           .then(res => {
                             slice.instance_total = slice.instance_total + 1;