aboutsummaryrefslogtreecommitdiffstats
path: root/old/moon_gui/static/app/pdp/action/pdp.controller.add.js
blob: d1c34c7950b4752ff547ae6ea8c90c874d90217f (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
/**
 * @author arnaud marhin<arnaud.marhin@orange.com>
 */

(function() {

	'use strict';
					
	angular
		.module('moon')
			.controller('PDPAddController', PDPAddController);
	
	PDPAddController.$inject = ['$scope', '$translate', 'alertService', 'formService', 'pdpService', 'policyService', 'utilService'];

	function PDPAddController($scope, $translate, alertService, formService, pdpService, policyService, utilService) {
		
		var add = this;
		
		/*
		 * 
		 */
		
		add.form = {};
		
		add.pdp = {};

		add.policies = [];

		add.selectedPolicy = null;

		add.loading = false;
		add.loadingPolicies = true;

		add.create = createPDP;
		
		resolvePolicies();
		
		/*
		 *  
		 */
		
		/**
		 * This function return an array of all policies/template ids
		 */
		function resolvePolicies() {

			policyService.findAllWithCallback(function(policies){

				add.policies = policies;
                add.loadingPolicies = false;
			});
			
		}
                
        function createPDP(pdp) {
        	
        	if(formService.isInvalid(add.form)) {
        		
        		formService.checkFieldsValidity(add.form);
        	        	
        	} else {

                add.loading = true;

        		pdpService.data.pdp.create({}, {

        			name: add.pdp.name,
					description: add.pdp.description,
					security_pipeline: [add.selectedPolicy.id],
					keystone_project_id: null

        		}, createSuccess, createError);
        			        	        	
    		}
        	
        	function createSuccess(data) {
    			
    			$translate('moon.pdp.add.success', { pdpName: pdp.name })
					.then(function (translatedValue) {
        				alertService.alertSuccess(translatedValue);
                });

                var createdPdp =  utilService.transformOne(data, 'pdps');

                add.loading = false;

    			$scope.$emit('event:pdpCreatedSuccess', createdPdp);
    			
    		}
    		
    		function createError(reason) {
    			
    			$translate('moon.pdp.add.error', { pdpName: pdp.name })
					.then(function (translatedValue) {
        				alertService.alertError(translatedValue);
                });

                add.loading = false;

    			$scope.$emit('event:pdpCreatedError');
    			
    		}
        	
        }
		
	}
	
})();