2023-01-09 18:15:35 +05:30
|
|
|
package schema
|
2021-03-30 13:16:00 +05:30
|
|
|
|
|
|
|
|
import (
|
2023-01-09 18:15:35 +05:30
|
|
|
"com.navi.medici.janus/config"
|
2021-05-12 15:26:13 +05:30
|
|
|
"fmt"
|
2021-03-30 13:16:00 +05:30
|
|
|
"github.com/riferrei/srclient"
|
2023-01-09 18:15:35 +05:30
|
|
|
"log"
|
|
|
|
|
"strings"
|
2021-03-30 13:16:00 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2023-01-09 18:15:35 +05:30
|
|
|
SchemaRegistryEndpoint string
|
|
|
|
|
TopicList []string
|
|
|
|
|
SchemaVersionMap = make(map[string]int)
|
2021-03-30 13:16:00 +05:30
|
|
|
)
|
|
|
|
|
|
2023-01-09 18:15:35 +05:30
|
|
|
func InitializeSchemaHandler(configuration config.Configurations) {
|
|
|
|
|
SchemaRegistryEndpoint = configuration.SchemaRegistry.Endpoint
|
|
|
|
|
TopicList = strings.Split(configuration.SchemaRegistry.Topics, ",")
|
|
|
|
|
// initialize schema version map which contains latest schema version for topic(s)
|
|
|
|
|
GetSchemaVersions()
|
|
|
|
|
}
|
2021-03-30 13:16:00 +05:30
|
|
|
|
2021-05-07 20:35:35 +05:30
|
|
|
func GetSchemaVersions() {
|
2021-03-30 13:16:00 +05:30
|
|
|
|
2021-05-07 20:35:35 +05:30
|
|
|
schemaRegistryClient := srclient.CreateSchemaRegistryClient(SchemaRegistryEndpoint)
|
2021-03-30 13:16:00 +05:30
|
|
|
schemaRegistryClient.CodecCreationEnabled(false)
|
2021-05-07 20:35:35 +05:30
|
|
|
for _, topic := range TopicList {
|
2021-03-30 13:16:00 +05:30
|
|
|
schema, err := schemaRegistryClient.GetLatestSchema(topic, false)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println(err)
|
|
|
|
|
} else {
|
|
|
|
|
schemaId := schema.ID()
|
2021-03-30 16:03:09 +05:30
|
|
|
SchemaVersionMap[topic] = schemaId
|
2021-03-30 13:16:00 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 16:03:09 +05:30
|
|
|
log.Println(SchemaVersionMap)
|
2021-05-12 15:26:13 +05:30
|
|
|
}
|
2021-03-30 13:16:00 +05:30
|
|
|
|
2021-05-12 15:26:13 +05:30
|
|
|
func AddSchema(topic string, schemaType string, schema string) error {
|
|
|
|
|
|
|
|
|
|
schemaRegistryClient := srclient.CreateSchemaRegistryClient(SchemaRegistryEndpoint)
|
|
|
|
|
schemaRegistryClient.CodecCreationEnabled(false)
|
|
|
|
|
existingSchema, _ := schemaRegistryClient.GetLatestSchema(topic, false)
|
|
|
|
|
fmt.Println("EXISTINGSCHEMA: ")
|
|
|
|
|
fmt.Println(existingSchema)
|
|
|
|
|
|
|
|
|
|
if existingSchema != nil {
|
|
|
|
|
compatible, errorCompatible := schemaRegistryClient.IsSchemaCompatible(topic, schema, "latest", srclient.SchemaType(schemaType), false)
|
|
|
|
|
if errorCompatible != nil {
|
|
|
|
|
return errorCompatible
|
2023-01-09 18:15:35 +05:30
|
|
|
} else if compatible == false {
|
2021-05-12 15:26:13 +05:30
|
|
|
return fmt.Errorf("given schema not compatible with the latest version")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, errroCreate := schemaRegistryClient.CreateSchema(topic, schema, srclient.SchemaType(schemaType), false)
|
|
|
|
|
// fmt.Println("SCHEMA_ID: ")
|
|
|
|
|
// fmt.Println(schemaObj.ID())
|
|
|
|
|
|
|
|
|
|
if errroCreate != nil {
|
|
|
|
|
return errroCreate
|
|
|
|
|
} else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-01-09 18:15:35 +05:30
|
|
|
}
|