com.amazonaws.services.elasticache.model.DescribeCacheClustersResult Java Examples

The following examples show how to use com.amazonaws.services.elasticache.model.DescribeCacheClustersResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ElastiCacheFactoryBeanTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
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
	assertThat(cache).isNotNull();
}
 
Example #2
Source File: ElastiCacheFactoryBeanTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void getObject_clusterWithRedisEngineConfigured_reportsError() throws Exception {
	// Arrange
	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)));

	// Assert
	assertThatThrownBy(elastiCacheFactoryBean::afterPropertiesSet)
			.isInstanceOf(IllegalArgumentException.class)
			.hasMessageContaining("engine");
}
 
Example #3
Source File: ElastiCacheFactoryBean.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Override
protected Cache createInstance() throws Exception {
	DescribeCacheClustersRequest describeCacheClustersRequest = new DescribeCacheClustersRequest()
			.withCacheClusterId(getCacheClusterName());
	describeCacheClustersRequest.setShowCacheNodeInfo(true);

	DescribeCacheClustersResult describeCacheClustersResult = this.amazonElastiCache
			.describeCacheClusters(describeCacheClustersRequest);

	CacheCluster cacheCluster = describeCacheClustersResult.getCacheClusters().get(0);
	if (!"available".equals(cacheCluster.getCacheClusterStatus())) {
		LOGGER.warn(
				"Cache cluster is not available now. Connection may fail during cache access. Current status is {}",
				cacheCluster.getCacheClusterStatus());
	}

	Endpoint configurationEndpoint = getEndpointForCache(cacheCluster);

	for (CacheFactory cacheFactory : this.cacheFactories) {
		if (cacheFactory.isSupportingCacheArchitecture(cacheCluster.getEngine())) {
			return cacheFactory.createCache(this.cacheClusterId,
					configurationEndpoint.getAddress(),
					configurationEndpoint.getPort());
		}
	}

	throw new IllegalArgumentException(
			"No CacheFactory configured for engine: " + cacheCluster.getEngine());
}
 
Example #4
Source File: ElastiCacheFactoryBeanTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
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
	assertThat(cache).isNotNull();
}
 
Example #5
Source File: ElastiCacheUtil.java    From pacbot with Apache License 2.0 4 votes vote down vote up
/**
 * Fetch elasti cache info.
 *
 * @param temporaryCredentials the temporary credentials
 * @param skipRegions the skip regions
 * @param accountId the accountId
 * @return the map
 */
public static Map<String,List<ElastiCacheVH>> fetchElastiCacheInfo(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName) {
    
    Map<String,List<ElastiCacheVH>> elastiCache = new LinkedHashMap<>();
    
    String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource \" ,\"type\": \"ElastiCache\"" ;
    String arnTemplate = "arn:aws:elasticache:%s:%s:cluster:%s";
    for(Region region : RegionUtils.getRegions()) { 
        try{
            if(!skipRegions.contains(region.getName())){ 
                
                List<CacheCluster> cacheClusterList = new ArrayList<>();
                AmazonElastiCache amazonElastiCache = AmazonElastiCacheClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
               
                String marker = null;
                DescribeCacheClustersResult  describeResult ;
                DescribeCacheClustersRequest rqst;
              
                do{        
                    rqst =new DescribeCacheClustersRequest().withMarker(marker);
                    rqst.setShowCacheNodeInfo(true);
                    describeResult = amazonElastiCache.describeCacheClusters(rqst);
                    cacheClusterList.addAll(describeResult.getCacheClusters());
                    marker = describeResult.getMarker();
                  
                }while(marker!=null);
                
                
                List<ReplicationGroup> replicationGroupList = new ArrayList<>();
                marker = null;
                DescribeReplicationGroupsResult  describeRGResult ;
                DescribeReplicationGroupsRequest rgRqst;
              
                do{        
                    rgRqst = new DescribeReplicationGroupsRequest().withMarker(marker);
                    describeRGResult = amazonElastiCache.describeReplicationGroups(rgRqst);
                    replicationGroupList.addAll(describeRGResult.getReplicationGroups());
                    marker = describeResult.getMarker();
                  
                }while(marker!=null);
                
                Map<String,List<CacheCluster>> cacheMap = cacheClusterList.stream().collect(Collectors.groupingBy(cluster-> cluster.getReplicationGroupId()!=null?cluster.getReplicationGroupId():cluster.getCacheClusterId()));
                Map<String,ReplicationGroup> replGrpMap = replicationGroupList.stream().collect(Collectors.toMap(rplGrp -> rplGrp.getReplicationGroupId(),rplGrp->rplGrp));
              
                
                List<ElastiCacheVH> elasticacheList = populateVH(cacheMap,replGrpMap);
             
                String engine;
                String arn ;
                for(ElastiCacheVH cacheVH :elasticacheList){
                	engine = cacheVH.getCluster().getEngine();
                	arn = String.format(arnTemplate, region.getName(),accountId,cacheVH.getCluster().getCacheClusterId()); 	
                    cacheVH.setArn(String.format(arnTemplate, region.getName(),accountId,cacheVH.getClusterName()));
 
                    if("memcached".equalsIgnoreCase(engine)){
                    	cacheVH.setTags(amazonElastiCache.listTagsForResource(new com.amazonaws.services.elasticache.model.ListTagsForResourceRequest().
                                withResourceName(arn)).getTagList());   
                    }
                    List<CacheSubnetGroup> subnetGroups = amazonElastiCache.describeCacheSubnetGroups( new DescribeCacheSubnetGroupsRequest().withCacheSubnetGroupName(cacheVH.getCluster().getCacheSubnetGroupName())).getCacheSubnetGroups();
                    subnetGroups.forEach(cacheGroup-> {
                        cacheVH.setVpc(cacheGroup.getVpcId());
                        cacheVH.setSubnets(cacheGroup.getSubnets().stream().map(Subnet::getSubnetIdentifier).collect(Collectors.toList()));
                    });
                       
                    List<ElastiCacheNodeVH> nodeDetails = getNodeDetails(cacheMap,replGrpMap,accountId, arnTemplate, region,
				amazonElastiCache, cacheVH);
                    cacheVH.setNodes(nodeDetails);
                }
      
                if(!elasticacheList.isEmpty()) {
                    log.debug(InventoryConstants.ACCOUNT + accountId +" Type : ElastiCache "+region.getName() + " >> "+elasticacheList.size());
                    elastiCache.put(accountId+delimiter+accountName+delimiter+region.getName(), elasticacheList);
                }
            }
        }catch(Exception e){
            log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
            ErrorManageUtil.uploadError(accountId,"","elastiCache",e.getMessage());
        }
    }
    return elastiCache;
}