/** * Returns the private IP address of the specified RDS instance. * * @param dbInstance the RDS instance * @return the private IP address of the specified RDS instance */ private static InetAddress getPrivateIpAddress(DBInstance dbInstance) { Preconditions.checkNotNull(dbInstance, "dbInstance is null"); InetAddress privateIpAddress = null; try { Endpoint endpoint = dbInstance.getEndpoint(); if (endpoint != null) { String endpointAddress = endpoint.getAddress(); if (endpointAddress != null) { privateIpAddress = InetAddress.getByName(endpointAddress); } } } catch (UnknownHostException e) { throw new IllegalArgumentException("Invalid private IP address", e); } return privateIpAddress; }
@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; }
@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; }
@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 ""; }
private DBInstance makeInstance(String instanceName, RdsInstanceStatus instanceStatus) { DBInstance dbInstance = new DBInstance(); dbInstance.setDBInstanceIdentifier(instanceName); dbInstance.setDBInstanceStatus(instanceStatus == null ? UNKNOWN_STATUS : instanceStatus.toString()); Endpoint endpoint = new Endpoint(); endpoint.setAddress(STAGE_ENDPOINT_ADDRESS); dbInstance.setEndpoint(endpoint); DBSubnetGroup dbSubnetGroup = new DBSubnetGroup(); dbSubnetGroup.setDBSubnetGroupName(SUBNET_GROUP); dbInstance.setDBSubnetGroup(dbSubnetGroup); return dbInstance; }
@Before public void setUp() throws Exception { this.clientProviderMock = mock(ClientProvider.class); this.jobsPropertiesMock = mock(JobsProperties.class); this.violationSinkMock = mock(ViolationSink.class); this.amazonRDSClientMock = mock(AmazonRDSClient.class); this.accountIdSupplierMock = mock(AccountIdSupplier.class); this.exceptionHandlerMock = mock(JobExceptionHandler.class); when(accountIdSupplierMock.get()).thenReturn(newHashSet("54321")); // Jobsproperties when(jobsPropertiesMock.getWhitelistedRegions()).thenReturn(newArrayList("eu-west-1")); // Dbinstances final Endpoint endpoint = new Endpoint(); endpoint.setAddress("aws.db.cn"); final Endpoint endpoint2 = new Endpoint(); endpoint2.setAddress("aws.db2.cn"); final DBInstance dbInstance1 = new DBInstance(); dbInstance1.setPubliclyAccessible(true); dbInstance1.setEndpoint(endpoint); final DBInstance dbInstance2 = new DBInstance(); dbInstance2.setPubliclyAccessible(false); dbInstance2.setEndpoint(endpoint); final DBInstance dbInstance3 = new DBInstance(); dbInstance3.setPubliclyAccessible(true); dbInstance3.setEndpoint(endpoint2); describeDBInstancesResultMock = new DescribeDBInstancesResult(); describeDBInstancesResultMock.setDBInstances(newArrayList(dbInstance1, dbInstance2, dbInstance3)); // clientprovider when(clientProviderMock.getClient(any(), any(String.class), any(Region.class))).thenReturn(amazonRDSClientMock); }
@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; }
/** * Runs the given database configuration (~setup) on an AWS database instance. Make sure the setup * fits the database (e.g. a MSSQL setup for a MSSQL database). */ private void runSetup(DBInstance dbInstance, Database database) { String setup = database.getSetup(); String buildFilePath = new LocaleRepository().getLocation() + SLASH + setup + "/build.xml"; Endpoint endpoint = dbInstance.getEndpoint(); File buildFile = new File(buildFilePath); this.console.write("Run database setup " + buildFilePath); this.console.write("Endpoint " + endpoint); Project project = new Project(); project.setUserProperty(Variable.ANT_FILE, buildFile.getAbsolutePath()); project.setUserProperty(Variable.DEST_ROOT_LOCAL, new LocaleRepository().getLocation() + SLASH + "tmp"); project.setUserProperty(Variable.DB_SERVER, endpoint.getAddress()); project.setUserProperty(Variable.DB_PORT, endpoint.getPort().toString()); project.setUserProperty(Variable.DB_USER, database.getUsername()); project.setUserProperty(Variable.DB_PASSWORD, database.getPassword()); project.setUserProperty(Variable.ENV_NLS_LANG, "American_America.UTF8"); project.setUserProperty(Variable.HEADLESS, "true"); project.init(); DefaultLogger consoleLogger = createConsoleLogger(); project.addBuildListener(consoleLogger); ProjectHelper helper = ProjectHelper.getProjectHelper(); project.addReference("ant.projectHelper", helper); helper.parse(project, buildFile); project.executeTarget(project.getDefaultTarget()); this.console.newLine(); }
@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; }
/** * Return the Endpoint for the dbInstanceId */ @Override public Endpoint getMasterDbEndpoint(String dbInstanceId) { return super.getMasterDbEndpoint(dbInstanceId); }
private void initializeDataSource() { // Use the RDS DB and the dbEndpointRetriever to discover the URL of the // database. If there // are read replicas, set the correct driver and use them. final String masterId = config .getProperty(ConfigurationSettings.ConfigProps.RDS_INSTANCEID); try { Endpoint master = dbEndpointRetriever.getMasterDbEndpoint(masterId); List<Endpoint> replicas = dbEndpointRetriever .getReadReplicaEndpoints(masterId); if (master != null) { LOG.info("Detected RDS Master database"); StringBuilder builder = new StringBuilder(); builder.append("jdbc:mysql:"); if (replicas != null) { builder.append("replication:"); super.setDriverClassName("com.mysql.jdbc.ReplicationDriver"); } else { super.setDriverClassName("com.mysql.jdbc.Driver"); } builder.append("//" + master.getAddress() + ":" + master.getPort()); if (replicas != null) { LOG.info("Detected RDS Read Replicas"); for (Endpoint endpoint : replicas) { builder.append("," + endpoint.getAddress() + ":" + endpoint.getPort()); } } else { LOG.info("No Read Replicas detected"); } builder.append("/" + config.getProperty(ConfigurationSettings.ConfigProps.RDS_DATABASE)); String connectionString = builder.toString(); LOG.info("MySQL Connection String: " + connectionString); super.setUrl(connectionString); } else { LOG.warn("No RDS master database detected!"); } } catch (Exception e) { LOG.warn("Failed to initialize datasource.", e); } }
@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); }
@Test public void parseInternal_customRdsInstance_createsRdsBeanAndUserTagsWithCustomRdsInstance() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); xmlBeanDefinitionReader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-customRdsInstance.xml", getClass())); AmazonRDS clientMock = beanFactory.getBean("amazonRds", AmazonRDS.class); when(clientMock.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") ) ); AmazonIdentityManagement amazonIdentityManagement = beanFactory.getBean("myIdentityService", AmazonIdentityManagement.class); when(amazonIdentityManagement.getUser()).thenReturn(new GetUserResult().withUser(new User("/", "aemruli", "123456789012", "arn:aws:iam::1234567890:user/aemruli", new Date()))); when(clientMock.listTagsForResource(new ListTagsForResourceRequest().withResourceName("arn:aws:rds:us-west-2:1234567890:db:test"))).thenReturn(new ListTagsForResourceResult().withTagList( new Tag().withKey("key1").withValue("value2") )); //Act Map<?, ?> dsTags = beanFactory.getBean("dsTags", Map.class); DataSource dataSource = beanFactory.getBean(DataSource.class); //Assert assertEquals("value2", dsTags.get("key1")); assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource); }
/** * Returns the port used for administrative database connections on the specified RDS instance. * * @param dbInstance the RDS instance * @return the port used for administrative database connections on the specified RDS instance */ private static Integer getPort(DBInstance dbInstance) { Preconditions.checkNotNull(dbInstance, "dbInstance is null"); Endpoint endpoint = dbInstance.getEndpoint(); return (endpoint == null) ? null : endpoint.getPort(); }
/** * First locate the master DB for the provided dbInstanceId, then * locate and return any Read Replicas it may have. Be sure the * replicas are in the 'available' state before returning */ @Override public List<Endpoint> getReadReplicaEndpoints(String dbInstanceId) { return super.getReadReplicaEndpoints(dbInstanceId); }
/** * Retrieve the DNS endpoint for the specified DB * @param dbInstanceId * @return */ public Endpoint getMasterDbEndpoint(String dbInstanceId);
/** * Retrieve a list of DNS endpoints for any Read Replicas associated with * the specified DB * @param dbInstanceId * @return */ public List<Endpoint> getReadReplicaEndpoints(String dbInstanceId);