Search in sources :

Example 51 with Tag

use of software.amazon.awssdk.services.s3.model.Tag in project photon-model by vmware.

the class AWSNetworkStateEnumerationAdapterService method createSubnetStateOperations.

/**
 * Create the subnet state operations for all the Subnets that need to be created or updated in
 * the system.
 */
private void createSubnetStateOperations(AWSNetworkStateCreationContext context, AWSNetworkStateCreationStage next) {
    if (context.subnets.isEmpty()) {
        logFine(() -> "No new subnets found.");
        handleNetworkStateChanges(context, next);
        return;
    }
    final List<Operation> subnetOperations = new ArrayList<>();
    for (String remoteSubnetId : context.subnets.keySet()) {
        SubnetStateWithParentVpcId subnetStateWithParentVpcId = context.subnets.get(remoteSubnetId);
        SubnetState subnetState = subnetStateWithParentVpcId.subnetState;
        // Update networkLink with "latest" (either created or updated)
        // NetworkState.documentSelfLink
        subnetState.networkLink = context.vpcs.get(subnetStateWithParentVpcId.parentVpcId).documentSelfLink;
        final Operation subnetStateOp;
        if (context.localSubnetStateMap.containsKey(remoteSubnetId)) {
            // If the local subnet state already exists for the Subnet -> Update it.
            subnetState.documentSelfLink = context.localSubnetStateMap.get(remoteSubnetId).documentSelfLink;
            // for already existing subnets, add internal tags only if missing
            if (subnetState.tagLinks == null || subnetState.tagLinks.isEmpty()) {
                setTagLinksToResourceState(subnetState, context.subnetInternalTagsMap, false);
            } else {
                context.subnetInternalTagLinksSet.stream().filter(tagLink -> !subnetState.tagLinks.contains(tagLink)).map(tagLink -> subnetState.tagLinks.add(tagLink)).collect(Collectors.toSet());
            }
            subnetStateOp = createPatchOperation(this, subnetState, subnetState.documentSelfLink);
        } else {
            // add tag links
            Subnet awsSubnet = context.awsSubnets.get(remoteSubnetId);
            setResourceTags(subnetState, awsSubnet.getTags());
            setTagLinksToResourceState(subnetState, context.subnetInternalTagsMap, false);
            subnetStateOp = createPostOperation(this, subnetState, SubnetService.FACTORY_LINK);
        }
        subnetOperations.add(subnetStateOp);
    }
    JoinedCompletionHandler joinCompletion = (ops, excs) -> {
        if (excs != null) {
            Entry<Long, Throwable> excEntry = excs.entrySet().iterator().next();
            Throwable exc = excEntry.getValue();
            Operation op = ops.get(excEntry.getKey());
            logSevere(() -> String.format("Error %s-ing a Subnet state: %s", op.getAction(), Utils.toString(excs)));
            finishWithFailure(context, exc);
            return;
        }
        logFine(() -> "Successfully created/updated all subnet states.");
        ops.values().stream().filter(op -> op.getStatusCode() != Operation.STATUS_CODE_NOT_MODIFIED).forEach(op -> {
            SubnetState subnetState = op.getBody(SubnetState.class);
            context.subnets.get(subnetState.id).subnetState = subnetState;
        });
        handleNetworkStateChanges(context, next);
    };
    OperationJoin.create(subnetOperations).setCompletion(joinCompletion).sendWith(this);
}
Also used : Arrays(java.util.Arrays) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) AWSNetworkUtils.mapVPCToNetworkState(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkUtils.mapVPCToNetworkState) DescribeSubnetsRequest(com.amazonaws.services.ec2.model.DescribeSubnetsRequest) DescribeVpcsRequest(com.amazonaws.services.ec2.model.DescribeVpcsRequest) AWSResourceType(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSResourceType) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) LifecycleState(com.vmware.photon.controller.model.resources.ComputeService.LifecycleState) ServiceDocument(com.vmware.xenon.common.ServiceDocument) AWSNetworkUtils.createQueryToGetExistingNetworkStatesFilteredByDiscoveredVPCs(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkUtils.createQueryToGetExistingNetworkStatesFilteredByDiscoveredVPCs) DescribeSubnetsResult(com.amazonaws.services.ec2.model.DescribeSubnetsResult) Utils(com.vmware.xenon.common.Utils) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Map(java.util.Map) SubnetStateWithParentVpcId(com.vmware.photon.controller.model.adapters.awsadapter.enumeration.AWSNetworkStateEnumerationAdapterService.AWSNetworkStateCreationContext.SubnetStateWithParentVpcId) AWS_ATTACHMENT_VPC_FILTER(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_ATTACHMENT_VPC_FILTER) InternetGatewayAttachment(com.amazonaws.services.ec2.model.InternetGatewayAttachment) AdapterUtils.createPatchOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPatchOperation) URI(java.net.URI) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) SubnetService(com.vmware.photon.controller.model.resources.SubnetService) AWS_MAIN_ROUTE_ASSOCIATION(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_MAIN_ROUTE_ASSOCIATION) AWSAsyncHandler(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSAsyncHandler) StatelessService(com.vmware.xenon.common.StatelessService) AWSNetworkUtils.mapSubnetToSubnetState(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkUtils.mapSubnetToSubnetState) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DescribeInternetGatewaysResult(com.amazonaws.services.ec2.model.DescribeInternetGatewaysResult) Set(java.util.Set) Occurance(com.vmware.xenon.services.common.QueryTask.Query.Occurance) AdapterUtils.getDeletionState(com.vmware.photon.controller.model.adapters.util.AdapterUtils.getDeletionState) Collectors(java.util.stream.Collectors) AWS_GATEWAY_ID(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_GATEWAY_ID) List(java.util.List) AWS_FILTER_VPC_ID(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.AWS_FILTER_VPC_ID) DescribeRouteTablesRequest(com.amazonaws.services.ec2.model.DescribeRouteTablesRequest) TagService(com.vmware.photon.controller.model.resources.TagService) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) Tag(com.amazonaws.services.ec2.model.Tag) DeferredResult(com.vmware.xenon.common.DeferredResult) TAG_KEY_TYPE(com.vmware.photon.controller.model.constants.PhotonModelConstants.TAG_KEY_TYPE) Entry(java.util.Map.Entry) TagsUtil.setTagLinksToResourceState(com.vmware.photon.controller.model.adapters.util.TagsUtil.setTagLinksToResourceState) DescribeRouteTablesResult(com.amazonaws.services.ec2.model.DescribeRouteTablesResult) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) HashMap(java.util.HashMap) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) JoinedCompletionHandler(com.vmware.xenon.common.OperationJoin.JoinedCompletionHandler) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) AWSConstants(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) Query(com.vmware.xenon.services.common.QueryTask.Query) UriPaths(com.vmware.photon.controller.model.UriPaths) TagsUtil.updateLocalTagStates(com.vmware.photon.controller.model.adapters.util.TagsUtil.updateLocalTagStates) AdapterUtils.createPostOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPostOperation) BiConsumer(java.util.function.BiConsumer) Filter(com.amazonaws.services.ec2.model.Filter) RouteTable(com.amazonaws.services.ec2.model.RouteTable) Subnet(com.amazonaws.services.ec2.model.Subnet) AWS_VPC_ROUTE_TABLE_ID(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_VPC_ROUTE_TABLE_ID) AmazonWebServiceRequest(com.amazonaws.AmazonWebServiceRequest) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) Vpc(com.amazonaws.services.ec2.model.Vpc) AdapterUriUtil(com.vmware.photon.controller.model.adapters.util.AdapterUriUtil) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) AWSNetworkUtils.createQueryToGetExistingSubnetStatesFilteredByDiscoveredSubnets(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkUtils.createQueryToGetExistingSubnetStatesFilteredByDiscoveredSubnets) NumericRange.createLessThanRange(com.vmware.xenon.services.common.QueryTask.NumericRange.createLessThanRange) AWSUtils(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils) AWS_VPC_ID_FILTER(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_VPC_ID_FILTER) InternetGateway(com.amazonaws.services.ec2.model.InternetGateway) NetworkService(com.vmware.photon.controller.model.resources.NetworkService) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) AWSClientManagerFactory(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) DescribeInternetGatewaysRequest(com.amazonaws.services.ec2.model.DescribeInternetGatewaysRequest) AWSUriPaths(com.vmware.photon.controller.model.adapters.awsadapter.AWSUriPaths) OperationJoin(com.vmware.xenon.common.OperationJoin) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) JoinedCompletionHandler(com.vmware.xenon.common.OperationJoin.JoinedCompletionHandler) Entry(java.util.Map.Entry) ArrayList(java.util.ArrayList) SubnetStateWithParentVpcId(com.vmware.photon.controller.model.adapters.awsadapter.enumeration.AWSNetworkStateEnumerationAdapterService.AWSNetworkStateCreationContext.SubnetStateWithParentVpcId) AdapterUtils.createPatchOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPatchOperation) AdapterUtils.createPostOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPostOperation) Operation(com.vmware.xenon.common.Operation) Subnet(com.amazonaws.services.ec2.model.Subnet) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) AWSNetworkUtils.mapSubnetToSubnetState(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkUtils.mapSubnetToSubnetState)

