Skip to content

Commit

Permalink
Get latest version supported for DescribeConfigsRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
Mongey committed Feb 8, 2019
1 parent 34153d9 commit b1945ce
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 6 deletions.
58 changes: 52 additions & 6 deletions kafka/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ type TopicMissingError struct {
func (e TopicMissingError) Error() string { return e.msg }

type Client struct {
client sarama.Client
kafkaConfig *sarama.Config
config *Config
client sarama.Client
kafkaConfig *sarama.Config
config *Config
supportedAPIs map[int]int
}

type Config struct {
Expand Down Expand Up @@ -56,11 +57,41 @@ func NewClient(config *Config) (*Client, error) {
return nil, err
}

return &Client{
client := &Client{
client: c,
config: config,
kafkaConfig: kc,
}, kc.Validate()
}

err = kc.Validate()
if err != nil {
return client, err
}

client.populateAPIVersions()
return client, nil
}

func (c *Client) populateAPIVersions() {
log.Printf("[DEBUG] retrieving supported APIs from broker")
broker, err := c.client.Controller()
if err != nil {
log.Printf("[ERROR] Unable to populate supported API versions. Error retrieving controller: %s", err)
return
}

resp, err := broker.ApiVersions(&sarama.ApiVersionsRequest{})
if err != nil {
log.Printf("[ERROR] Unable to populate supported API versions. %s", err)
return
}

m := map[int]int{}
for _, v := range resp.ApiVersions {
log.Printf("[TRACE] API key %d. Min %d, Max %d", v.ApiKey, v.MinVersion, v.MaxVersion)
m[int(v.ApiKey)] = int(v.MaxVersion)
}
c.supportedAPIs = m
}

func (c *Client) DeleteTopic(t string) error {
Expand Down Expand Up @@ -237,10 +268,25 @@ func (client *Client) ReadTopic(name string) (Topic, error) {
return topic, err
}

func (c *Client) getDescribeConfigAPIVersion() int16 {
return int16(c.versionForKey(32, 1))
}

func (c *Client) versionForKey(apiKey, wantedMaxVersion int) int {
if maxSupportedVersion, ok := c.supportedAPIs[apiKey]; ok {
if maxSupportedVersion < wantedMaxVersion {
return maxSupportedVersion
}
return wantedMaxVersion
}

return 0
}

func (c *Client) topicConfig(topic string) (map[string]*string, error) {
conf := map[string]*string{}
request := &sarama.DescribeConfigsRequest{
Version: 1,
Version: c.getDescribeConfigAPIVersion(),
Resources: []*sarama.ConfigResource{
{
Type: sarama.TopicResource,
Expand Down
28 changes: 28 additions & 0 deletions kafka/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package kafka

import "testing"

func Test_ClientAPIVersion(t *testing.T) {
// Default to 0
client := &Client{}
maxVersion := client.versionForKey(32, 1)
if maxVersion != 0 {
t.Errorf("Got %d, expected %d", maxVersion, 0)
}

// use the max version the broker supports, if it's less than the requested
// version
client.supportedAPIs = map[int]int{32: 0}
maxVersion = client.versionForKey(32, 1)
if maxVersion != 0 {
t.Errorf("Got %d, expected %d", maxVersion, 0)
}

// while the broker supports 2, terraform-provider-kafka only supports 1, so
// use that
client.supportedAPIs = map[int]int{32: 2}
maxVersion = client.versionForKey(32, 1)
if maxVersion != 1 {
t.Errorf("Got %d, expected %d", maxVersion, 1)
}
}

0 comments on commit b1945ce

Please sign in to comment.