Search in sources :

Example 41 with Json

use of com.sequenceiq.cloudbreak.common.json.Json in project cloudbreak by hortonworks.

the class GatewayTopologyToGatewayTopologyV4ResponseConverterTest method testConvertWithNoServicesAndException.

@Test
public void testConvertWithNoServicesAndException() throws IOException {
    GatewayTopology source = mock(GatewayTopology.class);
    when(source.getTopologyName()).thenReturn(TOPOLOGY);
    Json json = mock(Json.class);
    when(source.getExposedServices()).thenReturn(json);
    when(json.getValue()).thenReturn("{}");
    when(json.get(ExposedServices.class)).thenThrow(new IOException("Foo"));
    GatewayTopologyV4Response result = underTest.convert(source);
    assertNotNull(result);
    assertEquals(TOPOLOGY, result.getTopologyName());
    assertNull(result.getExposedServices());
}
Also used : GatewayTopology(com.sequenceiq.cloudbreak.domain.stack.cluster.gateway.GatewayTopology) Json(com.sequenceiq.cloudbreak.common.json.Json) IOException(java.io.IOException) GatewayTopologyV4Response(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.cluster.gateway.topology.GatewayTopologyV4Response) Test(org.junit.Test)

Example 42 with Json

use of com.sequenceiq.cloudbreak.common.json.Json in project cloudbreak by hortonworks.

the class GatewayTopologyToGatewayTopologyV4ResponseConverterTest method testConvertWithSomeServices.

@Test
public void testConvertWithSomeServices() throws IOException {
    GatewayTopology source = mock(GatewayTopology.class);
    when(source.getTopologyName()).thenReturn(TOPOLOGY);
    ExposedServices exposedServices = mock(ExposedServices.class);
    List<String> services = List.of("SERVICE1", "SERVICE2");
    when(exposedServices.getServices()).thenReturn(services);
    Json json = mock(Json.class);
    when(source.getExposedServices()).thenReturn(json);
    when(json.getValue()).thenReturn("{}");
    when(json.get(ExposedServices.class)).thenReturn(exposedServices);
    GatewayTopologyV4Response result = underTest.convert(source);
    assertNotNull(result);
    assertEquals(TOPOLOGY, result.getTopologyName());
    assertEquals(services, result.getExposedServices());
}
Also used : ExposedServices(com.sequenceiq.cloudbreak.domain.stack.cluster.gateway.ExposedServices) GatewayTopology(com.sequenceiq.cloudbreak.domain.stack.cluster.gateway.GatewayTopology) Json(com.sequenceiq.cloudbreak.common.json.Json) GatewayTopologyV4Response(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.cluster.gateway.topology.GatewayTopologyV4Response) Test(org.junit.Test)

Example 43 with Json

use of com.sequenceiq.cloudbreak.common.json.Json in project cloudbreak by hortonworks.

the class InteractiveCredentialCreationStatusHandler method accept.

@Override
public void accept(Event<InteractiveCredentialCreationStatus> interactiveCredentialCreationStatusEvent) {
    InteractiveCredentialCreationStatus interactiveCredentialCreationStatus = interactiveCredentialCreationStatusEvent.getData();
    String userCrn = interactiveCredentialCreationStatus.getExtendedCloudCredential().getUserCrn();
    ThreadBasedUserCrnProvider.doAs(userCrn, () -> {
        String message = interactiveCredentialCreationStatus.getMessage();
        InteractiveCredentialNotification notification = new InteractiveCredentialNotification().withEventTimestamp(new Date().getTime()).withUserId(interactiveCredentialCreationStatus.getExtendedCloudCredential().getUserCrn()).withCloud(interactiveCredentialCreationStatus.getExtendedCloudCredential().getCloudPlatform()).withEventMessage(message);
        ResourceEvent event;
        if (interactiveCredentialCreationStatus.isError()) {
            event = CREDENTIAL_AZURE_INTERACTIVE_FAILED;
            notification.withEventType(event.name());
            LOGGER.info("Interactive credential creation failed status: {}", new Json(notification).getValue());
        } else {
            event = CREDENTIAL_AZURE_INTERACTIVE_STATUS;
            notification.withEventType(event.name());
            LOGGER.info("Interactive credential creation success status: {}", new Json(notification).getValue());
        }
        notificationService.send(event, notification, ThreadBasedUserCrnProvider.getUserCrn());
    });
}
Also used : InteractiveCredentialCreationStatus(com.sequenceiq.cloudbreak.cloud.event.credential.InteractiveCredentialCreationStatus) ResourceEvent(com.sequenceiq.cloudbreak.event.ResourceEvent) Json(com.sequenceiq.cloudbreak.common.json.Json) Date(java.util.Date)