Example 52 with Tag

use of software.amazon.awssdk.services.s3.model.Tag in project photon-model by vmware.

the class TestAWSEnumerationTask method testTagEnumeration.

@Test
public void testTagEnumeration() throws Throwable {
    if (this.isMock) {
        return;
    }
    setUpTestVolume(this.host, this.client, this.awsTestContext, this.isMock);
    this.snapshotId = (String) this.awsTestContext.get(TestAWSSetupUtils.SNAPSHOT_KEY);
    this.ebsBlockDevice = new EbsBlockDevice().withSnapshotId(this.snapshotId);
    this.blockDeviceMapping = new BlockDeviceMapping().withDeviceName(BLOCK_DEVICE_NAME).withEbs(this.ebsBlockDevice);
    this.diskId = (String) this.awsTestContext.get(TestAWSSetupUtils.DISK_KEY);
    this.host.log("Running test: " + this.currentTestName.getMethodName());
    // VM tags
    Tag tag1 = new Tag(VM_TAG_KEY_1, VM_TAG_VALUE_1);
    Tag tag2 = new Tag(VM_TAG_KEY_2, VM_TAG_VALUE_2);
    Tag tag3 = new Tag(VM_TAG_KEY_3, VM_TAG_VALUE_3);
    List<Tag> vmTags = Arrays.asList(tag1, tag2, tag3);
    // SG tag
    List<Tag> sgTags = new ArrayList<>();
    sgTags.add(new Tag(INITIAL_SG_TAG, INITIAL_SG_TAG));
    // Network tag
    List<Tag> networkTags = new ArrayList<>();
    networkTags.add(new Tag(INITIAL_VPC_TAG, INITIAL_VPC_TAG));
    // Subnet tag
    List<Tag> subnetTags = new ArrayList<>();
    subnetTags.add(new Tag(INITIAL_SUBNET_TAG, INITIAL_SUBNET_TAG));
    // Disk tag
    List<Tag> diskTags = new ArrayList<>();
    diskTags.add(new Tag(INITIAL_DISK_TAG, INITIAL_DISK_TAG));
    try {
        String linuxVMId1 = provisionAWSEBSVMWithEC2Client(this.host, this.client, EC2_LINUX_AMI, this.subnetId, this.securityGroupId, this.blockDeviceMapping);
        this.instancesToCleanUp.add(linuxVMId1);
        waitForProvisioningToComplete(this.instancesToCleanUp, this.host, this.client, ZERO);
        // Tag the first VM with a name and add some additional tags
        tagResourcesWithName(this.client, VM_NAME, linuxVMId1);
        List<Tag> linuxVMId1Tags = Arrays.asList(tag1, tag2);
        // tag vm, default SG, VPC, Subnet and Disk
        tagResources(this.client, linuxVMId1Tags, linuxVMId1);
        tagResources(this.client, sgTags, this.securityGroupId);
        tagResources(this.client, networkTags, this.vpcId);
        tagResources(this.client, subnetTags, this.subnetId);
        tagResources(this.client, diskTags, this.diskId);
        enumerateResources(this.host, this.computeHost, this.endpointState, this.isMock, TEST_CASE_INITIAL);
        String linuxVMId2 = provisionAWSEBSVMWithEC2Client(this.host, this.client, EC2_LINUX_AMI, this.subnetId, this.securityGroupId, this.blockDeviceMapping);
        this.instancesToCleanUp.add(linuxVMId2);
        waitForProvisioningToComplete(this.instancesToCleanUp, this.host, this.client, ZERO);
        // Name the second VM and add some tags
        tagResourcesWithName(this.client, VM_UPDATED_NAME, linuxVMId2);
        List<Tag> linuxVMId2Tags = Arrays.asList(tag2, tag3);
        tagResources(this.client, linuxVMId2Tags, linuxVMId2);
        // Un-tag the resources
        unTagResources(this.client, sgTags, this.securityGroupId);
        unTagResources(this.client, networkTags, this.vpcId);
        unTagResources(this.client, subnetTags, this.subnetId);
        unTagResources(this.client, diskTags, this.diskId);
        // re-init tag arrays
        sgTags = new ArrayList<>();
        networkTags = new ArrayList<>();
        subnetTags = new ArrayList<>();
        diskTags = new ArrayList<>();
        // new key-value set remotely should result in a new tag state created locally
        // and a new tag link added to the SecurityGroupState, NetworkState, SubnetState and
        // DiskState
        sgTags.add(new Tag(SECONDARY_SG_TAG, SECONDARY_SG_TAG));
        networkTags.add(new Tag(SECONDARY_VPC_TAG, SECONDARY_VPC_TAG));
        subnetTags.add(new Tag(SECONDARY_SUBNET_TAG, SECONDARY_SUBNET_TAG));
        diskTags.add(new Tag(SECONDARY_DISK_TAG, SECONDARY_DISK_TAG));
        // tag again default SG, VPC, Subnet and Disk
        tagResources(this.client, diskTags, this.diskId);
        tagResources(this.client, sgTags, this.securityGroupId);
        tagResources(this.client, networkTags, this.vpcId);
        tagResources(this.client, subnetTags, this.subnetId);
        enumerateResources(this.host, this.computeHost, this.endpointState, this.isMock, TEST_CASE_INITIAL);
        validateComputeName(linuxVMId1, VM_NAME);
        validateComputeName(linuxVMId2, VM_UPDATED_NAME);
        // Validate tag states number
        int allTagsNumber = vmTags.size() + sgTags.size() + networkTags.size() + subnetTags.size() + diskTags.size();
        queryDocumentsAndAssertExpectedCount(this.host, allTagsNumber, TagService.FACTORY_LINK, false);
        ServiceDocumentQueryResult serviceDocumentQueryResult = queryAllFactoryResources(this.host, TagService.FACTORY_LINK);
        Map<String, TagState> tagsMap = new HashMap<>();
        for (Entry<String, Object> entry : serviceDocumentQueryResult.documents.entrySet()) {
            tagsMap.put(entry.getKey(), Utils.fromJson(entry.getValue(), TagState.class));
        }
        // validate security group tags
        Map<String, SecurityGroupState> allSecurityGroupStatesMap = ProvisioningUtils.<SecurityGroupState>getResourceStates(this.host, SecurityGroupService.FACTORY_LINK, SecurityGroupState.class);
        SecurityGroupState defaultSgState = allSecurityGroupStatesMap.get(this.securityGroupId);
        // ensure one link is deleted and one new is added to the sg state. One additional
        // link is an internal tag.
        assertNotNull(defaultSgState.tagLinks);
        assertEquals("Wrong number of security-group tag links found.", 1 + internalTagsCount1, defaultSgState.tagLinks.size());
        // validate vpc tags
        Map<String, NetworkState> allNetworkStatesMap = ProvisioningUtils.<NetworkState>getResourceStates(this.host, NetworkService.FACTORY_LINK, NetworkState.class);
        NetworkState defaultNetworkState = allNetworkStatesMap.get(this.vpcId);
        // ensure one link is deleted and one new is added to the network state. One additional
        // link is an internal tag.
        assertEquals("Wrong number of network tag links found.", 1 + internalTagsCount1, defaultNetworkState.tagLinks.size());
        // validate subnet tags
        Map<String, SubnetState> allSubnetStatesMap = ProvisioningUtils.<SubnetState>getResourceStates(this.host, SubnetService.FACTORY_LINK, SubnetState.class);
        SubnetState defaultSubnetState = allSubnetStatesMap.get(this.subnetId);
        // ensure one link is deleted and one new is added to the subnet state. One additional
        // link is an internal tag.
        assertEquals("Wrong number of subnet tag links found.", 1 + internalTagsCount1, defaultSubnetState.tagLinks.size());
        // validate disk tags
        Map<String, DiskState> allDiskStatesMap = ProvisioningUtils.<DiskState>getResourceStates(this.host, DiskService.FACTORY_LINK, DiskState.class);
        DiskState defaultDiskState = allDiskStatesMap.get(this.diskId);
        // ensure one link is deleted and one new is added to the disk state
        assertEquals("Wrong number of disk tag links found.", 1 + internalTagsCount1, defaultDiskState.tagLinks.size());
        // ensure EBS disk has an internal type tag set
        assertTrue(defaultDiskState.tagLinks.contains(TagsUtil.newTagState(TAG_KEY_TYPE, AWSResourceType.ebs_block.toString(), false, this.endpointState.tenantLinks).documentSelfLink));
        // validate vm tags
        Map<Tag, String> vmTagLinks = new HashMap<>();
        for (Tag tag : vmTags) {
            for (TagState tagState : tagsMap.values()) {
                if (tagState.key.equals(tag.getKey())) {
                    vmTagLinks.put(tag, tagState.documentSelfLink);
                }
            }
        }
        ComputeState linuxVMId1ComputeState = getComputeByAWSId(this.host, linuxVMId1);
        // compute has 2 remote tags + 1 local tag
        assertEquals(linuxVMId1Tags.size() + internalTagsCount1, linuxVMId1ComputeState.tagLinks.size());
        for (Tag tag : linuxVMId1Tags) {
            assertTrue(linuxVMId1ComputeState.tagLinks.contains(vmTagLinks.get(tag)));
        }
        ComputeState linuxVMId2ComputeState = getComputeByAWSId(this.host, linuxVMId2);
        assertEquals(linuxVMId2Tags.size() + internalTagsCount1, linuxVMId2ComputeState.tagLinks.size());
        for (Tag tag : linuxVMId2Tags) {
            assertTrue(linuxVMId2ComputeState.tagLinks.contains(vmTagLinks.get(tag)));
        }
    } catch (Throwable t) {
        this.host.log("Exception occurred during test execution: %s", t.getMessage());
        if (t instanceof AssertionError) {
            fail("Assert exception occurred during test execution: " + t.getMessage());
        }
    } finally {
        // un-tag default SG
        unTagResources(this.client, sgTags, this.securityGroupId);
        // un-tag default VPC
        unTagResources(this.client, networkTags, this.vpcId);
        // un-tag default Subnet
        unTagResources(this.client, subnetTags, this.subnetId);
        // un-tag default Disk
        unTagResources(this.client, diskTags, this.diskId);
        tearDownTestDisk(this.client, this.host, this.awsTestContext, this.isMock);
    }
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) HashMap(java.util.HashMap) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) ArrayList(java.util.ArrayList) BlockDeviceMapping(com.amazonaws.services.ec2.model.BlockDeviceMapping) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) TestAWSSetupUtils.deleteVMsOnThisEndpoint(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.deleteVMsOnThisEndpoint) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) EbsBlockDevice(com.amazonaws.services.ec2.model.EbsBlockDevice) Tag(com.amazonaws.services.ec2.model.Tag) Test(org.junit.Test)

