/
RedisApplicationDAO.groovy
117 lines (98 loc) · 3.22 KB
/
RedisApplicationDAO.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
113
114
115
116
117
/*
* 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.application.Application
import com.netflix.spinnaker.front50.model.application.ApplicationDAO
import org.springframework.data.redis.core.Cursor
import org.springframework.data.redis.core.RedisTemplate
import org.springframework.data.redis.core.ScanOptions
/**
* Implementation of {@link ApplicationDAO} interface, leveraging {@link RedisTemplate} to do the
* heavy lifting.
*/
class RedisApplicationDAO implements ApplicationDAO {
static final String BOOK_KEEPING_KEY = 'com.netflix.spinnaker:front50:applications'
RedisTemplate<String, Application> redisTemplate
@Override
Application findByName(String name) throws NotFoundException {
findById(name)
}
@Override
Collection<Application> search(Map<String, String> attributes) {
ApplicationDAO.Searcher.search(all(), attributes)
}
@Override
Application findById(String name) throws NotFoundException {
def app = redisTemplate.opsForHash().get(BOOK_KEEPING_KEY, name.toUpperCase())
if (!app) {
throw new NotFoundException("No application found by id ${name}")
}
app
}
@Override
Collection<Application> all() {
return all(true)
}
@Override
Collection<Application> all(boolean refresh) {
def applications = redisTemplate.opsForHash()
.scan(BOOK_KEEPING_KEY, ScanOptions.scanOptions().match('*').build())
.withCloseable { Cursor<Map> c ->
c.collect{ it.value }
}
if (!applications) {
throw new NotFoundException("No applications available")
}
applications
}
@Override
Collection<Application> history(String id, int maxResults) {
return [findById(id)]
}
@Override
Application create(String id, Application application) {
if (!application.createTs) {
application.createTs = System.currentTimeMillis() as String
}
application.name = id.toUpperCase()
redisTemplate.opsForHash().put(BOOK_KEEPING_KEY, application.name, application)
application
}
@Override
void update(String id, Application application) {
application.name = id
application.updateTs = System.currentTimeMillis() as String
create(id, application)
}
@Override
void delete(String id) {
redisTemplate.opsForHash().delete(BOOK_KEEPING_KEY, id.toUpperCase())
}
@Override
void bulkImport(Collection<Application> items) {
items.each { create(it.id, it) }
}
@Override
boolean isHealthy() {
try {
redisTemplate.opsForHash().get("", "")
return true
} catch (Exception e) {
return false
}
}
}