Search in sources :

Example 51 with Json

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

the class CloudResourceToResourceConverter method convert.

public Resource convert(CloudResource source) {
    Resource domainResource = new Resource();
    domainResource.setResourceType(source.getType());
    domainResource.setResourceName(source.getName());
    domainResource.setResourceReference(source.getReference());
    domainResource.setResourceStatus(source.getStatus());
    domainResource.setInstanceGroup(source.getGroup());
    domainResource.setInstanceId(source.getInstanceId());
    domainResource.setAvailabilityZone(source.getAvailabilityZone());
    Optional.ofNullable(source.getParameters().get(CloudResource.ATTRIBUTES)).ifPresent(attributes -> {
        try {
            Json attributesJson = new Json(attributes);
            domainResource.setAttributes(attributesJson);
        } catch (IllegalArgumentException e) {
            LOGGER.info("Failed to parse resource attributes. Attributes: [{}]", source.getStringParameter(CloudResource.ATTRIBUTES), e);
            throw new IllegalStateException("Cannot parse stored resource attributes");
        }
    });
    return domainResource;
}
Also used : CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Resource(com.sequenceiq.freeipa.entity.Resource) Json(com.sequenceiq.cloudbreak.common.json.Json)

Example 52 with Json

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

the class CDPRestCommonService method collectCrnAndNameIfPresent.

public Map<String, String> collectCrnAndNameIfPresent(RestCallDetails restCallDetails, CDPOperationDetails operationDetails, Map<String, String> restParams, String nameField, String crnField) {
    Map<String, String> params = new HashMap<>();
    RestRequestDetails restRequest = restCallDetails.getRestRequest();
    Json requestJson = getJson(restRequest.getBody());
    Json responseJson = getJson(restCallDetails.getRestResponse().getBody());
    Map<String, String> copyRestParams = new HashMap<>(restParams);
    copyRestParams.putAll(collectFromCrnOrNameProvider(restCallDetails, operationDetails, restParams, nameField, crnField));
    String resourceCrn = getCrn(requestJson, responseJson, operationDetails, copyRestParams, crnField);
    String name = getName(requestJson, responseJson, operationDetails, copyRestParams, nameField);
    checkNameOrCrnProvided(restRequest, resourceCrn, name);
    addNameAndCrnIfNotEmpty(nameField, crnField, params, resourceCrn, name);
    return params;
}
Also used : HashMap(java.util.HashMap) RestRequestDetails(com.sequenceiq.cloudbreak.structuredevent.event.rest.RestRequestDetails) Json(com.sequenceiq.cloudbreak.common.json.Json)

Example 53 with Json

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

the class CDPRestCommonService method getValueFromJson.

private String getValueFromJson(Json json, List<String> paths, Map<String, String> restParams, String idType) {
    String values = null;
    if (json.isArray() && idType.equals(restParams.get(ID_TYPE))) {
        List<String> asList = json.asArray();
        values = String.join(",", asList);
    } else if (json.isObject() && json.getMap().containsKey("responses")) {
        values = ((Collection<Object>) json.getMap().get("responses")).stream().map(obj -> (String) new Json(obj).getMap().get(idType)).collect(Collectors.joining(","));
    } else if (json.isObject()) {
        values = getFirstPath(json, paths);
    }
    return values;
}
Also used : RESOURCE_TYPE(com.sequenceiq.cloudbreak.structuredevent.rest.urlparser.CDPRestUrlParser.RESOURCE_TYPE) ID_TYPE(com.sequenceiq.cloudbreak.structuredevent.rest.urlparser.CDPRestUrlParser.ID_TYPE) Collection(java.util.Collection) HashMap(java.util.HashMap) JsonUtil(com.sequenceiq.cloudbreak.common.json.JsonUtil) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) Inject(javax.inject.Inject) Json(com.sequenceiq.cloudbreak.common.json.Json) List(java.util.List) Component(org.springframework.stereotype.Component) RestCallDetails(com.sequenceiq.cloudbreak.structuredevent.event.rest.RestCallDetails) RestRequestDetails(com.sequenceiq.cloudbreak.structuredevent.event.rest.RestRequestDetails) Map(java.util.Map) Optional(java.util.Optional) AnonymizerUtil(com.sequenceiq.cloudbreak.common.anonymizer.AnonymizerUtil) CDPOperationDetails(com.sequenceiq.cloudbreak.structuredevent.event.cdp.CDPOperationDetails) Collections(java.util.Collections) Collection(java.util.Collection) Json(com.sequenceiq.cloudbreak.common.json.Json)

