diff --git a/core/src/main/java/hudson/slaves/Cloud.java b/core/src/main/java/hudson/slaves/Cloud.java index 380db7782fe7..4bf79115d36d 100644 --- a/core/src/main/java/hudson/slaves/Cloud.java +++ b/core/src/main/java/hudson/slaves/Cloud.java @@ -311,10 +311,10 @@ public HttpResponse doDoDelete() throws IOException { } /** - * Accepts the update to the node configuration. + * Accepts the update to the node name. */ @POST - public HttpResponse doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, Descriptor.FormException { + public HttpResponse doRename(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, Descriptor.FormException { checkPermission(Jenkins.ADMINISTER); Jenkins j = Jenkins.get(); @@ -330,8 +330,49 @@ public HttpResponse doConfigSubmit(StaplerRequest req, StaplerResponse rsp) thro } j.clouds.replace(this, result); j.save(); - // take the user back to the root clouds page. - return FormApply.success(".."); + + String reqPath = req.getOriginalRequestURI(); + String[] uriTokens = reqPath.replaceFirst("^/", "").split("/"); + if (uriTokens.length < 3 || !"rename".equals(uriTokens[uriTokens.length - 1])) { + // We should never be here, expecting URI format jenkins/cloud/name/configSubmit + throw new ServletException("Expected cloud rename URI: " + reqPath); + } + String cloudId = uriTokens[uriTokens.length - 2]; + if (this.name.equals(cloudId)) { + // cloud name being used in URI + if (!proposedName.equals(this.name)) { + // name changed + cloudId = proposedName; + } else { + cloudId = this.name; + } + } + + // take the user to the renamed cloud top page. + return FormApply.success("../" + cloudId); + } + + /** + * Accepts the update to the node configuration. Node name is not allowed to be changed. + */ + @POST + public HttpResponse doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, Descriptor.FormException { + checkPermission(Jenkins.ADMINISTER); + + Jenkins j = Jenkins.get(); + Cloud cloud = j.getCloud(this.name); + if (cloud == null) { + throw new ServletException("No such cloud " + this.name); + } + Cloud result = cloud.reconfigure(req, req.getSubmittedForm()); + String proposedName = result.name; + if (!proposedName.equals(this.name)) { + throw new Descriptor.FormException(jenkins.agents.Messages.CloudSet_DoNotRename(), "name"); + } + j.clouds.replace(this, result); + j.save(); + // take the user back to the cloud top page. + return FormApply.success("."); } public Cloud reconfigure(@NonNull final StaplerRequest req, JSONObject form) throws Descriptor.FormException { diff --git a/core/src/main/resources/hudson/slaves/Cloud/rename.jelly b/core/src/main/resources/hudson/slaves/Cloud/rename.jelly new file mode 100644 index 000000000000..61cfa1e5c17d --- /dev/null +++ b/core/src/main/resources/hudson/slaves/Cloud/rename.jelly @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/core/src/main/resources/hudson/slaves/Cloud/sidepanel.jelly b/core/src/main/resources/hudson/slaves/Cloud/sidepanel.jelly index 551560f93d3e..8d1b15852ec5 100644 --- a/core/src/main/resources/hudson/slaves/Cloud/sidepanel.jelly +++ b/core/src/main/resources/hudson/slaves/Cloud/sidepanel.jelly @@ -30,6 +30,9 @@ THE SOFTWARE. + diff --git a/core/src/main/resources/jenkins/agents/Messages.properties b/core/src/main/resources/jenkins/agents/Messages.properties index 11a27f40642c..348c3be06860 100644 --- a/core/src/main/resources/jenkins/agents/Messages.properties +++ b/core/src/main/resources/jenkins/agents/Messages.properties @@ -1,6 +1,7 @@ CloudSet.DisplayName=Clouds CloudSet.CloudAlreadyExists=Cloud called ‘{0}’ already exists CloudSet.SpecifyCloudToCopy=Specify which cloud to copy +CloudSet.DoNotRename=Rename cloud using the "Rename" page CloudSet.NoSuchCloud=No such cloud: {0} CloudsLink.DisplayName=Clouds CloudsLink.Description=Add, remove, and configure cloud instances to provision agents on-demand.