|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * @ngdoc function |
| 5 | + * @name openshiftConsole.controller:CreateFromURLController |
| 6 | + * @description |
| 7 | + * Controller of the openshiftConsole |
| 8 | + */ |
| 9 | +angular.module('openshiftConsole') |
| 10 | + .controller('CreateFromURLController', function ($scope, $routeParams, $location, $filter, AuthService, DataService, AlertMessageService, Navigate, ProjectsService ) { |
| 11 | + AuthService.withUser(); |
| 12 | + |
| 13 | + AlertMessageService.getAlerts().forEach(function(alert) { |
| 14 | + $scope.alerts[alert.name] = alert.data; |
| 15 | + }); |
| 16 | + AlertMessageService.clearAlerts(); |
| 17 | + |
| 18 | + $scope.alerts = {}; |
| 19 | + $scope.selected = {}; |
| 20 | + |
| 21 | + var alertInvalidImageStream = function(imageStream) { |
| 22 | + $scope.alerts.invalidImageStream = { |
| 23 | + type: "error", |
| 24 | + message: "The requested image stream \"" + imageStream + "\" could not be loaded." |
| 25 | + }; |
| 26 | + }; |
| 27 | + |
| 28 | + var alertInvalidImageTag = function(imageTag) { |
| 29 | + $scope.alerts.invalidImageTag = { |
| 30 | + type: "error", |
| 31 | + message: "The requested image stream tag \"" + imageTag + "\" could not be loaded." |
| 32 | + }; |
| 33 | + }; |
| 34 | + |
| 35 | + var alertInvalidName = function(name) { |
| 36 | + $scope.alerts.invalidImageStream = { |
| 37 | + type: "error", |
| 38 | + 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." |
| 39 | + }; |
| 40 | + }; |
| 41 | + |
| 42 | + var alertInvalidNamespace = function(namespace) { |
| 43 | + $scope.alerts.invalidNamespace = { |
| 44 | + type: "error", |
| 45 | + message: "Resources from the namespace \"" + namespace + "\" are not permitted." |
| 46 | + }; |
| 47 | + }; |
| 48 | + |
| 49 | + var alertInvalidTemplate = function(template) { |
| 50 | + $scope.alerts.invalidTemplate = { |
| 51 | + type: "error", |
| 52 | + message: "The requested template \"" + template + "\" could not be loaded." |
| 53 | + }; |
| 54 | + }; |
| 55 | + |
| 56 | + var alertResourceRequired = function() { |
| 57 | + $scope.alerts.resourceRequired = { |
| 58 | + type: "error", |
| 59 | + message: "An image stream or template is required." |
| 60 | + }; |
| 61 | + }; |
| 62 | + |
| 63 | + var showInvalidResource = function() { |
| 64 | + $scope.alerts.invalidResource = { |
| 65 | + type: "error", |
| 66 | + message: "Image streams and templates cannot be combined." |
| 67 | + }; |
| 68 | + }; |
| 69 | + |
| 70 | + var getTemplateParamsMap = function () { |
| 71 | + try { |
| 72 | + return $routeParams.templateParamsMap && JSON.parse($routeParams.templateParamsMap) || {}; |
| 73 | + } |
| 74 | + catch (e) { |
| 75 | + $scope.alerts.invalidTemplateParams = { |
| 76 | + type: "error", |
| 77 | + message: "The templateParamsMap is not valid JSON. " + e |
| 78 | + }; |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + var namespaceWhitelist = window.OPENSHIFT_CONSTANTS.CREATE_FROM_URL_WHITELIST; |
| 83 | + var whiteListedCreateDetailsKeys = ['namespace', 'name', 'imageStream', 'imageTag', 'sourceURI', 'sourceRef', 'contextDir', 'template', 'templateParamsMap']; |
| 84 | + var createDetails = _.pick($routeParams, function(value, key) { |
| 85 | + // routeParams without a value (e.g., ?name&) return true, which results in "true" displaying in the UI |
| 86 | + return _.contains(whiteListedCreateDetailsKeys, key) && _.isString(value); |
| 87 | + }); |
| 88 | + // if no namespace is specified, set it to 'openshift' |
| 89 | + createDetails.namespace = createDetails.namespace || 'openshift'; |
| 90 | + |
| 91 | + var validateName = function (name) { |
| 92 | + return name.length < 25 && /^[a-z]([-a-z0-9]*[a-z0-9])?$/.test(name); |
| 93 | + }; |
| 94 | + |
| 95 | + var getResources = function() { |
| 96 | + if (createDetails.imageStream) { |
| 97 | + DataService |
| 98 | + .get("imagestreams", createDetails.imageStream, {namespace: createDetails.namespace}, { |
| 99 | + errorNotification: false |
| 100 | + }) |
| 101 | + .then(function(imageStream) { |
| 102 | + $scope.imageStream = imageStream; |
| 103 | + DataService |
| 104 | + .get("imagestreamtags", imageStream.metadata.name + ":" + createDetails.imageTag, {namespace: createDetails.namespace}, { |
| 105 | + errorNotification: false |
| 106 | + }) |
| 107 | + .then(function(imageStreamTag){ |
| 108 | + $scope.imageStreamTag = imageStreamTag; |
| 109 | + $scope.validationPassed = true; |
| 110 | + $scope.resource = imageStreamTag; |
| 111 | + createDetails.displayName = $filter('displayName')(imageStreamTag); |
| 112 | + }, function(){ |
| 113 | + alertInvalidImageTag(createDetails.imageTag); |
| 114 | + }); |
| 115 | + }, function() { |
| 116 | + alertInvalidImageStream(createDetails.imageStream); |
| 117 | + }); |
| 118 | + } |
| 119 | + if (createDetails.template) { |
| 120 | + DataService |
| 121 | + .get("templates", createDetails.template, {namespace: createDetails.namespace}, { |
| 122 | + errorNotification: false |
| 123 | + }) |
| 124 | + .then(function(template) { |
| 125 | + $scope.template = template; |
| 126 | + if(getTemplateParamsMap()) { |
| 127 | + $scope.validationPassed = true; |
| 128 | + $scope.resource = template; |
| 129 | + } |
| 130 | + }, function() { |
| 131 | + alertInvalidTemplate(createDetails.template); |
| 132 | + }); |
| 133 | + } |
| 134 | + }; |
| 135 | + |
| 136 | + if (!(_.includes(namespaceWhitelist, createDetails.namespace))) { |
| 137 | + alertInvalidNamespace(createDetails.namespace); |
| 138 | + } else { |
| 139 | + if (createDetails.imageStream && createDetails.template) { |
| 140 | + showInvalidResource(); |
| 141 | + } else if (!(createDetails.imageStream) && !(createDetails.template)) { |
| 142 | + alertResourceRequired(); |
| 143 | + } else if (createDetails.name && !(validateName(createDetails.name))) { |
| 144 | + alertInvalidName(createDetails.name); |
| 145 | + } else { |
| 146 | + getResources(); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + angular.extend($scope, { |
| 151 | + createDetails: createDetails, |
| 152 | + createWithProject: function(projectName) { |
| 153 | + projectName = projectName || $scope.selected.project.metadata.name; |
| 154 | + var url = $routeParams.imageStream ? |
| 155 | + Navigate.createFromImageURL($scope.imageStream, createDetails.imageTag, projectName, createDetails) : |
| 156 | + Navigate.createFromTemplateURL($scope.template, projectName, createDetails); |
| 157 | + $location.url(url); |
| 158 | + } |
| 159 | + }); |
| 160 | + |
| 161 | + $scope.projects = {}; |
| 162 | + $scope.canCreateProject = undefined; |
| 163 | + |
| 164 | + DataService |
| 165 | + .list("projects", $scope) |
| 166 | + .then(function(items) { |
| 167 | + $scope.loaded = true; |
| 168 | + $scope.projects = $filter('orderByDisplayName')(items.by("metadata.name")); |
| 169 | + $scope.noProjects = (_.isEmpty($scope.projects)); |
| 170 | + }); |
| 171 | + |
| 172 | + |
| 173 | + // Test if the user can submit project requests. Handle error notifications |
| 174 | + // ourselves because 403 responses are expected. |
| 175 | + ProjectsService |
| 176 | + .canCreate() |
| 177 | + .then(function() { |
| 178 | + $scope.canCreateProject = true; |
| 179 | + }, function() { |
| 180 | + $scope.canCreateProject = false; |
| 181 | + }); |
| 182 | + |
| 183 | + }); |
0 commit comments