@Test public void getObject_availableCluster_returnsConfiguredMemcachedClient() throws Exception { // Arrange AmazonElastiCache amazonElastiCache = mock(AmazonElastiCacheClient.class); DescribeCacheClustersRequest testCache = new DescribeCacheClustersRequest().withCacheClusterId("testCache"); testCache.setShowCacheNodeInfo(true); when(amazonElastiCache.describeCacheClusters(testCache)). thenReturn(new DescribeCacheClustersResult().withCacheClusters(new CacheCluster().withConfigurationEndpoint( new Endpoint().withAddress("localhost").withPort(45678)).withCacheClusterStatus("available").withEngine("memcached"))); ElastiCacheFactoryBean elasticCacheFactoryBean = new ElastiCacheFactoryBean(amazonElastiCache, "testCache", Collections.singletonList(new TestCacheFactory("testCache", "localhost", 45678))); // Act elasticCacheFactoryBean.afterPropertiesSet(); Cache cache = elasticCacheFactoryBean.getObject(); // Assert assertNotNull(cache); }
@Test public void getObject_availableClusterWithLogicalName_returnsConfigurationMemcachedClientWithPhysicalName() throws Exception { // Arrange AmazonElastiCache amazonElastiCache = mock(AmazonElastiCacheClient.class); DescribeCacheClustersRequest testCache = new DescribeCacheClustersRequest().withCacheClusterId("testCache"); testCache.setShowCacheNodeInfo(true); when(amazonElastiCache.describeCacheClusters(testCache)). thenReturn(new DescribeCacheClustersResult().withCacheClusters(new CacheCluster().withConfigurationEndpoint( new Endpoint().withAddress("localhost").withPort(45678)).withCacheClusterStatus("available").withEngine("memcached"))); ResourceIdResolver resourceIdResolver = mock(ResourceIdResolver.class); when(resourceIdResolver.resolveToPhysicalResourceId("test")).thenReturn("testCache"); ElastiCacheFactoryBean elastiCacheFactoryBean = new ElastiCacheFactoryBean(amazonElastiCache, "test", resourceIdResolver, Collections.<CacheFactory>singletonList(new TestCacheFactory("test", "localhost", 45678))); // Act elastiCacheFactoryBean.afterPropertiesSet(); Cache cache = elastiCacheFactoryBean.getObject(); // Assert assertNotNull(cache); }
@Test public void getObject_clusterWithRedisEngineConfigured_reportsError() throws Exception { //Arrange this.expectedException.expect(IllegalArgumentException.class); this.expectedException.expectMessage("engine"); AmazonElastiCache amazonElastiCache = mock(AmazonElastiCacheClient.class); DescribeCacheClustersRequest memcached = new DescribeCacheClustersRequest().withCacheClusterId("memcached"); memcached.setShowCacheNodeInfo(true); when(amazonElastiCache.describeCacheClusters(memcached)). thenReturn(new DescribeCacheClustersResult().withCacheClusters(new CacheCluster().withEngine("redis").withCacheNodes(new CacheNode(). withEndpoint(new Endpoint().withAddress("localhost").withPort(45678))))); ElastiCacheFactoryBean elastiCacheFactoryBean = new ElastiCacheFactoryBean(amazonElastiCache, "memcached", Collections.singletonList(new TestCacheFactory("testCache", "localhost", 45678))); // Act elastiCacheFactoryBean.afterPropertiesSet(); // Assert }
@Test public void parseInternal_clusterCacheConfigurationWithRegion_returnsConfiguredClusterCacheWithRegion() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); //Load xml file XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); xmlBeanDefinitionReader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-elastiCacheConfigRegionConfigured.xml", getClass())); //Act BeanDefinition beanDefinition = beanFactory.getBeanDefinition(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName())); //Assert assertNotNull(beanDefinition); assertNotNull(beanDefinition.getPropertyValues().get("customRegion")); }
/** * Collect data for ElastiCache. * * @param stats * current statistics object. * @param account * currently used credentials object. * @param region * currently used aws region. */ public static void scanElastiCache(AwsStats stats, AwsAccount account, Regions region) { if (region == Regions.EU_CENTRAL_1) return; LOG.debug("Scan for ElastiCache in region " + region.getName() + " in account " + account.getAccountId()); /* * Amazon ElastiCache */ try { AmazonElastiCache elastiCache = new AmazonElastiCacheClient(account.getCredentials()); elastiCache.setRegion(Region.getRegion(region)); List<CacheCluster> list = elastiCache.describeCacheClusters().getCacheClusters(); int totalItems = list.size(); for (CacheCluster cluster : list) { AwsResource res = new AwsResource(cluster.getCacheClusterId(), account.getAccountId(), AwsResourceType.ElastiCache, region); res.addInfo("Engine", cluster.getEngine()); res.addInfo("EngineVersion", cluster.getEngineVersion()); res.addInfo("NumCacheNodes", cluster.getNumCacheNodes()); stats.add(res); } LOG.info(totalItems + " ElastiCache in region " + region.getName() + " in account " + account.getAccountId()); } catch (AmazonServiceException ase) { LOG.error("Exception of ElastiCache: " + ase.getMessage()); } }
@Test public void parseInternal_cacheConfigWithExpiration_returnsConfiguredCacheThatRespectExpiration() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); //Register a mock object which will be used to replay service calls BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class); beanDefinitionBuilder.setFactoryMethod("mock"); beanDefinitionBuilder.addConstructorArgValue(AmazonElastiCache.class); beanFactory.registerBeanDefinition(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), beanDefinitionBuilder.getBeanDefinition()); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); reader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-cacheConfigWithExpiration.xml", getClass())); AmazonElastiCache client = beanFactory.getBean(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), AmazonElastiCache.class); //Replay invocation that will be called DescribeCacheClustersRequest memcached = new DescribeCacheClustersRequest().withCacheClusterId("memcached"); memcached.setShowCacheNodeInfo(true); when(client.describeCacheClusters(memcached)).thenReturn( new DescribeCacheClustersResult().withCacheClusters( new CacheCluster().withCacheClusterId("memcached"). withConfigurationEndpoint(new Endpoint().withAddress("localhost").withPort(Integer.parseInt(System.getProperty("memcachedPort")))). withCacheClusterStatus("available").withEngine("memcached") ) ); //Act CacheManager cacheManager = beanFactory.getBean(CacheManager.class); Cache cache = cacheManager.getCache("memcached"); cache.put("foo", "bar"); Thread.sleep(2000); //Assert assertNotNull(cacheManager); assertNotNull(cache); assertNull(cache.get("foo")); }
@Test public void parseInternal_clusterCacheConfiguration_returnsConfiguredClusterCache() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); //Register a mock object which will be used to replay service calls BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class); beanDefinitionBuilder.setFactoryMethod("mock"); beanDefinitionBuilder.addConstructorArgValue(AmazonElastiCache.class); beanFactory.registerBeanDefinition(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), beanDefinitionBuilder.getBeanDefinition()); //Load xml file XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); xmlBeanDefinitionReader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-elastiCacheConfig.xml", getClass())); AmazonElastiCache client = beanFactory.getBean(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), AmazonElastiCache.class); //Replay invocation that will be called DescribeCacheClustersRequest memcached = new DescribeCacheClustersRequest().withCacheClusterId("memcached"); memcached.setShowCacheNodeInfo(true); when(client.describeCacheClusters(memcached)).thenReturn( new DescribeCacheClustersResult().withCacheClusters( new CacheCluster().withCacheClusterId("memcached"). withConfigurationEndpoint(new Endpoint().withAddress("localhost").withPort(Integer.parseInt(System.getProperty("memcachedPort")))). withCacheClusterStatus("available").withEngine("memcached") ) ); //Act CacheManager cacheManager = beanFactory.getBean(CacheManager.class); Cache cache = cacheManager.getCache("memcached"); cache.put("foo", "bar"); cache.evict("foo"); //Assert assertNotNull(cacheManager); assertNotNull(cache); }
AWSDatabase(final Map<String, AmazonEC2Client> ec2Clients, final Map<String, AmazonRDSClient> rdsClients, final Map<String, AmazonDynamoDBClient> dynamoClients, final Map<String, AmazonSQSClient> sqsClients, final Map<String, AmazonElastiCacheClient> elasticacheClients, final AmazonIdentityManagementClient iamClient) { this(ec2Clients, rdsClients, dynamoClients, sqsClients, elasticacheClients, iamClient, null); }
@Bean @ConditionalOnMissingAmazonClient(AmazonElastiCache.class) public AmazonWebserviceClientFactoryBean<AmazonElastiCacheClient> amazonElastiCache() { return new AmazonWebserviceClientFactoryBean<>(AmazonElastiCacheClient.class, this.credentialsProvider, this.regionProvider); }
@Test public void parseInternal_mixedCacheConfig_returnsBothCaches() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); //Register a mock object which will be used to replay service calls BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class); beanDefinitionBuilder.setFactoryMethod("mock"); beanDefinitionBuilder.addConstructorArgValue(AmazonElastiCache.class); beanFactory.registerBeanDefinition(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), beanDefinitionBuilder.getBeanDefinition()); BeanDefinitionBuilder cacheBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class); cacheBuilder.setFactoryMethod("mock"); cacheBuilder.addConstructorArgValue(AmazonElastiCache.class); beanFactory.registerBeanDefinition(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), cacheBuilder.getBeanDefinition()); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); reader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-mixedCacheConfig.xml", getClass())); AmazonElastiCache client = beanFactory.getBean(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), AmazonElastiCache.class); //Replay invocation that will be called DescribeCacheClustersRequest memcached1 = new DescribeCacheClustersRequest().withCacheClusterId("memcached"); memcached1.setShowCacheNodeInfo(true); when(client.describeCacheClusters(memcached1)).thenReturn( new DescribeCacheClustersResult().withCacheClusters( new CacheCluster().withCacheClusterId("memcached"). withConfigurationEndpoint(new Endpoint().withAddress("localhost").withPort(Integer.parseInt(System.getProperty("memcachedPort")))). withCacheClusterStatus("available").withEngine("memcached") ) ); Cache cache = beanFactory.getBean("memc", Cache.class); when(cache.getName()).thenReturn("memc"); //Act CacheManager cacheManager = beanFactory.getBean(CacheManager.class); Cache memc = cacheManager.getCache("memc"); Cache memcached = cacheManager.getCache("memcached"); //Assert assertNotNull(cacheManager); assertNotNull(memcached); memc.put("foo", "bar"); memc.evict("foo"); memcached.put("foo", "bar"); memcached.evict("foo"); }
@Test public void parseInternal_clusterCacheConfigurationWithLogicalName_returnsConfiguredClusterCacheWithPhysicalName() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); //Register a mock object which will be used to replay service calls BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class); beanDefinitionBuilder.setFactoryMethod("mock"); beanDefinitionBuilder.addConstructorArgValue(AmazonElastiCache.class); beanFactory.registerBeanDefinition(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), beanDefinitionBuilder.getBeanDefinition()); BeanDefinitionBuilder resourceIdBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class); resourceIdBuilder.setFactoryMethod("mock"); resourceIdBuilder.addConstructorArgValue(ResourceIdResolver.class); beanFactory.registerBeanDefinition(GlobalBeanDefinitionUtils.RESOURCE_ID_RESOLVER_BEAN_NAME, resourceIdBuilder.getBeanDefinition()); //Load xml file XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); xmlBeanDefinitionReader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-elastiCacheConfigStackConfigured.xml", getClass())); AmazonElastiCache client = beanFactory.getBean(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), AmazonElastiCache.class); ResourceIdResolver resourceIdResolver = beanFactory.getBean(GlobalBeanDefinitionUtils.RESOURCE_ID_RESOLVER_BEAN_NAME, ResourceIdResolver.class); when(resourceIdResolver.resolveToPhysicalResourceId("testMemcached")).thenReturn("memcached"); //Replay invocation that will be called DescribeCacheClustersRequest memcached = new DescribeCacheClustersRequest().withCacheClusterId("memcached"); memcached.setShowCacheNodeInfo(true); when(client.describeCacheClusters(memcached)).thenReturn( new DescribeCacheClustersResult().withCacheClusters( new CacheCluster().withCacheClusterId("memcached"). withConfigurationEndpoint(new Endpoint().withAddress("localhost").withPort(Integer.parseInt(System.getProperty("memcachedPort")))). withCacheClusterStatus("available").withEngine("memcached") ) ); //Act CacheManager cacheManager = beanFactory.getBean(CacheManager.class); Cache cache = cacheManager.getCache("testMemcached"); cache.put("foo", "bar"); cache.evict("foo"); //Assert assertNotNull(cacheManager); assertNotNull(cache); }
public AWSDatabaseHolder(Config config) { maxAgeInMs = config.getDuration("maxAge", TimeUnit.MILLISECONDS); final DefaultAWSCredentialsProviderChain awsCredentialsProviderChain = new DefaultAWSCredentialsProviderChain(); final ClientConfiguration clientConfig = new ClientConfiguration(); clientConfig.setRetryPolicy(new RetryPolicy(null, null, config.getInt("maxErrorRetry"), true)); final AmazonEC2Client bootstrapEC2Client = new AmazonEC2Client(awsCredentialsProviderChain); ec2Clients = Maps.newHashMap(); rdsClients = Maps.newHashMap(); sqsClients = Maps.newHashMap(); dynamoDBClients = Maps.newHashMap(); elasticacheClients = Maps.newHashMap(); final List<Region> ec2Regions = bootstrapEC2Client.describeRegions().getRegions(); for (Region region : ec2Regions) { final String regionName = region.getRegionName(); final String endpoint = region.getEndpoint(); log.debug("Adding ec2 region {}", region); final AmazonEC2Client ec2Client = new AmazonEC2Client(awsCredentialsProviderChain, clientConfig); ec2Client.setEndpoint(endpoint); ec2Clients.put(regionName, ec2Client); final AmazonRDSClient rdsClient = new AmazonRDSClient(awsCredentialsProviderChain, clientConfig); rdsClient.setEndpoint(endpoint.replaceFirst("ec2\\.", "rds.")); rdsClients.put(regionName, rdsClient); final AmazonDynamoDBClient dynamoDBClient = new AmazonDynamoDBClient(awsCredentialsProviderChain, clientConfig); dynamoDBClient.setEndpoint(endpoint.replaceFirst("ec2\\.", "dynamodb.")); dynamoDBClients.put(regionName, dynamoDBClient); final AmazonSQSClient sqsClient = new AmazonSQSClient(awsCredentialsProviderChain, clientConfig); sqsClient.setEndpoint(endpoint.replaceFirst("ec2\\.", "sqs.")); sqsClients.put(regionName, sqsClient); final AmazonElastiCacheClient elastiCacheClient = new AmazonElastiCacheClient (awsCredentialsProviderChain, clientConfig); elastiCacheClient.setEndpoint(endpoint.replaceFirst("ec2\\.", "elasticache.")); elasticacheClients.put(regionName, elastiCacheClient); } this.iamClient = new AmazonIdentityManagementClient(awsCredentialsProviderChain, clientConfig); if (config.hasPath("accountNumber")) { this.awsAccountNumber = config.getString("accountNumber"); } else { this.awsAccountNumber = null; } rebuild(); }