-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduces a new Schema Registry Client wrapper around the lib
- Loading branch information
Dominik Liebler
committed
Jan 3, 2021
1 parent
7517e35
commit 0531050
Showing
17 changed files
with
331 additions
and
166 deletions.
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
70 changes: 70 additions & 0 deletions
70
src/main/kotlin/dev/domnikl/schema_registry_gitops/SchemaRegistryClient.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,70 @@ | ||
package dev.domnikl.schema_registry_gitops | ||
|
||
import io.confluent.kafka.schemaregistry.avro.AvroSchema | ||
import io.confluent.kafka.schemaregistry.client.SchemaRegistryClient | ||
import io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException | ||
|
||
class SchemaRegistryClient(private val schemaRegistryClient: SchemaRegistryClient) { | ||
fun subjects(): List<String> { | ||
return schemaRegistryClient.allSubjects.toList() | ||
} | ||
|
||
fun globalCompatibility(): Compatibility { | ||
return Compatibility.valueOf(schemaRegistryClient.getCompatibility("")) | ||
} | ||
|
||
fun updateGlobalCompatibility(compatibility: Compatibility): Compatibility { | ||
return Compatibility.valueOf(schemaRegistryClient.updateCompatibility("", compatibility.toString())) | ||
} | ||
|
||
fun compatibility(subject: String): Compatibility { | ||
return handleNotExisting { | ||
Compatibility.valueOf(schemaRegistryClient.getCompatibility(subject)) | ||
} ?: Compatibility.NONE | ||
} | ||
|
||
fun updateCompatibility(subject: Subject): Compatibility { | ||
return Compatibility.valueOf(schemaRegistryClient.updateCompatibility(subject.name, subject.compatibility.toString())) | ||
} | ||
|
||
fun testCompatibility(subject: Subject): Boolean { | ||
return schemaRegistryClient.testCompatibility(subject.name, subject.schema) | ||
} | ||
|
||
fun getLatestSchema(subject: String): AvroSchema { | ||
return AvroSchema(schemaRegistryClient.getLatestSchemaMetadata(subject).schema) | ||
} | ||
|
||
fun create(subject: Subject): Int { | ||
return schemaRegistryClient.register(subject.name, subject.schema) | ||
} | ||
|
||
fun evolve(subject: Subject): Int { | ||
return schemaRegistryClient.register(subject.name, subject.schema) | ||
} | ||
|
||
fun version(subject: Subject): Int? { | ||
return handleNotExisting { | ||
schemaRegistryClient.getVersion(subject.name, subject.schema) | ||
} | ||
} | ||
|
||
private fun <V> handleNotExisting(f: () -> V?): V? { | ||
return try { | ||
f() | ||
} catch (e: RestClientException) { | ||
when (e.errorCode) { | ||
ERROR_CODE_SUBJECT_NOT_FOUND -> null | ||
ERROR_CODE_VERSION_NOT_FOUND -> null | ||
ERROR_CODE_SCHEMA_NOT_FOUND -> null | ||
else -> throw e | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
private const val ERROR_CODE_SUBJECT_NOT_FOUND = 40401 | ||
private const val ERROR_CODE_VERSION_NOT_FOUND = 40402 | ||
private const val ERROR_CODE_SCHEMA_NOT_FOUND = 40403 | ||
} | ||
} |
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: 5 additions & 14 deletions
19
src/main/kotlin/dev/domnikl/schema_registry_gitops/state/Dumper.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
6 changes: 3 additions & 3 deletions
6
src/main/kotlin/dev/domnikl/schema_registry_gitops/state/Validator.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 |
---|---|---|
@@ -1,18 +1,18 @@ | ||
package dev.domnikl.schema_registry_gitops.state | ||
|
||
import dev.domnikl.schema_registry_gitops.SchemaRegistryClient | ||
import dev.domnikl.schema_registry_gitops.State | ||
import dev.domnikl.schema_registry_gitops.Subject | ||
import io.confluent.kafka.schemaregistry.client.SchemaRegistryClient | ||
|
||
class Validator(private val client: SchemaRegistryClient) { | ||
fun validate(state: State) = state.subjects.filterNot { isCompatible(it) }.map { it.name } | ||
|
||
private fun isCompatible(subject: Subject): Boolean { | ||
// if subject does not yet exist, it's always valid (as long as the schema itself is valid) | ||
if (!client.allSubjects.contains(subject.name)) { | ||
if (!client.subjects().contains(subject.name)) { | ||
return true | ||
} | ||
|
||
return client.testCompatibility(subject.name, subject.schema) | ||
return client.testCompatibility(subject) | ||
} | ||
} |
Oops, something went wrong.