Search in sources :

Example 1 with ComponentCandidate

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

the class ComponentServiceHelperTest method GIVEN_component_version_requirements_WHEN_resolve_component_version_THEN_send_service_request.

@Test
void GIVEN_component_version_requirements_WHEN_resolve_component_version_THEN_send_service_request() throws Exception {
    Map<String, Requirement> versionRequirements = new HashMap<>();
    versionRequirements.put("X", Requirement.buildNPM("^1.0"));
    versionRequirements.put("Y", Requirement.buildNPM("^1.5"));
    ResolvedComponentVersion componentVersion = ResolvedComponentVersion.builder().componentName(COMPONENT_A).componentVersion(v1_0_0.getValue()).recipe(SdkBytes.fromByteArray("new recipe".getBytes(Charsets.UTF_8))).build();
    ResolveComponentCandidatesResponse result = ResolveComponentCandidatesResponse.builder().resolvedComponentVersions(Collections.singletonList(componentVersion)).build();
    when(client.resolveComponentCandidates(any(ResolveComponentCandidatesRequest.class))).thenReturn(result);
    ResolvedComponentVersion componentVersionReturn = helper.resolveComponentVersion(COMPONENT_A, v1_0_0, versionRequirements);
    assertThat(componentVersionReturn, is(componentVersion));
    ArgumentCaptor<ResolveComponentCandidatesRequest> requestArgumentCaptor = ArgumentCaptor.forClass(ResolveComponentCandidatesRequest.class);
    verify(client).resolveComponentCandidates(requestArgumentCaptor.capture());
    ResolveComponentCandidatesRequest request = requestArgumentCaptor.getValue();
    assertThat(request.platform(), notNullValue());
    assertThat(request.platform().attributes(), notNullValue());
    Map<String, String> attributes = request.platform().attributes();
    assertThat(attributes, hasKey(PlatformResolver.OS_KEY));
    assertThat(attributes, hasKey(PlatformResolver.ARCHITECTURE_KEY));
    assertThat(request.componentCandidates().size(), is(1));
    ComponentCandidate candidate = request.componentCandidates().get(0);
    assertThat(candidate.componentName(), is(COMPONENT_A));
    assertThat(candidate.componentVersion(), is("1.0.0"));
    assertThat(candidate.versionRequirements(), IsMapContaining.hasEntry("X", ">=1.0.0 <2.0.0"));
    assertThat(candidate.versionRequirements(), IsMapContaining.hasEntry("Y", ">=1.5.0 <2.0.0"));
}
Also used : ResolvedComponentVersion(software.amazon.awssdk.services.greengrassv2data.model.ResolvedComponentVersion) Requirement(com.vdurmont.semver4j.Requirement) HashMap(java.util.HashMap) ResolveComponentCandidatesResponse(software.amazon.awssdk.services.greengrassv2data.model.ResolveComponentCandidatesResponse) ComponentCandidate(software.amazon.awssdk.services.greengrassv2data.model.ComponentCandidate) Matchers.containsString(org.hamcrest.Matchers.containsString) ResolveComponentCandidatesRequest(software.amazon.awssdk.services.greengrassv2data.model.ResolveComponentCandidatesRequest) Test(org.junit.jupiter.api.Test)

Example 2 with ComponentCandidate

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

the class ComponentServiceHelper method resolveComponentVersion.

/**
 * Resolve a component version with greengrass cloud service. The dependency resolution algorithm goes through the
 * dependencies node by node, so one component got resolve a time.
 *
 * @param componentName             component name to be resolve
 * @param localCandidateVersion     component local candidate version if available
 * @param versionRequirements       component dependents version requirement map
 * @return resolved component version and recipe
 * @throws NoAvailableComponentVersionException if no applicable version available in cloud service
 */
