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.6KB

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