forked from openshift/origin-web-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoscPersistentVolumeClaim.js
112 lines (99 loc) · 3.58 KB
/
oscPersistentVolumeClaim.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
"use strict";
angular.module("openshiftConsole")
.directive("oscPersistentVolumeClaim",
function($filter,
DataService,
LimitRangesService,
ModalsService) {
return {
restrict: 'E',
scope: {
claim: "=model",
projectName: "="
},
templateUrl: 'views/directives/osc-persistent-volume-claim.html',
link: function(scope) {
var amountAndUnit = $filter('amountAndUnit');
var usageValue = $filter('usageValue');
scope.storageClasses = [];
scope.claim.unit = 'Gi';
scope.units = [{
value: "Mi",
label: "MiB"
}, {
value: "Gi",
label: "GiB"
}, {
value: "Ti",
label: "TiB"
}, {
value: "M",
label: "MB"
}, {
value: "G",
label: "GB"
}, {
value: "T",
label: "TB"
}];
scope.claim.selectedLabels = [];
scope.groupUnits = function(unit) {
switch (unit.value) {
case 'Mi':
case 'Gi':
case 'Ti':
return 'Binary Units';
case 'M':
case 'G':
case 'T':
return 'Decimal Units';
}
return '';
};
scope.showComputeUnitsHelp = function() {
ModalsService.showComputeUnitsHelp();
};
var validateLimitRange = function() {
// Use usageValue filter to normalize units for comparison.
var value = scope.claim.amount && usageValue(scope.claim.amount + scope.claim.unit);
var min = _.has(scope, 'limits.min') && usageValue(scope.limits.min);
var max = _.has(scope, 'limits.max') && usageValue(scope.limits.max);
var minValid = true;
var maxValid = true;
// Test against limit range min if defined.
if (value && min) {
minValid = value >= min;
}
// Test against limit range max if defined.
if (value && max) {
maxValid = value <= max;
}
scope.persistentVolumeClaimForm.capacity.$setValidity('limitRangeMin', minValid);
scope.persistentVolumeClaimForm.capacity.$setValidity('limitRangeMax', maxValid);
};
DataService.list({group: 'storage.k8s.io', resource: 'storageclasses'}, {}, function(storageClasses) {
scope.storageClasses = storageClasses.by('metadata.name');
}, {errorNotification: false});
DataService.list('limitranges', { namespace: scope.projectName }, function(limitRangeData) {
var limitRanges = limitRangeData.by('metadata.name');
if (_.isEmpty(limitRanges)) {
return;
}
scope.limits = LimitRangesService.getEffectiveLimitRange(limitRanges, 'storage', 'PersistentVolumeClaim');
// If min === max, set the capacity to the required value and make the field readonly.
var requiredAmountAndUnit;
if (scope.limits.min && scope.limits.max) {
var minUsage = usageValue(scope.limits.min);
var maxUsage = usageValue(scope.limits.max);
if (minUsage === maxUsage) {
requiredAmountAndUnit = amountAndUnit(scope.limits.max);
scope.claim.amount = Number(requiredAmountAndUnit[0]);
scope.claim.unit = requiredAmountAndUnit[1];
scope.capacityReadOnly = true;
}
}
scope.$watchGroup(['claim.amount', 'claim.unit'], validateLimitRange);
});
}
};
});