Skip to content
Permalink
Browse files
Properly honor the configured regions in all list methods (list nodes…
…, images and security groups)
  • Loading branch information
nacx committed Apr 25, 2017
1 parent 54e5ce5 commit 996acec0872acba25c00baf84062e9c25c419f3c
Showing 2 changed files with 40 additions and 25 deletions.
@@ -220,8 +220,14 @@ private List<VMImage> listImagesByLocation(String location) {
}

private List<VMImage> listCustomImagesByResourceGroup(String resourceGroup) {
List<org.jclouds.azurecompute.arm.domain.Image> customImages = api.getVirtualMachineImageApi(resourceGroup).list();
return Lists.transform(customImages, customImagetoVmImage);
List<org.jclouds.azurecompute.arm.domain.Image> customImgs = api.getVirtualMachineImageApi(resourceGroup).list();
return ImmutableList.copyOf(transform(
filter(customImgs, new Predicate<org.jclouds.azurecompute.arm.domain.Image>() {
@Override
public boolean apply(org.jclouds.azurecompute.arm.domain.Image input) {
return regionIds.get().contains(input.location());
}
}), customImagetoVmImage));
}

@Override
@@ -241,15 +247,7 @@ public String apply(Location location) {
}

// We need to look for custom images in all resource groups
Iterable<ResourceGroup> resourceGroupsInLocation = filter(api.getResourceGroupApi().list(),
new Predicate<ResourceGroup>() {
@Override
public boolean apply(ResourceGroup input) {
return availableLocationNames.contains(input.location());
}
});

for (ResourceGroup resourceGroup : resourceGroupsInLocation) {
for (ResourceGroup resourceGroup : api.getResourceGroupApi().list()) {
osImages.addAll(listCustomImagesByResourceGroup(resourceGroup.name()));
}

@@ -345,7 +343,13 @@ public void suspendNode(final String id) {
public Iterable<VirtualMachine> listNodes() {
ImmutableList.Builder<VirtualMachine> nodes = builder();
for (ResourceGroup resourceGroup : api.getResourceGroupApi().list()) {
nodes.addAll(api.getVirtualMachineApi(resourceGroup.name()).list());
List<VirtualMachine> vms = api.getVirtualMachineApi(resourceGroup.name()).list();
nodes.addAll(filter(vms, new Predicate<VirtualMachine>() {
@Override
public boolean apply(VirtualMachine input) {
return regionIds.get().contains(input.location());
}
}));
}
return nodes.build();
}
@@ -23,7 +23,6 @@
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.transform;
import static org.jclouds.azurecompute.arm.config.AzureComputeProperties.TIMEOUT_RESOURCE_DELETED;
import static org.jclouds.compute.predicates.NodePredicates.locationId;

import java.net.URI;
import java.util.ArrayList;
@@ -55,13 +54,15 @@
import org.jclouds.compute.extensions.SecurityGroupExtension;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.domain.Location;
import org.jclouds.location.Region;
import org.jclouds.logging.Logger;
import org.jclouds.net.domain.IpPermission;
import org.jclouds.net.domain.IpProtocol;

import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
@@ -77,42 +78,52 @@ public class AzureComputeSecurityGroupExtension implements SecurityGroupExtensio
private final SecurityGroupAvailablePredicateFactory securityGroupAvailable;
private final Predicate<URI> resourceDeleted;
private final LoadingCache<String, ResourceGroup> defaultResourceGroup;
private final Supplier<Set<String>> regionIds;

@Inject
AzureComputeSecurityGroupExtension(AzureComputeApi api,
Function<NetworkSecurityGroup, SecurityGroup> groupConverter,
SecurityGroupAvailablePredicateFactory securityRuleAvailable,
@Named(TIMEOUT_RESOURCE_DELETED) Predicate<URI> resourceDeleted,
LoadingCache<String, ResourceGroup> defaultResourceGroup) {
LoadingCache<String, ResourceGroup> defaultResourceGroup,
@Region Supplier<Set<String>> regionIds) {
this.api = api;
this.securityGroupConverter = groupConverter;
this.securityGroupAvailable = securityRuleAvailable;
this.resourceDeleted = resourceDeleted;
this.defaultResourceGroup = defaultResourceGroup;
this.regionIds = regionIds;
}

@Override
public Set<SecurityGroup> listSecurityGroupsInLocation(Location location) {
return securityGroupsInLocations(ImmutableSet.of(location.getId()));
}

@Override
public Set<SecurityGroup> listSecurityGroups() {
ImmutableSet.Builder<SecurityGroup> securityGroups = ImmutableSet.builder();
return securityGroupsInLocations(regionIds.get());
}

private Set<SecurityGroup> securityGroupsInLocations(final Set<String> locations) {
List<SecurityGroup> securityGroups = new ArrayList<SecurityGroup>();
for (ResourceGroup rg : api.getResourceGroupApi().list()) {
securityGroups.addAll(securityGroupsInResourceGroup(rg.name()));
}
return securityGroups.build();

return ImmutableSet.copyOf(filter(securityGroups, new Predicate<SecurityGroup>() {
@Override
public boolean apply(SecurityGroup input) {
return locations.contains(input.getLocation().getId());
}
}));
}

private Set<SecurityGroup> securityGroupsInResourceGroup(String resourceGroup) {
List<NetworkSecurityGroup> networkGroups = api.getNetworkSecurityGroupApi(resourceGroup).list();
return ImmutableSet.copyOf(transform(filter(networkGroups, notNull()), securityGroupConverter));
}

@Override
public Set<SecurityGroup> listSecurityGroupsInLocation(Location location) {
// Even though the resource groups are in a location, each security group
// can be in a different resource group, so we need to inspect all teh
// existing resource groups, and filter afterwards
return ImmutableSet.copyOf(filter(listSecurityGroups(), locationId(location.getId())));
}

@Override
public Set<SecurityGroup> listSecurityGroupsForNode(String nodeId) {
logger.debug(">> getting security groups for node %s...", nodeId);

0 comments on commit 996acec

Please sign in to comment.