Skip to content
Permalink
Browse files

Add PATCH support for environment-variables in Environments API (#3080)…

… (#3109)

* ApiV2 of environments config
* Allow updating environment variables using PATCH api of environments.
* Remove environments with remote from api_v1
  • Loading branch information...
GaneshSPatil authored and varshavaradarajan committed Feb 28, 2017
1 parent 51a0085 commit 6986dcc97d544d3087eb66423c70e21d6e77893c
Showing with 1,301 additions and 118 deletions.
  1. +51 −26 config/config-api/src/com/thoughtworks/go/config/BasicEnvironmentConfig.java
  2. +5 −0 config/config-api/src/com/thoughtworks/go/config/EnvironmentConfig.java
  3. +1 −1 config/config-api/src/com/thoughtworks/go/config/EnvironmentVariablesConfig.java
  4. +28 −4 config/config-api/src/com/thoughtworks/go/config/merge/MergeEnvironmentConfig.java
  5. +1 −1 server/src/com/thoughtworks/go/config/update/EnvironmentCommand.java
  6. +90 −33 server/src/com/thoughtworks/go/config/update/PatchEnvironmentCommand.java
  7. +2 −2 server/src/com/thoughtworks/go/server/service/EnvironmentConfigService.java
  8. +22 −12 .../test/integration/com/thoughtworks/go/server/service/EnvironmentConfigServiceIntegrationTest.java
  9. +3 −3 server/test/unit/com/thoughtworks/go/config/update/AddEnvironmentCommandTest.java
  10. +92 −18 server/test/unit/com/thoughtworks/go/config/update/PatchEnvironmentCommandTest.java
  11. +3 −3 server/test/unit/com/thoughtworks/go/config/update/UpdateEnvironmentCommandTest.java
  12. +111 −0 server/webapp/WEB-INF/rails.new/app/controllers/api_v2/admin/environments_controller.rb
  13. +4 −4 .../WEB-INF/rails.new/app/controllers/{api_v1 → api_v2}/admin/environments_with_remote_controller.rb
  14. +35 −0 server/webapp/WEB-INF/rails.new/app/presenters/api_v2/agent_summary_representer.rb
  15. +81 −0 server/webapp/WEB-INF/rails.new/app/presenters/api_v2/config/environment_config_representer.rb
  16. +36 −0 server/webapp/WEB-INF/rails.new/app/presenters/api_v2/config/environments_config_representer.rb
  17. +5 −2 server/webapp/WEB-INF/rails.new/config/routes.rb
  18. +573 −0 server/webapp/WEB-INF/rails.new/spec/controllers/api_v2/admin/environments_controller_spec.rb
  19. +9 −9 ...NF/rails.new/spec/controllers/{api_v1 → api_v2}/admin/environments_with_remote_controller_spec.rb
  20. +36 −0 server/webapp/WEB-INF/rails.new/spec/presenters/api_v2/agent_summary_representer_spec.rb
  21. +79 −0 server/webapp/WEB-INF/rails.new/spec/presenters/api_v2/config/environment_config_representer_spec.rb
  22. +34 −0 ...er/webapp/WEB-INF/rails.new/spec/presenters/api_v2/config/environments_config_representer_spec.rb
@@ -35,10 +35,14 @@
*/
@ConfigTag("environment")
public class BasicEnvironmentConfig implements EnvironmentConfig {
@ConfigAttribute(value = NAME_FIELD, optional = false) private CaseInsensitiveString name;
@ConfigSubtag private EnvironmentVariablesConfig variables = new EnvironmentVariablesConfig();
@ConfigSubtag private EnvironmentAgentsConfig agents = new EnvironmentAgentsConfig();
@ConfigSubtag private EnvironmentPipelinesConfig pipelines = new EnvironmentPipelinesConfig();
@ConfigAttribute(value = NAME_FIELD, optional = false)
private CaseInsensitiveString name;
@ConfigSubtag
private EnvironmentVariablesConfig variables = new EnvironmentVariablesConfig();
@ConfigSubtag
private EnvironmentAgentsConfig agents = new EnvironmentAgentsConfig();
@ConfigSubtag
private EnvironmentPipelinesConfig pipelines = new EnvironmentPipelinesConfig();


private final ConfigErrors configErrors = new ConfigErrors();
@@ -72,9 +76,9 @@ public void validate(ValidationContext validationContext) {
@Override
public boolean validateTree(ConfigSaveValidationContext validationContext, CruiseConfig preprocessedConfig) {
validate(validationContext);
try{
try {
validateContainsOnlyPipelines(preprocessedConfig.getAllPipelineNames());
}catch (RuntimeException e){
} catch (RuntimeException e) {
errors().add("pipeline", e.getMessage());
}
validateContainsOnlyUuids(preprocessedConfig.agents().acceptedUuids());
@@ -176,10 +180,9 @@ public void validateContainsOnlyPipelines(List<CaseInsensitiveString> pipelineNa
@Override
public boolean hasSamePipelinesAs(EnvironmentConfig other) {
for (EnvironmentPipelineConfig pipeline : pipelines) {
for(CaseInsensitiveString name : other.getPipelineNames())
{
if(name.equals(pipeline.getName()))
return true;
for (CaseInsensitiveString name : other.getPipelineNames()) {
if (name.equals(pipeline.getName()))
return true;
}
}
return false;
@@ -210,9 +213,9 @@ public boolean equals(Object o) {
return true;
}

EnvironmentConfig that = as(EnvironmentConfig.class,o);
if(that == null)
return false;
EnvironmentConfig that = as(EnvironmentConfig.class, o);
if (that == null)
return false;

if (agents != null ? !agents.equals(that.getAgents()) : that.getAgents() != null) {
return false;
@@ -239,14 +242,15 @@ public int hashCode() {
return result;
}

private static <T> T as(Class<T> clazz, Object o){
if(clazz.isInstance(o)){
private static <T> T as(Class<T> clazz, Object o) {
if (clazz.isInstance(o)) {
return clazz.cast(o);
}
return null;
}

@Override public String toString() {
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}

@@ -255,6 +259,11 @@ public void addEnvironmentVariable(String name, String value) {
variables.add(new EnvironmentVariableConfig(name.trim(), value));
}

@Override
public void addEnvironmentVariable(EnvironmentVariableConfig variableConfig) {
variables.add(variableConfig);
}

@Override
public EnvironmentVariableContext createEnvironmentContext() {
EnvironmentVariableContext context = new EnvironmentVariableContext(
@@ -316,18 +325,20 @@ public void setConfigAttributes(Object attributes) {
variables.setConfigAttributes(attributeMap.get(VARIABLES_FIELD));
}
}

@Override
public EnvironmentVariablesConfig getPlainTextVariables() {
return variables.getPlainTextVariables();
}

@Override
public EnvironmentVariablesConfig getSecureVariables() {
return variables.getSecureVariables();
}

@Override
public EnvironmentConfig getLocal() {
if(this.isLocal())
if (this.isLocal())
return this;
else
return null;
@@ -341,23 +352,22 @@ public ConfigOrigin getOrigin() {
@Override
public void setOrigins(ConfigOrigin origins) {
this.origin = origins;
for(EnvironmentVariableConfig environmentVariableConfig : this.variables)
{
for (EnvironmentVariableConfig environmentVariableConfig : this.variables) {
environmentVariableConfig.setOrigins(origins);
}
}

@Override
public EnvironmentPipelinesConfig getRemotePipelines() {
if(this.isLocal())
if (this.isLocal())
return new EnvironmentPipelinesConfig();
else
return this.pipelines;
}

@Override
public EnvironmentAgentsConfig getLocalAgents() {
if(this.isLocal())
if (this.isLocal())
return this.agents;
else
return new EnvironmentAgentsConfig();
@@ -374,20 +384,35 @@ public boolean isEnvironmentEmpty() {

@Override
public boolean containsPipelineRemotely(CaseInsensitiveString pipelineName) {
if(this.isLocal())
if (this.isLocal()) {
return false;
if(!this.containsPipeline(pipelineName))
}
if (!this.containsPipeline(pipelineName)) {
return false;

}
return true;
}

@Override
public boolean containsAgentRemotely(String uuid) {
if(this.isLocal())
if (this.isLocal()) {
return false;
if(!this.hasAgent(uuid))
}
if (!this.hasAgent(uuid)) {
return false;
}

return true;
}

@Override
public boolean containsEnvironmentVariableRemotely(String variableName) {
if (this.isLocal()) {
return false;
}
if (!this.getVariables().hasVariable(variableName)) {
return false;
}

return true;
}
@@ -72,6 +72,8 @@

void addEnvironmentVariable(String name, String value);

void addEnvironmentVariable(EnvironmentVariableConfig variableConfig);

EnvironmentVariableContext createEnvironmentContext();

List<CaseInsensitiveString> getPipelineNames();
@@ -102,5 +104,8 @@

boolean containsAgentRemotely(String uuid);

boolean containsEnvironmentVariableRemotely(String variableName);

boolean validateTree(ConfigSaveValidationContext validationContext, CruiseConfig preprocessedConfig);

}
@@ -95,7 +95,7 @@ public EnvironmentVariablesConfig overrideWith(EnvironmentVariablesConfig enviro
return variablesConfig;
}

private void removeIfExists(String name) {
public void removeIfExists(String name) {
EnvironmentVariableConfig configToRemove = null;
for (EnvironmentVariableConfig config : this) {
if (config.getName().equals(name)) {
@@ -193,6 +193,11 @@ public void addEnvironmentVariable(String name, String value) {
this.getFirstEditablePart().addEnvironmentVariable(name, value);
}

@Override
public void addEnvironmentVariable(EnvironmentVariableConfig variableConfig) {
this.getFirstEditablePart().addEnvironmentVariable(variableConfig);
}

@Override
public void addAgent(String uuid) {
this.getFirstEditablePart().addAgent(uuid);
@@ -416,18 +421,37 @@ public boolean containsAgentRemotely(String uuid) {
return false;
}

public ConfigOrigin getOriginForPipeline(CaseInsensitiveString pipelineName){
@Override
public boolean containsEnvironmentVariableRemotely(String variableName) {
for (EnvironmentConfig part : this) {
if (part.containsEnvironmentVariableRemotely(variableName)) {
return true;
}
}
return false;
}

public ConfigOrigin getOriginForPipeline(CaseInsensitiveString pipelineName) {
for (EnvironmentConfig part : this) {
if (part.containsPipeline(pipelineName)) {
return part.getOrigin();
}
}
return null;
}

public ConfigOrigin getOriginForAgent(String agentUUID) {
for (EnvironmentConfig part : this) {
if(part.containsPipeline(pipelineName)){
if (part.hasAgent(agentUUID)) {
return part.getOrigin();
}
}
return null;
}

public ConfigOrigin getOriginForAgent(String agentUUID){
public ConfigOrigin getOriginForEnvironmentVariable(String variableName) {
for (EnvironmentConfig part : this) {
if(part.hasAgent(agentUUID)){
if (part.getVariables().hasVariable(variableName)) {
return part.getOrigin();
}
}
@@ -47,7 +47,7 @@ public boolean isValid(CruiseConfig preprocessedConfig) {

if (!isValid) {
String allErrors = new AllConfigErrors(preprocessedConfig.getAllErrors()).asString();
result.badRequest(actionFailed.addParam(allErrors));
result.unprocessableEntity(actionFailed.addParam(allErrors));
}

return isValid;

0 comments on commit 6986dcc

Please sign in to comment.
You can’t perform that action at this time.