Cleanup orphaned availability sets

This commit is contained in:
Ignasi Barrera 2017-02-06 21:13:57 +01:00
parent a312d661f6
commit e0918405c6
2 changed files with 43 additions and 3 deletions

View File

@ -18,6 +18,9 @@ package org.jclouds.azurecompute.arm.compute.functions;
import static com.google.common.base.Preconditions.checkArgument;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
@ -75,9 +78,16 @@ public class TemplateToAvailabilitySet implements Function<Template, Availabilit
checkArgument(location.equals(availabilitySet.location()), "The availability set %s does not belong to location %s",
options.getAvailabilitySetName(), location);
} else {
availabilitySet = api.getAvailabilitySetApi(resourceGroup).createOrUpdate(options.getAvailabilitySet().name(), location,
options.getAvailabilitySet().tags(), options.getAvailabilitySet().properties());
logger.debug(">> creating availability set [%s]", availabilitySet.name());
Map<String, String> tags = new HashMap<String, String>();
if (options.getAvailabilitySet().tags() != null) {
tags.putAll(options.getAvailabilitySet().tags());
}
tags.put("jclouds", options.getAvailabilitySet().name());
logger.debug(">> creating availability set [%s]", options.getAvailabilitySet().name());
availabilitySet = api.getAvailabilitySetApi(resourceGroup).createOrUpdate(
options.getAvailabilitySet().name(), location, tags, options.getAvailabilitySet().properties());
}
}

View File

@ -25,6 +25,7 @@ import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.azurecompute.arm.AzureComputeApi;
import org.jclouds.azurecompute.arm.domain.AvailabilitySet;
import org.jclouds.azurecompute.arm.domain.IdReference;
import org.jclouds.azurecompute.arm.domain.IpConfiguration;
import org.jclouds.azurecompute.arm.domain.NetworkInterfaceCard;
@ -95,6 +96,7 @@ public class CleanupResources {
cleanupVirtualMachineNICs(resourceGroupName, virtualMachine);
cleanupVirtualMachineStorage(resourceGroupName, virtualMachine);
cleanupAvailabilitySetIfOrphaned(resourceGroupName, virtualMachine);
return vmDeleted;
}
@ -163,6 +165,24 @@ public class CleanupResources {
return deleted;
}
public boolean cleanupAvailabilitySetIfOrphaned(String resourceGroup, VirtualMachine virtualMachine) {
boolean deleted = false;
IdReference availabilitySetRef = virtualMachine.properties().availabilitySet();
if (availabilitySetRef != null) {
String name = Iterables.getLast(Splitter.on("/").split(availabilitySetRef.id()));
AvailabilitySet availabilitySet = api.getAvailabilitySetApi(resourceGroup).get(name);
if (isOrphanedJcloudsAvailabilitySet(availabilitySet)) {
logger.debug(">> deleting orphaned availability set %s from %s...", name, resourceGroup);
URI uri = api.getAvailabilitySetApi(resourceGroup).delete(name);
deleted = uri == null || resourceDeleted.apply(uri);
}
}
return deleted;
}
public boolean deleteResourceGroupIfEmpty(String group) {
boolean deleted = false;
if (api.getResourceGroupApi().resources(group).isEmpty()) {
@ -187,6 +207,16 @@ public class CleanupResources {
});
}
private static boolean isOrphanedJcloudsAvailabilitySet(AvailabilitySet availabilitySet) {
// We check for the presence of the 'jclouds' tag to make sure we only
// delete availability sets that were automatically created by jclouds
return availabilitySet != null
&& availabilitySet.tags() != null
&& availabilitySet.tags().containsKey("jclouds")
&& (availabilitySet.properties().virtualMachines() == null || availabilitySet.properties()
.virtualMachines().isEmpty());
}
private List<String> getNetworkCardInterfaceNames(VirtualMachine virtualMachine) {
List<String> nics = Lists.newArrayList();
for (IdReference idReference : virtualMachine.properties().networkProfile().networkInterfaces()) {