forked from openshift/origin-web-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreatePersistentVolumeClaim.js
120 lines (109 loc) · 4.04 KB
/
createPersistentVolumeClaim.js
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
'use strict';
/**
* @ngdoc function
* @name openshiftConsole.controller:CreatePersistentVolumeClaimController
* @description
* # CreatePersistentVolumeClaimController
* Controller of the openshiftConsole
*/
angular.module('openshiftConsole')
.controller('CreatePersistentVolumeClaimController',
function($filter,
$routeParams,
$scope,
$window,
APIService,
ApplicationGenerator,
AuthorizationService,
DataService,
Navigate,
NotificationsService,
ProjectsService,
keyValueEditorUtils) {
$scope.projectName = $routeParams.project;
$scope.accessModes="ReadWriteOnce";
$scope.claim = {};
$scope.breadcrumbs = [
{
title: "Storage",
link: "project/" + $scope.projectName + "/browse/storage"
},
{
title: "Create Storage"
}
];
var pvcTemplate = {
kind: "PersistentVolumeClaim",
apiVersion: "v1",
metadata: {
name: undefined,
labels: {},
annotations: {}
},
spec: {
resources: {
requests:{}
}
}
};
var createPVCVersion = APIService.objectToResourceGroupVersion(pvcTemplate);
var hideErrorNotifications = function() {
NotificationsService.hideNotification("create-pvc-error");
};
$scope.$on('$destroy', hideErrorNotifications);
var navigateBack = function() {
$window.history.back();
};
$scope.cancel = navigateBack;
ProjectsService
.get($routeParams.project)
.then(_.spread(function(project, context) {
$scope.project = project;
if (!AuthorizationService.canI(createPVCVersion, 'create', $routeParams.project)) {
Navigate.toErrorPage('You do not have authority to create persistent volume claims in project ' + $routeParams.project + '.', 'access_denied');
return;
}
$scope.createPersistentVolumeClaim = function() {
hideErrorNotifications();
if ($scope.createPersistentVolumeClaimForm.$valid) {
$scope.disableInputs = true;
var claim = generatePersistentVolumeClaim();
DataService.create(createPVCVersion, null, claim, context)
.then(function(claim) { // Success
NotificationsService.addNotification({
type: "success",
message: "Persistent volume claim " + claim.metadata.name + " successfully created."
});
navigateBack();
},
function(result) { // Failure
$scope.disableInputs = false;
NotificationsService.addNotification({
id: "create-pvc-error",
type: "error",
message: "An error occurred requesting storage.",
details: $filter('getErrorDetails')(result)
});
});
}
};
function generatePersistentVolumeClaim() {
var pvc = angular.copy(pvcTemplate);
pvc.metadata.name = $scope.claim.name;
pvc.spec.accessModes = [$scope.claim.accessModes || "ReadWriteOnce"] ;
var unit = $scope.claim.unit || "Mi";
pvc.spec.resources.requests.storage = $scope.claim.amount + unit;
if ($scope.claim.selectedLabels) {
var selectorLabel = keyValueEditorUtils.mapEntries( keyValueEditorUtils.compactEntries($scope.claim.selectedLabels) );
if (!_.isEmpty(selectorLabel)) {
_.set(pvc, 'spec.selector.matchLabels', selectorLabel);
}
}
if ($scope.claim.storageClass && $scope.claim.storageClass.metadata.name !== "No Storage Class") {
//we can only have one storage class per claim
pvc.metadata.annotations["volume.beta.kubernetes.io/storage-class"] = $scope.claim.storageClass.metadata.name;
}
return pvc;
}
}));
});