Last active
November 21, 2018 00:04
-
-
Save joeyslalom/a5198255e1876ad323787d445c793eb4 to your computer and use it in GitHub Desktop.
Spring Shell + Kotlin fun
This file contains hidden or 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
// read a file in classpath | |
private fun fileToLines(classpathResource: String): List<String> { | |
val output = ByteArrayOutputStream() | |
ClassPathResource(classpathResource).inputStream.use { | |
it.copyTo(output) | |
} | |
return output.toString().split("\n") | |
} | |
// select a random member from cassandra | |
fun randomIsMember(): IsMember { | |
fun randomMember(): Member? { | |
val nextLong = random.nextLong() | |
val query = Query.query(Criteria.where("token(row_id)").gt(nextLong)).limit(1) | |
return try { | |
template.selectOne(query, Member::class.java) | |
} catch (e: MappingInstantiationException) { | |
log.warn("cannot map row where token(row_id) > $nextLong") | |
null | |
} | |
} | |
var member: Member? = null | |
while (member == null) { | |
member = randomMember() | |
} | |
return member.toIsMember() | |
} | |
// configure kubernetes integration with tectonic | |
@Configuration | |
class K8sConfig(@Value("\${k8s.server}") server: String, | |
@Value("\${k8s.config.path}") kubeConfigPath: String, | |
@Value("\${k8s.namespace}") val k8sNamespace: String) { | |
init { | |
val token = readToken(FileReader(kubeConfigPath)) | |
val client = Config.fromToken(server, token) | |
io.kubernetes.client.Configuration.setDefaultApiClient(client) | |
} | |
@Bean | |
fun k8sCoreV1Api() = CoreV1Api() | |
@Bean | |
fun k8sClient(api: CoreV1Api) = K8sClient(api, k8sNamespace) | |
} | |
@Suppress("UNCHECKED_CAST") | |
fun readToken(reader: Reader): String { | |
val yaml = Yaml(SafeConstructor()) | |
val config = yaml.load<Any>(reader) as Map<String, Any> | |
val users = config["users"] as List<Map<String, Any>> | |
val user = users.first()["user"] as Map<String, Any> | |
val authProvider = user["auth-provider"] as Map<String, Any> | |
val authConfig = authProvider["config"] as Map<String, String> | |
return authConfig["id-token"]!! | |
} | |
// get a list of s3 objects | |
fun objects(): List<S3ObjectSummary> { | |
var listObjects = amazonS3.listObjects(bucket) | |
val list: MutableList<S3ObjectSummary> = listObjects.objectSummaries | |
while (listObjects.isTruncated) { | |
listObjects = amazonS3.listNextBatchOfObjects(listObjects) | |
list += listObjects.objectSummaries | |
} | |
return list | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment