@@ -18,6 +18,7 @@ package com.netflix.spinnaker.keel.clouddriver
18
18
import com.github.benmanes.caffeine.cache.AsyncCache
19
19
import com.github.benmanes.caffeine.cache.AsyncLoadingCache
20
20
import com.netflix.spinnaker.keel.caffeine.CacheFactory
21
+ import com.netflix.spinnaker.keel.clouddriver.model.Certificate
21
22
import com.netflix.spinnaker.keel.clouddriver.model.Credential
22
23
import com.netflix.spinnaker.keel.clouddriver.model.Network
23
24
import com.netflix.spinnaker.keel.clouddriver.model.SecurityGroupSummary
@@ -56,7 +57,6 @@ class MemoryCloudDriverCache(
56
57
}
57
58
}
58
59
.handleNotFound()
59
- ? : notFound(" Security group with id $id not found in the $account account and $region region" )
60
60
}
61
61
62
62
private val securityGroupsByName: AsyncLoadingCache <Triple <String , String , String >, SecurityGroupSummary > = cacheFactory
@@ -72,34 +72,31 @@ class MemoryCloudDriverCache(
72
72
}
73
73
}
74
74
.handleNotFound()
75
- ? : notFound(" Security group with name $name not found in the $account account and $region region" )
76
75
}
77
76
78
77
private val networksById: AsyncLoadingCache <String , Network > = cacheFactory
79
- .asyncLoadingCache (cacheName = " networksById" ) { id ->
78
+ .asyncBulkLoadingCache (cacheName = " networksById" ) {
80
79
runCatching {
81
- cloudDriver.listNetworks(DEFAULT_SERVICE_ACCOUNT )[" aws" ]
82
- ?.firstOrNull { it.id == id }
83
- ?.also {
84
- networksByName.put(Triple (it.account, it.region, it.name), completedFuture(it))
85
- }
80
+ cloudDriver.listNetworks(" aws" , DEFAULT_SERVICE_ACCOUNT )
81
+ .associateBy { it.id }
86
82
}
87
- .handleNotFound()
88
- ? : notFound(" VPC network with id $id not found" )
83
+ .getOrElse { ex ->
84
+ throw CacheLoadingException (" Error loading networksById cache" , ex)
85
+ }
89
86
}
90
87
91
88
private val networksByName: AsyncLoadingCache <Triple <String , String , String ?>, Network > = cacheFactory
92
- .asyncLoadingCache (cacheName = " networksByName" ) { (account, region, name) ->
89
+ .asyncBulkLoadingCache (cacheName = " networksByName" ) {
93
90
runCatching {
94
91
cloudDriver
95
- .listNetworks(DEFAULT_SERVICE_ACCOUNT )[" aws" ]
96
- ?.firstOrNull { it.name == name && it.account == account && it.region == region }
97
- ?.also {
98
- networksById.put(it.id, completedFuture(it))
92
+ .listNetworks(" aws" , DEFAULT_SERVICE_ACCOUNT )
93
+ .associateBy {
94
+ Triple (it.account, it.region, it.name)
99
95
}
100
96
}
101
- .handleNotFound()
102
- ? : notFound(" VPC network named $name not found in $region " )
97
+ .getOrElse { ex ->
98
+ throw CacheLoadingException (" Error loading networksByName cache" , ex)
99
+ }
103
100
}
104
101
105
102
private data class AvailabilityZoneKey (
@@ -121,7 +118,7 @@ class MemoryCloudDriverCache(
121
118
.toSet()
122
119
}
123
120
.getOrElse { ex ->
124
- throw CacheLoadingException (" Error loading cache" , ex)
121
+ throw CacheLoadingException (" Error loading availabilityZones cache" , ex)
125
122
}
126
123
}
127
124
@@ -133,69 +130,98 @@ class MemoryCloudDriverCache(
133
130
cloudDriver.getCredential(name, DEFAULT_SERVICE_ACCOUNT )
134
131
}
135
132
.handleNotFound()
136
- ? : notFound(" Credentials with name $name not found" )
137
133
}
138
134
139
135
private val subnetsById: AsyncLoadingCache <String , Subnet > = cacheFactory
140
- .asyncLoadingCache (cacheName = " subnetsById" ) { subnetId ->
136
+ .asyncBulkLoadingCache (cacheName = " subnetsById" ) {
141
137
runCatching {
142
138
cloudDriver
143
139
.listSubnets(" aws" , DEFAULT_SERVICE_ACCOUNT )
144
- .find { it.id == subnetId }
140
+ .associateBy { it.id }
145
141
}
146
- .handleNotFound()
147
- ? : notFound(" Subnet with id $subnetId not found" )
142
+ .getOrElse { ex -> throw CacheLoadingException (" Error loading subnetsById cache" , ex) }
148
143
}
149
144
150
- private val subnetsByPurpose: AsyncLoadingCache <Triple <String , String , String >, Subnet > = cacheFactory
151
- .asyncLoadingCache (cacheName = " subnetsByPurpose" ) { (account, region, purpose) ->
145
+ private val subnetsByPurpose: AsyncLoadingCache <Triple <String , String , String ? >, Subnet > = cacheFactory
146
+ .asyncBulkLoadingCache (cacheName = " subnetsByPurpose" ) {
152
147
runCatching {
153
148
cloudDriver
154
149
.listSubnets(" aws" , DEFAULT_SERVICE_ACCOUNT )
155
- .find { it.account == account && it.region == region && it.purpose == purpose }
150
+ .associateBy { Triple ( it.account, it.region, it.purpose) }
156
151
}
157
- .handleNotFound()
158
- ? : notFound(" Subnet with purpose \" $purpose \" not found in $account :$region " )
152
+ .getOrElse { ex -> throw CacheLoadingException (" Error loading subnetsByPurpose cache" , ex) }
159
153
}
160
154
155
+ private val certificatesByName: AsyncLoadingCache <String , Certificate > =
156
+ cacheFactory
157
+ .asyncBulkLoadingCache(" certificatesByName" ) {
158
+ runCatching {
159
+ cloudDriver
160
+ .getCertificates()
161
+ .associateBy { it.serverCertificateName }
162
+ }
163
+ .getOrElse { ex -> throw CacheLoadingException (" Error loading certificatesByName cache" , ex) }
164
+ }
165
+
166
+ private val certificatesByArn: AsyncLoadingCache <String , Certificate > =
167
+ cacheFactory
168
+ .asyncBulkLoadingCache(" certificatesByArn" ) {
169
+ runCatching {
170
+ cloudDriver
171
+ .getCertificates()
172
+ .associateBy { it.arn }
173
+ }
174
+ .getOrElse { ex -> throw CacheLoadingException (" Error loading certificatesByArn cache" , ex) }
175
+ }
176
+
161
177
override fun credentialBy (name : String ): Credential =
162
178
runBlocking {
163
- credentials.get(name).await()
179
+ credentials.get(name).await() ? : notFound( " Credential with name $name not found " )
164
180
}
165
181
166
182
override fun securityGroupById (account : String , region : String , id : String ): SecurityGroupSummary =
167
183
runBlocking {
168
- securityGroupsById.get(Triple (account, region, id)).await()
184
+ securityGroupsById.get(Triple (account, region, id)).await()? : notFound( " Security group with id $id not found in $account : $region " )
169
185
}
170
186
171
187
override fun securityGroupByName (account : String , region : String , name : String ): SecurityGroupSummary =
172
188
runBlocking {
173
- securityGroupsByName.get(Triple (account, region, name)).await()
189
+ securityGroupsByName.get(Triple (account, region, name)).await()? : notFound( " Security group with name $name not found in $account : $region " )
174
190
}
175
191
176
192
override fun networkBy (id : String ): Network =
177
193
runBlocking {
178
- networksById.get(id).await()
194
+ networksById.get(id).await() ? : notFound( " VPC network with id $id not found " )
179
195
}
180
196
181
197
override fun networkBy (name : String? , account : String , region : String ): Network =
182
198
runBlocking {
183
- networksByName.get(Triple (account, region, name)).await()
199
+ networksByName.get(Triple (account, region, name)).await() ? : notFound( " VPC network named $name not found in $account : $region " )
184
200
}
185
201
186
202
override fun availabilityZonesBy (account : String , vpcId : String , purpose : String , region : String ): Set <String > =
187
203
runBlocking {
188
- availabilityZones.get(AvailabilityZoneKey (account, region, vpcId, purpose)).await()
204
+ availabilityZones.get(AvailabilityZoneKey (account, region, vpcId, purpose)).await() ? : notFound( " Availability zone with purpose \" $purpose \" not found in $account : $region " )
189
205
}
190
206
191
207
override fun subnetBy (subnetId : String ): Subnet =
192
208
runBlocking {
193
- subnetsById.get(subnetId).await()
209
+ subnetsById.get(subnetId).await() ? : notFound( " Subnet with id $subnetId not found " )
194
210
}
195
211
196
212
override fun subnetBy (account : String , region : String , purpose : String ): Subnet =
197
213
runBlocking {
198
- subnetsByPurpose.get(Triple (account, region, purpose)).await()
214
+ subnetsByPurpose.get(Triple (account, region, purpose)).await() ? : notFound(" Subnet with purpose \" $purpose \" not found in $account :$region " )
215
+ }
216
+
217
+ override fun certificateByName (name : String ): Certificate =
218
+ runBlocking {
219
+ certificatesByName.get(name).await() ? : notFound(" Certificate with name $name not found" )
220
+ }
221
+
222
+ override fun certificateByArn (arn : String ): Certificate =
223
+ runBlocking {
224
+ certificatesByArn.get(arn).await() ? : notFound(" Certificate with ARN $arn not found" )
199
225
}
200
226
}
201
227
0 commit comments