@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; }
@Test public 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 assertEquals("value1", userTagMap.get("key1")); assertEquals("value2", userTagMap.get("key2")); }
@Test public 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 assertEquals("value1", userTagMap.get("key1")); assertEquals("value2", userTagMap.get("key2")); }
@Test public 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 assertEquals("value2", dsTags.get("key1")); }
private Collection<Tag> convertToTags(RDSInstanceTemplate template, String instanceId) { List<Tag> userDefinedTags = rdsTagHelper.getUserDefinedTags(template); return rdsTagHelper.getInstanceTags(template, instanceId, userDefinedTags); }
@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); }
public RDSInstance(DBInstance instance, DBCluster cluster, List<Tag> tagList) { this.allocatedStorage = instance.getAllocatedStorage(); this.autoMinorVersionUpgrade = instance.getAutoMinorVersionUpgrade(); this.availabilityZone = instance.getAvailabilityZone(); this.backupRetentionPeriod = instance.getBackupRetentionPeriod(); this.characterSetName = instance.getCharacterSetName(); this.dBInstanceClass = instance.getDBInstanceClass(); this.dBInstanceIdentifier = instance.getDBInstanceIdentifier(); this.dBInstanceStatus = instance.getDBInstanceStatus(); this.dBClusterIdentifier = instance.getDBClusterIdentifier(); this.dBName = instance.getDBName(); this.dBParameterGroups = instance.getDBParameterGroups(); this.dBSecurityGroups = instance.getDBSecurityGroups(); this.dBSubnetGroup = instance.getDBSubnetGroup(); this.endpoint = instance.getEndpoint(); if(this.endpoint != null) { this.hostname = endpoint.getAddress(); this.privateIP = getPrivateIp(hostname); } else { this.hostname = null; this.privateIP = null; } this.engine = instance.getEngine(); this.engineVersion = instance.getEngineVersion(); this.instanceCreateTime = instance.getInstanceCreateTime(); this.iops = instance.getIops(); this.latestRestorableTime = instance.getLatestRestorableTime(); this.licenseModel = instance.getLicenseModel(); this.masterUsername = instance.getMasterUsername(); this.multiAZ = instance.getMultiAZ(); this.optionGroupMemberships = instance.getOptionGroupMemberships(); this.pendingModifiedValues = instance.getPendingModifiedValues(); this.preferredBackupWindow = instance.getPreferredBackupWindow(); this.preferredMaintenanceWindow = instance.getPreferredMaintenanceWindow(); this.publiclyAccessible = instance.getPubliclyAccessible(); this.readReplicaDBInstanceIdentifiers = instance.getReadReplicaDBInstanceIdentifiers(); this.readReplicaSourceDBInstanceIdentifier = instance.getReadReplicaSourceDBInstanceIdentifier(); this.secondaryAvailabilityZone = instance.getSecondaryAvailabilityZone(); this.statusInfos = instance.getStatusInfos(); this.vpcSecurityGroups = instance.getVpcSecurityGroups(); this.isMaster = checkIfMaster(instance, cluster); this.tags = new HashMap<>(tagList.size()); for(Tag tag : tagList) { this.tags.put(tag.getKey(), tag.getValue()); } }
/** * Creates an implementation-specific tag with the specified key and value. * * @param tagKey the tag key * @param tagValue the tag value * @return an implementation-specific tag with the specified key and value */ @Override public Tag createTagImpl(String tagKey, String tagValue) { return new Tag().withKey(tagKey).withValue(tagValue); }