-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(edda): implement edda AccountProvider (#119)
- Loading branch information
Showing
6 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
swabbie-edda/src/main/kotlin/com/netflix/spinnaker/swabbie/edda/EddaEndpointsService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.netflix.spinnaker.swabbie.edda | ||
|
||
import retrofit.http.GET | ||
|
||
interface EddaEndpointsService { | ||
@GET("/api/v1/edda/endpoints.json") | ||
fun getEddaEndpoints(): EddaEndpointsContainer | ||
|
||
data class EddaEndpointsContainer( | ||
val edda_endpoints: EddaEndpoints | ||
) { | ||
data class EddaEndpoints( | ||
val by_account: List<String>?, | ||
val by_cluster: List<String>? | ||
) | ||
|
||
fun get(): List<String> = edda_endpoints.by_cluster ?: emptyList() | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...-edda/src/main/kotlin/com/netflix/spinnaker/swabbie/edda/providers/EddaAccountProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.netflix.spinnaker.swabbie.edda.providers | ||
|
||
import com.netflix.spinnaker.swabbie.AccountProvider | ||
import com.netflix.spinnaker.swabbie.InMemoryCache | ||
import com.netflix.spinnaker.swabbie.edda.EddaEndpointsService | ||
import com.netflix.spinnaker.swabbie.model.Account | ||
import com.netflix.spinnaker.swabbie.model.Region | ||
import com.netflix.spinnaker.swabbie.model.SpinnakerAccount | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression | ||
import org.springframework.context.annotation.Lazy | ||
import org.springframework.stereotype.Component | ||
|
||
@ConditionalOnExpression("\${eddaEndpoints.enabled:false}") | ||
@Component | ||
class EddaAccountProvider( | ||
private val eddaEndpointsService: EddaEndpointsService, | ||
@Lazy private val accountCache: InMemoryCache<SpinnakerAccount> | ||
) : AccountProvider { | ||
override fun getAccounts(): Set<Account> = accountCache.get() | ||
|
||
fun load(): Set<SpinnakerAccount> { | ||
return eddaEndpointsService.getEddaEndpoints().get() | ||
.asSequence() | ||
.mapNotNull { endpointToAccount(it) } | ||
.toSet() | ||
} | ||
|
||
// i.e. "http://edda-account.region.foo.bar.com", | ||
private fun endpointToAccount(endpoint: String): SpinnakerAccount? { | ||
val regex = """^https?://edda-([\w\-]+)\.([\w\-]+)\..*$""".toRegex() | ||
val match = regex.matchEntire(endpoint) ?: return null | ||
val (account, region) = match.destructured | ||
|
||
return SpinnakerAccount(true, null, "aws", account, endpoint, listOf(Region(false, region))) | ||
} | ||
} | ||
|
||
@ConditionalOnExpression("\${eddaEndpoints.enabled:false}") | ||
@Component | ||
class EddaAccountCache( | ||
eddaAccountProvider: EddaAccountProvider | ||
) : InMemoryCache<SpinnakerAccount>(eddaAccountProvider.load()) |