use of com.thoughtworks.go.server.exceptions.RulesViolationException in project gocd by gocd.
the class RulesService method validateSecretConfigReferences.
public void validateSecretConfigReferences(PackageMaterial packageMaterial) {
PackageRepository pkgRepository = packageMaterial.getPackageDefinition().getRepository();
Map<CaseInsensitiveString, StringBuilder> errors = validate(packageMaterial.getSecretParams(), pkgRepository.getClass(), pkgRepository.getName(), "Package Material");
if (!errors.isEmpty()) {
throw new RulesViolationException(errorString(errors));
}
}
use of com.thoughtworks.go.server.exceptions.RulesViolationException in project gocd by gocd.
the class BuildAssignmentServiceTest method shouldFailTheJobWhenRulesViolationErrorOccursForElasticConfiguration.
@Test
void shouldFailTheJobWhenRulesViolationErrorOccursForElasticConfiguration() throws IOException, IllegalArtifactLocationException {
PipelineConfig pipelineWithElasticJob = PipelineConfigMother.pipelineWithElasticJob(elasticProfileId1);
JobPlan jobPlan = new InstanceFactory().createJobPlan(pipelineWithElasticJob.first().getJobs().first(), schedulingContext);
jobPlans.add(jobPlan);
JobInstance jobInstance = mock(JobInstance.class);
doThrow(new RulesViolationException("some rules related violation message")).when(elasticAgentPluginService).shouldAssignWork(elasticAgentInstance.elasticAgentMetadata(), null, jobPlan.getElasticProfile(), jobPlan.getClusterProfile(), jobPlan.getIdentifier());
when(jobInstance.getState()).thenReturn(JobState.Scheduled);
when(jobInstanceService.buildById(anyLong())).thenReturn(jobInstance);
buildAssignmentService.onTimer();
assertThatCode(() -> {
JobPlan matchingJob = buildAssignmentService.findMatchingJob(elasticAgentInstance);
assertThat(matchingJob).isNull();
assertThat(buildAssignmentService.jobPlans()).containsExactly(jobPlan);
}).doesNotThrowAnyException();
InOrder inOrder = inOrder(jobInstanceService, scheduleService, consoleService, jobStatusTopic);
inOrder.verify(jobInstanceService).buildById(jobPlan.getJobId());
inOrder.verify(consoleService).appendToConsoleLog(jobPlan.getIdentifier(), "\nThis job was failed by GoCD. This job is configured to run on an elastic agent, there were errors while resolving secrets for the the associated elastic configurations.\nReasons: some rules related violation message");
inOrder.verify(scheduleService).failJob(jobInstance);
inOrder.verify(jobStatusTopic).post(new JobStatusMessage(jobPlan.getIdentifier(), JobState.Scheduled, elasticAgentInstance.getUuid()));
}
use of com.thoughtworks.go.server.exceptions.RulesViolationException in project gocd by gocd.
the class PackageDefinitionService method checkConnection.
public void checkConnection(final PackageDefinition packageDefinition, final LocalizedOperationResult result) {
try {
String pluginId = packageDefinition.getRepository().getPluginConfiguration().getId();
secretParamResolver.resolve(packageDefinition);
Result checkConnectionResult = packageRepositoryExtension.checkConnectionToPackage(pluginId, buildPackageConfigurations(packageDefinition), buildRepositoryConfigurations(packageDefinition.getRepository()));
String messages = checkConnectionResult.getMessagesForDisplay();
if (!checkConnectionResult.isSuccessful()) {
result.connectionError("Package check Failed. Reason(s): " + messages);
return;
}
result.setMessage("OK. " + messages);
} catch (Exception e) {
if (e instanceof RulesViolationException || e instanceof SecretResolutionFailureException) {
result.unprocessableEntity("Package check Failed. Reason(s): " + e.getMessage());
} else {
result.internalServerError("Package check Failed. Reason(s): " + e.getMessage());
}
}
}
use of com.thoughtworks.go.server.exceptions.RulesViolationException in project gocd by gocd.
the class ElasticAgentPluginService method jobCompleted.
public void jobCompleted(JobInstance job) {
AgentInstance agentInstance = agentService.findAgent(job.getAgentUuid());
if (!agentInstance.isElastic()) {
LOGGER.debug("Agent {} is not elastic. Skipping further execution.", agentInstance.getUuid());
return;
}
if (job.isAssignedToAgent()) {
jobCreationTimeMap.remove(job.getId());
}
String pluginId = agentInstance.elasticAgentMetadata().elasticPluginId();
String elasticAgentId = agentInstance.elasticAgentMetadata().elasticAgentId();
JobIdentifier jobIdentifier = job.getIdentifier();
ElasticProfile elasticProfile = job.getPlan().getElasticProfile();
ClusterProfile clusterProfile = job.getPlan().getClusterProfile();
try {
secretParamResolver.resolve(elasticProfile);
Map<String, String> elasticProfileConfiguration = elasticProfile.getConfigurationAsMap(true, true);
Map<String, String> clusterProfileConfiguration = emptyMap();
if (clusterProfile != null) {
secretParamResolver.resolve(clusterProfile);
clusterProfileConfiguration = clusterProfile.getConfigurationAsMap(true, true);
}
elasticAgentPluginRegistry.reportJobCompletion(pluginId, elasticAgentId, jobIdentifier, elasticProfileConfiguration, clusterProfileConfiguration);
} catch (RulesViolationException | SecretResolutionFailureException e) {
String description = format("The job completion call to the plugin for the job identifier [%s] failed for secrets resolution: %s ", jobIdentifier.toString(), e.getMessage());
ServerHealthState healthState = error("Failed to notify plugin", description, general(scopeForJob(jobIdentifier)));
healthState.setTimeout(Timeout.FIVE_MINUTES);
serverHealthService.update(healthState);
LOGGER.error(description);
}
}
use of com.thoughtworks.go.server.exceptions.RulesViolationException in project gocd by gocd.
the class PackageRepositoryServiceTest method shouldSetResultAsUnprocessableEntityIfRulesViolationForCheckConnection.
@Test
void shouldSetResultAsUnprocessableEntityIfRulesViolationForCheckConnection() {
HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
doThrow(new RulesViolationException("some rule violation message")).when(secretParamResolver).resolve(any(PackageRepository.class));
service.checkConnection(packageRepository("some-plugin"), result);
assertThat(result.isSuccessful()).isFalse();
assertThat(result.httpCode()).isEqualTo(HttpStatus.SC_UNPROCESSABLE_ENTITY);
assertThat(result.message()).isEqualTo("Could not connect to package repository. Reason(s): some rule violation message");
}
Aggregations