com.amazonaws.services.rds.AmazonRDS Java Examples
The following examples show how to use
com.amazonaws.services.rds.AmazonRDS.
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: InventoryUtilTest.java From pacbot with Apache License 2.0 | 6 votes |
/** * Fetch RDSDB snapshots test. * * @throws Exception the exception */ @SuppressWarnings("static-access") @Test public void fetchRDSDBSnapshotsTest() throws Exception { mockStatic(AmazonRDSClientBuilder.class); AmazonRDS rdsClient = PowerMockito.mock(AmazonRDS.class); AmazonRDSClientBuilder amazonRDSClientBuilder = PowerMockito.mock(AmazonRDSClientBuilder.class); AWSStaticCredentialsProvider awsStaticCredentialsProvider = PowerMockito.mock(AWSStaticCredentialsProvider.class); PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenReturn(awsStaticCredentialsProvider); when(amazonRDSClientBuilder.standard()).thenReturn(amazonRDSClientBuilder); when(amazonRDSClientBuilder.withCredentials(anyObject())).thenReturn(amazonRDSClientBuilder); when(amazonRDSClientBuilder.withRegion(anyString())).thenReturn(amazonRDSClientBuilder); when(amazonRDSClientBuilder.build()).thenReturn(rdsClient); DescribeDBSnapshotsResult describeDBSnapshotsResult = new DescribeDBSnapshotsResult(); List<DBSnapshot> snapshots = new ArrayList<>(); snapshots.add(new DBSnapshot()); describeDBSnapshotsResult.setDBSnapshots(snapshots); when(rdsClient.describeDBSnapshots(anyObject())).thenReturn(describeDBSnapshotsResult); assertThat(inventoryUtil.fetchRDSDBSnapshots(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), "skipRegions", "account","accountName").size(), is(1)); }
Example #2
Source File: AmazonRdsDataSourceFactoryBeanTest.java From spring-cloud-aws with Apache License 2.0 | 6 votes |
@Test void afterPropertiesSet_noInstanceFound_reportsIllegalStateException() throws Exception { // Arrange AmazonRDS amazonRDS = mock(AmazonRDS.class); when(amazonRDS.describeDBInstances( new DescribeDBInstancesRequest().withDBInstanceIdentifier("test"))) .thenThrow(new DBInstanceNotFoundException("foo")); AmazonRdsDataSourceFactoryBean amazonRdsDataSourceFactoryBean = new AmazonRdsDataSourceFactoryBean( amazonRDS, "test", "foo"); // Assert assertThatThrownBy(amazonRdsDataSourceFactoryBean::afterPropertiesSet) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("No database instance with id:'test'"); }
Example #3
Source File: DatabaseInstanceStatusRetryPolicyTest.java From spring-cloud-aws with Apache License 2.0 | 6 votes |
@Test void canRetry_noExceptionRegistered_returnsTrue() throws Exception { // Arrange AmazonRDS amazonRDS = mock(AmazonRDS.class); DatabaseInstanceStatusRetryPolicy policy = new DatabaseInstanceStatusRetryPolicy( amazonRDS, "test"); RetryContext retryContext = new RetryContextSupport(null); // Act policy.open(retryContext); // Assert assertThat(policy.canRetry(retryContext)).isTrue(); }
Example #4
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 #5
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 #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: AwsRdsStartService.java From cloudbreak with Apache License 2.0 | 6 votes |
public void start(AuthenticatedContext ac, DatabaseStack dbStack) throws ExecutionException, TimeoutException, InterruptedException { AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential()); String regionName = ac.getCloudContext().getLocation().getRegion().value(); AmazonRDS rdsClient = awsClient.createRdsClient(credentialView, regionName); String dbInstanceIdentifier = dbStack.getDatabaseServer().getServerId(); StartDBInstanceRequest startDBInstanceRequest = new StartDBInstanceRequest(); startDBInstanceRequest.setDBInstanceIdentifier(dbInstanceIdentifier); try { rdsClient.startDBInstance(startDBInstanceRequest); } catch (RuntimeException ex) { throw new CloudConnectorException(ex.getMessage(), ex); } Waiter<DescribeDBInstancesRequest> rdsWaiter = rdsClient.waiters().dBInstanceAvailable(); DescribeDBInstancesRequest describeDBInstancesRequest = new DescribeDBInstancesRequest().withDBInstanceIdentifier(dbInstanceIdentifier); StackCancellationCheck stackCancellationCheck = new StackCancellationCheck(ac.getCloudContext().getId()); run(rdsWaiter, describeDBInstancesRequest, stackCancellationCheck); }
Example #8
Source File: AwsRdsStopService.java From cloudbreak with Apache License 2.0 | 6 votes |
public void stop(AuthenticatedContext ac, DatabaseStack dbStack) throws ExecutionException, TimeoutException, InterruptedException { AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential()); String regionName = ac.getCloudContext().getLocation().getRegion().value(); AmazonRDS rdsClient = awsClient.createRdsClient(credentialView, regionName); String dbInstanceIdentifier = dbStack.getDatabaseServer().getServerId(); StopDBInstanceRequest stopDBInstanceRequest = new StopDBInstanceRequest(); stopDBInstanceRequest.setDBInstanceIdentifier(dbInstanceIdentifier); LOGGER.debug("RDS stop request"); try { rdsClient.stopDBInstance(stopDBInstanceRequest); } catch (RuntimeException ex) { throw new CloudConnectorException(ex.getMessage(), ex); } Waiter<DescribeDBInstancesRequest> rdsWaiter = customAmazonWaiterProvider .getDbInstanceStopWaiter(rdsClient); DescribeDBInstancesRequest describeDBInstancesRequest = new DescribeDBInstancesRequest().withDBInstanceIdentifier(dbInstanceIdentifier); StackCancellationCheck stackCancellationCheck = new StackCancellationCheck(ac.getCloudContext().getId()); run(rdsWaiter, describeDBInstancesRequest, stackCancellationCheck); LOGGER.debug("RDS stop process finished. DB Instance ID: {}", dbInstanceIdentifier); }
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: 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 #11
Source File: InventoryUtil.java From pacbot with Apache License 2.0 | 6 votes |
/** * Fetch CloudTrails info. * * @param temporaryCredentials the temporary credentials * @param account the account * @return the map */ public static Map<String,List<Trail>> fetchCloudTrails(BasicSessionCredentials temporaryCredentials, String skipRegions,String account, String accountName){ log.info("Fetch CloudTrails info start"); Map<String,List<Trail>> cloudTrails = new LinkedHashMap<>(); String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+account + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"Cloud Trail\" , \"region\":\"" ; for(Region region : RegionUtils.getRegions()){ try{ if(!skipRegions.contains(region.getName())){ AWSCloudTrail cloudTrailClient = AWSCloudTrailClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build(); DescribeTrailsResult rslt = cloudTrailClient.describeTrails(); List<Trail> trailTemp = rslt.getTrailList(); if(! trailTemp.isEmpty() ){ cloudTrails.put(account+delimiter+accountName+delimiter+region.getName(), trailTemp); } } }catch(Exception e){ if(region.isServiceSupported(AmazonRDS.ENDPOINT_PREFIX)){ log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}"); ErrorManageUtil.uploadError(account,region.getName(),"cloudtrail",e.getMessage()); } } } return cloudTrails; }
Example #12
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 #13
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 #14
Source File: DatabaseInstanceStatusRetryPolicyTest.java From spring-cloud-aws with Apache License 2.0 | 6 votes |
@Test 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 assertThat(policy.canRetry(retryContext)).isFalse(); policy.close(retryContext); }
Example #15
Source File: ResourceTaggingManager.java From pacbot with Apache License 2.0 | 6 votes |
/** * * @param resourceId * @param clientMap * @param pacTag * @return */ private Boolean setRDSDBTag(final String resourceId, final Map<String, Object> clientMap, Map<String, String> pacTag ) { AmazonRDS rdsClient = (AmazonRDS) clientMap.get("client"); com.amazonaws.services.rds.model.Tag tag = new com.amazonaws.services.rds.model.Tag(); for(Map.Entry<String, String> tags : pacTag.entrySet()){ tag.setKey(tags.getKey()); tag.setValue(tags.getValue()); } AddTagsToResourceRequest request = new AddTagsToResourceRequest().withResourceName(resourceId); request.setTags(Arrays.asList(tag)); try { AddTagsToResourceResult response = rdsClient.addTagsToResource(request); return Boolean.TRUE; } catch (AmazonServiceException ase) { logger.error("error tagging rds - > " + resourceId, ase); throw ase; } }
Example #16
Source File: AmazonRdsDataSourceUserTagsFactoryBeanTest.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
@Test void getObject_instanceWithTagsAndNoResourceIdResolverAndDefaultRegion_mapWithTagsReturned() throws Exception { // Arrange AmazonRDS amazonRds = mock(AmazonRDS.class); AmazonIdentityManagement amazonIdentityManagement = mock( AmazonIdentityManagement.class); AmazonRdsDataSourceUserTagsFactoryBean factoryBean = new AmazonRdsDataSourceUserTagsFactoryBean( amazonRds, "test", amazonIdentityManagement); when(amazonIdentityManagement.getUser()).thenReturn( new GetUserResult().withUser(new User("/", "aemruli", "123456789012", "arn:aws:iam::1234567890:user/aemruli", new Date()))); when(amazonRds.listTagsForResource(new ListTagsForResourceRequest() .withResourceName("arn:aws:rds:us-west-2:1234567890:db:test"))) .thenReturn(new ListTagsForResourceResult().withTagList( new Tag().withKey("key1").withValue("value1"), new Tag().withKey("key2").withValue("value2"))); // Act factoryBean.afterPropertiesSet(); Map<String, String> userTagMap = factoryBean.getObject(); // Assert assertThat(userTagMap.get("key1")).isEqualTo("value1"); assertThat(userTagMap.get("key2")).isEqualTo("value2"); }
Example #17
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 #18
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 #19
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 #20
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 #21
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 #22
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 #23
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 #24
Source File: AmazonRdsDataSourceUserTagsFactoryBeanTest.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
@Test void getObject_instanceWithOutTags_emptyMapReturned() throws Exception { // Arrange AmazonRDS amazonRds = mock(AmazonRDS.class); ResourceIdResolver resourceIdResolver = mock(ResourceIdResolver.class); AmazonIdentityManagement amazonIdentityManagement = mock( AmazonIdentityManagement.class); AmazonRdsDataSourceUserTagsFactoryBean factoryBean = new AmazonRdsDataSourceUserTagsFactoryBean( amazonRds, "test", amazonIdentityManagement); factoryBean.setResourceIdResolver(resourceIdResolver); factoryBean.setResourceIdResolver(resourceIdResolver); factoryBean.setRegion(Region.getRegion(Regions.EU_WEST_1)); when(resourceIdResolver.resolveToPhysicalResourceId("test")) .thenReturn("stack-test"); when(amazonIdentityManagement.getUser()).thenReturn( new GetUserResult().withUser(new User("/", "aemruli", "123456789012", "arn:aws:iam::1234567890:user/aemruli", new Date()))); when(amazonRds.listTagsForResource(new ListTagsForResourceRequest() .withResourceName("arn:aws:rds:eu-west-1:1234567890:db:stack-test"))) .thenReturn(new ListTagsForResourceResult()); // Act factoryBean.afterPropertiesSet(); Map<String, String> userTagMap = factoryBean.getObject(); // Assert assertThat(userTagMap.isEmpty()).isTrue(); }
Example #25
Source File: AmazonRdsDataSourceUserTagsFactoryBeanTest.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
@Test void getObject_instanceWithTagsConfiguredWithCustomResourceResolverAndCustomRegion_mapWithTagsReturned() throws Exception { // Arrange AmazonRDS amazonRds = mock(AmazonRDS.class); ResourceIdResolver resourceIdResolver = mock(ResourceIdResolver.class); AmazonIdentityManagement amazonIdentityManagement = mock( AmazonIdentityManagement.class); AmazonRdsDataSourceUserTagsFactoryBean factoryBean = new AmazonRdsDataSourceUserTagsFactoryBean( amazonRds, "test", amazonIdentityManagement); factoryBean.setResourceIdResolver(resourceIdResolver); factoryBean.setRegion(Region.getRegion(Regions.EU_WEST_1)); when(resourceIdResolver.resolveToPhysicalResourceId("test")) .thenReturn("stack-test"); when(amazonIdentityManagement.getUser()).thenReturn( new GetUserResult().withUser(new User("/", "aemruli", "123456789012", "arn:aws:iam::1234567890:user/aemruli", new Date()))); when(amazonRds.listTagsForResource(new ListTagsForResourceRequest() .withResourceName("arn:aws:rds:eu-west-1:1234567890:db:stack-test"))) .thenReturn(new ListTagsForResourceResult().withTagList( new Tag().withKey("key1").withValue("value1"), new Tag().withKey("key2").withValue("value2"))); // Act factoryBean.afterPropertiesSet(); Map<String, String> userTagMap = factoryBean.getObject(); // Assert assertThat(userTagMap.get("key1")).isEqualTo("value1"); assertThat(userTagMap.get("key2")).isEqualTo("value2"); }
Example #26
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 #27
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 #28
Source File: InventoryUtilTest.java From pacbot with Apache License 2.0 | 5 votes |
/** * Fetch RDS instance info test. * * @throws Exception the exception */ @SuppressWarnings("static-access") @Test public void fetchRDSInstanceInfoTest() throws Exception { mockStatic(AmazonRDSClientBuilder.class); AmazonRDS rdsClient = PowerMockito.mock(AmazonRDS.class); AmazonRDSClientBuilder amazonRDSClientBuilder = PowerMockito.mock(AmazonRDSClientBuilder.class); AWSStaticCredentialsProvider awsStaticCredentialsProvider = PowerMockito.mock(AWSStaticCredentialsProvider.class); PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenReturn(awsStaticCredentialsProvider); when(amazonRDSClientBuilder.standard()).thenReturn(amazonRDSClientBuilder); when(amazonRDSClientBuilder.withCredentials(anyObject())).thenReturn(amazonRDSClientBuilder); when(amazonRDSClientBuilder.withRegion(anyString())).thenReturn(amazonRDSClientBuilder); when(amazonRDSClientBuilder.build()).thenReturn(rdsClient); DescribeDBInstancesResult describeDBInstancesResult = new DescribeDBInstancesResult(); List<DBInstance> rdsList = new ArrayList<>(); DBInstance dBInstance = new DBInstance(); dBInstance.setDBInstanceArn("dBInstanceArn"); rdsList.add(dBInstance); describeDBInstancesResult.setDBInstances(rdsList); when(rdsClient.describeDBInstances(anyObject())).thenReturn(describeDBInstancesResult); ListTagsForResourceResult listTagsForResourceResult = new ListTagsForResourceResult(); listTagsForResourceResult.setTagList(new ArrayList<>()); when(rdsClient.listTagsForResource(anyObject())).thenReturn(listTagsForResourceResult); assertThat(inventoryUtil.fetchRDSInstanceInfo(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), "skipRegions", "account","accountName").size(), is(1)); }
Example #29
Source File: InventoryUtilTest.java From pacbot with Apache License 2.0 | 5 votes |
/** * Fetch RDS cluster info test. * * @throws Exception the exception */ @SuppressWarnings("static-access") @Test public void fetchRDSClusterInfoTest() throws Exception { mockStatic(AmazonRDSClientBuilder.class); AmazonRDS rdsClient = PowerMockito.mock(AmazonRDS.class); AmazonRDSClientBuilder amazonRDSClientBuilder = PowerMockito.mock(AmazonRDSClientBuilder.class); AWSStaticCredentialsProvider awsStaticCredentialsProvider = PowerMockito.mock(AWSStaticCredentialsProvider.class); PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenReturn(awsStaticCredentialsProvider); when(amazonRDSClientBuilder.standard()).thenReturn(amazonRDSClientBuilder); when(amazonRDSClientBuilder.withCredentials(anyObject())).thenReturn(amazonRDSClientBuilder); when(amazonRDSClientBuilder.withRegion(anyString())).thenReturn(amazonRDSClientBuilder); when(amazonRDSClientBuilder.build()).thenReturn(rdsClient); DescribeDBClustersResult describeDBClustersResult = new DescribeDBClustersResult(); List<DBCluster> rdsList = new ArrayList<>(); DBCluster dBCluster = new DBCluster(); dBCluster.setDBClusterArn("dBClusterArn");; rdsList.add(dBCluster); describeDBClustersResult.setDBClusters(rdsList); when(rdsClient.describeDBClusters(anyObject())).thenReturn(describeDBClustersResult); ListTagsForResourceResult listTagsForResourceResult = new ListTagsForResourceResult(); listTagsForResourceResult.setTagList(new ArrayList<>()); when(rdsClient.listTagsForResource(anyObject())).thenReturn(listTagsForResourceResult); assertThat(inventoryUtil.fetchRDSClusterInfo(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), "skipRegions", "account","accountName").size(), is(1)); }
Example #30
Source File: InventoryUtil.java From pacbot with Apache License 2.0 | 5 votes |
/** * Fetch RDSDB snapshots. * * @param temporaryCredentials the temporary credentials * @param skipRegions the skip regions * @param accountId the accountId * @param accountName the account name * @return the map */ public static Map<String,List<DBSnapshot>> fetchRDSDBSnapshots(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){ Map<String,List<DBSnapshot>> snapshots = new LinkedHashMap<>(); String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"RDS Snapshot\" , \"region\":\"" ; for(Region region : RegionUtils.getRegions()){ try{ if(!skipRegions.contains(region.getName())){ AmazonRDS rdsClient = AmazonRDSClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build(); DescribeDBSnapshotsResult rslt ; List<DBSnapshot> snapshotsTemp = new ArrayList<>(); String marker = null; do{ rslt = rdsClient.describeDBSnapshots(new DescribeDBSnapshotsRequest().withIncludePublic(false).withIncludeShared(false).withMarker(marker)); snapshotsTemp.addAll(rslt.getDBSnapshots()); marker = rslt.getMarker(); }while(marker!=null); if(! snapshotsTemp.isEmpty() ){ log.debug(InventoryConstants.ACCOUNT + accountId +" Type : RDS Snapshot" +region.getName() + " >> "+snapshotsTemp.size()); snapshots.put(accountId+delimiter+accountName+delimiter+region.getName(), snapshotsTemp); } } }catch(Exception e){ if(region.isServiceSupported(AmazonRDS.ENDPOINT_PREFIX)){ log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}"); ErrorManageUtil.uploadError(accountId,region.getName(),"rdssnapshot",e.getMessage()); } } } return snapshots; }