use of com.thoughtworks.go.server.exceptions.RulesViolationException in project gocd by gocd.
the class PackageDefinitionServiceTest method shouldSetResultAsUnprocessableEntityIfRulesViolationForCheckConnection.
@Test
void shouldSetResultAsUnprocessableEntityIfRulesViolationForCheckConnection() {
PackageDefinition packageDefinition = PackageDefinitionMother.create("1", "name", new Configuration(), packageRepository);
HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
doThrow(new RulesViolationException("some rule violation message")).when(secretParamResolver).resolve(packageDefinition);
service.checkConnection(packageDefinition, result);
assertThat(result.isSuccessful()).isFalse();
assertThat(result.httpCode()).isEqualTo(HttpStatus.SC_UNPROCESSABLE_ENTITY);
assertThat(result.message()).isEqualTo("Package check Failed. Reason(s): some rule violation message");
}
use of com.thoughtworks.go.server.exceptions.RulesViolationException in project gocd by gocd.
the class StatusReportsController method agentStatusReport.
public ModelAndView agentStatusReport(Request request, Response response) throws Exception {
String pluginId = request.params("plugin_id");
String elasticAgentId = parseElasticAgentId(request);
String jobIdString = request.queryParams("job_id");
long jobId;
try {
jobId = Long.parseLong(jobIdString);
} catch (NumberFormatException e) {
return errorPage(response, HttpStatus.UNPROCESSABLE_ENTITY.value(), "Agent Status Report", "Please provide a valid job_id for Agent Status Report.");
}
try {
JobInstance jobInstance = jobInstanceService.buildById(jobId);
String agentStatusReport = elasticAgentPluginService.getAgentStatusReport(pluginId, jobInstance.getIdentifier(), elasticAgentId);
Map<Object, Object> object = new HashMap<>();
object.put("viewTitle", "Agent Status Report");
object.put("viewFromPlugin", agentStatusReport);
return new ModelAndView(object, "status_reports/index.ftlh");
} catch (RecordNotFoundException e) {
return errorPage(response, 404, "Agent Status Report", e.getMessage());
} catch (DataRetrievalFailureException | UnsupportedOperationException e) {
String message = String.format("Status Report for plugin with id: '%s' for agent '%s' is not found.", pluginId, elasticAgentId);
return errorPage(response, 404, "Agent Status Report", message);
} catch (RulesViolationException | SecretResolutionFailureException e) {
LOGGER.error(e.getMessage(), e);
return errorPage(response, 500, "Agent Status Report", e.getMessage());
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
return errorPage(response, 500, "Agent Status Report", UNKNOWN_ERROR_MESSAGE);
}
}
use of com.thoughtworks.go.server.exceptions.RulesViolationException in project gocd by gocd.
the class ClusterProfilesChangedPluginNotifier method onEntityConfigChange.
@Override
public void onEntityConfigChange(ClusterProfile updatedClusterProfile) {
try {
LOGGER.debug("Resolving secrets for updated cluster profile: {}", updatedClusterProfile);
secretParamResolver.resolve(updatedClusterProfile);
} catch (RulesViolationException | SecretResolutionFailureException e) {
logAndRaiseServerHealthMessage(updatedClusterProfile, e.getMessage());
return;
}
Map<String, String> updatedClusterConfigMap = updatedClusterProfile.getConfigurationAsMap(true, true);
if (goConfigService.getElasticConfig().getClusterProfiles().find(updatedClusterProfile.getId()) == null) {
registry.notifyPluginAboutClusterProfileChanged(updatedClusterProfile.getPluginId(), ClusterProfilesChangedStatus.DELETED, updatedClusterConfigMap, null);
updateClusterProfilesCopy();
return;
}
ClusterProfile oldClusterProfile = existingClusterProfiles.find(updatedClusterProfile.getId());
if (oldClusterProfile == null) {
registry.notifyPluginAboutClusterProfileChanged(updatedClusterProfile.getPluginId(), ClusterProfilesChangedStatus.CREATED, null, updatedClusterConfigMap);
updateClusterProfilesCopy();
return;
}
try {
LOGGER.debug("Resolving secrets for older cluster profile: {}", oldClusterProfile);
secretParamResolver.resolve(oldClusterProfile);
} catch (RulesViolationException | SecretResolutionFailureException e) {
logAndRaiseServerHealthMessage(oldClusterProfile, e.getMessage());
return;
}
// cluster profile has been updated without changing plugin id
Map<String, String> olderClusterConfigMap = oldClusterProfile.getConfigurationAsMap(true, true);
if (oldClusterProfile.getPluginId().equals(updatedClusterProfile.getPluginId())) {
registry.notifyPluginAboutClusterProfileChanged(updatedClusterProfile.getPluginId(), ClusterProfilesChangedStatus.UPDATED, olderClusterConfigMap, updatedClusterConfigMap);
updateClusterProfilesCopy();
} else {
// cluster profile has been updated including changing plugin id.
// this internally results in deletion of a profile belonging to old plugin id and creation of the profile belonging to new plugin id
registry.notifyPluginAboutClusterProfileChanged(updatedClusterProfile.getPluginId(), ClusterProfilesChangedStatus.CREATED, null, updatedClusterConfigMap);
registry.notifyPluginAboutClusterProfileChanged(oldClusterProfile.getPluginId(), ClusterProfilesChangedStatus.DELETED, olderClusterConfigMap, null);
updateClusterProfilesCopy();
}
}
use of com.thoughtworks.go.server.exceptions.RulesViolationException in project gocd by gocd.
the class PackageRepositoryService method checkConnection.
public void checkConnection(final PackageRepository packageRepository, final LocalizedOperationResult result) {
try {
secretParamResolver.resolve(packageRepository);
Result checkConnectionResult = packageRepositoryExtension.checkConnectionToRepository(packageRepository.getPluginConfiguration().getId(), populateConfiguration(packageRepository.getConfiguration()));
String messages = checkConnectionResult.getMessagesForDisplay();
if (!checkConnectionResult.isSuccessful()) {
result.connectionError("Could not connect to package repository. Reason(s): " + messages);
return;
}
result.setMessage("Connection OK. " + messages);
return;
} catch (Exception e) {
if (e instanceof RulesViolationException || e instanceof SecretResolutionFailureException) {
result.unprocessableEntity("Could not connect to package repository. Reason(s): " + e.getMessage());
} else {
result.internalServerError("Could not connect to package repository. Reason(s): " + e.getMessage());
}
}
}
use of com.thoughtworks.go.server.exceptions.RulesViolationException in project gocd by gocd.
the class ElasticProfileServiceTest method shouldReturn422WhenExceptionRelatedToRulesOccur.
@Test
void shouldReturn422WhenExceptionRelatedToRulesOccur() {
ElasticProfile elasticProfile = new ElasticProfile("ldap", clusterProfileId, create("key", false, "{{SECRET:[id][key]}}"));
Username username = new Username("username");
HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
doThrow(new RulesViolationException("some rules violation message")).when(secretParamResolver).resolve(elasticProfile);
elasticProfileService.create(username, elasticProfile, result);
assertThat(result.httpCode()).isEqualTo(422);
assertThat(result.message()).isEqualTo("Validations failed for agentProfile 'ldap'. Error(s): [some rules violation message]. Please correct and resubmit.");
verifyNoInteractions(validator);
}
Aggregations