-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathcreateFromURL.js
183 lines (162 loc) · 6.49 KB
/
createFromURL.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'use strict';
/**
* @ngdoc function
* @name openshiftConsole.controller:CreateFromURLController
* @description
* Controller of the openshiftConsole
*/
angular.module('openshiftConsole')
.controller('CreateFromURLController', function ($scope, $routeParams, $location, $filter, AuthService, DataService, AlertMessageService, Navigate, ProjectsService ) {
AuthService.withUser();
AlertMessageService.getAlerts().forEach(function(alert) {
$scope.alerts[alert.name] = alert.data;
});
AlertMessageService.clearAlerts();
$scope.alerts = {};
$scope.selected = {};
var alertInvalidImageStream = function(imageStream) {
$scope.alerts.invalidImageStream = {
type: "error",
message: "The requested image stream \"" + imageStream + "\" could not be loaded."
};
};
var alertInvalidImageTag = function(imageTag) {
$scope.alerts.invalidImageTag = {
type: "error",
message: "The requested image stream tag \"" + imageTag + "\" could not be loaded."
};
};
var alertInvalidName = function(name) {
$scope.alerts.invalidImageStream = {
type: "error",
message: "The app name \"" + name + "\" is not valid. An app name is an alphanumeric (a-z, and 0-9) string with a maximum length of 24 characters, where the first character is a letter (a-z), and the '-' character is allowed anywhere except the first or last character."
};
};
var alertInvalidNamespace = function(namespace) {
$scope.alerts.invalidNamespace = {
type: "error",
message: "Resources from the namespace \"" + namespace + "\" are not permitted."
};
};
var alertInvalidTemplate = function(template) {
$scope.alerts.invalidTemplate = {
type: "error",
message: "The requested template \"" + template + "\" could not be loaded."
};
};
var alertResourceRequired = function() {
$scope.alerts.resourceRequired = {
type: "error",
message: "An image stream or template is required."
};
};
var showInvalidResource = function() {
$scope.alerts.invalidResource = {
type: "error",
message: "Image streams and templates cannot be combined."
};
};
var getTemplateParamsMap = function () {
try {
return $routeParams.templateParamsMap && JSON.parse($routeParams.templateParamsMap) || {};
}
catch (e) {
$scope.alerts.invalidTemplateParams = {
type: "error",
message: "The templateParamsMap is not valid JSON. " + e
};
}
};
var namespaceWhitelist = window.OPENSHIFT_CONSTANTS.CREATE_FROM_URL_WHITELIST;
var whiteListedCreateDetailsKeys = ['namespace', 'name', 'imageStream', 'imageTag', 'sourceURI', 'sourceRef', 'contextDir', 'template', 'templateParamsMap'];
var createDetails = _.pick($routeParams, function(value, key) {
// routeParams without a value (e.g., ?name&) return true, which results in "true" displaying in the UI
return _.contains(whiteListedCreateDetailsKeys, key) && _.isString(value);
});
// if no namespace is specified, set it to 'openshift'
createDetails.namespace = createDetails.namespace || 'openshift';
var validateName = function (name) {
return name.length < 25 && /^[a-z]([-a-z0-9]*[a-z0-9])?$/.test(name);
};
var getResources = function() {
if (createDetails.imageStream) {
DataService
.get("imagestreams", createDetails.imageStream, {namespace: createDetails.namespace}, {
errorNotification: false
})
.then(function(imageStream) {
$scope.imageStream = imageStream;
DataService
.get("imagestreamtags", imageStream.metadata.name + ":" + createDetails.imageTag, {namespace: createDetails.namespace}, {
errorNotification: false
})
.then(function(imageStreamTag){
$scope.imageStreamTag = imageStreamTag;
$scope.validationPassed = true;
$scope.resource = imageStreamTag;
createDetails.displayName = $filter('displayName')(imageStreamTag);
}, function(){
alertInvalidImageTag(createDetails.imageTag);
});
}, function() {
alertInvalidImageStream(createDetails.imageStream);
});
}
if (createDetails.template) {
DataService
.get("templates", createDetails.template, {namespace: createDetails.namespace}, {
errorNotification: false
})
.then(function(template) {
$scope.template = template;
if(getTemplateParamsMap()) {
$scope.validationPassed = true;
$scope.resource = template;
}
}, function() {
alertInvalidTemplate(createDetails.template);
});
}
};
if (!(_.includes(namespaceWhitelist, createDetails.namespace))) {
alertInvalidNamespace(createDetails.namespace);
} else {
if (createDetails.imageStream && createDetails.template) {
showInvalidResource();
} else if (!(createDetails.imageStream) && !(createDetails.template)) {
alertResourceRequired();
} else if (createDetails.name && !(validateName(createDetails.name))) {
alertInvalidName(createDetails.name);
} else {
getResources();
}
}
angular.extend($scope, {
createDetails: createDetails,
createWithProject: function(projectName) {
projectName = projectName || $scope.selected.project.metadata.name;
var url = $routeParams.imageStream ?
Navigate.createFromImageURL($scope.imageStream, createDetails.imageTag, projectName, createDetails) :
Navigate.createFromTemplateURL($scope.template, projectName, createDetails);
$location.url(url);
}
});
$scope.projects = {};
$scope.canCreateProject = undefined;
DataService
.list("projects", $scope)
.then(function(items) {
$scope.loaded = true;
$scope.projects = $filter('orderByDisplayName')(items.by("metadata.name"));
$scope.noProjects = (_.isEmpty($scope.projects));
});
// Test if the user can submit project requests. Handle error notifications
// ourselves because 403 responses are expected.
ProjectsService
.canCreate()
.then(function() {
$scope.canCreateProject = true;
}, function() {
$scope.canCreateProject = false;
});
});