Search in sources :

Example 1 with ListTagsForResourceRequest

use of com.amazonaws.services.rds.model.ListTagsForResourceRequest in project spring-cloud-aws by awspring.

the class AmazonRdsDataSourceUserTagsFactoryBeanTest method getObject_instanceWithTagsAndNoResourceIdResolverAndDefaultRegion_mapWithTagsReturned.

@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");
}
Also used : GetUserResult(com.amazonaws.services.identitymanagement.model.GetUserResult) User(com.amazonaws.services.identitymanagement.model.User) AmazonRDS(com.amazonaws.services.rds.AmazonRDS) ListTagsForResourceRequest(com.amazonaws.services.rds.model.ListTagsForResourceRequest) ListTagsForResourceResult(com.amazonaws.services.rds.model.ListTagsForResourceResult) Tag(com.amazonaws.services.rds.model.Tag) AmazonIdentityManagement(com.amazonaws.services.identitymanagement.AmazonIdentityManagement) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 2 with ListTagsForResourceRequest

use of com.amazonaws.services.rds.model.ListTagsForResourceRequest in project spring-cloud-aws by awspring.

the class AmazonRdsDataSourceBeanDefinitionParserTest method parseInternal_userTagsDefined_createsUserTagBeanDefinition.

@Test
void parseInternal_userTagsDefined_createsUserTagBeanDefinition() 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());
    BeanDefinitionBuilder identityBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class);
    identityBuilder.setFactoryMethod("mock");
    identityBuilder.addConstructorArgValue(AmazonIdentityManagement.class);
    beanFactory.registerBeanDefinition(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonIdentityManagement.class.getName()), identityBuilder.getBeanDefinition());
    XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
    xmlBeanDefinitionReader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-userTags.xml", getClass()));
    AmazonRDS client = beanFactory.getBean(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonRDSClient.class.getName()), AmazonRDS.class);
    AmazonIdentityManagement amazonIdentityManagement = beanFactory.getBean(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonIdentityManagement.class.getName()), AmazonIdentityManagement.class);
    when(amazonIdentityManagement.getUser()).thenReturn(new GetUserResult().withUser(new User("/", "aemruli", "123456789012", "arn:aws:iam::1234567890:user/aemruli", new Date())));
    when(client.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);
    // Assert
    assertThat(dsTags.get("key1")).isEqualTo("value2");
}
Also used : GetUserResult(com.amazonaws.services.identitymanagement.model.GetUserResult) User(com.amazonaws.services.identitymanagement.model.User) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) AmazonRDS(com.amazonaws.services.rds.AmazonRDS) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ListTagsForResourceResult(com.amazonaws.services.rds.model.ListTagsForResourceResult) ClassPathResource(org.springframework.core.io.ClassPathResource) Date(java.util.Date) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) ListTagsForResourceRequest(com.amazonaws.services.rds.model.ListTagsForResourceRequest) Tag(com.amazonaws.services.rds.model.Tag) AmazonIdentityManagement(com.amazonaws.services.identitymanagement.AmazonIdentityManagement) Test(org.junit.jupiter.api.Test)

Example 3 with ListTagsForResourceRequest

use of com.amazonaws.services.rds.model.ListTagsForResourceRequest in project spring-cloud-aws by awspring.

the class AmazonRdsDataSourceBeanDefinitionParserTest method parseInternal_customRdsInstance_createsRdsBeanAndUserTagsWithCustomRdsInstance.

