|
1 | 1 | package org.csanchez.jenkins.plugins.kubernetes;
|
2 | 2 |
|
| 3 | +import java.io.IOException; |
3 | 4 | import java.io.Serializable;
|
4 | 5 | import java.math.BigInteger;
|
5 | 6 | import java.nio.charset.StandardCharsets;
|
|
15 | 16 | import java.util.UUID;
|
16 | 17 | import java.util.logging.Level;
|
17 | 18 | import java.util.logging.Logger;
|
18 |
| - |
| 19 | +import javax.servlet.ServletException; |
19 | 20 | import edu.umd.cs.findbugs.annotations.CheckForNull;
|
20 | 21 | import edu.umd.cs.findbugs.annotations.NonNull;
|
21 | 22 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
|
37 | 38 | import org.kohsuke.accmod.Restricted;
|
38 | 39 | import org.kohsuke.accmod.restrictions.DoNotUse;
|
39 | 40 | import org.kohsuke.accmod.restrictions.NoExternalUse;
|
| 41 | +import org.kohsuke.stapler.AncestorInPath; |
40 | 42 | import org.kohsuke.stapler.DataBoundConstructor;
|
41 | 43 | import org.kohsuke.stapler.DataBoundSetter;
|
42 |
| - |
| 44 | +import org.kohsuke.stapler.HttpRedirect; |
| 45 | +import org.kohsuke.stapler.HttpResponse; |
| 46 | +import org.kohsuke.stapler.StaplerRequest; |
| 47 | +import org.kohsuke.stapler.verb.POST; |
43 | 48 | import hudson.Extension;
|
44 | 49 | import hudson.Util;
|
45 | 50 | import hudson.model.labels.LabelAtom;
|
46 | 51 | import hudson.slaves.NodeProperty;
|
| 52 | +import hudson.util.FormApply; |
47 | 53 | import hudson.util.XStream2;
|
48 | 54 | import io.fabric8.kubernetes.api.model.Pod;
|
49 | 55 | import io.fabric8.kubernetes.client.KubernetesClient;
|
50 | 56 | import java.io.StringReader;
|
51 | 57 | import jenkins.model.Jenkins;
|
| 58 | +import net.sf.json.JSONObject; |
52 | 59 |
|
53 | 60 | /**
|
54 | 61 | * Kubernetes Pod Template
|
@@ -631,6 +638,44 @@ public void addEnvVars(List<TemplateEnvVar> envVars) {
|
631 | 638 | }
|
632 | 639 | }
|
633 | 640 |
|
| 641 | + /** |
| 642 | + * Deletes the template. |
| 643 | + */ |
| 644 | + @POST |
| 645 | + public HttpResponse doDoDelete(@AncestorInPath KubernetesCloud kubernetesCloud) throws IOException { |
| 646 | + Jenkins j = Jenkins.get(); |
| 647 | + j.checkPermission(Jenkins.ADMINISTER); |
| 648 | + if (kubernetesCloud == null) { |
| 649 | + throw new IllegalStateException("Cloud could not be found"); |
| 650 | + } |
| 651 | + kubernetesCloud.removeTemplate(this); |
| 652 | + j.save(); |
| 653 | + // take the user back. |
| 654 | + return new HttpRedirect("../../templates"); |
| 655 | + } |
| 656 | + |
| 657 | + @POST |
| 658 | + public HttpResponse doConfigSubmit(StaplerRequest req, @AncestorInPath KubernetesCloud kubernetesCloud) throws IOException, ServletException, Descriptor.FormException { |
| 659 | + Jenkins j = Jenkins.get(); |
| 660 | + j.checkPermission(Jenkins.ADMINISTER); |
| 661 | + if (kubernetesCloud == null) { |
| 662 | + throw new IllegalStateException("Cloud could not be found"); |
| 663 | + } |
| 664 | + kubernetesCloud.removeTemplate(this); |
| 665 | + PodTemplate newTemplate = reconfigure(req, req.getSubmittedForm()); |
| 666 | + kubernetesCloud.addTemplate(newTemplate); |
| 667 | + j.save(); |
| 668 | + // take the user back. |
| 669 | + return FormApply.success("../../templates"); |
| 670 | + } |
| 671 | + |
| 672 | + private PodTemplate reconfigure(@NonNull final StaplerRequest req, JSONObject form) throws Descriptor.FormException { |
| 673 | + if (form == null) { |
| 674 | + return null; |
| 675 | + } |
| 676 | + return getDescriptor().newInstance(req, form); |
| 677 | + } |
| 678 | + |
634 | 679 | @DataBoundSetter
|
635 | 680 | public void setEnvVars(List<TemplateEnvVar> envVars) {
|
636 | 681 | if (envVars != null) {
|
|
0 commit comments