@SuppressWarnings("PMD.PreserveStackTrace")
ResolvedComponentVersion resolveComponentVersion(String componentName, Semver localCandidateVersion, Map<String, Requirement> versionRequirements) throws NoAvailableComponentVersionException {
    ComponentPlatform platform = ComponentPlatform.builder().attributes(platformResolver.getCurrentPlatform()).build();
    Map<String, String> versionRequirementsInString = versionRequirements.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().toString()));
    ComponentCandidate candidate = ComponentCandidate.builder().componentName(componentName).componentVersion(localCandidateVersion == null ? null : localCandidateVersion.getValue()).versionRequirements(versionRequirementsInString).build();
    ResolveComponentCandidatesRequest request = ResolveComponentCandidatesRequest.builder().platform(platform).componentCandidates(Collections.singletonList(candidate)).build();
    ResolveComponentCandidatesResponse result;
    try {
        result = clientFactory.getGreengrassV2DataClient().resolveComponentCandidates(request);
    } catch (ResourceNotFoundException e) {
        logger.atDebug().kv("componentName", componentName).kv("versionRequirements", versionRequirements).log("No applicable version found in cloud registry", e);
        throw new NoAvailableComponentVersionException("No cloud component version satisfies the requirements.", componentName, versionRequirements);
    }
    Validate.isTrue(result.resolvedComponentVersions() != null && result.resolvedComponentVersions().size() == 1, "Component service returns invalid response. It should have one resolved component version");
    return result.resolvedComponentVersions().get(0);
}
Also used : ComponentPlatform(software.amazon.awssdk.services.greengrassv2data.model.ComponentPlatform) ResolvedComponentVersion(software.amazon.awssdk.services.greengrassv2data.model.ResolvedComponentVersion) NoAvailableComponentVersionException(com.aws.greengrass.componentmanager.exceptions.NoAvailableComponentVersionException) GreengrassServiceClientFactory(com.aws.greengrass.util.GreengrassServiceClientFactory) Collectors(java.util.stream.Collectors) Inject(javax.inject.Inject) Requirement(com.vdurmont.semver4j.Requirement) Validate(org.apache.commons.lang3.Validate) Semver(com.vdurmont.semver4j.Semver) ResolveComponentCandidatesResponse(software.amazon.awssdk.services.greengrassv2data.model.ResolveComponentCandidatesResponse) Map(java.util.Map) ResourceNotFoundException(software.amazon.awssdk.services.greengrassv2data.model.ResourceNotFoundException) LogManager(com.aws.greengrass.logging.impl.LogManager) Collections(java.util.Collections) PlatformResolver(com.aws.greengrass.config.PlatformResolver) Logger(com.aws.greengrass.logging.api.Logger) ComponentCandidate(software.amazon.awssdk.services.greengrassv2data.model.ComponentCandidate) ResolveComponentCandidatesRequest(software.amazon.awssdk.services.greengrassv2data.model.ResolveComponentCandidatesRequest) ComponentPlatform(software.amazon.awssdk.services.greengrassv2data.model.ComponentPlatform) ResolveComponentCandidatesResponse(software.amazon.awssdk.services.greengrassv2data.model.ResolveComponentCandidatesResponse) NoAvailableComponentVersionException(com.aws.greengrass.componentmanager.exceptions.NoAvailableComponentVersionException) ComponentCandidate(software.amazon.awssdk.services.greengrassv2data.model.ComponentCandidate) ResourceNotFoundException(software.amazon.awssdk.services.greengrassv2data.model.ResourceNotFoundException) Map(java.util.Map) ResolveComponentCandidatesRequest(software.amazon.awssdk.services.greengrassv2data.model.ResolveComponentCandidatesRequest)

Aggregations

Requirement (com.vdurmont.semver4j.Requirement)2 ComponentCandidate (software.amazon.awssdk.services.greengrassv2data.model.ComponentCandidate)2 ResolveComponentCandidatesRequest (software.amazon.awssdk.services.greengrassv2data.model.ResolveComponentCandidatesRequest)2 ResolveComponentCandidatesResponse (software.amazon.awssdk.services.greengrassv2data.model.ResolveComponentCandidatesResponse)2 ResolvedComponentVersion (software.amazon.awssdk.services.greengrassv2data.model.ResolvedComponentVersion)2 NoAvailableComponentVersionException (com.aws.greengrass.componentmanager.exceptions.NoAvailableComponentVersionException)1 PlatformResolver (com.aws.greengrass.config.PlatformResolver)1 Logger (com.aws.greengrass.logging.api.Logger)1 LogManager (com.aws.greengrass.logging.impl.LogManager)1 GreengrassServiceClientFactory (com.aws.greengrass.util.GreengrassServiceClientFactory)1 Semver (com.vdurmont.semver4j.Semver)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Inject (javax.inject.Inject)1 Validate (org.apache.commons.lang3.Validate)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Test (org.junit.jupiter.api.Test)1 ComponentPlatform (software.amazon.awssdk.services.greengrassv2data.model.ComponentPlatform)1