Search in sources :

Example 1 with PatchEnvironmentRequest

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;
}
Also used : EnvironmentVariableConfig(com.thoughtworks.go.config.EnvironmentVariableConfig) JsonReader(com.thoughtworks.go.api.representers.JsonReader) List(java.util.List) Collections.emptyList(java.util.Collections.emptyList) PatchEnvironmentRequest(com.thoughtworks.go.apiv3.environments.model.PatchEnvironmentRequest) EnvironmentVariableRepresenter(com.thoughtworks.go.api.representers.EnvironmentVariableRepresenter) ArrayList(java.util.ArrayList) EnvironmentVariableConfig(com.thoughtworks.go.config.EnvironmentVariableConfig) ArrayList(java.util.ArrayList) PatchEnvironmentRequest(com.thoughtworks.go.apiv3.environments.model.PatchEnvironmentRequest) JsonReader(com.thoughtworks.go.api.representers.JsonReader)

Example 2 with PatchEnvironmentRequest

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);
}
Also used : CrudController(com.thoughtworks.go.api.CrudController) EntityType(com.thoughtworks.go.config.exceptions.EntityType) SupportedEntity(com.thoughtworks.go.config.policy.SupportedEntity) Autowired(org.springframework.beans.factory.annotation.Autowired) MessageJson(com.thoughtworks.go.api.util.MessageJson) StringUtils(org.apache.commons.lang3.StringUtils) HashSet(java.util.HashSet) EntityHashingService(com.thoughtworks.go.server.service.EntityHashingService) Collectors.toCollection(java.util.stream.Collectors.toCollection) JsonReader(com.thoughtworks.go.api.representers.JsonReader) EnvironmentConfigService(com.thoughtworks.go.server.service.EnvironmentConfigService) ConfigElementForEdit(com.thoughtworks.go.domain.ConfigElementForEdit) Request(spark.Request) SparkSpringController(com.thoughtworks.go.spark.spring.SparkSpringController) ApiVersion(com.thoughtworks.go.api.ApiVersion) Routes(com.thoughtworks.go.spark.Routes) EnvironmentsRepresenter.toJSON(com.thoughtworks.go.apiv3.environments.representers.EnvironmentsRepresenter.toJSON) Comparator.comparing(java.util.Comparator.comparing) ApiController(com.thoughtworks.go.api.ApiController) GsonTransformer(com.thoughtworks.go.api.util.GsonTransformer) Set(java.util.Set) HaltApiResponses(com.thoughtworks.go.api.util.HaltApiResponses) IOException(java.io.IOException) com.thoughtworks.go.config(com.thoughtworks.go.config) String.format(java.lang.String.format) ApiAuthenticationHelper(com.thoughtworks.go.api.spring.ApiAuthenticationHelper) Consumer(java.util.function.Consumer) OutputWriter(com.thoughtworks.go.api.base.OutputWriter) Component(org.springframework.stereotype.Component) PatchEnvironmentRequestRepresenter(com.thoughtworks.go.apiv3.environments.representers.PatchEnvironmentRequestRepresenter) PatchEnvironmentRequest(com.thoughtworks.go.apiv3.environments.model.PatchEnvironmentRequest) Response(spark.Response) Optional(java.util.Optional) HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) EnvironmentVariableRepresenter.toJSONArray(com.thoughtworks.go.api.representers.EnvironmentVariableRepresenter.toJSONArray) EnvironmentRepresenter(com.thoughtworks.go.apiv3.environments.representers.EnvironmentRepresenter) Spark(spark.Spark) HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) PatchEnvironmentRequest(com.thoughtworks.go.apiv3.environments.model.PatchEnvironmentRequest) JsonReader(com.thoughtworks.go.api.representers.JsonReader)

Aggregations

JsonReader (com.thoughtworks.go.api.representers.JsonReader)2 PatchEnvironmentRequest (com.thoughtworks.go.apiv3.environments.model.PatchEnvironmentRequest)2 ApiController (com.thoughtworks.go.api.ApiController)1 ApiVersion (com.thoughtworks.go.api.ApiVersion)1 CrudController (com.thoughtworks.go.api.CrudController)1 OutputWriter (com.thoughtworks.go.api.base.OutputWriter)1 EnvironmentVariableRepresenter (com.thoughtworks.go.api.representers.EnvironmentVariableRepresenter)1 EnvironmentVariableRepresenter.toJSONArray (com.thoughtworks.go.api.representers.EnvironmentVariableRepresenter.toJSONArray)1 ApiAuthenticationHelper (com.thoughtworks.go.api.spring.ApiAuthenticationHelper)1 GsonTransformer (com.thoughtworks.go.api.util.GsonTransformer)1 HaltApiResponses (com.thoughtworks.go.api.util.HaltApiResponses)1 MessageJson (com.thoughtworks.go.api.util.MessageJson)1 EnvironmentRepresenter (com.thoughtworks.go.apiv3.environments.representers.EnvironmentRepresenter)1 EnvironmentsRepresenter.toJSON (com.thoughtworks.go.apiv3.environments.representers.EnvironmentsRepresenter.toJSON)1 PatchEnvironmentRequestRepresenter (com.thoughtworks.go.apiv3.environments.representers.PatchEnvironmentRequestRepresenter)1 com.thoughtworks.go.config (com.thoughtworks.go.config)1 EnvironmentVariableConfig (com.thoughtworks.go.config.EnvironmentVariableConfig)1 EntityType (com.thoughtworks.go.config.exceptions.EntityType)1 SupportedEntity (com.thoughtworks.go.config.policy.SupportedEntity)1 ConfigElementForEdit (com.thoughtworks.go.domain.ConfigElementForEdit)1