summaryrefslogtreecommitdiff
path: root/extensions/mistral/static/mistral/js/mistral.workbook.controllers.js
blob: 77d15e99b4bbbf75119f01a2bac92311c3b54268 (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
/**
 * Created by tsufiev on 2/24/15.
 */
(function() {
  'use strict';

  angular.module('mistral')
    .value('baseActionID', 'action')
    .value('baseWorkflowID', 'workflow')
    .controller('workbookCtrl',
    ['$scope', 'mistral.workbook.models', '$http',
      'baseActionID', 'baseWorkflowID',
      function($scope, models, $http, baseActionId, baseWorkflowId) {
        $scope.init = function(id, yaml, commitUrl, discardUrl) {
          $scope.workbookID = id;
          $scope.commitUrl = commitUrl;
          $scope.discardUrl = discardUrl;
          if ( id !== undefined ) {
            $scope.workbook = models.Workbook.create(jsyaml.safeLoad(yaml));
          } else {
            $scope.workbook = models.Workbook.create({name: 'My Workbook'});
          }
          $scope.root = models.Root.create();
          $scope.root.set('workbook', $scope.workbook);

          $scope.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',
            regexp = /(workflow|action)([0-9]+)/,
            container = $scope.workbook.get(containerName);
          if ( !container ) {
            throw 'Base should be either "action" or "workflow"!';
          }
          return getNextIDSuffix(container, regexp);
        }

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

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

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

          $http({
            url: $scope.commitUrl,
            method: 'POST',
            data: data
          }).success(function(data, status, headers, config) {
            document.location = $scope.discardUrl;
          });
        };

        $scope.discardWorkbook = function() {
          document.location = $scope.discardUrl;
        };

      }])
})();