diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java index 23c1a33105..376d0043b8 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java @@ -83,7 +83,7 @@ * * @author Carlos Sanchez carlos@apache.org */ -public class KubernetesCloud extends Cloud { +public class KubernetesCloud extends Cloud implements PodTemplateGroup { public static final int DEFAULT_MAX_REQUESTS_PER_HOST = 32; public static final Integer DEFAULT_WAIT_FOR_POD_SEC = 600; @@ -591,6 +591,12 @@ public Collection provision(@NonNull final Cloud.Cl return Collections.emptyList(); } + @Override + public void replaceTemplate(PodTemplate oldTemplate, PodTemplate newTemplate){ + this.removeTemplate(oldTemplate); + this.addTemplate(newTemplate); + } + @Override public boolean canProvision(@NonNull Cloud.CloudState state) { return getTemplate(state.getLabel()) != null; @@ -660,10 +666,16 @@ public void addTemplate(PodTemplate t) { * * @param t docker template */ + @Override public void removeTemplate(PodTemplate t) { this.templates.remove(t); } + @Override + public String getPodTemplateGroupUrl() { + return "../../templates"; + } + /** * Add a dynamic pod template. Won't be displayed in UI, and persisted separately from the cloud instance. * @param t the template to add diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java index 120b0ffeb6..4ea8e1fe7c 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java @@ -642,31 +642,30 @@ public void addEnvVars(List envVars) { * Deletes the template. */ @POST - public HttpResponse doDoDelete(@AncestorInPath KubernetesCloud kubernetesCloud) throws IOException { + public HttpResponse doDoDelete(@AncestorInPath PodTemplateGroup owner) throws IOException { Jenkins j = Jenkins.get(); j.checkPermission(Jenkins.ADMINISTER); - if (kubernetesCloud == null) { + if (owner == null) { throw new IllegalStateException("Cloud could not be found"); } - kubernetesCloud.removeTemplate(this); + owner.removeTemplate(this); j.save(); // take the user back. - return new HttpRedirect("../../templates"); + return new HttpRedirect(owner.getPodTemplateGroupUrl()); } @POST - public HttpResponse doConfigSubmit(StaplerRequest req, @AncestorInPath KubernetesCloud kubernetesCloud) throws IOException, ServletException, Descriptor.FormException { + public HttpResponse doConfigSubmit(StaplerRequest req, @AncestorInPath PodTemplateGroup owner) throws IOException, ServletException, Descriptor.FormException { Jenkins j = Jenkins.get(); j.checkPermission(Jenkins.ADMINISTER); - if (kubernetesCloud == null) { + if (owner == null) { throw new IllegalStateException("Cloud could not be found"); } - kubernetesCloud.removeTemplate(this); PodTemplate newTemplate = reconfigure(req, req.getSubmittedForm()); - kubernetesCloud.addTemplate(newTemplate); + owner.replaceTemplate(this, newTemplate); j.save(); // take the user back. - return FormApply.success("../../templates"); + return FormApply.success(owner.getPodTemplateGroupUrl()); } private PodTemplate reconfigure(@NonNull final StaplerRequest req, JSONObject form) throws Descriptor.FormException { diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java new file mode 100644 index 0000000000..7ce876ea7f --- /dev/null +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java @@ -0,0 +1,21 @@ +package org.csanchez.jenkins.plugins.kubernetes; +/** + * A group of pod templates that can be saved together. + */ +public interface PodTemplateGroup { + /** + * Replaces the old template with the new template. + * @param oldTemplate the old template to replace + * @param newTemplate the new template to replace with + */ + void replaceTemplate(PodTemplate oldTemplate, PodTemplate newTemplate); + /** + * Removes the template from the group. + * @param podTemplate the template to remove + */ + void removeTemplate(PodTemplate podTemplate); + /** + * @return the URL to redirect to after the template is saved. + */ + String getPodTemplateGroupUrl(); +}