Example 54 with Json

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

the class CDPRestCommonServiceTest method testWhenNameAndResourceCrnComeFromTheMapParameterButExistInRequestAndResponse.

@Test
public void testWhenNameAndResourceCrnComeFromTheMapParameterButExistInRequestAndResponse() {
    Map<String, String> restParams = Map.of(CLUSTER_NAME, "name1", CLUSTER_CRN, "crn1");
    RestCallDetails restCallDetails = new RestCallDetails();
    RestRequestDetails request = new RestRequestDetails();
    request.setBody(new Json(Map.of("name", "name2", "resourceCrn", "crn2")).getValue());
    RestResponseDetails response = new RestResponseDetails();
    response.setBody(new Json(Map.of("name", "name3", "resourceCrn", "crn3")).getValue());
    restCallDetails.setRestRequest(request);
    restCallDetails.setRestResponse(response);
    Map<String, String> expected = new HashMap<>();
    expected.put(CLUSTER_NAME, "name1");
    expected.put(CLUSTER_CRN, "crn1");
    Map<String, String> actual = underTest.collectCrnAndNameIfPresent(restCallDetails, null, restParams, CLUSTER_NAME, CLUSTER_CRN);
    assertEquals(expected, actual);
}
Also used : RestCallDetails(com.sequenceiq.cloudbreak.structuredevent.event.rest.RestCallDetails) HashMap(java.util.HashMap) RestRequestDetails(com.sequenceiq.cloudbreak.structuredevent.event.rest.RestRequestDetails) Json(com.sequenceiq.cloudbreak.common.json.Json) RestResponseDetails(com.sequenceiq.cloudbreak.structuredevent.event.rest.RestResponseDetails) Test(org.junit.jupiter.api.Test)

Example 55 with Json

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

the class CDPRestCommonServiceTest method testWhenNameAndResourceCrnComeFromTheOperationDetails.

@Test
public void testWhenNameAndResourceCrnComeFromTheOperationDetails() {
    RestCallDetails restCallDetails = new RestCallDetails();
    RestRequestDetails request = new RestRequestDetails();
    RestResponseDetails response = new RestResponseDetails();
    response.setBody(new Json(Map.of("responses", List.of(Map.of("name", "name1", "crn", "crn1"), Map.of("name", "name2", "crn", "crn2")))).getValue());
    CDPOperationDetails operationDetails = new CDPOperationDetails();
    operationDetails.setResourceCrn("opCrn");
    operationDetails.setResourceName("opName");
    restCallDetails.setRestRequest(request);
    restCallDetails.setRestResponse(response);
    Map<String, Object> expected = new HashMap<>();
    expected.put("names", "opName");
    expected.put("crns", "opCrn");
    Map<String, String> actual = underTest.collectCrnAndNameIfPresent(restCallDetails, operationDetails, Collections.emptyMap(), "names", "crns");
    assertEquals(expected, actual);
}
Also used : RestCallDetails(com.sequenceiq.cloudbreak.structuredevent.event.rest.RestCallDetails) HashMap(java.util.HashMap) RestRequestDetails(com.sequenceiq.cloudbreak.structuredevent.event.rest.RestRequestDetails) Json(com.sequenceiq.cloudbreak.common.json.Json) CDPOperationDetails(com.sequenceiq.cloudbreak.structuredevent.event.cdp.CDPOperationDetails) RestResponseDetails(com.sequenceiq.cloudbreak.structuredevent.event.rest.RestResponseDetails) Test(org.junit.jupiter.api.Test)

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