Skip to content

Instantly share code, notes, and snippets.

@cakism
Created March 21, 2019 12:27
Show Gist options
  • Save cakism/2a8556f31828ab57ce3e40075a6dbec1 to your computer and use it in GitHub Desktop.
Save cakism/2a8556f31828ab57ce3e40075a6dbec1 to your computer and use it in GitHub Desktop.
An example client to Google Cloud Runtime Config, built in Kotlin with the Cloud Runtime Config API Java Lib
package com.experiments.services
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport
import com.google.api.client.http.HttpRequestInitializer
import com.google.api.client.json.jackson2.JacksonFactory
import com.google.api.services.runtimeconfig.v1beta1.CloudRuntimeConfig
import com.google.api.services.runtimeconfig.v1beta1.CloudRuntimeConfigScopes
import com.google.api.services.runtimeconfig.v1beta1.model.Variable
import com.google.api.services.runtimeconfig.v1beta1.model.WatchVariableRequest
import lib.createLogger
import java.util.*
class RuntimeConfigService {
private val log = createLogger()
private val PROJECT = "YOUR-PROJECT-HERE" //Get your project code from the google cloud console
private val CONFIG = "myConfigTest"
private val SERVICE_ACCOUNT_JSON = "service-accounts/MY_SERVICE_ACCOUNT.json"
private fun getVariablesPath(key: String)= "projects/$PROJECT/configs/$CONFIG/variables/$key"
private val RUNTIME_CONFIG_CLIENT by lazy {
val httpTransport = GoogleNetHttpTransport.newTrustedTransport()
val jacksonFactory = JacksonFactory.getDefaultInstance()
val googleCredential = authorize()
CloudRuntimeConfig(httpTransport, jacksonFactory, setHttpTimeout(googleCredential))
}
fun create(key: String, value: String) {
require(key.length < 256)
val response = RUNTIME_CONFIG_CLIENT.projects().configs().variables()
.create(getVariablesPath(key), Variable().setText(value)).execute()
log.info("Created $key with value $value in GCP runtime config. Response: $response")
}
fun update(key: String, value: String) {
require(key.length < 256)
val response = RUNTIME_CONFIG_CLIENT.projects().configs().variables()
.update(getVariablesPath(key), Variable().setText(value)).execute()
log.info("Updated $key with value $value in GCP runtime config. Response: $response")
}
fun get(key: String): Variable? {
require(key.length < 256)
val response =
RUNTIME_CONFIG_CLIENT.Projects().Configs().variables().get(getVariablesPath(key)).execute()
log.info("Fetching $key with value $response from GCP runtime config")
return response
}
fun delete(key: String) {
require(key.length < 256)
RUNTIME_CONFIG_CLIENT.Projects().Configs().variables().delete(getVariablesPath(key)).execute()
log.info("Deleted $key from GCP runtime config")
}
fun watch(key: String): Variable? {
require(key.length < 256)
val response =
RUNTIME_CONFIG_CLIENT.Projects().Configs().variables().watch(getVariablesPath(key), WatchVariableRequest()).execute()
log.info("Fetching $key with value $response from GCP runtime config")
return response
}
private fun authorize() = GoogleCredential
.fromStream(this::class.java.classLoader.getResource(SERVICE_ACCOUNT_JSON).openStream())
.createScoped(Collections.singleton(CloudRuntimeConfigScopes.CLOUDRUNTIMECONFIG))
private fun setHttpTimeout(requestInitializer: HttpRequestInitializer): HttpRequestInitializer {
return HttpRequestInitializer {
requestInitializer.initialize(it)
it.connectTimeout = 3 * 60000
it.readTimeout = 3 * 60000
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment