Skip to content

Commit 642d7f4

Browse files
committed
Added environments api
1 parent 1425331 commit 642d7f4

File tree

37 files changed

+1196
-141
lines changed

37 files changed

+1196
-141
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,4 @@ app-server/target/*
230230
jetty6/target/*
231231
jetty9/target/*
232232

233+
.generators

common/src/com/thoughtworks/go/server/domain/Username.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/*************************GO-LICENSE-START*********************************
2-
* Copyright 2014 ThoughtWorks, Inc.
1+
/*
2+
* Copyright 2016 ThoughtWorks, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -12,7 +12,7 @@
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
15-
*************************GO-LICENSE-END***********************************/
15+
*/
1616

1717
package com.thoughtworks.go.server.domain;
1818

@@ -33,6 +33,10 @@ public Username(final CaseInsensitiveString userName) {
3333
this(userName, CaseInsensitiveString.str(userName));
3434
}
3535

36+
public Username(final String userName) {
37+
this(new CaseInsensitiveString(userName));
38+
}
39+
3640
public Username(final Username userName, String displayName) {
3741
this(userName.getUsername(), displayName);
3842
}

config/config-api/src/com/thoughtworks/go/config/BasicEnvironmentConfig.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,20 @@ public CaseInsensitiveString name() {
167167
return name;
168168
}
169169

170+
public void setName(CaseInsensitiveString name) {
171+
this.name = name;
172+
}
173+
170174
@Override
171175
public EnvironmentAgentsConfig getAgents() {
172176
return agents;
173177
}
174178

