Skip to content

Commit

Permalink
fix(web): Include better context in save pipeline template ops (spinn…
Browse files Browse the repository at this point in the history
  • Loading branch information
robzienert authored and danielpeach committed May 24, 2018
1 parent a36792c commit ce710f1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping(value = "/pipelineTemplates")
Expand Down Expand Up @@ -65,15 +66,22 @@ public Collection<Map> list(@RequestParam(defaultValue = "global") List<String>
@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.ACCEPTED)
public Map create(@RequestBody Map<String, Object> pipelineTemplate) {
PipelineTemplate template;
try {
template = objectMapper.convertValue(pipelineTemplate, PipelineTemplate.class);
} catch (IllegalArgumentException e) {
throw new RuntimeException("Pipeline template is invalid", e);
}

List<Map<String, Object>> jobs = new ArrayList<>();
Map<String, Object> job = new HashMap<>();
job.put("type", "createPipelineTemplate");
job.put("pipelineTemplate", pipelineTemplate);
jobs.add(job);

Map<String, Object> operation = new HashMap<>();
operation.put("description", "Create pipeline template");
operation.put("application", getApplicationFromTemplate(pipelineTemplate));
operation.put("description", "Create pipeline template '" + getNameFromTemplate(template) + "'");
operation.put("application", getApplicationFromTemplate(template));
operation.put("job", jobs);

return taskService.create(operation);
Expand All @@ -93,6 +101,13 @@ public Map get(@PathVariable String id) {
@RequestMapping(value = "/{id}", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.ACCEPTED)
public Map update(@PathVariable String id, @RequestBody Map<String, Object> pipelineTemplate) {
PipelineTemplate template;
try {
template = objectMapper.convertValue(pipelineTemplate, PipelineTemplate.class);
} catch (IllegalArgumentException e) {
throw new RuntimeException("Pipeline template is invalid", e);
}

List<Map<String, Object>> jobs = new ArrayList<>();
Map<String, Object> job = new HashMap<>();
job.put("type", "updatePipelineTemplate");
Expand All @@ -101,32 +116,36 @@ public Map update(@PathVariable String id, @RequestBody Map<String, Object> pipe
jobs.add(job);

Map<String, Object> operation = new HashMap<>();
operation.put("description", "Update pipeline template '" + id + "'");
operation.put("application", getApplicationFromTemplate(pipelineTemplate));
operation.put("description", "Update pipeline template '" + getNameFromTemplate(template) + "'");
operation.put("application", getApplicationFromTemplate(template));
operation.put("job", jobs);

return taskService.create(operation);
}

private String getApplicationFromTemplate(Map<String, Object> pipelineTemplate) {
PipelineTemplate template;
try {
template = objectMapper.convertValue(pipelineTemplate, PipelineTemplate.class);
} catch (IllegalArgumentException e) {
return DEFAULT_APPLICATION;
}
private String getNameFromTemplate(PipelineTemplate template) {
return Optional.ofNullable(template.metadata.name).orElse(template.id);
}

private String getApplicationFromTemplate(PipelineTemplate template) {
List<String> scopes = template.metadata.scopes;
return (scopes.isEmpty() || scopes.size() > 1) ? DEFAULT_APPLICATION : scopes.get(0);
}

@JsonIgnoreProperties(ignoreUnknown = true)
private static class PipelineTemplate {
static class PipelineTemplate {
@JsonProperty
String id;

@JsonProperty
private Metadata metadata = new Metadata();
Metadata metadata = new Metadata();

static class Metadata {
@JsonProperty
String name;

private static class Metadata {
@JsonProperty
private List<String> scopes = new ArrayList<>();
List<String> scopes = new ArrayList<>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class PipelineTemplateControllerSpec extends Specification {
response.status == 202
1 * taskService.create([
application: app,
description: "Create pipeline template",
description: "Create pipeline template 'foo'",
job: [
[
type: 'createPipelineTemplate',
Expand Down

0 comments on commit ce710f1

Please sign in to comment.