private void getRDSInstances() { DescribeDBInstancesResult response = rdsClient.describeDBInstances(new DescribeDBInstancesRequest()); List<PulseInstance> rds = new ArrayList<PulseInstance>(); PulseInstance pulse = null; for (DBInstance instance : response.getDBInstances()) { ListTagsForResourceResult result = rdsClient.listTagsForResource(new ListTagsForResourceRequest().withResourceName(instance.getDBInstanceArn())); for (com.amazonaws.services.rds.model.Tag tag : result.getTagList()) { if (tag.getKey().equals(properties.getTag()) && (tag.getValue().equals("TRUE") || tag.getValue().equals("True") || tag.getValue().equals("true"))) { pulse = new PulseInstance(); pulse.setId(instance.getDBInstanceIdentifier()); pulse.setName(instance.getDBInstanceIdentifier()); pulse.setAddress(instance.getEndpoint().getAddress()); pulse.setType(instance.getDBInstanceClass()); rds.add(pulse); } } } GetMonitoringInstances.rds = rds; }
public void describe(String dbInstanceIdentifier) throws Exception { while (true) { DescribeDBInstancesRequest request = new DescribeDBInstancesRequest() .withDBInstanceIdentifier(dbInstanceIdentifier); DescribeDBInstancesResult response = client.describeDBInstances(request); DBInstance dbInstance = response.getDBInstances().get(0); if (!dbInstance.getDBInstanceStatus().equalsIgnoreCase("creating")) { System.out.println("Done! " + response); System.out.println(dbInstance.getEndpoint().getAddress()); System.out.println(dbInstance.getEndpoint().getPort()); break; } System.out.println("Not done - will wait 10s: " + response); Thread.sleep(10000L); } }
@Test public void testCheck() throws Exception { final FetchRdsJob fetchRdsJob = new FetchRdsJob(accountIdSupplierMock, clientProviderMock, jobsPropertiesMock, violationSinkMock, exceptionHandlerMock); when(amazonRDSClientMock.describeDBInstances(any(DescribeDBInstancesRequest.class))).thenReturn(describeDBInstancesResultMock); fetchRdsJob.run(); ArgumentCaptor<Violation> violations = ArgumentCaptor.forClass(Violation.class); verify(violationSinkMock, times(2)).put(violations.capture()); verify(accountIdSupplierMock, times(1)).get(); verify(amazonRDSClientMock, times(1)).describeDBInstances(any(DescribeDBInstancesRequest.class)); verify(jobsPropertiesMock, times(1)).getWhitelistedRegions(); verify(clientProviderMock, times(1)).getClient(any(), any(String.class), any(Region.class)); // Regression test for #479: Make sure that the metadata lists the correct endpoints. assertArrayEquals(new String[] {"aws.db.cn", "aws.db2.cn"}, violations.getAllValues().stream() .map(v -> ((Map<String, Object>) v.getMetaInfo()).get("unsecuredDatabase")) .toArray()); }
@Override public List<Endpoint> getReadReplicaEndpoints(String dbInstanceId) { List<Endpoint> endpoints = null; DescribeDBInstancesResult result = rds.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier(dbInstanceId)); // If the master exist and has any read replicas if(result.getDBInstances().size() == 1 && result.getDBInstances().get(0).getReadReplicaDBInstanceIdentifiers().size() > 0) { endpoints = new ArrayList<Endpoint>(); for(String readReplicaId : result.getDBInstances().get(0).getReadReplicaDBInstanceIdentifiers()) { DBInstance rrInstance = rds.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier(readReplicaId)).getDBInstances().get(0); if (rrInstance.getDBInstanceStatus().equals("available")) { endpoints.add(rrInstance.getEndpoint()); } } if(endpoints.size() == 0) { endpoints = null; } } return endpoints; }
private boolean isDatabaseAvailable(RetryContext context) { DescribeDBInstancesResult describeDBInstancesResult; try { describeDBInstancesResult = this.amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier((String) context.getAttribute(DB_INSTANCE_ATTRIBUTE_NAME))); } catch (DBInstanceNotFoundException e) { LOGGER.warn("Database Instance with name {} has been removed or is not configured correctly, no retry possible", getDbInstanceIdentifier()); //Database has been deleted while operating, hence we can not retry return false; } if (describeDBInstancesResult.getDBInstances().size() == 1) { DBInstance dbInstance = describeDBInstancesResult.getDBInstances().get(0); InstanceStatus instanceStatus = InstanceStatus.fromDatabaseStatus(dbInstance.getDBInstanceStatus()); if (LOGGER.isTraceEnabled()) { LOGGER.trace("Status of database to be retried is {}", instanceStatus); } return instanceStatus.isRetryable(); } else { throw new IllegalStateException("Multiple databases found for same identifier, this is likely an incompatibility with the Amazon SDK"); } }
@Test public void afterPropertiesSet_noInstanceFound_reportsIllegalStateException() throws Exception { //Arrange this.expectedException.expect(IllegalStateException.class); this.expectedException.expectMessage("No database instance with id:'test'"); AmazonRDS amazonRDS = mock(AmazonRDS.class); when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))).thenThrow(new DBInstanceNotFoundException("foo")); AmazonRdsDataSourceFactoryBean amazonRdsDataSourceFactoryBean = new AmazonRdsDataSourceFactoryBean(amazonRDS, "test", "foo"); //Act amazonRdsDataSourceFactoryBean.afterPropertiesSet(); //Assert }
@Bean public AmazonRDS amazonRDS() { AmazonRDSClient client = Mockito.mock(AmazonRDSClient.class); when(client.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("test"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ).withReadReplicaDBInstanceIdentifiers("read1") ) ); return client; }
@Test public void canRetry_retryPossibleDueToAvailableDatabase_returnsTrue() throws Exception { //Arrange AmazonRDS amazonRDS = mock(AmazonRDS.class); DatabaseInstanceStatusRetryPolicy policy = new DatabaseInstanceStatusRetryPolicy(amazonRDS, "test"); when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))). thenReturn(new DescribeDBInstancesResult().withDBInstances(new DBInstance().withDBInstanceStatus("available"))); RetryContext retryContext = policy.open(new RetryContextSupport(null)); //Act policy.registerThrowable(retryContext, new TransientDataAccessResourceException("not available")); //Assert assertTrue(policy.canRetry(retryContext)); policy.close(retryContext); }
@Test public void canRetry_withResourceIdResolver_returnsTrue() throws Exception { //Arrange AmazonRDS amazonRDS = mock(AmazonRDS.class); ResourceIdResolver resourceIdResolver = mock(ResourceIdResolver.class); DatabaseInstanceStatusRetryPolicy policy = new DatabaseInstanceStatusRetryPolicy(amazonRDS, "foo"); when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))). thenReturn(new DescribeDBInstancesResult().withDBInstances(new DBInstance().withDBInstanceStatus("available"))); when(resourceIdResolver.resolveToPhysicalResourceId("foo")).thenReturn("test"); policy.setResourceIdResolver(resourceIdResolver); RetryContext retryContext = policy.open(new RetryContextSupport(null)); //Act policy.registerThrowable(retryContext, new TransientDataAccessResourceException("not available")); //Assert assertTrue(policy.canRetry(retryContext)); policy.close(retryContext); }
@Test public void canRetry_retryNotPossibleDueToNoDatabase_returnsFalse() throws Exception { //Arrange AmazonRDS amazonRDS = mock(AmazonRDS.class); DatabaseInstanceStatusRetryPolicy policy = new DatabaseInstanceStatusRetryPolicy(amazonRDS, "test"); when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))). thenThrow(new DBInstanceNotFoundException("test")); RetryContext retryContext = policy.open(new RetryContextSupport(null)); //Act policy.registerThrowable(retryContext, new TransientDataAccessResourceException("not available")); //Assert assertFalse(policy.canRetry(retryContext)); policy.close(retryContext); }
@Test public void canRetry_multipleDatabasesFoundForInstanceIdentifier_reportsException() throws Exception { //Arrange this.expectedException.expect(IllegalStateException.class); this.expectedException.expectMessage("Multiple databases found for same identifier"); AmazonRDS amazonRDS = mock(AmazonRDS.class); DatabaseInstanceStatusRetryPolicy policy = new DatabaseInstanceStatusRetryPolicy(amazonRDS, "test"); DescribeDBInstancesResult describeDBInstancesResult = new DescribeDBInstancesResult().withDBInstances(new DBInstance(), new DBInstance()); when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))). thenReturn(describeDBInstancesResult); RetryContext retryContext = policy.open(new RetryContextSupport(null)); //Act policy.registerThrowable(retryContext, new TransientDataAccessResourceException("not available")); //Assert policy.canRetry(retryContext); }
@Bean public AmazonRDSClient amazonRDS() { AmazonRDSClient client = Mockito.mock(AmazonRDSClient.class); when(client.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("test"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ).withReadReplicaDBInstanceIdentifiers("read1") ) ); return client; }
public String getDBEndPoint(String dbInstanceId, int timeout){ DescribeDBInstancesRequest ddbir = new DescribeDBInstancesRequest() .withDBInstanceIdentifier(dbInstanceId); System.out.println("Waiting for DB endpoints"); while(timeout -- > 0){ System.out.print("-"); DescribeDBInstancesResult ddbi = rdsClient.describeDBInstances(ddbir); Endpoint endpoint = ddbi.getDBInstances().get(0).getEndpoint(); if(endpoint != null && endpoint.toString().length()!=0) return endpoint.getAddress()+":"+endpoint.getPort(); try { Thread.sleep(1000); } catch (InterruptedException ex) { Logger.getLogger(BeanstalkConnector.class.getName()).log(Level.SEVERE, null, ex); } } return ""; }
/** * @inheritDoc */ @Override public DatabaseInstance describeInstance( String instanceName, Identity identity ) { AmazonRDS rds = ActivityUtils.createClient( AmazonRDSClient.class, identity ); DescribeDBInstancesResult results = rds.describeDBInstances( new DescribeDBInstancesRequest().withDBInstanceIdentifier( instanceName ) ); if ( results.getDBInstances().isEmpty() ) { return null; } DBInstance result = results.getDBInstances().get( 0 ); return toDatabaseInstance( result ); }
/** * Gets a description of the requested RDS instance. Throws if not found. */ public DBInstance describeInstance(String instanceName) { LOGGER.debug("describeDBInstances(instanceName: " + instanceName + ")"); StopWatch stopWatch = new StopWatch(); try { stopWatch.start(); DescribeDBInstancesRequest request = new DescribeDBInstancesRequest(); request.setDBInstanceIdentifier(instanceName); DescribeDBInstancesResult result = awsRdsClient.describeDBInstances(request); if (result == null || CollectionUtils.isEmpty(result.getDBInstances())) { // Note: this branch should never execute. We expect Amazon to throw DBInstanceNotFoundException instead. throw new DBSnapshotNotFoundException("RDS cannot find instance '" + instanceName + "'"); } else if (result.getDBInstances().size() > 1) { LOGGER.warn("Expected 1 instance named '" + instanceName + "', found " + result.getDBInstances().size()); } return result.getDBInstances().get(0); } finally { stopWatch.stop(); LOGGER.debug("describeDBInstances time elapsed: " + stopWatch); } }
private static DBInstance getDbInstance(final AmazonRDSClient amazonRds, final String rdsInstanceId) { try { final DescribeDBInstancesRequest request = new DescribeDBInstancesRequest() .withDBInstanceIdentifier(rdsInstanceId); final DescribeDBInstancesResult response = amazonRds.describeDBInstances(request); return response.getDBInstances().get(0); } catch (DBInstanceNotFoundException e) { throw new IllegalStateException("Could not find DB instance!"); } }
@Override public Map<String, InstanceState> getInstanceState( RDSInstanceTemplate template, Collection<String> virtualInstanceIds) { Map<String, InstanceState> instanceStateByVirtualInstanceId = Maps.newHashMapWithExpectedSize(virtualInstanceIds.size()); // RDS does not allow batching of DB instance status requests. for (String virtualInstanceId : virtualInstanceIds) { InstanceState instanceState; try { DescribeDBInstancesResult result = client.describeDBInstances( new DescribeDBInstancesRequest() .withDBInstanceIdentifier(virtualInstanceId)); LOG.info("<< Result: {}", result); // Paging not required, should only ever be one instance returned if (result.getDBInstances().size() > 0) { DBInstance dbInstance = result.getDBInstances().get(0); RDSStatus status = RDSStatus.valueOfRDSString(dbInstance.getDBInstanceStatus()); instanceState = RDSInstanceState.fromRdsStatus(status); } else { instanceState = RDSInstanceState.fromRdsStatus(null); } } catch (DBInstanceNotFoundException e) { instanceState = RDSInstanceState.fromRdsStatus(null); } instanceStateByVirtualInstanceId.put(virtualInstanceId, instanceState); } return instanceStateByVirtualInstanceId; }
/** * Iterates through the instances identified by the specified virtual instance IDs * and calls the specified handler on each instance. * * @param virtualInstanceIds the virtual instance IDs * @param instanceHandler the instance handler */ private void forEachInstance(Collection<String> virtualInstanceIds, RDSProvider.InstanceHandler instanceHandler) { // TODO RDS does not currently support the withFilters parameter, so we have to load one at a // time by id //DescribeDBInstancesResult result = client.describeDBInstances(new DescribeDBInstancesRequest() // .withFilters(new Filter().withFilterName("tag:" + Tags.CLOUDERA_DIRECTOR_ID) // .withFilterValue(virtualInstanceIds))); for (String virtualInstanceId : virtualInstanceIds) { DescribeDBInstancesResult result = client.describeDBInstances(new DescribeDBInstancesRequest() .withDBInstanceIdentifier(virtualInstanceId)); forEachInstance(result, instanceHandler); } }
@Scheduled(fixedRate = 300_000) public void run() { for (final String accountId : allAccountIds.get()) { for (final String region : jobsProperties.getWhitelistedRegions()) { try { final AmazonRDSClient amazonRDSClient = clientProvider.getClient(AmazonRDSClient.class, accountId, Region.getRegion(Regions.fromName(region))); Optional<String> marker = Optional.empty(); do { final DescribeDBInstancesRequest request = new DescribeDBInstancesRequest(); marker.ifPresent(request::setMarker); final DescribeDBInstancesResult result = amazonRDSClient.describeDBInstances(request); marker = Optional.ofNullable(trimToNull(result.getMarker())); result.getDBInstances().stream() .filter(DBInstance::getPubliclyAccessible) .filter(dbInstance -> dbInstance.getEndpoint() != null) .forEach(dbInstance -> { final Map<String, Object> metadata = newHashMap(); metadata.put("unsecuredDatabase", dbInstance.getEndpoint().getAddress()); metadata.put("errorMessages", "Unsecured Database! Your DB can be reached from outside"); writeViolation(accountId, region, metadata, dbInstance.getEndpoint().getAddress()); }); } while (marker.isPresent()); } catch (final Exception e) { jobExceptionHandler.onException(e, ImmutableMap.of( "job", this.getClass().getSimpleName(), "aws_account_id", accountId, "aws_region", region)); } } } }
@Override public Endpoint getMasterDbEndpoint(String dbInstanceId) { Endpoint endpoint = null; DescribeDBInstancesResult result = rds.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier(dbInstanceId)); if(result.getDBInstances().size() == 1) { endpoint = result.getDBInstances().get(0).getEndpoint(); } return endpoint; }
/** * Retrieves the {@link com.amazonaws.services.rds.model.DBInstance} information * * @param identifier * - the database identifier used * @return - the db instance * @throws IllegalStateException * if the db instance is not found */ protected DBInstance getDbInstance(String identifier) throws IllegalStateException { DBInstance instance; try { DescribeDBInstancesResult describeDBInstancesResult = this.amazonRds.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier(identifier)); instance = describeDBInstancesResult.getDBInstances().get(0); } catch (DBInstanceNotFoundException e) { throw new IllegalStateException(MessageFormat.format("No database instance with id:''{0}'' found. Please specify a valid db instance", identifier)); } return instance; }
@Test public void afterPropertiesSet_instanceWithoutReadReplica_createsNoDataSourceRouter() throws Exception { //Arrange AmazonRDS amazonRDS = mock(AmazonRDS.class); DataSourceFactory dataSourceFactory = mock(DataSourceFactory.class); when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("test"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ) ) ); AmazonRdsReadReplicaAwareDataSourceFactoryBean amazonRdsDataSourceFactoryBean = new AmazonRdsReadReplicaAwareDataSourceFactoryBean(amazonRDS, "test", "secret"); amazonRdsDataSourceFactoryBean.setDataSourceFactory(dataSourceFactory); when(dataSourceFactory.createDataSource(new DataSourceInformation(DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret"))).thenReturn(mock(DataSource.class)); //Act amazonRdsDataSourceFactoryBean.afterPropertiesSet(); //Assert DataSource datasource = amazonRdsDataSourceFactoryBean.getObject(); assertNotNull(datasource); verify(dataSourceFactory, times(1)).createDataSource(new DataSourceInformation(DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret")); }
@Test public void newInstance_withResourceIdResolver_createsInstanceWithResolvedName() throws Exception { //Arrange AmazonRDS amazonRDS = mock(AmazonRDS.class); DataSourceFactory dataSourceFactory = mock(DataSourceFactory.class); ResourceIdResolver resourceIdResolver = mock(ResourceIdResolver.class); DataSource dataSource = mock(DataSource.class); when(resourceIdResolver.resolveToPhysicalResourceId("test")).thenReturn("bar"); when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("bar"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("bar"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ) ) ); when(dataSourceFactory.createDataSource(new DataSourceInformation(DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret"))).thenReturn(dataSource); AmazonRdsDataSourceFactoryBean amazonRdsDataSourceFactoryBean = new AmazonRdsDataSourceFactoryBean(amazonRDS, "test", "secret"); amazonRdsDataSourceFactoryBean.setDataSourceFactory(dataSourceFactory); amazonRdsDataSourceFactoryBean.setResourceIdResolver(resourceIdResolver); //Act amazonRdsDataSourceFactoryBean.afterPropertiesSet(); //Assert assertNotNull(amazonRdsDataSourceFactoryBean.getObject()); verify(dataSourceFactory, times(1)).createDataSource(new DataSourceInformation(DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret")); }
@Test public void afterPropertiesSet_noUserNameSet_createsInstanceWithUserNameFromMetaData() throws Exception { //Arrange AmazonRDS amazonRDS = mock(AmazonRDS.class); DataSourceFactory dataSourceFactory = mock(DataSourceFactory.class); DataSource dataSource = mock(DataSource.class); when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("test"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ) ) ); when(dataSourceFactory.createDataSource(new DataSourceInformation(DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret"))).thenReturn(dataSource); AmazonRdsDataSourceFactoryBean amazonRdsDataSourceFactoryBean = new AmazonRdsDataSourceFactoryBean(amazonRDS, "test", "secret"); amazonRdsDataSourceFactoryBean.setDataSourceFactory(dataSourceFactory); //Act amazonRdsDataSourceFactoryBean.afterPropertiesSet(); //Assert DataSource datasource = amazonRdsDataSourceFactoryBean.getObject(); assertNotNull(datasource); verify(dataSourceFactory, times(1)).createDataSource(new DataSourceInformation(DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret")); }
@Test public void destroyInstance_shutdownInitiated_destroysDynamicDataSource() throws Exception { AmazonRDS amazonRDS = mock(AmazonRDS.class); DataSourceFactory dataSourceFactory = mock(DataSourceFactory.class); DataSource dataSource = mock(DataSource.class); when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("test"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ) ) ); when(dataSourceFactory.createDataSource(new DataSourceInformation(DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret"))).thenReturn(dataSource); AmazonRdsDataSourceFactoryBean amazonRdsDataSourceFactoryBean = new AmazonRdsDataSourceFactoryBean(amazonRDS, "test", "secret"); amazonRdsDataSourceFactoryBean.setDataSourceFactory(dataSourceFactory); amazonRdsDataSourceFactoryBean.afterPropertiesSet(); amazonRdsDataSourceFactoryBean.getObject(); amazonRdsDataSourceFactoryBean.destroy(); verify(dataSourceFactory, times(1)).closeDataSource(dataSource); }
@Test public void afterPropertiesSet_customUserNameSet_createsInstanceWithCustomUserNameAndIgnoresMetaDataUserName() throws Exception { AmazonRDS amazonRDS = mock(AmazonRDS.class); DataSourceFactory dataSourceFactory = mock(DataSourceFactory.class); when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("test"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ) ) ); AmazonRdsDataSourceFactoryBean amazonRdsDataSourceFactoryBean = new AmazonRdsDataSourceFactoryBean(amazonRDS, "test", "secret"); amazonRdsDataSourceFactoryBean.setUsername("superAdmin"); amazonRdsDataSourceFactoryBean.setDataSourceFactory(dataSourceFactory); amazonRdsDataSourceFactoryBean.afterPropertiesSet(); amazonRdsDataSourceFactoryBean.getObject(); verify(dataSourceFactory, times(1)).createDataSource(new DataSourceInformation(DatabaseType.MYSQL, "localhost", 3306, "test", "superAdmin", "secret")); }
@Bean public AmazonRDS amazonRDS() { AmazonRDSClient client = Mockito.mock(AmazonRDSClient.class); when(client.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("test"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ).withReadReplicaDBInstanceIdentifiers("read1") ) ); when(client.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("read1"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("read1"). withDBInstanceIdentifier("read1"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ) ) ); return client; }
@Test public void parseInternal_minimalConfiguration_createsBeanDefinitionWithoutReadReplicas() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class); beanDefinitionBuilder.setFactoryMethod("mock"); beanDefinitionBuilder.addConstructorArgValue(AmazonRDS.class); beanFactory.registerBeanDefinition(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonRDSClient.class.getName()), beanDefinitionBuilder.getBeanDefinition()); XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); xmlBeanDefinitionReader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-minimal.xml", getClass())); AmazonRDS client = beanFactory.getBean(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonRDSClient.class.getName()), AmazonRDS.class); when(client.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("test"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ).withReadReplicaDBInstanceIdentifiers("read1") ) ); //Act DataSource dataSource = beanFactory.getBean(DataSource.class); //Assert assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource); }
@Test public void parseInternal_noCredentialsDefined_returnsClientWithDefaultCredentialsProvider() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class); beanDefinitionBuilder.setFactoryMethod("mock"); beanDefinitionBuilder.addConstructorArgValue(AmazonRDS.class); beanFactory.registerBeanDefinition(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonRDSClient.class.getName()), beanDefinitionBuilder.getBeanDefinition()); XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); xmlBeanDefinitionReader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-noCredentials.xml", getClass())); AmazonRDS client = beanFactory.getBean(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonRDSClient.class.getName()), AmazonRDS.class); when(client.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("test"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ).withReadReplicaDBInstanceIdentifiers("read1") ) ); //Act DataSource dataSource = beanFactory.getBean(DataSource.class); //Assert assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource); }
@Bean public AmazonRDS amazonRDS() { AmazonRDSClient client = Mockito.mock(AmazonRDSClient.class); when(client.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("test"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ) ) ); when(client.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("anotherOne"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("anotherOne"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ) ) ); return client; }
public String getDBStatus(String dbInstanceId){ DescribeDBInstancesRequest ddbir = new DescribeDBInstancesRequest() .withDBInstanceIdentifier(dbInstanceId); DescribeDBInstancesResult ddbi = rdsClient.describeDBInstances(ddbir); try{ return ddbi.getDBInstances().get(0).getStatusInfos().toString(); }catch(Exception e){ throw new RuntimeException("DBInstance not found"); } }
private ModelAndView showBackupDetails( JobInput input ) throws IOException { AWSCredentials creds = new BasicAWSCredentials( input.getAwsAccessKeyId(), input.getAwsSecretKey() ); ModelAndView mav = new ModelAndView().addObject( "input", input ).addObject( "inputData", input.serializeTo() ); if ( input.getSourceAndDestination() == null ) { SourceAndDestination defaultDetails = new SourceAndDestination(); input.setSourceAndDestination( defaultDetails ); } switch ( input.getActionType() ) { case BACKUP_INSTANCE: mav.addObject( "allInstances", rds.describeDBInstances( decorate( new DescribeDBInstancesRequest(), creds ) ).getDBInstances() ); case CONVERT_SNAPSHOT: if ( input.getActionType() == ActionType.CONVERT_SNAPSHOT ) { mav.addObject( "allSnapshots", rds.describeDBSnapshots( decorate( new DescribeDBSnapshotsRequest(), creds ) ).getDBSnapshots() ); } mav.setViewName( "create/backup_details.vm" ); mav.addObject( "allBuckets", s3.listBuckets( decorate( new ListBucketsRequest(), creds ) ) ); mav.addObject( "sourceAndDestination", input.getSourceAndDestination() ); return mav; default: throw new AssertionError( "Unexpected action type " + input.getActionType() ); } }
/** * Sets up the mock rds client to return a fakeResult for the describe-db-instances call. */ private void setupMock(DescribeDBInstancesResult fakeResult) { when(mockRdsClient.describeDBInstances(any(DescribeDBInstancesRequest.class))).thenReturn(fakeResult); }
@Test public void afterPropertiesSet_instanceWithReadReplica_createsDataSourceRouter() throws Exception { //Arrange AmazonRDS amazonRDS = mock(AmazonRDS.class); DataSourceFactory dataSourceFactory = mock(DataSourceFactory.class); when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("test"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ).withReadReplicaDBInstanceIdentifiers("read1", "read2") ) ); when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("read1"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("read1"). withDBInstanceIdentifier("read1"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ) ) ); when(amazonRDS.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("read2"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("read2"). withDBInstanceIdentifier("read2"). withEngine("mysql"). withMasterUsername("admin"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ) ) ); DataSource createdDataSource = mock(DataSource.class); Connection connection = mock(Connection.class); when(dataSourceFactory.createDataSource(new DataSourceInformation(DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret"))).thenReturn(createdDataSource); when(dataSourceFactory.createDataSource(new DataSourceInformation(DatabaseType.MYSQL, "localhost", 3306, "read1", "admin", "secret"))).thenReturn(createdDataSource); when(dataSourceFactory.createDataSource(new DataSourceInformation(DatabaseType.MYSQL, "localhost", 3306, "read2", "admin", "secret"))).thenReturn(createdDataSource); when(createdDataSource.getConnection()).thenReturn(connection); AmazonRdsReadReplicaAwareDataSourceFactoryBean amazonRdsDataSourceFactoryBean = new AmazonRdsReadReplicaAwareDataSourceFactoryBean(amazonRDS, "test", "secret"); amazonRdsDataSourceFactoryBean.setDataSourceFactory(dataSourceFactory); //Act amazonRdsDataSourceFactoryBean.afterPropertiesSet(); //Assert DataSource datasource = amazonRdsDataSourceFactoryBean.getObject(); assertNotNull(datasource); assertTrue(datasource instanceof LazyConnectionDataSourceProxy); ReadOnlyRoutingDataSource source = (ReadOnlyRoutingDataSource) ((LazyConnectionDataSourceProxy) datasource).getTargetDataSource(); assertEquals(3, source.getDataSources().size()); }
@Test public void parseInternal_fullConfiguration_createsBeanDefinitionWithoutReadReplicas() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class); beanDefinitionBuilder.setFactoryMethod("mock"); beanDefinitionBuilder.addConstructorArgValue(AmazonRDS.class); beanFactory.registerBeanDefinition(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonRDSClient.class.getName()), beanDefinitionBuilder.getBeanDefinition()); XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); xmlBeanDefinitionReader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-fullConfiguration.xml", getClass())); AmazonRDS client = beanFactory.getBean(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonRDSClient.class.getName()), AmazonRDS.class); when(client.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))).thenReturn( new DescribeDBInstancesResult(). withDBInstances(new DBInstance(). withDBInstanceStatus("available"). withDBName("test"). withDBInstanceIdentifier("test"). withEngine("mysql"). withEndpoint(new Endpoint(). withAddress("localhost"). withPort(3306) ) ) ); BeanDefinition definition = beanFactory.getBeanDefinition("test"); assertEquals("test", definition.getConstructorArgumentValues().getArgumentValue(1, String.class).getValue()); assertEquals("password", definition.getConstructorArgumentValues().getArgumentValue(2, String.class).getValue()); assertEquals("myUser", definition.getPropertyValues().getPropertyValue("username").getValue()); assertEquals("fooDb", definition.getPropertyValues().getPropertyValue("databaseName").getValue()); DataSource dataSource = beanFactory.getBean(DataSource.class); //Assert assertNotNull(dataSource); assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource); }