Skip to content

Commit

Permalink
add separate page for cloud rename
Browse files Browse the repository at this point in the history
  • Loading branch information
car-roll committed Aug 15, 2023
1 parent 1741953 commit c1d02fe
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
54 changes: 52 additions & 2 deletions core/src/main/java/hudson/slaves/Cloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ public HttpResponse doDoDelete() throws IOException {
* Accepts the update to the node configuration.
*/
@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();
Expand All @@ -330,8 +330,58 @@ public HttpResponse doConfigSubmit(StaplerRequest req, StaplerResponse rsp) thro
}
j.clouds.replace(this, result);
j.save();

System.out.println("zzzz: " + req.getContextPath());
// List<Ancestor> history = req.getAncestors();
// Ancestor previous = history.get(history.size() - 1);
// System.out.println("previous url: " + previous.getUrl());
// System.out.println("previous full url: " + previous.getFullUrl());
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 returnUri = Arrays.stream(uriTokens, 0, uriTokens.length - 2).collect(Collectors.joining("/"));
String cloudId = uriTokens[uriTokens.length - 2];
boolean redirect = false;
if (this.name.equals(cloudId)) {
// cloud name being used in URI
if (!proposedName.equals(this.name)) {
// name changed
cloudId = proposedName;
redirect = true;
} else {
cloudId = this.name;
}
}

// take the user back to the root clouds page.
return FormApply.success("..");
// return FormApply.success("..");
return FormApply.success("../" + cloudId);
}

/**
* Accepts the update to the node configuration.
*/
@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 {
Expand Down
46 changes: 46 additions & 0 deletions core/src/main/resources/hudson/slaves/Cloud/rename.jelly
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!--
The MIT License
Copyright (c) 2023, CloudBees Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:l="/lib/layout" xmlns:f="/lib/form">
<l:layout permission="${app.ADMINISTER}" title="${%title(it.name)}">
<st:include page="sidepanel.jelly"/>
<l:breadcrumb title="${%Rename Cloud}"/>
<l:main-panel>
<f:form method="post" action="rename" name="config" class="jenkins-form">
<j:set var="instance" value="${it}"/>
<j:set var="descriptor" value="${instance.descriptor}"/>

<f:entry title="${%Name}" field="name">
<f:textbox />
</f:entry>

<f:bottomButtonBar>
<f:submit value="${%Save}"/>
</f:bottomButtonBar>
</f:form>
<st:adjunct includes="lib.form.confirm"/>
</l:main-panel>
</l:layout>
</j:jelly>
3 changes: 3 additions & 0 deletions core/src/main/resources/hudson/slaves/Cloud/sidepanel.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ THE SOFTWARE.
<l:task contextMenu="false" href="." icon="symbol-computer" title="${%Status}"/>
<l:task href="configure" icon="symbol-settings"
title="${app.hasPermission(app.ADMINISTER) ? '%Configure' : '%View Configuration'}"/>
<l:task href="rename" icon="symbol-changes"
permission="${app.ADMINISTER}"
title="${%Rename}"/>
<l:delete permission="${app.ADMINISTER}" title="${%Delete Cloud}" message="${%delete.cloud(it.displayName)}"/>
<t:actions />
</l:tasks>
Expand Down
1 change: 1 addition & 0 deletions core/src/main/resources/jenkins/agents/Messages.properties
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit c1d02fe

Please sign in to comment.