com.amazonaws.services.rds.model.DBInstance Java Examples
The following examples show how to use
com.amazonaws.services.rds.model.DBInstance.
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: RdsTableProviderTest.java From aws-athena-query-federation with Apache License 2.0 | 6 votes |
@Override protected void setUpRead() { final AtomicLong requestCount = new AtomicLong(0); when(mockRds.describeDBInstances(any(DescribeDBInstancesRequest.class))) .thenAnswer((InvocationOnMock invocation) -> { DescribeDBInstancesResult mockResult = mock(DescribeDBInstancesResult.class); List<DBInstance> values = new ArrayList<>(); values.add(makeValue(getIdValue())); values.add(makeValue(getIdValue())); values.add(makeValue("fake-id")); when(mockResult.getDBInstances()).thenReturn(values); if (requestCount.incrementAndGet() < 3) { when(mockResult.getMarker()).thenReturn(String.valueOf(requestCount.get())); } return mockResult; }); }
Example #2
Source File: RdsTableProvider.java From aws-athena-query-federation with Apache License 2.0 | 6 votes |
/** * Calls DescribeDBInstances on the AWS RDS Client returning all DB Instances that match the supplied predicate and attempting * to push down certain predicates (namely queries for specific DB Instance) to EC2. * * @See TableProvider */ @Override public void readWithConstraint(BlockSpiller spiller, ReadRecordsRequest recordsRequest, QueryStatusChecker queryStatusChecker) { boolean done = false; DescribeDBInstancesRequest request = new DescribeDBInstancesRequest(); ValueSet idConstraint = recordsRequest.getConstraints().getSummary().get("instance_id"); if (idConstraint != null && idConstraint.isSingleValue()) { request.setDBInstanceIdentifier(idConstraint.getSingleValue().toString()); } while (!done) { DescribeDBInstancesResult response = rds.describeDBInstances(request); for (DBInstance instance : response.getDBInstances()) { instanceToRow(instance, spiller); } request.setMarker(response.getMarker()); if (response.getMarker() == null || !queryStatusChecker.isQueryRunning()) { done = true; } } }
Example #3
Source File: AmazonRdsDataSourceFactoryBeanTest.java From spring-cloud-aws with Apache License 2.0 | 6 votes |
@Test 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")); }
Example #4
Source File: AmazonRdsInstanceConfigurationTest.java From spring-cloud-aws with Apache License 2.0 | 6 votes |
@Bean 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; }
Example #5
Source File: AmazonRdsDataSourceFactoryBean.java From spring-cloud-aws with Apache License 2.0 | 6 votes |
/** * 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; }
Example #6
Source File: AmazonRdsInstanceConfigurationTest.java From spring-cloud-aws with Apache License 2.0 | 6 votes |
@Bean 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; }
Example #7
Source File: AmazonRdsInstanceConfigurationTest.java From spring-cloud-aws with Apache License 2.0 | 6 votes |
@Bean 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; }
Example #8
Source File: AmazonRdsInstanceConfigurationTest.java From spring-cloud-aws with Apache License 2.0 | 6 votes |
@Bean 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; }
Example #9
Source File: AmazonRdsInstanceConfigurationTest.java From spring-cloud-aws with Apache License 2.0 | 6 votes |
@Bean 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; }
Example #10
Source File: CreateDbInstance.java From obevo with Apache License 2.0 | 6 votes |
private 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); } }
Example #11
Source File: CreateDbInstance.java From obevo with Apache License 2.0 | 6 votes |
private void createSqlServer(String dbInstanceIdentifier) throws Exception { // http://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_CreateDBInstance.html CreateDBInstanceRequest request = new CreateDBInstanceRequest() .withEngine("sqlserver-ex") .withEngineVersion("13.00.2164.0.v1") .withLicenseModel("license-included") .withAllocatedStorage(20) .withStorageType("gp2") // SSD .withBackupRetentionPeriod(0) .withDBInstanceClass("db.t2.micro") .withDBInstanceIdentifier(dbInstanceIdentifier) //.withDBName("DBDEPLOY") .withMasterUsername("deploybuilddbo") .withMasterUserPassword("deploybuilddb0"); DBInstance response = client.createDBInstance(request); System.out.println(response); describe(dbInstanceIdentifier); }
Example #12
Source File: CreateDbInstance.java From obevo with Apache License 2.0 | 6 votes |
public void createPostgresql(String dbInstanceIdentifier) throws Exception { // http://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_CreateDBInstance.html CreateDBInstanceRequest request = new CreateDBInstanceRequest() .withEngine("postgres") .withEngineVersion("9.6.2") .withLicenseModel("postgresql-license") .withAllocatedStorage(5) .withStorageType("gp2") // SSD .withBackupRetentionPeriod(0) .withDBInstanceClass("db.t2.micro") .withDBInstanceIdentifier(dbInstanceIdentifier) .withDBName("DBDEPLOY") .withMasterUsername("deploybuilddbo") .withMasterUserPassword("deploybuilddb0"); DBInstance response = client.createDBInstance(request); System.out.println(response); describe(dbInstanceIdentifier); }
Example #13
Source File: CreateDbInstance.java From obevo with Apache License 2.0 | 6 votes |
public void createOracle(String dbInstanceIdentifier) throws Exception { // http://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_CreateDBInstance.html CreateDBInstanceRequest request = new CreateDBInstanceRequest() .withEngine("oracle-se2") //.withEngineVersion("12.1.0.2.v8") .withLicenseModel("license-included") .withAllocatedStorage(10) .withStorageType("gp2") // SSD .withBackupRetentionPeriod(0) .withDBInstanceClass("db.t2.micro") .withDBInstanceIdentifier(dbInstanceIdentifier) .withDBName("DBDEPLOY") .withMasterUsername("deploybuilddbo") .withMasterUserPassword("deploybuilddb0") //.withVpcSecurityGroupIds("default") ; DBInstance response = client.createDBInstance(request); System.out.println(response); describe(dbInstanceIdentifier); }
Example #14
Source File: DatabaseInstanceStatusRetryPolicyTest.java From spring-cloud-aws with Apache License 2.0 | 6 votes |
@Test 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 assertThat(policy.canRetry(retryContext)).isTrue(); policy.close(retryContext); }
Example #15
Source File: AmazonRdsDatabaseAutoConfigurationTest.java From spring-cloud-aws with Apache License 2.0 | 6 votes |
@Bean 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; }
Example #16
Source File: AmazonRdsInstanceConfigurationTest.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
@Bean 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; }
Example #17
Source File: DatabaseInstanceStatusRetryPolicyTest.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
@Test 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 assertThat(policy.canRetry(retryContext)).isTrue(); policy.close(retryContext); }
Example #18
Source File: DatabaseInstanceStatusRetryPolicyTest.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
@Test void canRetry_multipleDatabasesFoundForInstanceIdentifier_reportsException() throws Exception { // Arrange 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 assertThatThrownBy(() -> policy.canRetry(retryContext)) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("Multiple databases found for same identifier"); }
Example #19
Source File: AmazonRdsDatabaseAutoConfigurationTest.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
@Bean 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; }
Example #20
Source File: AmazonRdsDatabaseAutoConfigurationTest.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
@Bean 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; }
Example #21
Source File: AWSDatabase.java From billow with Apache License 2.0 | 5 votes |
private String rdsARN(String partition, String regionName, String accountNumber, DBInstance instance) { return String.format( "arn:%s:rds:%s:%s:db:%s", partition, regionName, accountNumber, instance.getDBInstanceIdentifier() ); }
Example #22
Source File: Handler.java From billow with Apache License 2.0 | 5 votes |
public Handler(MetricRegistry registry, AWSDatabaseHolder dbHolder, long maxDBAgeInMs) { this.mapper = new ObjectMapper(); this.mapper.addMixInAnnotations(DBInstance.class, DBInstanceMixin.class); this.mapper.addMixInAnnotations(PendingModifiedValues.class, PendingModifiedValuesMixin.class); this.mapper.addMixInAnnotations(DBInstanceStatusInfo.class, DBInstanceStatusInfoMixin.class); this.mapper.registerModule(new JodaModule()); this.mapper.registerModule(new GuavaModule()); this.registry = registry; this.dbHolder = dbHolder; this.maxDBAgeInMs = maxDBAgeInMs; }
Example #23
Source File: RDSInstance.java From billow with Apache License 2.0 | 5 votes |
public static boolean checkIfMaster(DBInstance instance, DBCluster cluster) { if (instance.getDBClusterIdentifier() == null || cluster == null) { // It's NOT a member of a DB cluster return instance.getReadReplicaSourceDBInstanceIdentifier() == null; } else { // It's a member of a DB cluster for (DBClusterMember member : cluster.getDBClusterMembers()) { if (member.getDBInstanceIdentifier().equals(instance.getDBInstanceIdentifier()) && member.isClusterWriter()) { return true; } } return false; } }
Example #24
Source File: DatabaseInstanceStatusRetryPolicy.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
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"); } }
Example #25
Source File: AmazonRdsDataSourceFactoryBeanTest.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
@Test 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); }
Example #26
Source File: AmazonRdsDataSourceFactoryBeanTest.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
@Test 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(); assertThat(datasource).isNotNull(); verify(dataSourceFactory, times(1)).createDataSource(new DataSourceInformation( DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret")); }
Example #27
Source File: AmazonRdsDataSourceFactoryBeanTest.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
@Test 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 assertThat(amazonRdsDataSourceFactoryBean.getObject()).isNotNull(); verify(dataSourceFactory, times(1)).createDataSource(new DataSourceInformation( DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret")); }
Example #28
Source File: AmazonRdsReadReplicaAwareDataSourceFactoryBeanTest.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
@Test 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 factoryBean = new AmazonRdsReadReplicaAwareDataSourceFactoryBean( amazonRDS, "test", "secret"); factoryBean.setDataSourceFactory(dataSourceFactory); when(dataSourceFactory.createDataSource(new DataSourceInformation( DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret"))) .thenReturn(mock(DataSource.class)); // Act factoryBean.afterPropertiesSet(); // Assert DataSource datasource = factoryBean.getObject(); assertThat(datasource).isNotNull(); verify(dataSourceFactory, times(1)).createDataSource(new DataSourceInformation( DatabaseType.MYSQL, "localhost", 3306, "test", "admin", "secret")); }
Example #29
Source File: AmazonRdsReadReplicaAwareDataSourceFactoryBean.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
/** * Constructs a * {@link org.springframework.cloud.aws.jdbc.datasource.ReadOnlyRoutingDataSource} * data source that contains the regular data source as a default, and all * read-replicas as additional data source. The * {@link org.springframework.cloud.aws.jdbc.datasource.ReadOnlyRoutingDataSource} is * additionally wrapped with a * {@link org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy}, because * the read-only flag is only available after the transactional context has been * established. This is only the case if the physical connection is requested after * the transaction start and not while starting a transaction. * @return a ReadOnlyRoutingDataSource that is wrapped with a * LazyConnectionDataSourceProxy * @throws Exception if the underlying data source setup throws any exception */ @Override protected DataSource createInstance() throws Exception { DBInstance dbInstance = getDbInstance(getDbInstanceIdentifier()); // If there is no read replica available, delegate to super class if (dbInstance.getReadReplicaDBInstanceIdentifiers().isEmpty()) { return super.createInstance(); } HashMap<Object, Object> replicaMap = new HashMap<>( dbInstance.getReadReplicaDBInstanceIdentifiers().size()); for (String replicaName : dbInstance.getReadReplicaDBInstanceIdentifiers()) { replicaMap.put(replicaName, createDataSourceInstance(replicaName)); } // Create the data source ReadOnlyRoutingDataSource dataSource = new ReadOnlyRoutingDataSource(); dataSource.setTargetDataSources(replicaMap); dataSource.setDefaultTargetDataSource( createDataSourceInstance(getDbInstanceIdentifier())); // Initialize the class dataSource.afterPropertiesSet(); return new LazyConnectionDataSourceProxy(dataSource); }
Example #30
Source File: AmazonRdsDataSourceFactoryBean.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
private DataSourceInformation fromRdsInstance(DBInstance dbInstance) { Assert.notNull(dbInstance, "DbInstance must not be null"); Assert.notNull(dbInstance.getEndpoint(), "The database instance has no endpoint available!"); return new DataSourceInformation(DatabaseType.fromEngine(dbInstance.getEngine()), dbInstance.getEndpoint().getAddress(), dbInstance.getEndpoint().getPort(), StringUtils.hasText(this.databaseName) ? this.databaseName : dbInstance.getDBName(), StringUtils.hasText(this.username) ? this.username : dbInstance.getMasterUsername(), this.password); }