Search in sources :

Example 1 with GetComponentVersionArtifactRequest

use of software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactRequest in project aws-greengrass-nucleus by aws-greengrass.

the class GreengrassRepositoryDownloaderTest method GIVEN_artifact_url_WHEN_attempt_download_THEN_task_succeed.

@Test
void GIVEN_artifact_url_WHEN_attempt_download_THEN_task_succeed() throws Exception {
    // build downloader
    Path mockArtifactPath = ComponentTestResourceHelper.getPathForTestPackage(ComponentTestResourceHelper.MONITORING_SERVICE_PACKAGE_NAME, "1.0.0").resolve("monitor_artifact_100.txt");
    String checksum = Base64.getEncoder().encodeToString(MessageDigest.getInstance(SHA256).digest(Files.readAllBytes(mockArtifactPath)));
    ComponentArtifact artifact = ComponentArtifact.builder().algorithm(SHA256).checksum(checksum).artifactUri(new URI("greengrass:774pP05xtua0RCcwj9uALSdAqGr_vC631EdOBkJxnec=/artifact.txt")).build();
    ComponentIdentifier pkgId = new ComponentIdentifier("CoolService", new Semver("1.0.0"));
    lenient().when(componentStore.getRecipeMetadata(pkgId)).thenReturn(new RecipeMetadata(TEST_ARN));
    Path testCache = ComponentTestResourceHelper.getPathForLocalTestCache();
    Path saveToPath = testCache.resolve("CoolService").resolve("1.0.0");
    Files.createDirectories(saveToPath);
    GreengrassRepositoryDownloader downloader = spy(new GreengrassRepositoryDownloader(clientFactory, pkgId, artifact, saveToPath, componentStore));
    assertThat(downloader.getArtifactFilename(), is("artifact.txt"));
    // mock requests to get downloadSize and local file name
    GetComponentVersionArtifactResponse result = GetComponentVersionArtifactResponse.builder().preSignedUrl("https://www.amazon.com/artifact.txt").build();
    when(client.getComponentVersionArtifact(getComponentVersionArtifactRequestArgumentCaptor.capture())).thenReturn(result);
    // mock requests to return partial stream
    doReturn(httpClient).when(downloader).getSdkHttpClient();
    doReturn(request).when(httpClient).prepareRequest(any());
    when(request.call()).thenReturn(HttpExecuteResponse.builder().response(SdkHttpResponse.builder().statusCode(HTTP_OK).putHeader(CONTENT_LENGTH_HEADER, String.valueOf(Files.size(mockArtifactPath))).build()).responseBody(AbortableInputStream.create(Files.newInputStream(mockArtifactPath))).build()).thenReturn(HttpExecuteResponse.builder().response(SdkHttpResponse.builder().statusCode(HTTP_PARTIAL).putHeader(CONTENT_LENGTH_HEADER, String.valueOf(Files.size(mockArtifactPath))).build()).responseBody(AbortableInputStream.create(Files.newInputStream(mockArtifactPath))).build());
    downloader.download();
    GetComponentVersionArtifactRequest generatedRequest = getComponentVersionArtifactRequestArgumentCaptor.getValue();
    assertEquals(TEST_ARN, generatedRequest.arn());
    assertEquals("774pP05xtua0RCcwj9uALSdAqGr_vC631EdOBkJxnec=/artifact.txt", generatedRequest.artifactName());
    byte[] originalFile = Files.readAllBytes(mockArtifactPath);
    Path artifactFilePath = saveToPath.resolve("artifact.txt");
    byte[] downloadFile = Files.readAllBytes(artifactFilePath);
    assertThat(Arrays.equals(originalFile, downloadFile), is(true));
    ComponentTestResourceHelper.cleanDirectory(testCache);
}
Also used : Path(java.nio.file.Path) RecipeMetadata(com.aws.greengrass.componentmanager.models.RecipeMetadata) ComponentIdentifier(com.aws.greengrass.componentmanager.models.ComponentIdentifier) GetComponentVersionArtifactResponse(software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactResponse) ComponentArtifact(com.aws.greengrass.componentmanager.models.ComponentArtifact) Semver(com.vdurmont.semver4j.Semver) URI(java.net.URI) GetComponentVersionArtifactRequest(software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactRequest) Test(org.junit.jupiter.api.Test)

Example 2 with GetComponentVersionArtifactRequest

use of software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactRequest in project aws-greengrass-nucleus by aws-greengrass.

the class GreengrassRepositoryDownloader method getArtifactDownloadURL.

@SuppressWarnings({ "PMD.AvoidCatchingGenericException", "PMD.AvoidRethrowingException" })
private String getArtifactDownloadURL(ComponentIdentifier componentIdentifier, String artifactName) throws InterruptedException, PackageDownloadException {
    String arn;
    try {
        arn = componentStore.getRecipeMetadata(componentIdentifier).getComponentVersionArn();
    } catch (PackageLoadingException e) {
        throw new PackageDownloadException(getErrorString("Failed to get component version arn from component store"), e);
    }
    try {
        return RetryUtils.runWithRetry(clientExceptionRetryConfig, () -> {
            GetComponentVersionArtifactRequest getComponentArtifactRequest = GetComponentVersionArtifactRequest.builder().artifactName(artifactName).arn(arn).build();
            GetComponentVersionArtifactResponse getComponentArtifactResult = clientFactory.getGreengrassV2DataClient().getComponentVersionArtifact(getComponentArtifactRequest);
            return getComponentArtifactResult.preSignedUrl();
        }, "get-artifact-size", logger);
    } catch (InterruptedException e) {
        throw e;
    } catch (Exception e) {
        throw new PackageDownloadException(getErrorString("Failed to get download size"), e);
    }
}
Also used : GetComponentVersionArtifactResponse(software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactResponse) PackageDownloadException(com.aws.greengrass.componentmanager.exceptions.PackageDownloadException) PackageLoadingException(com.aws.greengrass.componentmanager.exceptions.PackageLoadingException) GetComponentVersionArtifactRequest(software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactRequest) PackageLoadingException(com.aws.greengrass.componentmanager.exceptions.PackageLoadingException) PackageDownloadException(com.aws.greengrass.componentmanager.exceptions.PackageDownloadException) IOException(java.io.IOException) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException)

Aggregations

GetComponentVersionArtifactRequest (software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactRequest)2 GetComponentVersionArtifactResponse (software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactResponse)2 PackageDownloadException (com.aws.greengrass.componentmanager.exceptions.PackageDownloadException)1 PackageLoadingException (com.aws.greengrass.componentmanager.exceptions.PackageLoadingException)1 ComponentArtifact (com.aws.greengrass.componentmanager.models.ComponentArtifact)1 ComponentIdentifier (com.aws.greengrass.componentmanager.models.ComponentIdentifier)1 RecipeMetadata (com.aws.greengrass.componentmanager.models.RecipeMetadata)1 Semver (com.vdurmont.semver4j.Semver)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Path (java.nio.file.Path)1 Test (org.junit.jupiter.api.Test)1 SdkClientException (software.amazon.awssdk.core.exception.SdkClientException)1