summaryrefslogtreecommitdiff
path: root/extensions/mistral/static/mistral/js/mistral.workbook.controllers.js
blob: f4830dd831fb31fdc3261bb0c4fb0be756263779 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
 * Created by tsufiev on 2/24/15.
 */
(function() {
  'use strict';

  angular
    .module('mistral')
    .value('baseActionID', 'action')
    .value('baseWorkflowID', 'workflow')
    .controller('WorkbookController', WorkbookController);

  WorkbookController.$inject = ['mistral.workbook.models', '$http',
    '$window', 'baseActionID', 'baseWorkflowID'];

  function WorkbookController(models, $http, $window,
                              baseActionId, baseWorkflowId) {
    var vm = this;
    vm.init = function(id, yaml, commitUrl, discardUrl) {
      vm.workbookID = id;
      vm.commitUrl = commitUrl;
      vm.discardUrl = discardUrl;
      if (angular.isDefined(id)) {
        vm.workbook = models.Workbook.create(jsyaml.safeLoad(yaml));
      } else {
        vm.workbook = models.Workbook.create({name: 'My Workbook'});
      }
      vm.root = models.Root.create();
      vm.root.set('workbook', vm.workbook);

      vm.root.set('standardActions', {
        'nova.create_server': ['image', 'flavor', 'network_id'],
        'neutron.create_network': ['name', 'create_subnet'],
        'glance.create_image': ['image_url']
      });
    };

    function getNextIDSuffix(container, regexp) {
      var max = Math.max.apply(Math, container.getIDs().map(function(id) {
        var match = regexp.exec(id);
        return match && +match[2];
      }));
      return max > 0 ? max + 1 : 1;
    }

    function getWorkbookNextIDSuffix(base) {
      var containerName = base + 's';
      var regexp = /(workflow|action)([0-9]+)/;
      var container = vm.workbook.get(containerName);
      if ( !container ) {
        throw new Error('Base should be either "action" or "workflow"!');
      }
      return getNextIDSuffix(container, regexp);
    }

    vm.addAction = function() {
      var nextSuffix = getWorkbookNextIDSuffix(baseActionId);
      var newID = baseActionId + nextSuffix;
      vm.workbook.get('actions').push(
        {name: 'Action ' + nextSuffix}, {id: newID});
    };

    vm.addWorkflow = function() {
      var nextSuffix = getWorkbookNextIDSuffix(baseWorkflowId);
      var newID = baseWorkflowId + nextSuffix;
      vm.workbook.get('workflows').push(
        {name: 'Workflow ' + nextSuffix}, {id: newID});
    };

    vm.commitWorkbook = function() {
      var data = {
        name: vm.workbook.get('name').get(),
        yaml: vm.workbook.toYAML()
      };

      $http({
        url: vm.commitUrl,
        method: 'POST',
        data: data
      }).success(function() {
        $window.location.href = vm.discardUrl;
      });
    };

    vm.discardWorkbook = function() {
      $window.location.href = vm.discardUrl;
    };

  }
})();