Skip to content

Commit fa66bdf

Browse files
authored
fix(pipelineTemplates): throw exception when not supported (spinnaker#294)
1 parent 683f635 commit fa66bdf

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

front50-web/src/main/groovy/com/netflix/spinnaker/front50/controllers/IntentController.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,59 +17,57 @@
1717
package com.netflix.spinnaker.front50.controllers;
1818

1919
import com.fasterxml.jackson.databind.ObjectMapper;
20+
import com.netflix.spinnaker.front50.exception.BadRequestException;
2021
import com.netflix.spinnaker.front50.exception.NotFoundException;
2122
import com.netflix.spinnaker.front50.exceptions.DuplicateEntityException;
22-
import com.netflix.spinnaker.front50.exceptions.InvalidRequestException;
2323
import com.netflix.spinnaker.front50.model.intent.Intent;
2424
import com.netflix.spinnaker.front50.model.intent.IntentDAO;
2525
import org.springframework.beans.factory.annotation.Autowired;
26-
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2726
import org.springframework.web.bind.annotation.*;
2827

2928
import java.util.List;
3029

3130
@RestController
32-
@ConditionalOnBean(IntentDAO.class)
3331
@RequestMapping("intents")
3432
public class IntentController {
3533

36-
@Autowired
37-
IntentDAO intentDAO;
34+
@Autowired(required = false)
35+
IntentDAO intentDAO = null;
3836

3937
@Autowired
4038
ObjectMapper objectMapper;
4139

4240
@RequestMapping(value = "", method = RequestMethod.GET)
4341
List<Intent> list(@RequestParam(required = false, value = "status") List<String> status) {
44-
return (List<Intent>) intentDAO.getIntentsByStatus(status);
42+
return (List<Intent>) getIntentDAO().getIntentsByStatus(status);
4543
}
4644

4745
@RequestMapping(value = "{id}", method = RequestMethod.GET)
4846
Intent get(@PathVariable String id) {
49-
return intentDAO.findById(id.toLowerCase());
47+
return getIntentDAO().findById(id.toLowerCase());
5048
}
5149

5250
@RequestMapping(value = "", method = RequestMethod.POST)
5351
Intent upsert(@RequestBody Intent intent) {
5452
intent.setLastModified(System.currentTimeMillis());
5553

5654
if (intentExists(intent.getId())){
57-
intentDAO.update(intent.getId(), intent);
55+
getIntentDAO().update(intent.getId(), intent);
5856
} else {
59-
intentDAO.create(intent.getId(), intent);
57+
getIntentDAO().create(intent.getId(), intent);
6058
}
6159

62-
return intentDAO.findById(intent.getId());
60+
return getIntentDAO().findById(intent.getId());
6361
}
6462

6563
@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
6664
void delete(@PathVariable String id) {
67-
intentDAO.delete(id.toLowerCase());
65+
getIntentDAO().delete(id.toLowerCase());
6866
}
6967

7068
private void checkForDuplicateIntents(String id) {
7169
try {
72-
intentDAO.findById(id.toLowerCase());
70+
getIntentDAO().findById(id.toLowerCase());
7371
} catch (NotFoundException e) {
7472
return;
7573
}
@@ -78,10 +76,17 @@ private void checkForDuplicateIntents(String id) {
7876

7977
private boolean intentExists(String id) {
8078
try {
81-
intentDAO.findById(id.toLowerCase());
79+
getIntentDAO().findById(id.toLowerCase());
8280
} catch (NotFoundException e) {
8381
return false;
8482
}
8583
return true;
8684
}
85+
86+
private IntentDAO getIntentDAO() {
87+
if (intentDAO == null) {
88+
throw new BadRequestException("Intents are not supported with your current storage backend");
89+
}
90+
return intentDAO;
91+
}
8792
}

front50-web/src/main/groovy/com/netflix/spinnaker/front50/controllers/PipelineTemplateController.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.netflix.spinnaker.front50.model.pipeline.PipelineTemplateDAO;
2828
import com.netflix.spinnaker.front50.model.pipeline.TemplateConfiguration;
2929
import org.springframework.beans.factory.annotation.Autowired;
30-
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
3130
import org.springframework.web.bind.annotation.PathVariable;
3231
import org.springframework.web.bind.annotation.RequestBody;
3332
import org.springframework.web.bind.annotation.RequestMapping;
@@ -42,12 +41,11 @@
4241
import java.util.stream.Collectors;
4342

4443
@RestController
45-
@ConditionalOnBean(PipelineTemplateDAO.class)
4644
@RequestMapping("pipelineTemplates")
4745
public class PipelineTemplateController {
4846

49-
@Autowired
50-
PipelineTemplateDAO pipelineTemplateDAO;
47+
@Autowired(required = false)
48+
PipelineTemplateDAO pipelineTemplateDAO = null;
5149

5250
@Autowired
5351
PipelineDAO pipelineDAO;
@@ -59,29 +57,29 @@ public class PipelineTemplateController {
5957

6058
@RequestMapping(value = "", method = RequestMethod.GET)
6159
List<PipelineTemplate> list(@RequestParam(required = false, value = "scopes") List<String> scopes) {
62-
return (List<PipelineTemplate>) pipelineTemplateDAO.getPipelineTemplatesByScope(scopes);
60+
return (List<PipelineTemplate>) getPipelineTemplateDAO().getPipelineTemplatesByScope(scopes);
6361
}
6462

6563
@RequestMapping(value = "", method = RequestMethod.POST)
6664
void save(@RequestBody PipelineTemplate pipelineTemplate) {
6765
checkForDuplicatePipelineTemplate(pipelineTemplate.getId());
68-
pipelineTemplateDAO.create(pipelineTemplate.getId(), pipelineTemplate);
66+
getPipelineTemplateDAO().create(pipelineTemplate.getId(), pipelineTemplate);
6967
}
7068

7169
@RequestMapping(value = "{id}", method = RequestMethod.GET)
7270
PipelineTemplate get(@PathVariable String id) {
73-
return pipelineTemplateDAO.findById(id);
71+
return getPipelineTemplateDAO().findById(id);
7472
}
7573

7674
@RequestMapping(value = "{id}", method = RequestMethod.PUT)
7775
PipelineTemplate update(@PathVariable String id, @RequestBody PipelineTemplate pipelineTemplate) {
78-
PipelineTemplate existingPipelineTemplate = pipelineTemplateDAO.findById(id);
76+
PipelineTemplate existingPipelineTemplate = getPipelineTemplateDAO().findById(id);
7977
if (!pipelineTemplate.getId().equals(existingPipelineTemplate.getId())) {
8078
throw new InvalidRequestException("The provided id " + id + " doesn't match the pipeline template id " + pipelineTemplate.getId());
8179
}
8280

8381
pipelineTemplate.setLastModified(System.currentTimeMillis());
84-
pipelineTemplateDAO.update(id, pipelineTemplate);
82+
getPipelineTemplateDAO().update(id, pipelineTemplate);
8583

8684
return pipelineTemplate;
8785
}
@@ -90,7 +88,7 @@ PipelineTemplate update(@PathVariable String id, @RequestBody PipelineTemplate p
9088
void delete(@PathVariable String id) {
9189
checkForDependentConfigs(id, true);
9290
checkForDependentTemplates(id);
93-
pipelineTemplateDAO.delete(id);
91+
getPipelineTemplateDAO().delete(id);
9492
}
9593

9694
@RequestMapping(value = "{id}/dependentPipelines", method = RequestMethod.GET)
@@ -164,7 +162,7 @@ void checkForDependentConfigs(String templateId, boolean recursive) {
164162
List<String> getDependentTemplates(String templateId, Optional<Collection<PipelineTemplate>> templates) {
165163
List<String> dependentTemplateIds = new ArrayList<>();
166164

167-
final Collection<PipelineTemplate> pipelineTemplates = templates.orElse(pipelineTemplateDAO.all());
165+
final Collection<PipelineTemplate> pipelineTemplates = templates.orElse(getPipelineTemplateDAO().all());
168166
pipelineTemplates.forEach(template -> {
169167
if (template.getSource() != null
170168
&& template.getSource().equalsIgnoreCase("spinnaker://" + templateId)) {
@@ -186,10 +184,17 @@ void checkForDependentTemplates(String templateId) {
186184

187185
private void checkForDuplicatePipelineTemplate(String id) {
188186
try {
189-
pipelineTemplateDAO.findById(id);
187+
getPipelineTemplateDAO().findById(id);
190188
} catch (NotFoundException e) {
191189
return;
192190
}
193191
throw new DuplicateEntityException("A pipeline template with the id " + id + " already exists");
194192
}
193+
194+
private PipelineTemplateDAO getPipelineTemplateDAO() {
195+
if (pipelineTemplateDAO == null) {
196+
throw new BadRequestException("Pipeline Templates are not supported with your current storage backend");
197+
}
198+
return pipelineTemplateDAO;
199+
}
195200
}

0 commit comments

Comments
 (0)