@Test
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
    assertThat(dsTags.get("key1")).isEqualTo("value2");
    assertThat(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource).isTrue();
}
Also used : GetUserResult(com.amazonaws.services.identitymanagement.model.GetUserResult) User(com.amazonaws.services.identitymanagement.model.User) DBInstance(com.amazonaws.services.rds.model.DBInstance) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) AmazonRDS(com.amazonaws.services.rds.AmazonRDS) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ListTagsForResourceResult(com.amazonaws.services.rds.model.ListTagsForResourceResult) ClassPathResource(org.springframework.core.io.ClassPathResource) Date(java.util.Date) DescribeDBInstancesRequest(com.amazonaws.services.rds.model.DescribeDBInstancesRequest) DataSource(javax.sql.DataSource) Endpoint(com.amazonaws.services.rds.model.Endpoint) ListTagsForResourceRequest(com.amazonaws.services.rds.model.ListTagsForResourceRequest) Tag(com.amazonaws.services.rds.model.Tag) DescribeDBInstancesResult(com.amazonaws.services.rds.model.DescribeDBInstancesResult) AmazonIdentityManagement(com.amazonaws.services.identitymanagement.AmazonIdentityManagement) Test(org.junit.jupiter.api.Test)

Example 4 with ListTagsForResourceRequest

use of com.amazonaws.services.rds.model.ListTagsForResourceRequest in project spring-cloud-aws by awspring.

the class AmazonRdsDataSourceUserTagsFactoryBean method createInstance.

@Override
protected Map<String, String> createInstance() throws Exception {
    LinkedHashMap<String, String> userTags = new LinkedHashMap<>();
    ListTagsForResourceResult tagsForResource = this.amazonRds.listTagsForResource(new ListTagsForResourceRequest().withResourceName(getDbInstanceResourceName()));
    for (Tag tag : tagsForResource.getTagList()) {
        userTags.put(tag.getKey(), tag.getValue());
    }
    return userTags;
}
Also used : ListTagsForResourceRequest(com.amazonaws.services.rds.model.ListTagsForResourceRequest) ListTagsForResourceResult(com.amazonaws.services.rds.model.ListTagsForResourceResult) Tag(com.amazonaws.services.rds.model.Tag) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with ListTagsForResourceRequest

use of com.amazonaws.services.rds.model.ListTagsForResourceRequest in project spring-cloud-aws by awspring.

the class AmazonRdsDataSourceUserTagsFactoryBeanTest method getObject_instanceWithTagsConfiguredWithCustomResourceResolverAndCustomRegion_mapWithTagsReturned.

@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");
}
Also used : GetUserResult(com.amazonaws.services.identitymanagement.model.GetUserResult) User(com.amazonaws.services.identitymanagement.model.User) ResourceIdResolver(io.awspring.cloud.core.env.ResourceIdResolver) AmazonRDS(com.amazonaws.services.rds.AmazonRDS) ListTagsForResourceRequest(com.amazonaws.services.rds.model.ListTagsForResourceRequest) ListTagsForResourceResult(com.amazonaws.services.rds.model.ListTagsForResourceResult) Tag(com.amazonaws.services.rds.model.Tag) AmazonIdentityManagement(com.amazonaws.services.identitymanagement.AmazonIdentityManagement) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Aggregations

ListTagsForResourceRequest (com.amazonaws.services.rds.model.ListTagsForResourceRequest)7 ListTagsForResourceResult (com.amazonaws.services.rds.model.ListTagsForResourceResult)6 AmazonIdentityManagement (com.amazonaws.services.identitymanagement.AmazonIdentityManagement)5 GetUserResult (com.amazonaws.services.identitymanagement.model.GetUserResult)5 User (com.amazonaws.services.identitymanagement.model.User)5 AmazonRDS (com.amazonaws.services.rds.AmazonRDS)5 Tag (com.amazonaws.services.rds.model.Tag)5 Date (java.util.Date)5 Test (org.junit.jupiter.api.Test)5 ResourceIdResolver (io.awspring.cloud.core.env.ResourceIdResolver)2 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 DBInstance (com.amazonaws.services.rds.model.DBInstance)1 DescribeDBInstancesRequest (com.amazonaws.services.rds.model.DescribeDBInstancesRequest)1 DescribeDBInstancesResult (com.amazonaws.services.rds.model.DescribeDBInstancesResult)1 Endpoint (com.amazonaws.services.rds.model.Endpoint)1 LinkedHashMap (java.util.LinkedHashMap)1 DataSource (javax.sql.DataSource)1 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)1