Skip to content

Commit 6950e8b

Browse files
author
igreenfi
committed
Add new parameter 'ignoreIfNotExists'
1 parent f488a3d commit 6950e8b

File tree

2 files changed

+20
-4
lines changed
  • src/main
    • java/org/jenkinsci/plugins/vsphere/builders
    • resources/org/jenkinsci/plugins/vsphere/builders/PowerOff

2 files changed

+20
-4
lines changed

src/main/java/org/jenkinsci/plugins/vsphere/builders/PowerOff.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,19 @@ public class PowerOff extends VSphereBuildStep implements SimpleBuildStep {
4343
private final boolean evenIfSuspended;
4444
private final boolean shutdownGracefully;
4545

46+
47+
private final boolean ignoreIfNotExists;
48+
4649
@DataBoundConstructor
47-
public PowerOff( final String vm, final boolean evenIfSuspended, final boolean shutdownGracefully) throws VSphereException {
50+
public PowerOff( final String vm, final boolean evenIfSuspended, final boolean shutdownGracefully, final boolean ignoreIfNotExists) throws VSphereException {
4851
this.vm = vm;
4952
this.evenIfSuspended = evenIfSuspended;
5053
this.shutdownGracefully = shutdownGracefully;
54+
this.ignoreIfNotExists = ignoreIfNotExists;
55+
}
56+
57+
public boolean isIgnoreIfNotExists() {
58+
return ignoreIfNotExists;
5159
}
5260

5361
public boolean isEvenIfSuspended() {
@@ -118,12 +126,17 @@ private boolean powerOff(final Run<?, ?> run, Launcher launcher, final TaskListe
118126

119127
VSphereLogger.vsLogger(jLogger, "Shutting Down VM " + expandedVm + "...");
120128
VirtualMachine vsphereVm = vsphere.getVmByName(expandedVm);
121-
if (vsphereVm == null) {
129+
if (vsphereVm == null && !ignoreIfNotExists) {
122130
throw new RuntimeException(Messages.validation_notFound("vm " + expandedVm));
123131
}
124-
vsphere.powerOffVm( vsphereVm, evenIfSuspended, shutdownGracefully);
125132

126-
VSphereLogger.vsLogger(jLogger, "Successfully shutdown \""+expandedVm+"\"");
133+
if (vsphereVm != null) {
134+
vsphere.powerOffVm(vsphereVm, evenIfSuspended, shutdownGracefully);
135+
136+
VSphereLogger.vsLogger(jLogger, "Successfully shutdown \"" + expandedVm + "\"");
137+
} else {
138+
VSphereLogger.vsLogger(jLogger, "Does not exists, BUT ignore it! \"" + expandedVm + "\"");
139+
}
127140

128141
return true;
129142
}

src/main/resources/org/jenkinsci/plugins/vsphere/builders/PowerOff/config.jelly

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ limitations under the License.
2525
<f:entry title="${%Shutdown gracefully?}" field="shutdownGracefully">
2626
<f:checkbox />
2727
</f:entry>
28+
<f:entry title="${%ignore If Not Exists?}" field="ignoreIfNotExists">
29+
<f:checkbox />
30+
</f:entry>
2831

2932
<f:validateButton title="${%Check Data}" progress="${%Testing...}" method="testData" with="serverName,vm"/>
3033
</j:jelly>

0 commit comments

Comments
 (0)