forked from content-services/content-sources-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
topics.go
82 lines (74 loc) · 2.7 KB
/
topics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package event
import (
"github.com/content-services/content-sources-backend/pkg/event/schema"
clowder "github.com/redhatinsights/app-common-go/pkg/api/v1"
"github.com/rs/zerolog/log"
)
// TopicMap is used to map between real and internal topics, this is
// it could be that the name we indicate for the topics into the
// clowderapp resource be different from the real created in kafka,
// so this type allow to preproce the mappings, and use them when
// needed to translate them into the producer and consumer functions
type TopicTranslation struct {
internalToReal map[string]string
realToInternal map[string]string
}
// It store the mapping between the internal topic managed by
// the service and the real topic managed by kafka
var TopicTranslationConfig *TopicTranslation = nil
// NewDefaultTopicMap Build a default topic map that map
// all the allowed topics to itselfs
// Return A TopicMap initialized as default values
func NewTopicTranslationWithDefaults() *TopicTranslation {
var tm *TopicTranslation = &TopicTranslation{
internalToReal: make(map[string]string),
realToInternal: make(map[string]string),
}
for _, topic := range schema.AllowedTopics {
tm.internalToReal[topic] = topic
tm.realToInternal[topic] = topic
}
return tm
}
// NewTopicTranslationWithClowder Build a topic map based into the
// clowder configuration.
func NewTopicTranslationWithClowder(cfg *clowder.AppConfig) *TopicTranslation {
if cfg == nil {
return NewTopicTranslationWithDefaults()
}
var tm *TopicTranslation = &TopicTranslation{
internalToReal: make(map[string]string),
realToInternal: make(map[string]string),
}
for _, topic := range cfg.Kafka.Topics {
tm.internalToReal[topic.RequestedName] = topic.Name
tm.realToInternal[topic.Name] = topic.RequestedName
log.Debug().Str(topic.RequestedName, topic.Name).Msg("internalToReal")
log.Debug().Str(topic.Name, topic.RequestedName).Msg("realToInternal")
}
return tm
}
// GetInternal translates the topic's "Name" to the "RequestedName".
// This will be used by consumers.
// Returns an empty string when the topic is not found
// Example:
// "name": "platform-tmp-12345",
// "requestedName": "platform.notifications.ingress"
func (tm *TopicTranslation) GetInternal(realTopic string) string {
if val, ok := tm.realToInternal[realTopic]; ok {
return val
}
return ""
}
// GetReal translates the topic's "RequestedName" to the "Name".
// This will be used by producers.
// Returns an empty string when the topic is not found.
// Example:
// "name": "platform-tmp-12345",
// "requestedName": "platform.notifications.ingress"
func (tm *TopicTranslation) GetReal(internalTopic string) string {
if val, ok := tm.internalToReal[internalTopic]; ok {
return val
}
return ""
}