SecurityGroupApi securityGroupApi = getNeutronSecurityGroupApi(regionId); if (securityGroupApi != null) { org.jclouds.openstack.neutron.v2.domain.SecurityGroup group = securityGroupApi .name(regionSecurityGroupNameAndPorts.getName()).description("security group created by jclouds") .build()); return createSecurityGroupFrom(group, location, regionSecurityGroupNameAndPorts.getPorts()); } else { authorizeGroupToItselfAndAllIPsToTCPPort(api.get(), novaSecurityGroup, port);
@Override public SecurityGroupInZone apply(ZoneSecurityGroupNameAndPorts zoneSecurityGroupNameAndPorts) { checkNotNull(zoneSecurityGroupNameAndPorts, "zoneSecurityGroupNameAndPorts"); String zoneId = zoneSecurityGroupNameAndPorts.getZone(); Optional<? extends SecurityGroupApi> api = novaApi.getSecurityGroupExtensionForZone(zoneId); checkArgument(api.isPresent(), "Security groups are required, but the extension is not availablein zone %s!", zoneId); logger.debug(">> creating securityGroup %s", zoneSecurityGroupNameAndPorts); try { SecurityGroup securityGroup = api.get().createWithDescription( zoneSecurityGroupNameAndPorts.getName(), zoneSecurityGroupNameAndPorts.getName()); logger.debug("<< created securityGroup(%s)", securityGroup); for (int port : zoneSecurityGroupNameAndPorts.getPorts()) { authorizeGroupToItselfAndAllIPsToTCPPort(api.get(), securityGroup, port); } return new SecurityGroupInZone(api.get().get(securityGroup.getId()), zoneId); } catch (IllegalStateException e) { logger.trace("<< trying to find securityGroup(%s): %s", zoneSecurityGroupNameAndPorts, e.getMessage()); SecurityGroup group = find(api.get().list(), nameEquals(zoneSecurityGroupNameAndPorts .getName())); logger.debug("<< reused securityGroup(%s)", group.getId()); return new SecurityGroupInZone(group, zoneId); } }
CreateSecurityGroupIfNeeded fn = new CreateSecurityGroupIfNeeded(apiWhenSecurityGroupsExist); assertEquals(fn.apply( new ZoneSecurityGroupNameAndPorts("az-1.region-a.geo-1", "jclouds_mygroup", ImmutableSet.of(22, 8080))) .toString(), new SecurityGroupInZone(new ParseComputeServiceTypicalSecurityGroupTest().expected(),
private SecurityGroup createSecurityGroupFrom(final org.jclouds.openstack.neutron.v2.domain.SecurityGroup group, Location location, Set<Integer> ports) { SecurityGroup securityGroup = neutronSecurityGroupToSecurityGroup.create(location).apply(group); logger.debug("<< created securityGroup(%s)", securityGroup); SecurityGroupApi securityGroupApi = getNeutronSecurityGroupApi(location.getId()); try { for (int inboundPort : ports) { logger.debug(">> authorizing securityGroup(%s) permission to 0.0.0.0/0 on port %d", securityGroup, inboundPort); securityGroupApi.create( Rule.CreateRule.createBuilder(RuleDirection.INGRESS, RegionAndId.fromSlashEncoded(securityGroup.getId()).getId()).protocol(RuleProtocol.TCP) .portRangeMin(inboundPort).portRangeMax(inboundPort).remoteIpPrefix("0.0.0.0/0").build()); logger.debug("<< authorized securityGroup(%s) permission to 0.0.0.0/0 on port %d", securityGroup, inboundPort); } return securityGroup; } catch (IllegalStateException e) { logger.trace("<< trying to find securityGroup(%s): %s", group, e.getMessage()); return securityGroupApi.listSecurityGroups().concat() .filter(new Predicate<org.jclouds.openstack.neutron.v2.domain.SecurityGroup>() { @Override public boolean apply(@Nullable org.jclouds.openstack.neutron.v2.domain.SecurityGroup input) { return input.getName().equals(group.getName()); } }).transform(neutronSecurityGroupToSecurityGroup.create(location)).first().orNull(); } }
@Override public SecurityGroupInRegion apply(RegionSecurityGroupNameAndPorts regionSecurityGroupNameAndPorts) { checkNotNull(regionSecurityGroupNameAndPorts, "regionSecurityGroupNameAndPorts"); String regionId = regionSecurityGroupNameAndPorts.getRegion(); Optional<? extends SecurityGroupApi> api = novaApi.getSecurityGroupApi(regionId); checkArgument(api.isPresent(), "Security groups are required, but the extension is not available in region %s!", regionId); logger.debug(">> creating securityGroup %s", regionSecurityGroupNameAndPorts); try { SecurityGroup securityGroup = api.get().createWithDescription( regionSecurityGroupNameAndPorts.getName(), regionSecurityGroupNameAndPorts.getName()); logger.debug("<< created securityGroup(%s)", securityGroup); for (int port : regionSecurityGroupNameAndPorts.getPorts()) { authorizeGroupToItselfAndAllIPsToTCPPort(api.get(), securityGroup, port); } return new SecurityGroupInRegion(api.get().get(securityGroup.getId()), regionId); } catch (IllegalStateException e) { logger.trace("<< trying to find securityGroup(%s): %s", regionSecurityGroupNameAndPorts, e.getMessage()); SecurityGroup group = find(api.get().list(), nameEquals(regionSecurityGroupNameAndPorts .getName())); logger.debug("<< reused securityGroup(%s)", group.getId()); return new SecurityGroupInRegion(group, regionId); } }
CreateSecurityGroupIfNeeded fn = new CreateSecurityGroupIfNeeded(apiCanCreateSecurityGroup); assertEquals(fn.apply( new ZoneSecurityGroupNameAndPorts("az-1.region-a.geo-1", "jclouds_mygroup", ImmutableSet.of(22, 8080))) .toString(), new SecurityGroupInZone(new ParseComputeServiceTypicalSecurityGroupTest().expected(),
private SecurityGroup createSecurityGroupFrom(final org.jclouds.openstack.neutron.v2.domain.SecurityGroup group, Location location, Set<Integer> ports) { SecurityGroup securityGroup = neutronSecurityGroupToSecurityGroup.create(location).apply(group); logger.debug("<< created securityGroup(%s)", securityGroup); SecurityGroupApi securityGroupApi = getNeutronSecurityGroupApi(location.getId()); try { for (int inboundPort : ports) { logger.debug(">> authorizing securityGroup(%s) permission to 0.0.0.0/0 on port %d", securityGroup, inboundPort); securityGroupApi.create( Rule.CreateRule.createBuilder(RuleDirection.INGRESS, RegionAndId.fromSlashEncoded(securityGroup.getId()).getId()).protocol(RuleProtocol.TCP) .portRangeMin(inboundPort).portRangeMax(inboundPort).remoteIpPrefix("0.0.0.0/0").build()); logger.debug("<< authorized securityGroup(%s) permission to 0.0.0.0/0 on port %d", securityGroup, inboundPort); } return securityGroup; } catch (IllegalStateException e) { logger.trace("<< trying to find securityGroup(%s): %s", group, e.getMessage()); return securityGroupApi.listSecurityGroups().concat() .filter(new Predicate<org.jclouds.openstack.neutron.v2.domain.SecurityGroup>() { @Override public boolean apply(@Nullable org.jclouds.openstack.neutron.v2.domain.SecurityGroup input) { return input.getName().equals(group.getName()); } }).transform(neutronSecurityGroupToSecurityGroup.create(location)).first().orNull(); } }
SecurityGroupApi securityGroupApi = getNeutronSecurityGroupApi(regionId); if (securityGroupApi != null) { org.jclouds.openstack.neutron.v2.domain.SecurityGroup group = securityGroupApi .name(regionSecurityGroupNameAndPorts.getName()).description("security group created by jclouds") .build()); return createSecurityGroupFrom(group, location, regionSecurityGroupNameAndPorts.getPorts()); } else { authorizeGroupToItselfAndAllIPsToTCPPort(api.get(), novaSecurityGroup, port);
@Override public SecurityGroupInZone apply(ZoneSecurityGroupNameAndPorts zoneSecurityGroupNameAndPorts) { checkNotNull(zoneSecurityGroupNameAndPorts, "zoneSecurityGroupNameAndPorts"); String zoneId = zoneSecurityGroupNameAndPorts.getZone(); Optional<? extends SecurityGroupApi> api = novaApi.getSecurityGroupExtensionForZone(zoneId); checkArgument(api.isPresent(), "Security groups are required, but the extension is not availablein zone %s!", zoneId); logger.debug(">> creating securityGroup %s", zoneSecurityGroupNameAndPorts); try { SecurityGroup securityGroup = api.get().createWithDescription( zoneSecurityGroupNameAndPorts.getName(), zoneSecurityGroupNameAndPorts.getName()); logger.debug("<< created securityGroup(%s)", securityGroup); for (int port : zoneSecurityGroupNameAndPorts.getPorts()) { authorizeGroupToItselfAndAllIPsToTCPPort(api.get(), securityGroup, port); } return new SecurityGroupInZone(api.get().get(securityGroup.getId()), zoneId); } catch (IllegalStateException e) { logger.trace("<< trying to find securityGroup(%s): %s", zoneSecurityGroupNameAndPorts, e.getMessage()); SecurityGroup group = find(api.get().list(), nameEquals(zoneSecurityGroupNameAndPorts .getName())); logger.debug("<< reused securityGroup(%s)", group.getId()); return new SecurityGroupInZone(group, zoneId); } }
CreateSecurityGroupIfNeeded fn = new CreateSecurityGroupIfNeeded(apiCanCreateSecurityGroup, locationIndex, securityGroupInRegionSecurityGroupFunction, factory); fn.apply(new RegionSecurityGroupNameAndPorts("az-1.region-a.geo-1", "jclouds_mygroup", ImmutableSet.of(22, 8080))).toString(), expected.toString().trim());
@Override public SecurityGroupInZone apply(ZoneSecurityGroupNameAndPorts zoneSecurityGroupNameAndPorts) { checkNotNull(zoneSecurityGroupNameAndPorts, "zoneSecurityGroupNameAndPorts"); String zoneId = zoneSecurityGroupNameAndPorts.getZone(); Optional<? extends SecurityGroupApi> api = novaApi.getSecurityGroupExtensionForZone(zoneId); checkArgument(api.isPresent(), "Security groups are required, but the extension is not availablein zone %s!", zoneId); logger.debug(">> creating securityGroup %s", zoneSecurityGroupNameAndPorts); try { SecurityGroup securityGroup = api.get().createWithDescription( zoneSecurityGroupNameAndPorts.getName(), zoneSecurityGroupNameAndPorts.getName()); logger.debug("<< created securityGroup(%s)", securityGroup); for (int port : zoneSecurityGroupNameAndPorts.getPorts()) { authorizeGroupToItselfAndAllIPsToTCPPort(api.get(), securityGroup, port); } return new SecurityGroupInZone(api.get().get(securityGroup.getId()), zoneId); } catch (IllegalStateException e) { logger.trace("<< trying to find securityGroup(%s): %s", zoneSecurityGroupNameAndPorts, e.getMessage()); SecurityGroup group = find(api.get().list(), nameEquals(zoneSecurityGroupNameAndPorts .getName())); logger.debug("<< reused securityGroup(%s)", group.getId()); return new SecurityGroupInZone(group, zoneId); } }
CreateSecurityGroupIfNeeded fn = new CreateSecurityGroupIfNeeded(apiWhenSecurityGroupsExist, locationIndex, securityGroupInRegionSecurityGroupFunction, factory); fn.apply(new RegionSecurityGroupNameAndPorts("az-1.region-a.geo-1", "jclouds_mygroup", ImmutableSet.of(22, 8080))).toString(), expected.toString().trim() );