use of com.thoughtworks.go.apiv3.environments.model.PatchEnvironmentRequest in project gocd by gocd.
the class PatchEnvironmentRequestRepresenter method fromJSON.
public static PatchEnvironmentRequest fromJSON(JsonReader jsonReader) {
List<String> pipelineToAdd = new ArrayList<>();
List<String> pipelineToRemove = new ArrayList<>();
List<EnvironmentVariableConfig> envVariablesToAdd = new ArrayList<>();
List<String> envVariableToRemove = new ArrayList<>();
jsonReader.optJsonObject("pipelines").ifPresent(reader -> {
pipelineToAdd.addAll(reader.readStringArrayIfPresent("add").orElse(emptyList()));
pipelineToRemove.addAll(reader.readStringArrayIfPresent("remove").orElse(emptyList()));
});
jsonReader.optJsonObject("environment_variables").ifPresent(reader -> {
envVariableToRemove.addAll(reader.readStringArrayIfPresent("remove").orElse(emptyList()));
reader.readArrayIfPresent("add", array -> array.forEach(envVariable -> envVariablesToAdd.add(EnvironmentVariableRepresenter.fromJSON(new JsonReader(envVariable.getAsJsonObject())))));
});
PatchEnvironmentRequest patchRequest = new PatchEnvironmentRequest(pipelineToAdd, pipelineToRemove, envVariablesToAdd, envVariableToRemove);
return patchRequest;
}
use of com.thoughtworks.go.apiv3.environments.model.PatchEnvironmentRequest in project gocd by gocd.
the class EnvironmentsControllerV3 method partialUpdate.
public String partialUpdate(Request request, Response response) {
JsonReader jsonReader = GsonTransformer.getInstance().jsonReaderFrom(request.body());
PatchEnvironmentRequest req = PatchEnvironmentRequestRepresenter.fromJSON(jsonReader);
HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
String environmentName = request.params("name");
EnvironmentConfig environmentConfig = fetchEntityFromConfig(environmentName);
Optional<EnvironmentVariableConfig> parsingErrors = req.getEnvironmentVariablesToAdd().stream().filter(envVar -> !envVar.errors().isEmpty()).findFirst();
if (parsingErrors.isPresent()) {
EnvironmentVariablesConfig configs = new EnvironmentVariablesConfig(req.getEnvironmentVariablesToAdd());
response.status(422);
return MessageJson.create("Error parsing patch request", writer -> toJSONArray(writer, "environment_variables", configs));
}
environmentConfigService.patchEnvironment(environmentConfig, req.getPipelineToAdd(), req.getPipelineToRemove(), req.getEnvironmentVariablesToAdd(), req.getEnvironmentVariablesToRemove(), currentUsername(), result);
EnvironmentConfig updateConfigElement = fetchEntityFromConfig(environmentName);
setEtagHeader(updateConfigElement, response);
return handleCreateOrUpdateResponse(request, response, updateConfigElement, result);
}
Aggregations