179+
public void setAgents(List<EnvironmentAgentConfig> agents) {
180+
this.agents.clear();
181+
this.agents.addAll(agents);
182+
}
183+
175184
@Override
176185
public boolean equals(Object o) {
177186
if (this == o) {
@@ -246,6 +255,11 @@ public EnvironmentPipelinesConfig getPipelines() {
246255
return pipelines;
247256
}
248257

258+
public void setPipelines(List<EnvironmentPipelineConfig> pipelines) {
259+
this.pipelines.clear();
260+
this.pipelines.addAll(pipelines);
261+
}
262+
249263
@Override
250264
public boolean hasVariable(String variableName) {
251265
return variables.hasVariable(variableName);
@@ -256,6 +270,10 @@ public EnvironmentVariablesConfig getVariables() {
256270
return variables;
257271
}
258272

273+
public void setVariables(EnvironmentVariablesConfig environmentVariables) {
274+
this.variables = environmentVariables;
275+
}
276+
259277
@Override
260278
public void setConfigAttributes(Object attributes) {
261279
if (attributes == null) {

config/config-api/src/com/thoughtworks/go/config/EnvironmentAgentConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public String getUuid() {
5151
return uuid;
5252
}
5353

54+
public void setUuid(String uuid) {
55+
this.uuid = uuid;
56+
}
57+
5458
@Override
5559
public boolean equals(Object o) {
5660
if (this == o) {

config/config-api/src/com/thoughtworks/go/config/EnvironmentPipelineConfig.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/*************************GO-LICENSE-START*********************************
2-
* Copyright 2014 ThoughtWorks, Inc.
1+
/*
2+
* Copyright 2016 ThoughtWorks, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -12,7 +12,7 @@
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
15-
*************************GO-LICENSE-END***********************************/
15+
*/
1616

1717
package com.thoughtworks.go.config;
1818

@@ -24,21 +24,34 @@
2424
* @understands a reference to an existing pipeline that is associated to an Environment
2525
*/
2626
@ConfigTag("pipeline")
27-
public class EnvironmentPipelineConfig implements Validatable{
27+
public class EnvironmentPipelineConfig implements Validatable {
2828
public static final String ORIGIN = "origin";
2929

30-
@ConfigAttribute(value = "name", optional = false) private CaseInsensitiveString pipelineName;
30+
@ConfigAttribute(value = "name", optional = false)
31+
private CaseInsensitiveString name;
3132
private ConfigErrors configErrors = new ConfigErrors();
3233

3334
public EnvironmentPipelineConfig() {
3435
}
3536

3637
public EnvironmentPipelineConfig(CaseInsensitiveString pipelineName) {
37-
this.pipelineName = pipelineName;
38+
this.name = pipelineName;
39+
}
40+
41+
public EnvironmentPipelineConfig(String pipelineName) {
42+
this.name = new CaseInsensitiveString(pipelineName);
3843
}
3944

4045
public CaseInsensitiveString getName() {
41-
return pipelineName;
46+
return name;
47+
}
48+
49+
public void setName(String pipelineName) {
50+
setName(new CaseInsensitiveString(pipelineName));
51+
}
52+
53+
public void setName(CaseInsensitiveString pipelineName) {
54+
this.name = pipelineName;
4255
}
4356

4457
public boolean isReferenceOf(List<CaseInsensitiveString> pipelineNames) {
@@ -50,7 +63,7 @@ public boolean isReferenceOf(List<CaseInsensitiveString> pipelineNames) {
5063
}
5164

5265
private boolean sameAs(final CaseInsensitiveString pipelineName) {
53-
return this.pipelineName.equals(pipelineName);
66+
return this.name.equals(pipelineName);
5467
}
5568

5669
public boolean equals(Object o) {
@@ -61,12 +74,12 @@ public boolean equals(Object o) {
6174
return false;
6275
}
6376
EnvironmentPipelineConfig that = (EnvironmentPipelineConfig) o;
64-
return pipelineName == null ? that.pipelineName == null : sameAs(that.pipelineName);
77+
return name == null ? that.name == null : sameAs(that.name);
6578
}
6679

6780
@Override
6881
public int hashCode() {
69-
return pipelineName != null ? pipelineName.hashCode() : 0;
82+
return name != null ? name.hashCode() : 0;
7083
}
7184

7285
public void validate(ValidationContext validationContext) {

config/config-api/src/com/thoughtworks/go/config/exceptions/NoSuchEnvironmentException.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/*************************GO-LICENSE-START*********************************
2-
* Copyright 2014 ThoughtWorks, Inc.
1+
/*
2+
* Copyright 2016 ThoughtWorks, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -12,7 +12,7 @@
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
15-
*************************GO-LICENSE-END***********************************/
15+
*/
1616

1717
package com.thoughtworks.go.config.exceptions;
1818

server/src/com/thoughtworks/go/config/ConfigModifyingUser.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/*************************GO-LICENSE-START*********************************
2-
* Copyright 2014 ThoughtWorks, Inc.
1+
/*
2+
* Copyright 2016 ThoughtWorks, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -12,10 +12,11 @@
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
15-
*************************GO-LICENSE-END***********************************/
15+
*/
1616

1717
package com.thoughtworks.go.config;
1818

19+
import com.thoughtworks.go.server.domain.Username;
1920
import com.thoughtworks.go.server.util.UserHelper;
2021

2122
/**
@@ -32,6 +33,10 @@ public ConfigModifyingUser(String userName) {
3233
this.userName = userName;
3334
}
3435

36+
public ConfigModifyingUser(Username username) {
37+
this(username.getUsername().toString());
38+
}
39+
3540
public String getUserName() {
3641
return userName;
3742
}

server/src/com/thoughtworks/go/config/GoConfigDao.java

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
import com.rits.cloning.Cloner;
2020
import com.thoughtworks.go.config.commands.CheckedUpdateCommand;
2121
import com.thoughtworks.go.config.commands.EntityConfigUpdateCommand;
22+
import com.thoughtworks.go.config.update.AddEnvironmentCommand;
2223
import com.thoughtworks.go.config.update.ConfigUpdateCheckFailedException;
2324
import com.thoughtworks.go.config.validation.GoConfigValidity;
2425
import com.thoughtworks.go.listener.ConfigChangedListener;
2526
import com.thoughtworks.go.presentation.TriStateSelection;
2627
import com.thoughtworks.go.server.domain.Username;
28+
import com.thoughtworks.go.server.service.EnvironmentConfigService;
2729
import com.thoughtworks.go.server.util.UserHelper;
2830
import com.thoughtworks.go.util.ExceptionUtils;
2931
import org.slf4j.Logger;
@@ -36,7 +38,6 @@
3638
import java.util.List;
3739

3840
import static com.thoughtworks.go.util.ExceptionUtils.bomb;
39-
import static com.thoughtworks.go.util.ExceptionUtils.bombIfNull;
4041

4142
/**
4243
* @understands how to modify the cruise config sources
@@ -64,8 +65,8 @@ public void addPipeline(PipelineConfig pipelineConfig, String groupName) {
6465
updateConfig(pipelineAdder(pipelineConfig, groupName));
6566
}
6667

67-
public void addEnvironment(BasicEnvironmentConfig environmentConfig) {
68-
updateConfig(environmentAdder(environmentConfig));
68+
public void addEnvironment(BasicEnvironmentConfig environmentConfig, Username user) {
69+
updateConfig(new AddEnvironmentCommand(environmentConfig, user));
6970
}
7071

7172
public CruiseConfig loadForEditing() {
@@ -211,7 +212,6 @@ public String unmodifiedMd5() {
211212
}
212213

213214

214-
215215
private UpdateConfigCommand pipelineAdder(final PipelineConfig pipelineConfig, final String groupName) {
216216
return new UpdateConfigCommand() {
217217
public CruiseConfig update(CruiseConfig cruiseConfig) {
@@ -221,16 +221,6 @@ public CruiseConfig update(CruiseConfig cruiseConfig) {
221221
};
222222
}
223223

224-
private UpdateConfigCommand environmentAdder(final BasicEnvironmentConfig environmentConfig) {
225-
return new UpdateConfigCommand() {
226-
public CruiseConfig update(CruiseConfig cruiseConfig) {
227-
cruiseConfig.addEnvironment(environmentConfig);
228-
return cruiseConfig;
229-
}
230-
};
231-
}
232-
233-
234224
public UpdateConfigCommand mailHostUpdater(final MailHost mailHost) {
235225
return new UpdateConfigCommand() {
236226
public CruiseConfig update(CruiseConfig cruiseConfig) {
@@ -308,39 +298,8 @@ public CruiseConfig update(CruiseConfig cruiseConfig) throws Exception {
308298
}
309299
}
310300

311-
public static class ModifyEnvironmentCommand implements UpdateConfigCommand {
312-
private final String uuid;
313-
private final String environmentName;
314-
private final TriStateSelection.Action action;
315-
316-
public ModifyEnvironmentCommand(String uuid, String environmentName, TriStateSelection.Action action) {
317-
this.uuid = uuid;
318-
this.environmentName = environmentName;
319-
this.action = action;
320-
}
321-
322-
public CruiseConfig update(CruiseConfig cruiseConfig) throws Exception {
323-
AgentConfig agentConfig = cruiseConfig.agents().getAgentByUuid(uuid);
324-
bombIfNull(agentConfig, "Unable to set agent resources; Agent [" + uuid + "] not found.");
325-
EnvironmentConfig environmentConfig = cruiseConfig.getEnvironments().find(new CaseInsensitiveString(environmentName));
326-
if (environmentConfig == null) {
327-
agentConfig.addError("environments", "Environment [" + environmentName + "] does not exist.");
328-
} else {
329-
if (action.equals(TriStateSelection.Action.add)) {
330-
environmentConfig.addAgentIfNew(uuid);
331-
} else if (action.equals(TriStateSelection.Action.remove)) {
332-
environmentConfig.removeAgent(uuid);
333-
} else if (action.equals(TriStateSelection.Action.nochange)) {
334-
//do nothing
335-
} else {
336-
bomb(String.format("unsupported action '%s'", action));
337-
}
338-
}
339-
return cruiseConfig;
340-
}
341-
}
342-
343301
public GoConfigHolder loadConfigHolder() {
344302
return cachedConfigService.loadConfigHolder();
345303
}
304+
346305
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.thoughtworks.go.config.update;
2+
3+
import com.thoughtworks.go.config.*;
4+
import com.thoughtworks.go.server.domain.Username;
5+
6+
public class AddEnvironmentCommand implements UpdateConfigCommand, UserAware {
7+
private final BasicEnvironmentConfig environmentConfig;
8+
private Username user;
9+
10+
public AddEnvironmentCommand(BasicEnvironmentConfig environmentConfig, Username user) {
11+
this.environmentConfig = environmentConfig;
12+
this.user = user;
13+
}
14+
15+
public CruiseConfig update(CruiseConfig cruiseConfig) {
16+
cruiseConfig.addEnvironment(environmentConfig);
17+
return cruiseConfig;
18+
}
19+
20+
@Override
21+
public ConfigModifyingUser user() {
22+
return new ConfigModifyingUser(user);
23+
}
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.thoughtworks.go.config.update;
2+
3+
import com.thoughtworks.go.config.*;
4+
import com.thoughtworks.go.server.domain.Username;
5+
6+
public class DeleteEnvironmentCommand implements UpdateConfigCommand, UserAware {
7+
private EnvironmentConfig environmentConfig;
8+
private Username username;
9+
10+
public DeleteEnvironmentCommand(EnvironmentConfig environmentConfig, Username username) {
11+
this.environmentConfig = environmentConfig;
12+
this.username = username;
13+
}
14+
15+
@Override
16+
public CruiseConfig update(CruiseConfig cruiseConfig) throws Exception {
17+
cruiseConfig.getEnvironments().remove(environmentConfig);
18+
return cruiseConfig;
19+
}
20+
21+
@Override
22+
public ConfigModifyingUser user() {
23+
return new ConfigModifyingUser(username);
24+
}
25+
}

0 commit comments

Comments
 (0)