|
| 1 | +/* |
| 2 | + * Copyright 2016 ThoughtWorks, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.thoughtworks.go.config.update; |
| 18 | + |
| 19 | +import com.thoughtworks.go.config.*; |
| 20 | +import com.thoughtworks.go.config.commands.EntityConfigUpdateCommand; |
| 21 | +import com.thoughtworks.go.i18n.Localizable; |
| 22 | +import com.thoughtworks.go.i18n.LocalizedMessage; |
| 23 | +import com.thoughtworks.go.server.domain.Username; |
| 24 | +import com.thoughtworks.go.server.service.GoConfigService; |
| 25 | +import com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult; |
| 26 | +import com.thoughtworks.go.server.service.result.LocalizedOperationResult; |
| 27 | +import com.thoughtworks.go.serverhealth.HealthStateType; |
| 28 | + |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +public class PatchEnvironmentCommand extends EnvironmentCommand implements EntityConfigUpdateCommand<EnvironmentConfig> { |
| 33 | + private final GoConfigService goConfigService; |
| 34 | + private final EnvironmentConfig environmentConfig; |
| 35 | + private final List<String> pipelinesToAdd; |
| 36 | + private final List<String> pipelinesToRemove; |
| 37 | + private final List<String> agentsToAdd; |
| 38 | + private final List<String> agentsToRemove; |
| 39 | + private final Username username; |
| 40 | + private final Localizable.CurryableLocalizable actionFailed; |
| 41 | + private final HttpLocalizedOperationResult result; |
| 42 | + |
| 43 | + public PatchEnvironmentCommand(GoConfigService goConfigService, EnvironmentConfig environmentConfig, List<String> pipelinesToAdd, List<String> pipelinesToRemove, List<String> agentsToAdd, List<String> agentsToRemove, Username username, Localizable.CurryableLocalizable actionFailed, HttpLocalizedOperationResult result) { |
| 44 | + super(actionFailed, environmentConfig, result); |
| 45 | + |
| 46 | + this.goConfigService = goConfigService; |
| 47 | + this.environmentConfig = environmentConfig; |
| 48 | + this.pipelinesToAdd = pipelinesToAdd; |
| 49 | + this.pipelinesToRemove = pipelinesToRemove; |
| 50 | + this.agentsToAdd = agentsToAdd; |
| 51 | + this.agentsToRemove = agentsToRemove; |
| 52 | + this.username = username; |
| 53 | + this.actionFailed = actionFailed; |
| 54 | + this.result = result; |
| 55 | + } |
| 56 | + |
| 57 | + private boolean validateAgents(List<String> uuids, LocalizedOperationResult result, Agents agents) { |
| 58 | + List<String> unknownAgents = new ArrayList<>(); |
| 59 | + |
| 60 | + for (String uuid : uuids) { |
| 61 | + AgentConfig agent = agents.getAgentByUuid(uuid); |
| 62 | + if (agent.isNull()) { |
| 63 | + unknownAgents.add(uuid); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if (!unknownAgents.isEmpty()) { |
| 68 | + result.badRequest(LocalizedMessage.string("AGENTS_WITH_UUIDS_NOT_FOUND", unknownAgents)); |
| 69 | + return false; |
| 70 | + } |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + private boolean validatePipelines(List<String> pipelines, HttpLocalizedOperationResult result, CruiseConfig config) { |
| 75 | + ArrayList<String> unknownPipelines = new ArrayList<>(); |
| 76 | + |
| 77 | + for (String pipeline : pipelines) { |
| 78 | + try{ |
| 79 | + config.pipelineConfigByName(new CaseInsensitiveString(pipeline)); |
| 80 | + } catch(PipelineNotFoundException e){ |
| 81 | + unknownPipelines.add(pipeline); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (!unknownPipelines.isEmpty()) { |
| 86 | + result.badRequest(LocalizedMessage.string("PIPELINES_WITH_NAMES_NOT_FOUND", unknownPipelines)); |
| 87 | + return false; |
| 88 | + } |
| 89 | + return true; |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + @Override |
| 95 | + public void update(CruiseConfig preprocessedConfig) throws Exception { |
| 96 | + EnvironmentsConfig environments = preprocessedConfig.getEnvironments(); |
| 97 | + int index = environments.indexOf(environmentConfig); |
| 98 | + EnvironmentConfig preprocessedEnvironmentConfig = environments.get(index); |
| 99 | + |
| 100 | + if(isValidConfig(preprocessedConfig)){ |
| 101 | + for (String uuid : agentsToAdd) { |
| 102 | + preprocessedEnvironmentConfig.addAgent(uuid); |
| 103 | + } |
| 104 | + |
| 105 | + for (String uuid : agentsToRemove) { |
| 106 | + preprocessedEnvironmentConfig.removeAgent(uuid); |
| 107 | + } |
| 108 | + |
| 109 | + for (String pipelineName : pipelinesToAdd) { |
| 110 | + preprocessedEnvironmentConfig.addPipeline(new CaseInsensitiveString(pipelineName)); |
| 111 | + } |
| 112 | + |
| 113 | + for (String pipelineName : pipelinesToRemove) { |
| 114 | + preprocessedEnvironmentConfig.removePipeline(new CaseInsensitiveString(pipelineName)); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void clearErrors() { |
| 121 | + BasicCruiseConfig.clearErrors(environmentConfig); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public boolean canContinue(CruiseConfig cruiseConfig) { |
| 126 | + if (!goConfigService.isAdministrator(username.getUsername())) { |
| 127 | + Localizable noPermission = LocalizedMessage.string("NO_PERMISSION_TO_UPDATE_ENVIRONMENT", environmentConfig.name().toString(), username.getDisplayName()); |
| 128 | + result.unauthorized(noPermission, HealthStateType.unauthorised()); |
| 129 | + return false; |
| 130 | + } |
| 131 | + return true; |
| 132 | + } |
| 133 | + |
| 134 | + public boolean isValidConfig(CruiseConfig preprocessedConfig) { |
| 135 | + boolean isValid = validateAgents(agentsToAdd, result, preprocessedConfig.agents()); |
| 136 | + isValid = isValid && validateAgents(agentsToRemove, result, preprocessedConfig.agents()); |
| 137 | + isValid = isValid && validatePipelines(pipelinesToAdd, result, preprocessedConfig); |
| 138 | + isValid = isValid && validatePipelines(pipelinesToRemove, result, preprocessedConfig); |
| 139 | + return isValid; |
| 140 | + } |
| 141 | +} |
0 commit comments