Framework for building UIs for OpenStack projects dealing with complex input data
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mistral.workbook.controllers.js 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * Created by tsufiev on 2/24/15.
  3. */
  4. (function() {
  5. 'use strict';
  6. angular.module('mistral')
  7. .value('baseActionID', 'action')
  8. .value('baseWorkflowID', 'workflow')
  9. .controller('workbookCtrl',
  10. ['$scope', 'mistral.workbook.models', '$http',
  11. 'baseActionID', 'baseWorkflowID',
  12. function($scope, models, $http, baseActionId, baseWorkflowId) {
  13. $scope.init = function(id, yaml, commitUrl, discardUrl) {
  14. $scope.workbookID = id;
  15. $scope.commitUrl = commitUrl;
  16. $scope.discardUrl = discardUrl;
  17. if ( id !== undefined ) {
  18. $scope.workbook = models.Workbook.create(jsyaml.safeLoad(yaml));
  19. } else {
  20. $scope.workbook = models.Workbook.create({name: 'My Workbook'});
  21. }
  22. $scope.root = models.Root.create();
  23. $scope.root.set('workbook', $scope.workbook);
  24. $scope.root.set('standardActions', {
  25. 'nova.create_server': ['image', 'flavor', 'network_id'],
  26. 'neutron.create_network': ['name', 'create_subnet'],
  27. 'glance.create_image': ['image_url']
  28. });
  29. };
  30. function getNextIDSuffix(container, regexp) {
  31. var max = Math.max.apply(Math, container.getIDs().map(function(id) {
  32. var match = regexp.exec(id);
  33. return match && +match[2];
  34. }));
  35. return max > 0 ? max + 1 : 1;
  36. }
  37. function getWorkbookNextIDSuffix(base) {
  38. var containerName = base + 's',
  39. regexp = /(workflow|action)([0-9]+)/,
  40. container = $scope.workbook.get(containerName);
  41. if ( !container ) {
  42. throw 'Base should be either "action" or "workflow"!';
  43. }
  44. return getNextIDSuffix(container, regexp);
  45. }
  46. $scope.addAction = function() {
  47. var nextSuffix = getWorkbookNextIDSuffix(baseActionId),
  48. newID = baseActionId + nextSuffix;
  49. $scope.workbook.get('actions').push(
  50. {name: 'Action ' + nextSuffix}, {id: newID});
  51. };
  52. $scope.addWorkflow = function() {
  53. var nextSuffix = getWorkbookNextIDSuffix(baseWorkflowId),
  54. newID = baseWorkflowId + nextSuffix;
  55. $scope.workbook.get('workflows').push(
  56. {name: 'Workflow ' + nextSuffix}, {id: newID});
  57. };
  58. $scope.commitWorkbook = function() {
  59. var data = {
  60. name: $scope.workbook.get('name').get(),
  61. yaml: $scope.workbook.toYAML()
  62. };
  63. $http({
  64. url: $scope.commitUrl,
  65. method: 'POST',
  66. data: data
  67. }).success(function(data, status, headers, config) {
  68. document.location = $scope.discardUrl;
  69. });
  70. };
  71. $scope.discardWorkbook = function() {
  72. document.location = $scope.discardUrl;
  73. };
  74. }])
  75. })();