|
| 1 | +package org.csanchez.jenkins.plugins.kubernetes; |
| 2 | + |
| 3 | +import hudson.Extension; |
| 4 | +import hudson.model.AbstractDescribableImpl; |
| 5 | +import hudson.model.Descriptor; |
| 6 | +import org.jenkinsci.Symbol; |
| 7 | +import io.fabric8.kubernetes.api.model.ContainerPort; |
| 8 | + |
| 9 | +import java.io.Serializable; |
| 10 | + |
| 11 | +import org.kohsuke.stapler.DataBoundConstructor; |
| 12 | +import org.kohsuke.stapler.DataBoundSetter; |
| 13 | + |
| 14 | +public class PortMapping extends AbstractDescribableImpl<PortMapping> implements Serializable { |
| 15 | + |
| 16 | + private String name; |
| 17 | + private Integer containerPort; |
| 18 | + private Integer hostPort; |
| 19 | + |
| 20 | + @DataBoundConstructor |
| 21 | + public PortMapping(String name, Integer containerPort) { |
| 22 | + this.name = name; |
| 23 | + this.containerPort = containerPort; |
| 24 | + } |
| 25 | + |
| 26 | + public PortMapping(String name, Integer containerPort, Integer hostPort) { |
| 27 | + this.name = name; |
| 28 | + this.containerPort = containerPort; |
| 29 | + this.hostPort = hostPort; |
| 30 | + } |
| 31 | + |
| 32 | + @DataBoundSetter |
| 33 | + public void setName(String name) { |
| 34 | + this.name = name; |
| 35 | + } |
| 36 | + |
| 37 | + public String getName() { |
| 38 | + return name; |
| 39 | + } |
| 40 | + |
| 41 | + @DataBoundSetter |
| 42 | + public void setContainerPort(Integer containerPort) { |
| 43 | + this.containerPort = containerPort; |
| 44 | + } |
| 45 | + |
| 46 | + public Integer getContainerPort() { |
| 47 | + return containerPort; |
| 48 | + } |
| 49 | + |
| 50 | + @DataBoundSetter |
| 51 | + public void setHostPort(Integer hostPort) { |
| 52 | + this.hostPort = hostPort; |
| 53 | + } |
| 54 | + |
| 55 | + public Integer getHostPort() { |
| 56 | + return hostPort; |
| 57 | + } |
| 58 | + |
| 59 | + public ContainerPort toPort() { |
| 60 | + ContainerPort p = new ContainerPort(); |
| 61 | + p.setName(name); |
| 62 | + p.setContainerPort(containerPort); |
| 63 | + if(hostPort != null) { |
| 64 | + p.setHostPort(hostPort); |
| 65 | + } |
| 66 | + return p; |
| 67 | + } |
| 68 | + |
| 69 | + public String toString() { |
| 70 | + return String.format("%s,%d", name, containerPort); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public int hashCode() { |
| 75 | + final int prime = 31; |
| 76 | + int result = 1; |
| 77 | + result = prime * result + ((name == null) ? 0 : name.hashCode()); |
| 78 | + result = prime * result + ((containerPort == null) ? 0 : containerPort); |
| 79 | + result = prime * result + ((hostPort == null) ? 0 : hostPort); |
| 80 | + return result; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public boolean equals(Object obj) { |
| 85 | + if (this == obj) |
| 86 | + return true; |
| 87 | + if (obj == null) |
| 88 | + return false; |
| 89 | + if (getClass() != obj.getClass()) |
| 90 | + return false; |
| 91 | + PortMapping other = (PortMapping) obj; |
| 92 | + if (name == null) { |
| 93 | + if (other.name != null) |
| 94 | + return false; |
| 95 | + } else if (!name.equals(other.name)) |
| 96 | + return false; |
| 97 | + if (containerPort == null) { |
| 98 | + if (other.containerPort != null) |
| 99 | + return false; |
| 100 | + } else if (!containerPort.equals(other.containerPort)) |
| 101 | + return false; |
| 102 | + if (hostPort == null) { |
| 103 | + if (other.hostPort != null) |
| 104 | + return false; |
| 105 | + } else if (!hostPort.equals(other.hostPort)) |
| 106 | + return false; |
| 107 | + return true; |
| 108 | + } |
| 109 | + |
| 110 | + @Extension |
| 111 | + @Symbol("portMapping") |
| 112 | + public static class DescriptorImpl extends Descriptor<PortMapping> { |
| 113 | + @Override |
| 114 | + public String getDisplayName() { |
| 115 | + return "Container Exposed Ports"; |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments