Skip to content

Commit d76cc36

Browse files
committed
Add ability to bypass failure on missing snapshot
1 parent d81bb31 commit d76cc36

4 files changed

Lines changed: 29 additions & 12 deletions

File tree

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ public class DeleteSnapshot extends VSphereBuildStep {
4040
private final String vm;
4141
private final String snapshotName;
4242
private final boolean consolidate;
43+
private final boolean failOnNoExist;
4344

4445
@DataBoundConstructor
45-
public DeleteSnapshot(final String vm, final String snapshotName, final boolean consolidate) throws VSphereException {
46+
public DeleteSnapshot(final String vm, final String snapshotName, final boolean consolidate, final boolean failOnNoExist) throws VSphereException {
4647
this.vm = vm;
4748
this.snapshotName = snapshotName;
4849
this.consolidate = consolidate;
50+
this.failOnNoExist = failOnNoExist;
4951
}
5052

5153
public String getVm() {
@@ -60,6 +62,10 @@ public boolean isConsolidate() {
6062
return consolidate;
6163
}
6264

65+
public boolean isFailOnNoExist() {
66+
return failOnNoExist;
67+
}
68+
6369
public boolean perform(final AbstractBuild<?, ?> build, Launcher launcher, final BuildListener listener) throws VSphereException {
6470
return deleteSnapshot(build, launcher, listener);
6571
//TODO throw AbortException instead of returning value
@@ -79,7 +85,7 @@ private boolean deleteSnapshot(final AbstractBuild<?, ?> build, Launcher launche
7985
String expandedVm = env.expand(vm);
8086

8187
VSphereLogger.vsLogger(jLogger, "Deleting snapshot \""+expandedSnap+"\" of VM "+expandedVm+"...");
82-
vsphere.deleteSnapshot(expandedVm, expandedSnap, consolidate);
88+
vsphere.deleteSnapshot(expandedVm, expandedSnap, consolidate, failOnNoExist);
8389
VSphereLogger.vsLogger(jLogger, "Complete.");
8490

8591
return true;

src/main/java/org/jenkinsci/plugins/vsphere/tools/VSphere.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,26 +234,30 @@ public void revertToSnapshot(String vmName, String snapName) throws VSphereExcep
234234
throw new VSphereException(e);
235235
}
236236
}
237-
238-
public void deleteSnapshot(String vmName, String snapName, boolean consolidate) throws VSphereException{
237+
238+
public void deleteSnapshot(String vmName, String snapName, boolean consolidate, boolean failOnNoExist) throws VSphereException{
239239

240240
VirtualMachine vm = getVmByName(vmName);
241241
VirtualMachineSnapshot snap = getSnapshotInTree(vm, snapName);
242-
243-
if (snap == null) {
242+
243+
if (snap == null && failOnNoExist) {
244244
throw new VSphereException("Virtual Machine snapshot cannot be found");
245245
}
246246

247247
try{
248-
//Does not delete subtree; Implicitly consolidates disk
249-
Task task = snap.removeSnapshot_Task(false);
250-
if (!task.waitForTask().equals(Task.SUCCESS)) {
251-
throw new VSphereException("Could not delete snapshot");
248+
249+
Task task;
250+
if (snap!=null){
251+
//Does not delete subtree; Implicitly consolidates disk
252+
task = snap.removeSnapshot_Task(false);
253+
if (!task.waitForTask().equals(Task.SUCCESS)) {
254+
throw new VSphereException("Could not delete snapshot");
255+
}
252256
}
253-
257+
254258
if(!consolidate)
255259
return;
256-
260+
257261
//This might be redundant, but I think it consolidates all disks,
258262
//where as the removeSnapshot only consolidates the individual disk
259263
task = vm.consolidateVMDisks_Task();

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,9 @@ limitations under the License.
2727
<f:checkbox />
2828
</f:entry>
2929

30+
<f:entry title="${%Fail on null?}" field="failOnNoExist">
31+
<f:checkbox />
32+
</f:entry>
33+
3034
<f:validateButton title="${%Check Data}" progress="${%Testing...}" method="testData" with="serverName,vm,snapshotName"/>
3135
</j:jelly>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
If the snapshot you are trying to delete doesnt exist, fail this build step. Sometimes, the job doesnt care if deletion was successful and only wants it gone---if this is the case, leave unchecked.
3+
</div>

0 commit comments

Comments
 (0)