|
1
|
|
|
package dev.hltech.dredd.interfaces.rest.environment; |
|
2
|
|
|
|
|
3
|
|
|
import com.google.common.collect.ImmutableSet; |
|
4
|
|
|
import dev.hltech.dredd.domain.ServiceVersion; |
|
5
|
|
|
import dev.hltech.dredd.domain.environment.EnvironmentAggregate; |
|
6
|
|
|
import dev.hltech.dredd.domain.environment.EnvironmentAggregate.EnvironmentAggregateBuilder; |
|
7
|
|
|
import dev.hltech.dredd.domain.environment.EnvironmentRepository; |
|
8
|
|
|
import io.swagger.annotations.ApiOperation; |
|
9
|
|
|
import io.swagger.annotations.ApiResponse; |
|
10
|
|
|
import io.swagger.annotations.ApiResponses; |
|
11
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
12
|
|
|
import org.springframework.http.MediaType; |
|
13
|
|
|
import org.springframework.web.bind.annotation.*; |
|
14
|
|
|
|
|
15
|
|
|
import java.util.List; |
|
16
|
|
|
import java.util.Set; |
|
17
|
|
|
|
|
18
|
|
|
import static com.google.common.base.MoreObjects.firstNonNull; |
|
19
|
|
|
import static dev.hltech.dredd.domain.environment.EnvironmentAggregate.DEFAULT_NAMESPACE; |
|
20
|
|
|
import static java.util.stream.Collectors.toList; |
|
21
|
|
|
import static java.util.stream.Collectors.toSet; |
|
22
|
|
|
|
|
23
|
|
|
@RestController |
|
24
|
|
|
public class EnvironmentController { |
|
25
|
|
|
|
|
26
|
|
|
private final EnvironmentRepository environmentRepository; |
|
27
|
|
|
|
|
28
|
|
|
@Autowired |
|
29
|
|
|
public EnvironmentController(EnvironmentRepository environmentRepository) { |
|
30
|
|
|
this.environmentRepository = environmentRepository; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
@GetMapping(value = "environments", produces = MediaType.APPLICATION_JSON_VALUE) |
|
34
|
|
|
@ApiOperation(value = "Get names of all persisted environments", nickname = "Get environments") |
|
35
|
|
|
@ApiResponses(value = { |
|
36
|
|
|
@ApiResponse(code = 200, message = "Success", response = String.class, responseContainer = "list"), |
|
37
|
|
|
@ApiResponse(code = 500, message = "Failure")}) |
|
38
|
|
|
public Set<String> getEnvironmentNames() { |
|
39
|
|
|
return environmentRepository.getNames(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
@GetMapping(value = "environments/{name}", produces = MediaType.APPLICATION_JSON_VALUE) |
|
43
|
|
|
@ApiOperation(value = "Get services from the environment", nickname = "Get Services") |
|
44
|
|
|
@ApiResponses(value = { |
|
45
|
|
|
@ApiResponse(code = 200, message = "Success", response = ServiceDto.class, responseContainer = "list"), |
|
46
|
|
|
@ApiResponse(code = 500, message = "Failure")}) |
|
47
|
|
|
public List<ServiceDto> getEnvironment(@PathVariable("name") String name) { |
|
48
|
|
|
return environmentRepository.get(name).getAllServices() |
|
49
|
|
|
.stream() |
|
50
|
|
|
.map(sv -> new ServiceDto(sv.getName(), sv.getVersion())) |
|
51
|
|
|
.collect(toList()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
@PutMapping(value = "environments/{name}") |
|
55
|
|
|
@ApiOperation(value = "Update the environment", nickname = "update environment") |
|
56
|
|
|
@ApiResponses(value = { |
|
57
|
|
|
@ApiResponse(code = 200, message = "Success"), |
|
58
|
|
|
@ApiResponse(code = 500, message = "Failure")}) |
|
59
|
|
|
public void overwriteEnvironment( |
|
60
|
|
|
@PathVariable("name") String name, |
|
61
|
|
|
@RequestHeader(value = "X-JUDGE-D-AGENT-SPACE", defaultValue = DEFAULT_NAMESPACE, required = false) String agentSpace, |
|
62
|
|
|
@RequestBody Set<ServiceForm> services |
|
63
|
|
|
) { |
|
64
|
|
|
agentSpace = firstNonNull(agentSpace, DEFAULT_NAMESPACE); |
|
65
|
|
|
EnvironmentAggregate environment = environmentRepository.get(name); |
|
66
|
|
|
Set<String> supportedSpaces = ImmutableSet.<String>builder().addAll(environment.getSpaceNames()).add(agentSpace).build(); |
|
67
|
|
|
EnvironmentAggregateBuilder builder = EnvironmentAggregate.builder(name); |
|
68
|
|
|
for (String space : supportedSpaces) { |
|
69
|
|
|
if (agentSpace.equals(space)) { |
|
70
|
|
|
Set<ServiceVersion> collect = services.stream().map(sf -> new ServiceVersion(sf.getName(), sf.getVersion())).collect(toSet()); |
|
71
|
|
|
|
|
72
|
|
|
builder.withServiceVersions(agentSpace, collect); |
|
73
|
|
|
} else { |
|
74
|
|
|
builder.withServiceVersions(space, environment.getServices(space)); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
environmentRepository.persist(builder.build()); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|