Skip to content

Commit

Permalink
Fixed more RFS version* SonarQube
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Helma <chelma+github@amazon.com>
  • Loading branch information
chelma committed Sep 19, 2024
1 parent d2dce08 commit a7106a8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.fasterxml.jackson.databind.node.ObjectNode;
Expand Down Expand Up @@ -109,8 +110,6 @@ private List<String> createTemplates(
IClusterMetadataContext context
) {

var templatesToCreate = new HashMap<String, ObjectNode>();
var templateList = new ArrayList<String>();
log.info("Setting {} ...", templateType);

if (templates == null) {
Expand All @@ -121,7 +120,17 @@ private List<String> createTemplates(
if (templateAllowlist != null && templateAllowlist.isEmpty()) {
log.info("No {} in specified allowlist", templateType);
return List.of();
} else if (templateAllowlist != null) {
}

var templatesToCreate = getTemplatesToCreate(templates, templateAllowlist, templateType);

return processTemplateCreation(templatesToCreate, templateType, mode, context);
}

private Map<String, ObjectNode> getTemplatesToCreate(ObjectNode templates, List<String> templateAllowlist, TemplateTypes templateType) {
var templatesToCreate = new HashMap<String, ObjectNode>();

if (templateAllowlist != null) {
for (String templateName : templateAllowlist) {
if (!templates.has(templateName) || templates.get(templateName) == null) {
log.warn("{} not found: {}", templateType, templateName);
Expand All @@ -131,23 +140,29 @@ private List<String> createTemplates(
templatesToCreate.put(templateName, settings);
}
} else {
// Get the template names
List<String> templateKeys = new ArrayList<>();
templates.fieldNames().forEachRemaining(templateKeys::add);

// Create each template
for (String templateName : templateKeys) {
templates.fieldNames().forEachRemaining(templateName -> {
ObjectNode settings = (ObjectNode) templates.get(templateName);
templatesToCreate.put(templateName, settings);
}
});
}

return templatesToCreate;
}

private List<String> processTemplateCreation(
Map<String, ObjectNode> templatesToCreate,
TemplateTypes templateType,
MigrationMode mode,
IClusterMetadataContext context
) {

List<String> templateList = new ArrayList<>();

templatesToCreate.forEach((templateName, templateBody) -> {
log.info("Creating {}: {}", templateType, templateName);

if (mode == MigrationMode.SIMULATE) {
var alreadyExists = templateType.alreadyExistsCheck.templateAlreadyExists(client, templateName);
if (!alreadyExists) {
if (!templateType.alreadyExistsCheck.templateAlreadyExists(client, templateName)) {
templateList.add(templateName);
} else {
log.warn("Template {} already exists on the target, it will not be created during a migration", templateName);
Expand Down
44 changes: 28 additions & 16 deletions RFS/src/main/java/com/rfs/version_universal/RemoteReaderClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,31 +117,43 @@ Mono<ObjectNode> getJsonForTemplateApis(HttpResponse resp) {
if (resp.statusCode != 200) {
return Mono.error(new OperationFailed("Unexpected status code " + resp.statusCode, resp));
}

try {
var tree = (ObjectNode) objectMapper.readTree(resp.body);

if (tree.size() == 1) {
var dearrayed = objectMapper.createObjectNode();
// This is OK because there is only a single item in this collection
var fieldName = tree.fieldNames().next();
var arrayOfItems = tree.get(fieldName);
for (var child : arrayOfItems) {
var node = (ObjectNode)child;
if (node.size() == 2) {
var fields = node.fieldNames();
var f1 = fields.next();
var f2 = fields.next();
var itemName = node.get(f1).isTextual() ? node.get(f1).asText() : node.get(f2).asText();
var detailsNode = !node.get(f1).isTextual() ? node.get(f1) : node.get(f2);
dearrayed.set(itemName, detailsNode);
}
}
return Mono.just(dearrayed);
return Mono.just(handleSingleItemTree(tree));
}

return Mono.just(tree);
} catch (Exception e) {
return logAndReturnJsonError(e, resp);
}
}

private ObjectNode handleSingleItemTree(ObjectNode tree) {
var dearrayed = objectMapper.createObjectNode();
var fieldName = tree.fieldNames().next();
var arrayOfItems = tree.get(fieldName);

for (var child : arrayOfItems) {
processChildNode((ObjectNode) child, dearrayed);
}

return dearrayed;
}

private void processChildNode(ObjectNode node, ObjectNode dearrayed) {
if (node.size() == 2) {
var fields = node.fieldNames();
var f1 = fields.next();
var f2 = fields.next();
var itemName = node.get(f1).isTextual() ? node.get(f1).asText() : node.get(f2).asText();
var detailsNode = !node.get(f1).isTextual() ? node.get(f1) : node.get(f2);

dearrayed.set(itemName, detailsNode);
}
}

Mono<ObjectNode> logAndReturnJsonError(Exception e, HttpResponse resp) {
String errorPrefix = "Unable to get json response: ";
Expand Down

0 comments on commit a7106a8

Please sign in to comment.