aboutsummaryrefslogtreecommitdiffstats
path: root/moon_dashboard/moon/static/moon/pdp/pdp.controller.js
blob: 1859b1f81c239462483523fb26e8247516cd013d (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
(function () {
  'use strict';

  angular
    .module('moon')
    .controller('moon.pdp.controller',
      controller);

  controller.$inject = ['moon.util.service', 'moon.pdp.service', 'horizon.framework.widgets.form.ModalFormService'];

  function controller(util, pdpService, ModalFormService) {
    var self = this;
    self.model = pdpService;
    pdpService.initialize();

    self.createPdp = function createPdp() {
      var schema = {
        type: "object",
        properties: {
          name: { type: "string", minLength: 2, title: gettext("Name") },
          description: { type: "string", minLength: 2, title: gettext("Description") }
        },
        required: ['name', 'description']
      };
      var pdp = { name: '', description: '' };
      var config = {
        title: gettext('Create PDP'),
        schema: schema,
        form: ['name', { key: 'description', type: 'textarea' }],
        model: pdp
      };
      ModalFormService.open(config).then(submit);

      function submit(form) {
        pdpService.createPdp(form.model);
      }
    }

    self.updatePdp = function updatePdp(pdp) {
      var schema = {
        type: "object",
        properties: {
          name: { type: "string", minLength: 2, title: gettext("Name") },
          description: { type: "string", minLength: 2, title: gettext("Description") }
        },
        required: ['name', 'description']
      };
      var config = {
        title: gettext('Update PDP'),
        schema: schema,
        form: ['name', { key: 'description', type: 'textarea' }],
        model: angular.copy(pdp)
      };
      ModalFormService.open(config).then(submit);

      function submit(form) {
        pdpService.updatePdp(form.model);
      }
    }

    self.removePdp = function removePdp(pdp) {
      if (confirm(gettext('Are you sure to delete this PDP?')))
        pdpService.removePdp(pdp);
    }

    self.addPolicy = function addPolicy(pdp) {
      var schema = {
        type: "object",
        properties: {
          id: { type: "string", title: gettext("Select a Policy:") }
        },
        required: ['id']
      };
      var titleMap = util.arrayToTitleMap(pdpService.policies)
      var config = {
        title: gettext('Add Policy'),
        schema: schema,
        form: [{ key: 'id', type: 'select', titleMap: titleMap }],
        model: {}
      };
      ModalFormService.open(config).then(submit);

      function submit(form) {
        var pdpCopy = angular.copy(pdp);
        pdpCopy.security_pipeline.push(pdpService.getPolicy(form.model.id));
        pdpService.updatePdp(pdpCopy);
      }
    }

    self.removePolicyFromPdp = function removePolicyFromPdp(pdp, policy) {
      if (confirm(gettext('Are you sure to remove this Policy from PDP?'))) {
        var pdpCopy = angular.copy(pdp);
        pdpCopy.security_pipeline.splice(pdp.security_pipeline.indexOf(policy), 1);
        pdpService.updatePdp(pdpCopy);
      }
    }

    self.changeProject = function changeProject(pdp) {
      var schema = {
        type: "object",
        properties: {
          id: { type: "string", title: gettext("Select a Project:") }
        },
        required: ['id']
      };
      var model = {id : pdp.keystone_project_id};

      var titleMap = util.arrayToTitleMap(pdpService.projects)
      var config = {
        title: gettext('Change Project'),
        schema: schema,
        form: [{ key: 'id', type: 'select', titleMap: titleMap }],
        model: model
      };
      ModalFormService.open(config).then(submit);

      function submit(form) {
        var pdpCopy = angular.copy(pdp);
        pdpCopy.project = pdpService.getProject(form.model.id);
        pdpService.updatePdp(pdpCopy);
      }
    }

  }
})();