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);
}
}