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;
}
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;
}
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;
}
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);
}
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);
}
Aggregations