|
| 1 | +package com.playtika.testcontainers.aerospike.enterprise; |
| 2 | + |
| 3 | +import com.playtika.testcontainer.aerospike.AerospikeProperties; |
| 4 | +import com.playtika.testcontainer.aerospike.EmbeddedAerospikeBootstrapConfiguration; |
| 5 | +import jakarta.annotation.PostConstruct; |
| 6 | +import lombok.extern.slf4j.Slf4j; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 9 | +import org.springframework.boot.autoconfigure.AutoConfiguration; |
| 10 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; |
| 11 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 12 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 13 | +import org.springframework.context.annotation.PropertySource; |
| 14 | +import org.springframework.core.env.Environment; |
| 15 | +import org.testcontainers.containers.GenericContainer; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | + |
| 19 | +@Slf4j |
| 20 | +@AutoConfiguration(after = EmbeddedAerospikeBootstrapConfiguration.class) |
| 21 | +@ConditionalOnExpression("${embedded.containers.enabled:true}") |
| 22 | +@ConditionalOnProperty(value = "embedded.aerospike.enabled", matchIfMissing = true) |
| 23 | +@EnableConfigurationProperties(AerospikeEnterpriseProperties.class) |
| 24 | +@PropertySource("classpath:/embedded-enterprise-aerospike.properties") |
| 25 | +public class SetupEnterpriseAerospikeBootstrapConfiguration { |
| 26 | + |
| 27 | + private static final String DOCKER_IMAGE = "aerospike/aerospike-server-enterprise:6.3.0.16_1"; |
| 28 | + private static final String AEROSPIKE_DOCKER_IMAGE_PROPERTY = "embedded.aerospike.dockerImage"; |
| 29 | + private static final ImageVersion SUITABLE_IMAGE_VERSION = new ImageVersion(6, 3); |
| 30 | + private static final String TEXT_TO_DOCUMENTATION = "Documentation: https://github.com/PlaytikaOSS/testcontainers-spring-boot/blob/develop/embedded-aerospike-enterprise/README.adoc"; |
| 31 | + |
| 32 | + private GenericContainer<?> aerospikeContainer; |
| 33 | + private AerospikeProperties aerospikeProperties; |
| 34 | + private AerospikeEnterpriseProperties aerospikeEnterpriseProperties; |
| 35 | + private Environment environment; |
| 36 | + |
| 37 | + @Autowired |
| 38 | + public void setEnvironment(Environment environment) { |
| 39 | + this.environment = environment; |
| 40 | + } |
| 41 | + |
| 42 | + @Autowired |
| 43 | + @Qualifier(AerospikeProperties.BEAN_NAME_AEROSPIKE) |
| 44 | + public void setAerospikeContainer(GenericContainer<?> aerospikeContainer) { |
| 45 | + this.aerospikeContainer = aerospikeContainer; |
| 46 | + } |
| 47 | + |
| 48 | + @Autowired |
| 49 | + public void setAerospikeProperties(AerospikeProperties aerospikeProperties) { |
| 50 | + this.aerospikeProperties = aerospikeProperties; |
| 51 | + } |
| 52 | + |
| 53 | + @Autowired |
| 54 | + public void setAerospikeEnterpriseProperties(AerospikeEnterpriseProperties aerospikeEnterpriseProperties) { |
| 55 | + this.aerospikeEnterpriseProperties = aerospikeEnterpriseProperties; |
| 56 | + } |
| 57 | + |
| 58 | + @PostConstruct |
| 59 | + public void setupEnterpriseAerospike() throws IOException, InterruptedException { |
| 60 | + verifyAerospikeImage(); |
| 61 | + AerospikeEnterpriseConfigurer aerospikeEnterpriseConfigurer = new AerospikeEnterpriseConfigurer(aerospikeProperties, aerospikeEnterpriseProperties); |
| 62 | + aerospikeEnterpriseConfigurer.configure(aerospikeContainer); |
| 63 | + } |
| 64 | + |
| 65 | + private void verifyAerospikeImage() { |
| 66 | + log.info("Verify Aerospike Enterprise Image"); |
| 67 | + |
| 68 | + String dockerImage = environment.getProperty(AEROSPIKE_DOCKER_IMAGE_PROPERTY); |
| 69 | + if (dockerImage == null) { |
| 70 | + throw new IllegalStateException("Aerospike enterprise docker image not provided, set up 'embedded.aerospike.dockerImage' property.\n" |
| 71 | + + TEXT_TO_DOCUMENTATION); |
| 72 | + } |
| 73 | + |
| 74 | + if (!isEnterpriseImage(dockerImage)) { |
| 75 | + throw illegalAerospikeImageException(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private IllegalStateException illegalAerospikeImageException() { |
| 80 | + return new IllegalStateException( |
| 81 | + "You should use enterprise image for the Aerospike container with equal or higher version: " + DOCKER_IMAGE + ". " |
| 82 | + + "Container enable 'disallow-expunge' config option to prevent non-durable deletes, and this config option is available starting with version 6.3. " |
| 83 | + + "Enterprise image is required, as non-durable deletes are not available in Community." |
| 84 | + + TEXT_TO_DOCUMENTATION |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + private boolean isEnterpriseImage(String dockerImage) { |
| 89 | + return dockerImage.contains("enterprise") |
| 90 | + && isSuitableVersion(dockerImage); |
| 91 | + } |
| 92 | + |
| 93 | + private boolean isSuitableVersion(String dockerImage) { |
| 94 | + int index = dockerImage.indexOf(":"); |
| 95 | + if (index == -1) { |
| 96 | + throw new IllegalStateException("Invalid docker image version format: " + dockerImage + ".\n" |
| 97 | + + TEXT_TO_DOCUMENTATION); |
| 98 | + } |
| 99 | + String version = dockerImage.substring(index + 1); |
| 100 | + ImageVersion imageVersion = ImageVersion.parse(version); |
| 101 | + return imageVersion.compareTo(SUITABLE_IMAGE_VERSION) >= 0; |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments