Example #1
0
  public ConfigUpdateResponse updateConfigFromUI(
      final UpdateConfigFromUI command,
      final String md5,
      Username username,
      final LocalizedOperationResult result) {
    UiBasedConfigUpdateCommand updateCommand =
        new UiBasedConfigUpdateCommand(md5, command, result, cachedGoPartials);
    UpdatedNodeSubjectResolver updatedConfigResolver = new UpdatedNodeSubjectResolver();
    try {
      ConfigSaveState configSaveState = updateConfig(updateCommand);
      return latestUpdateResponse(
          command, updateCommand, updatedConfigResolver, clonedConfigForEdit(), configSaveState);
    } catch (ConfigFileHasChangedException e) {
      CruiseConfig updatedConfig = handleMergeException(md5, updateCommand);
      result.conflict(LocalizedMessage.string("SAVE_FAILED_WITH_REASON", e.getMessage()));
      return latestUpdateResponse(
          command, updateCommand, new OldNodeSubjectResolver(), updatedConfig, null);
    } catch (ConfigUpdateCheckFailedException e) {
      // result is already set
    } catch (Exception e) {
      ConfigMergeException mergeException = ExceptionUtils.getCause(e, ConfigMergeException.class);
      ConfigMergePostValidationException mergePostValidationException =
          ExceptionUtils.getCause(e, ConfigMergePostValidationException.class);
      if (mergeException != null || mergePostValidationException != null) {
        CruiseConfig updatedConfig = handleMergeException(md5, updateCommand);
        result.conflict(LocalizedMessage.string("SAVE_FAILED_WITH_REASON", e.getMessage()));
        return latestUpdateResponse(
            command, updateCommand, new OldNodeSubjectResolver(), updatedConfig, null);
      }
      GoConfigInvalidException ex = ExceptionUtils.getCause(e, GoConfigInvalidException.class);
      if (ex != null) {
        CruiseConfig badConfig = ex.getCruiseConfig();
        setMD5(md5, badConfig);
        Validatable node = updatedConfigResolver.getNode(command, updateCommand.cruiseConfig());
        BasicCruiseConfig.copyErrors(command.updatedNode(badConfig), node);
        result.badRequest(LocalizedMessage.string("SAVE_FAILED"));
        return new ConfigUpdateResponse(
            badConfig,
            node,
            subjectFromNode(command, updatedConfigResolver, node),
            updateCommand,
            null);
      } else {
        result.badRequest(LocalizedMessage.string("SAVE_FAILED_WITH_REASON", e.getMessage()));
      }
    }

    CruiseConfig newConfigSinceNoOtherConfigExists = clonedConfigForEdit();
    setMD5(md5, newConfigSinceNoOtherConfigExists);
    return latestUpdateResponse(
        command,
        updateCommand,
        new OldNodeSubjectResolver(),
        newConfigSinceNoOtherConfigExists,
        null);
  }
Example #2
0
 private CruiseConfig handleMergeException(String md5, UiBasedConfigUpdateCommand updateCommand) {
   CruiseConfig updatedConfig;
   try {
     updateCommand.update(clonedConfigForEdit());
     updatedConfig = updateCommand.cruiseConfig();
   } catch (Exception oops) {
     // Ignore this. We are trying to retain the user's input. However, if things have changed so
     // massively that we cannot apply this update we cannot do anything.
     // But hey, at least we tried...
     updatedConfig = clonedConfigForEdit();
   }
   setMD5(md5, updatedConfig);
   return updatedConfig;
 }