下面列出了怎么用com.amazonaws.services.s3.model.BucketTaggingConfiguration的API类实例代码及写法,或者点击链接到github查看源代码。
public Bucket createBucketForInstance(String instanceId, ServiceDefinition service, String planId,
String organizationGuid, String spaceGuid) {
String bucketName = getBucketNameForInstance(instanceId);
logger.info("Creating bucket '{}' for serviceInstanceId '{}'", bucketName, instanceId);
Bucket bucket = s3.createBucket(bucketName, Region.fromValue(region));
// TODO allow for additional, custom tagging options
BucketTaggingConfiguration bucketTaggingConfiguration = new BucketTaggingConfiguration();
TagSet tagSet = new TagSet();
tagSet.setTag("serviceInstanceId", instanceId);
tagSet.setTag("serviceDefinitionId", service.getId());
tagSet.setTag("planId", planId);
tagSet.setTag("organizationGuid", organizationGuid);
tagSet.setTag("spaceGuid", spaceGuid);
bucketTaggingConfiguration.withTagSets(tagSet);
s3.setBucketTaggingConfiguration(bucket.getName(), bucketTaggingConfiguration);
return bucket;
}
private ServiceInstance createServiceInstance(BucketTaggingConfiguration taggingConfiguration) {
// While the Java API has multiple TagSets, it would appear from
// http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUTtagging.html
// that only one TagSet is supported.
TagSet tagSet = taggingConfiguration.getTagSet();
String serviceInstanceId = tagSet.getTag("serviceInstanceId");
if (serviceInstanceId == null) {
// could occur if someone used this broker AWS ID to a bucket
// outside of the broker process
return null;
}
String serviceDefinitionId = tagSet.getTag("serviceDefinitionId");
String planId = tagSet.getTag("planId");
String organizationGuid = tagSet.getTag("organizationGuid");
String spaceGuid = tagSet.getTag("spaceGuid");
ServiceInstance serviceInstance = new ServiceInstance(serviceInstanceId, serviceDefinitionId, planId,
organizationGuid, spaceGuid, null);
return serviceInstance;
}
/**
* Fetch S 3 info test.
*
* @throws Exception the exception
*/
@SuppressWarnings({ "static-access"})
@Test
public void fetchS3InfoTest() throws Exception {
mockStatic(AmazonS3ClientBuilder.class);
AmazonS3 amazonS3Client = PowerMockito.mock(AmazonS3.class);
AmazonS3ClientBuilder amazonRDSClientBuilder = PowerMockito.mock(AmazonS3ClientBuilder.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(amazonS3Client);
List<Bucket> s3buckets = new ArrayList<>();
Bucket bucket = new Bucket();
bucket.setName("name");
s3buckets.add(bucket);
when(amazonS3Client.listBuckets()).thenReturn(s3buckets);
when(amazonS3Client.getBucketLocation(anyString())).thenReturn("bucketLocation");
mockStatic(com.amazonaws.services.s3.model.Region.class);
com.amazonaws.services.s3.model.Region value = null;
when(com.amazonaws.services.s3.model.Region.fromValue(anyString())).thenReturn(value.US_West);
when(value.US_West.toAWSRegion()).thenReturn(getRegions().get(0));
when(amazonS3Client.getBucketVersioningConfiguration(anyString())).thenReturn(new BucketVersioningConfiguration());
BucketTaggingConfiguration tagConfig = new BucketTaggingConfiguration();
List<TagSet> tagSets = new ArrayList<>();
TagSet tagSet = new TagSet();
tagSet.setTag("key", "value");
tagSets.add(tagSet);
tagSets.add(tagSet);
tagConfig.setTagSets(tagSets);
when(amazonS3Client.getBucketTaggingConfiguration(anyString())).thenReturn(tagConfig);
assertThat(inventoryUtil.fetchS3Info(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"),
"skipRegions", "account","accountName").size(), is(1));
}
public ServiceInstance findServiceInstance(String instanceId) {
String bucketName = getBucketNameForInstance(instanceId);
if (s3.doesBucketExist(bucketName)) {
BucketTaggingConfiguration taggingConfiguration = s3.getBucketTaggingConfiguration(bucketName);
return createServiceInstance(taggingConfiguration);
}
return null;
}
public List<ServiceInstance> getAllServiceInstances() {
List<ServiceInstance> serviceInstances = Lists.newArrayList();
for (Bucket bucket : s3.listBuckets()) {
BucketTaggingConfiguration taggingConfiguration = s3.getBucketTaggingConfiguration(bucket.getName());
ServiceInstance serviceInstance = createServiceInstance(taggingConfiguration);
serviceInstances.add(serviceInstance);
}
return serviceInstances;
}
/**
* Tag resource.
*
* @param resourceId
* the resource id
* @param clientMap
* the client map
* @param serviceType
* the service type
* @param pacTag
* the pac tag
* @return the boolean
* @throws Exception
* the exception
*/
public Boolean tagResource(final String resourceId, final Map<String, Object> clientMap, AWSService serviceType,
Map<String, String> pacTag) throws Exception {
switch (serviceType) {
case S3: {
try {
AmazonS3 s3Client = (AmazonS3) clientMap.get("client");
BucketTaggingConfiguration bucketTaggingConfiguration = s3Client
.getBucketTaggingConfiguration(resourceId);
if (bucketTaggingConfiguration == null) {
saveTags(resourceId, new BucketTaggingConfiguration(Collections.singletonList(new TagSet(pacTag))),
s3Client);
} else {
List<TagSet> existingTargets = bucketTaggingConfiguration.getAllTagSets();
Map<String, String> existingTags = existingTargets.get(0).getAllTags();
Map<String, String> allTags = Maps.newHashMap();
if (bucketAlreadyTaggedAndTagValueNotAltered(existingTags, pacTag))
return Boolean.TRUE;
allTags.putAll(existingTags);
allTags.putAll(pacTag);
bucketTaggingConfiguration.setTagSets(existingTargets);
saveTags(resourceId,
new BucketTaggingConfiguration(Collections.singletonList(new TagSet(allTags))), s3Client);
}
return Boolean.TRUE;
} catch (Exception exception) {
logger.error("error tagging bucekt - > " + resourceId, exception);
throw exception;
}
}
case EC2: {
return setEC2VolumeTag(resourceId, clientMap, pacTag);
}
case SNAPSHOT: {
return setEC2VolumeTag(resourceId, clientMap, pacTag);
}
case VOLUME: {
return setEC2VolumeTag(resourceId, clientMap, pacTag);
}
case RDSDB: {
return setRDSDBTag(resourceId,clientMap,pacTag);
}
case ELASTICSEARCH:{
return setElasticSearchTag(resourceId,clientMap,pacTag);
}
case EFS:{
return setEFSTag(resourceId,clientMap,pacTag);
}
case REDSHIFT:{
return setRedshiftTag(resourceId,clientMap,pacTag);
}
default:
throw new OperationNotPermittedException("this resource tagging is not imlemented yet");
}
}
/**
* Gets the pacman tag value.
*
* @param resourceId
* the resource id
* @param clientMap
* the client map
* @param serviceType
* the service type
* @return the pacman tag value
*/
public String getPacmanTagValue(String resourceId, Map<String, Object> clientMap, AWSService serviceType) {
switch (serviceType) {
case S3: {
try {
AmazonS3 s3Client = (AmazonS3) clientMap.get("client");
BucketTaggingConfiguration bucketTaggingConfiguration = s3Client
.getBucketTaggingConfiguration(resourceId);
if (null == bucketTaggingConfiguration) {// this is the case
// when bucket does not
// exists , this is the
// case when inventory
// sync is delayed
return null;
}
List<TagSet> existingTargets = bucketTaggingConfiguration.getAllTagSets();
Map<String, String> existingTags = existingTargets.get(0).getAllTags();
return existingTags.get(CommonUtils.getPropValue(PacmanSdkConstants.PACMAN_AUTO_FIX_TAG_NAME));
} catch (Exception exception) {
logger.error("error tagging bucekt - > " + resourceId, exception);
throw exception;
}
}
case EC2: {
return getEC2PacManTagValue(resourceId, clientMap);
}
case VOLUME: {
return getEC2PacManTagValue(resourceId, clientMap);
}
case SNAPSHOT: {
return getEC2PacManTagValue(resourceId, clientMap);
}
case IAM: {
return "";
}
case ELB_APP: {
return getAppElbPacManTagValue(resourceId, clientMap);
}
case ELB_CLASSIC: {
return "";
}
case REDSHIFT: {
return "";
}
case RDS: {
return "";
}
case ELASTICSEARCH: {
return "";
}
default:
throw new OperationNotPermittedException("this resource tagging is not imlemented yet");
}
}
/** Unsupported Operation. */
@Override public BucketTaggingConfiguration getBucketTaggingConfiguration(String bucketName) {
throw new UnsupportedOperationException("Operation not supported");
}
/** Unsupported Operation. */
@Override public BucketTaggingConfiguration getBucketTaggingConfiguration(
GetBucketTaggingConfigurationRequest getBucketTaggingConfigurationReq) {
throw new UnsupportedOperationException("Operation not supported");
}
/** Unsupported Operation. */
@Override public void setBucketTaggingConfiguration(String bucketName,
BucketTaggingConfiguration bucketTaggingConfiguration) {
throw new UnsupportedOperationException("Operation not supported");
}
@Override
public BucketTaggingConfiguration getBucketTaggingConfiguration(String bucketName) {
// TODO Auto-generated method stub
return null;
}
@Override
public void setBucketTaggingConfiguration(String bucketName, BucketTaggingConfiguration bucketTaggingConfiguration) {
// TODO Auto-generated method stub
}
/**
* Save tags.
*
* @param resourceId
* the resource id
* @param bucketTaggingConfiguration
* the bucket tagging configuration
* @param s3Client
* the s 3 client
*/
private void saveTags(final String resourceId, final BucketTaggingConfiguration bucketTaggingConfiguration,
AmazonS3 s3Client) {
s3Client.setBucketTaggingConfiguration(resourceId, bucketTaggingConfiguration);
}