Example 53 with Tag

use of software.amazon.awssdk.services.s3.model.Tag in project photon-model by vmware.

the class AWSInstanceContext method getAWSTags.

/**
 * Returns a set of {@link Tag} objects to be used to tag this instance in AWS.
 * Also adds a {@link AWSConstants#AWS_TAG_NAME}  tag.
 *
 * @return A set with AWS Tag objects
 */
public Collection<Tag> getAWSTags() {
    Set<Tag> instanceTags = new HashSet<>();
    instanceTags.add(new Tag(AWS_TAG_NAME, this.child.name));
    tagStates.forEach(tagState -> {
        Tag tag = new Tag(tagState.key, tagState.value);
        instanceTags.add(tag);
    });
    return instanceTags;
}
Also used : Tag(com.amazonaws.services.ec2.model.Tag) HashSet(java.util.HashSet)

Example 54 with Tag

use of software.amazon.awssdk.services.s3.model.Tag in project herd by FINRAOS.

the class StoragePolicyProcessorHelperServiceTest method runExecuteStoragePolicyTransitionTest.

private void runExecuteStoragePolicyTransitionTest() {
    // Create S3FileTransferRequestParamsDto to access the S3 bucket location.
    // Since test S3 key prefix represents a directory, we add a trailing '/' character to it.
    S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = S3FileTransferRequestParamsDto.builder().withS3BucketName(S3_BUCKET_NAME).withS3KeyPrefix(TEST_S3_KEY_PREFIX + "/").build();
    // Create a list of storage files.
    List<StorageFile> storageFiles = new ArrayList<>();
    for (String file : LOCAL_FILES) {
        storageFiles.add(new StorageFile(String.format(String.format("%s/%s", TEST_S3_KEY_PREFIX, file)), FILE_SIZE_1_KB, ROW_COUNT));
    }
    try {
        // Put relative S3 files into the S3 bucket.
        for (StorageFile storageFile : storageFiles) {
            s3Operations.putObject(new PutObjectRequest(S3_BUCKET_NAME, storageFile.getFilePath(), new ByteArrayInputStream(new byte[(int) FILE_SIZE_1_KB]), null), null);
        }
        // Execute a storage policy transition.
        storagePolicyProcessorHelperService.executeStoragePolicyTransition(new StoragePolicyTransitionParamsDto(new BusinessObjectDataKey(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, PARTITION_VALUE, NO_SUBPARTITION_VALUES, DATA_VERSION), STORAGE_NAME, NO_S3_ENDPOINT, S3_BUCKET_NAME, TEST_S3_KEY_PREFIX, NO_STORAGE_UNIT_STATUS, NO_STORAGE_UNIT_STATUS, storageFiles, S3_ARCHIVE_TO_GLACIER_TAG_KEY, S3_ARCHIVE_TO_GLACIER_TAG_VALUE, S3_OBJECT_TAGGER_ROLE_ARN, S3_OBJECT_TAGGER_ROLE_SESSION_NAME));
        // Validate that all S3 files are now tagged.
        for (StorageFile storageFile : storageFiles) {
            GetObjectTaggingResult getObjectTaggingResult = s3Operations.getObjectTagging(new GetObjectTaggingRequest(S3_BUCKET_NAME, storageFile.getFilePath()), null);
            assertEquals(Arrays.asList(new Tag(S3_ARCHIVE_TO_GLACIER_TAG_KEY, S3_ARCHIVE_TO_GLACIER_TAG_VALUE)), getObjectTaggingResult.getTagSet());
        }
    } finally {
        // Delete test files from S3 storage.
        if (!s3Dao.listDirectory(s3FileTransferRequestParamsDto).isEmpty()) {
            s3Dao.deleteDirectory(s3FileTransferRequestParamsDto);
        }
        s3Operations.rollback();
    }
}
Also used : GetObjectTaggingRequest(com.amazonaws.services.s3.model.GetObjectTaggingRequest) S3FileTransferRequestParamsDto(org.finra.herd.model.dto.S3FileTransferRequestParamsDto) ByteArrayInputStream(java.io.ByteArrayInputStream) StorageFile(org.finra.herd.model.api.xml.StorageFile) ArrayList(java.util.ArrayList) StoragePolicyTransitionParamsDto(org.finra.herd.model.dto.StoragePolicyTransitionParamsDto) Tag(com.amazonaws.services.s3.model.Tag) BusinessObjectDataKey(org.finra.herd.model.api.xml.BusinessObjectDataKey) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) GetObjectTaggingResult(com.amazonaws.services.s3.model.GetObjectTaggingResult)

