From a2863b2a3c3d57e9c9909c5b9f4bba8ac071b3dc Mon Sep 17 00:00:00 2001 From: Svetoslav Neykov Date: Fri, 9 Jun 2017 12:41:16 +0300 Subject: [PATCH] JCLOUDS-1307: Invalidate SG on removal even if already externally deleted --- .../CloudStackSecurityGroupExtension.java | 20 +++++++++++-------- .../extensions/EC2SecurityGroupExtension.java | 10 ++++------ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/extensions/CloudStackSecurityGroupExtension.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/extensions/CloudStackSecurityGroupExtension.java index 627fa557a4..2d56b5ca2a 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/extensions/CloudStackSecurityGroupExtension.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/compute/extensions/CloudStackSecurityGroupExtension.java @@ -23,6 +23,7 @@ import static com.google.common.collect.Iterables.transform; import static org.jclouds.cloudstack.predicates.SecurityGroupPredicates.ruleCidrMatches; import static org.jclouds.cloudstack.predicates.SecurityGroupPredicates.ruleGroupMatches; +import java.util.Map; import java.util.Set; import javax.inject.Inject; @@ -150,6 +151,7 @@ public class CloudStackSecurityGroupExtension implements SecurityGroupExtension api.getSecurityGroupApi().getSecurityGroup(id); if (group == null) { + invalidateCache(id); return false; } @@ -159,17 +161,19 @@ public class CloudStackSecurityGroupExtension implements SecurityGroupExtension api.getSecurityGroupApi().deleteSecurityGroup(id); - // TODO find something better here maybe - hard to map zones to groups - for (Location location : locations.get()) { - groupCreator.invalidate(ZoneSecurityGroupNamePortsCidrs.builder() - .zone(location.getId()) - .name(group.getName()) - .build()); - } - + invalidateCache(id); return true; } + private void invalidateCache(String id) { + for (Map.Entry sg : groupCreator.asMap().entrySet()) { + if (id.equals(sg.getValue().getId())) { + groupCreator.invalidate(sg.getKey()); + break; + } + } + } + @Override public SecurityGroup addIpPermission(IpPermission ipPermission, SecurityGroup group) { checkNotNull(group, "group"); diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/compute/extensions/EC2SecurityGroupExtension.java b/apis/ec2/src/main/java/org/jclouds/ec2/compute/extensions/EC2SecurityGroupExtension.java index 081d3cccfa..faed4037cd 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/compute/extensions/EC2SecurityGroupExtension.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/compute/extensions/EC2SecurityGroupExtension.java @@ -172,17 +172,15 @@ public class EC2SecurityGroupExtension implements SecurityGroupExtension { String region = parts[0]; String groupName = parts[1]; + boolean deleted = false; if (!client.getSecurityGroupApi().get().describeSecurityGroupsInRegion(region, groupName).isEmpty()) { client.getSecurityGroupApi().get().deleteSecurityGroupInRegion(region, groupName); - // TODO: test this clear happens - groupCreator.invalidate(new RegionNameAndIngressRules(region, groupName, null, false, null)); - return true; + deleted = true; } - - return false; + groupCreator.invalidate(new RegionNameAndIngressRules(region, groupName, null, false, null)); + return deleted; } - @Override public SecurityGroup addIpPermission(IpPermission ipPermission, SecurityGroup group) { String region = AWSUtils.getRegionFromLocationOrNull(group.getLocation());