Example 44 with Json

use of com.sequenceiq.cloudbreak.common.json.Json in project cloudbreak by hortonworks.

the class CredentialService method updateAuthorizationCodeOfAzureCredential.

private void updateAuthorizationCodeOfAzureCredential(Credential credential, String authorizationCode) {
    try {
        CredentialAttributes credentialAttributes = new Json(credential.getAttributes()).get(CredentialAttributes.class);
        if (credentialAttributes.getAzure() != null && credentialAttributes.getAzure().getCodeGrantFlowBased() != null) {
            CodeGrantFlowAttributes codeGrantFlowAttributes = credentialAttributes.getAzure().getCodeGrantFlowBased();
            codeGrantFlowAttributes.setAuthorizationCode(authorizationCode);
            credential.setAttributes(new Json(credentialAttributes).getValue());
        }
    } catch (IOException e) {
        LOGGER.info("Attributes of credential '{}' could not get from JSON attributes", credential.getName());
    }
}
Also used : CredentialAttributes(com.sequenceiq.environment.credential.attributes.CredentialAttributes) CodeGrantFlowAttributes(com.sequenceiq.environment.credential.attributes.azure.CodeGrantFlowAttributes) Json(com.sequenceiq.cloudbreak.common.json.Json) IOException(java.io.IOException)

Example 45 with Json

use of com.sequenceiq.cloudbreak.common.json.Json in project cloudbreak by hortonworks.

the class CredentialService method create.

public Credential create(Credential credential, @Nonnull String accountId, @Nonnull String creatorUserCrn, CredentialType type) {
    repository.findByNameAndAccountId(credential.getName(), accountId, getEnabledPlatforms(), type).map(Credential::getName).ifPresent(name -> {
        throw new BadRequestException("Credential already exists with name: " + name);
    });
    LOGGER.debug("Validating credential for cloudPlatform {} and creator {}.", credential.getCloudPlatform(), creatorUserCrn);
    credentialValidator.validateCredentialCloudPlatform(credential.getCloudPlatform(), creatorUserCrn, type);
    LOGGER.debug("Validating credential parameters for cloudPlatform {} and creator {}.", credential.getCloudPlatform(), creatorUserCrn);
    credentialValidator.validateParameters(Platform.platform(credential.getCloudPlatform()), new Json(credential.getAttributes()));
    String credentialCrn = createCRN(accountId);
    credential.setResourceCrn(credentialCrn);
    credential.setCreator(creatorUserCrn);
    credential.setAccountId(accountId);
    Credential verifiedCredential = credentialAdapter.verify(credential, accountId, Boolean.TRUE).getCredential();
    if (verifiedCredential.getVerificationStatusText() != null) {
        throw new BadRequestException(verifiedCredential.getVerificationStatusText());
    }
    try {
        Credential createdCredential = transactionService.required(() -> {
            Credential created = repository.save(verifiedCredential);
            ownerAssignmentService.assignResourceOwnerRoleIfEntitled(creatorUserCrn, credentialCrn, accountId);
            return created;
        });
        sendCredentialNotification(createdCredential, ResourceEvent.CREDENTIAL_CREATED);
        return createdCredential;
    } catch (TransactionService.TransactionExecutionException e) {
        LOGGER.error("Error happened during credential creation: ", e);
        throw new InternalServerErrorException(e);
    }
}
Also used : Credential(com.sequenceiq.environment.credential.domain.Credential) TransactionService(com.sequenceiq.cloudbreak.common.service.TransactionService) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) Json(com.sequenceiq.cloudbreak.common.json.Json)

Aggregations

Json (com.sequenceiq.cloudbreak.common.json.Json)266 Test (org.junit.jupiter.api.Test)95 HashMap (java.util.HashMap)49 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)31 Template (com.sequenceiq.freeipa.entity.Template)26 AwsInstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.instance.AwsInstanceTemplate)25 List (java.util.List)24 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)24 AzureInstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.instance.AzureInstanceTemplate)23 Map (java.util.Map)22 Stack (com.sequenceiq.cloudbreak.domain.stack.Stack)21 InstanceMetaData (com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData)21 ArrayList (java.util.ArrayList)21 Test (org.junit.Test)21 InstanceGroup (com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup)20 IOException (java.io.IOException)20 Cluster (com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster)18 RestRequestDetails (com.sequenceiq.cloudbreak.structuredevent.event.rest.RestRequestDetails)16 DetailedEnvironmentResponse (com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse)16 BadRequestException (com.sequenceiq.cloudbreak.common.exception.BadRequestException)14