Example 55 with Tag

use of software.amazon.awssdk.services.s3.model.Tag in project cloudbreak by hortonworks.

the class AwsMetadataCollector method getTag.

private String getTag(Instance instance) {
    for (Tag tag : instance.getTags()) {
        if (TAG_NAME.equals(tag.getKey())) {
            String value = tag.getValue();
            LOGGER.info("Instance: {} was already tagged: {}", instance.getInstanceId(), value);
            return value;
        }
    }
    return null;
}
Also used : Tag(com.amazonaws.services.ec2.model.Tag)

Aggregations

Tag (com.amazonaws.services.ec2.model.Tag)38 ArrayList (java.util.ArrayList)27 Tag (com.amazonaws.services.s3.model.Tag)18 HashMap (java.util.HashMap)17 Test (org.junit.Test)16 Instance (com.amazonaws.services.ec2.model.Instance)15 List (java.util.List)15 S3FileTransferRequestParamsDto (org.finra.herd.model.dto.S3FileTransferRequestParamsDto)14 File (java.io.File)11 Map (java.util.Map)10 GetObjectTaggingRequest (com.amazonaws.services.s3.model.GetObjectTaggingRequest)9 GetObjectTaggingResult (com.amazonaws.services.s3.model.GetObjectTaggingResult)9 HashSet (java.util.HashSet)9 Utils (com.vmware.xenon.common.Utils)8 CreateTagsRequest (com.amazonaws.services.ec2.model.CreateTagsRequest)7 Reservation (com.amazonaws.services.ec2.model.Reservation)7 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)5 TagsUtil.newTagState (com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState)5 TagState (com.vmware.photon.controller.model.resources.TagService.TagState)5 DeferredResult (com.vmware.xenon.common.DeferredResult)5