/
RedisPipelineStrategyDAO.groovy
112 lines (93 loc) · 3.05 KB
/
RedisPipelineStrategyDAO.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Copyright 2016 Pivotal, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.spinnaker.front50.redis
import com.netflix.spinnaker.kork.web.exceptions.NotFoundException
import com.netflix.spinnaker.front50.model.pipeline.Pipeline
import com.netflix.spinnaker.front50.model.pipeline.PipelineStrategyDAO
import org.springframework.data.redis.core.Cursor
import org.springframework.data.redis.core.RedisTemplate
import org.springframework.data.redis.core.ScanOptions
import org.springframework.util.Assert
class RedisPipelineStrategyDAO implements PipelineStrategyDAO {
static final String BOOK_KEEPING_KEY = 'com.netflix.spinnaker:front50:pipeline-strategies'
RedisTemplate<String, Pipeline> redisTemplate
@Override
String getPipelineId(String application, String pipelineName) {
all().find { pipeline ->
pipeline.name == pipelineName && pipeline.application == application
}.id
}
@Override
Collection<Pipeline> getPipelinesByApplication(String application) {
all().findAll { pipeline ->
pipeline.application == application
}
}
@Override
Pipeline findById(String id) throws NotFoundException {
def results = redisTemplate.opsForHash().get(BOOK_KEEPING_KEY, id)
if (!results) {
throw new NotFoundException("No pipeline found with id '${id}'");
}
results
}
@Override
Collection<Pipeline> all() {
return all(true)
}
@Override
Collection<Pipeline> all(boolean refresh) {
return redisTemplate.opsForHash()
.scan(BOOK_KEEPING_KEY, ScanOptions.scanOptions().match('*').build())
.withCloseable { Cursor<Map> c ->
c.collect{ it.value }
}
}
@Override
Collection<Pipeline> history(String id, int maxResults) {
return []
}
@Override
Pipeline create(String id, Pipeline item) {
Assert.notNull(item.application, "application field must NOT be null!")
Assert.notNull(item.name, "name field must NOT be null!")
item.id = id ?: UUID.randomUUID().toString()
redisTemplate.opsForHash().put(BOOK_KEEPING_KEY, item.id, item)
item
}
@Override
void update(String id, Pipeline item) {
item.lastModified = System.currentTimeMillis()
create(id, item)
}
@Override
void delete(String id) {
redisTemplate.opsForHash().delete(BOOK_KEEPING_KEY, id)
}
@Override
void bulkImport(Collection<Pipeline> items) {
items.each { create(it.id, it) }
}
@Override
boolean isHealthy() {
try {
redisTemplate.opsForHash().get("", "")
return true
} catch (Exception e) {
return false
}
}
}