Skip to content

Commit

Permalink
MID-8842 ninja - warning when updating files (with skip option)
Browse files Browse the repository at this point in the history
(cherry picked from commit 5d634f6)
  • Loading branch information
1azyman committed Jul 19, 2023
1 parent 9bcbfb6 commit 9718f66
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;

/**
* // todo doesn't take into account "skipped" items from verification phase
* Handles upgrade of single object, filters out items that are not applicable for upgrade based on options selected by user.
*/
public class UpgradeObjectHandler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;

import com.evolveum.midpoint.ninja.util.ConsoleFormat;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
Expand All @@ -23,9 +25,6 @@
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;

// todo handle initial objects somehow
// compare vanilla previous with vanilla new ones and also vanilla previous with current in MP repository,
// apply only non conflicting delta items, report it to user
public class UpgradeObjectsAction extends AbstractRepositorySearchAction<UpgradeObjectsOptions, Void> {

private Map<UUID, Set<String>> skipUpgradeForOids;
Expand All @@ -52,14 +51,15 @@ public Void execute() throws Exception {
log.info("Upgrade will skip {} objects", skipUpgradeForOids.size());

if (!options.getFiles().isEmpty()) {
// todo this is another check whether we want to upgrade objects in files
// log.info(ConsoleFormat.formatWarn("WARNING: File update will remove XML comments and change formatting. Do you wish to proceed? (Y/n)"));
// String result = NinjaUtils.readInput(input -> StringUtils.isEmpty(input) || input.equalsIgnoreCase("y"));
//
// if (result.trim().equalsIgnoreCase("n")) {
// log.info("Upgrade aborted");
// return null;
// }
if (!options.isSkipUpgradeWarning()) {
log.info(ConsoleFormat.formatWarn("WARNING: File update will remove XML comments and change formatting. Do you wish to proceed? (Y/n)"));
String result = NinjaUtils.readInput(input -> StringUtils.isEmpty(input) || input.equalsIgnoreCase("y"));

if (result.trim().equalsIgnoreCase("n")) {
log.info("Upgrade aborted");
return null;
}
}

return upgradeObjectsInFiles();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class UpgradeObjectsOptions extends ExportOptions {
public static final String P_PHASE = "--upgrade-phase";
public static final String P_TYPE = "--upgrade-type";
public static final String P_PRIORITY = "--upgrade-priority";
public static final String P_SKIP_UPGRADE_WARNING = "--skip-upgrade-warning";

// @ParametersDelegate
// private OutputOptions outputOptions = new OutputOptions();
Expand All @@ -50,6 +51,9 @@ public class UpgradeObjectsOptions extends ExportOptions {
@Parameter(names = { P_PRIORITY }, descriptionKey = "upgradeObjects.priorities", variableArity = true)
private List<UpgradePriority> priorities = new ArrayList<>();

@Parameter(names = { P_SKIP_UPGRADE_WARNING }, descriptionKey = "upgradeObjects.skipUpgradeWarning")
private boolean skipUpgradeWarning;

public File getVerification() {
return verification;
}
Expand Down Expand Up @@ -101,4 +105,12 @@ public List<UpgradePriority> getPriorities() {
public void setPriorities(List<UpgradePriority> priorities) {
this.priorities = priorities;
}

public boolean isSkipUpgradeWarning() {
return skipUpgradeWarning;
}

public void setSkipUpgradeWarning(boolean skipUpgradeWarning) {
this.skipUpgradeWarning = skipUpgradeWarning;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
import java.util.Objects;
import java.util.UUID;

import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.schema.DeltaConversionOptions;
import com.evolveum.midpoint.schema.DeltaConvertor;

import com.evolveum.midpoint.util.exception.SchemaException;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
Expand Down Expand Up @@ -200,9 +206,38 @@ public <T extends Objectable> UpgradeValidationResult verify(Writer writer, Pris
throw new IllegalArgumentException("Unknown report style " + options.getReportStyle());
}

if (createDeltaFile) {
writeDeltaXml(result);
}

return result;
}

private void writeDeltaXml(UpgradeValidationResult result) {
result.getItems().stream().filter(item -> item.getDelta() != null).forEach(item -> {
try {
deltaWriter.write(DeltaConvertor.serializeDelta(
(ObjectDelta) item.getDelta(), DeltaConversionOptions.createSerializeReferenceNames(), "xml"));
} catch (SchemaException | IOException ex) {
// todo error handling
ex.printStackTrace();
}
});
}

public static String getItemPathFromRecord(CSVRecord record) {
if (record == null || record.size() != REPORT_HEADER.size()) {
return "";
}

String path = record.get(4);
if (StringUtils.isBlank(path)) {
return "";
}

return path.trim();
}

public static String getIdentifierFromRecord(CSVRecord record) {
if (record == null || record.size() != REPORT_HEADER.size()) {
return null;
Expand Down
1 change: 1 addition & 0 deletions tools/ninja/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,4 @@ upgradeObjects.phases=Upgrade phase.
upgradeObjects.types=Upgrade type.
upgradeObjects.priorities=Upgrade priority.
downloadDistribution=Download target midPoint distribution (latest LTS).
upgradeObjects.skipUpgradeWarning=Skip warning about files being updated.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public void test200UpgradeFiles() throws Exception {
"upgrade-objects",
"--file", TARGET_FILES.getPath(),
"--verification-file", OUTPUT.getPath(),
"--skip-upgrade-warning",
"--upgrade-phase", "before");

then();
Expand Down

0 comments on commit 9718f66

Please sign in to comment.