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