Skip to content

Commit

Permalink
Support importing config values
Browse files Browse the repository at this point in the history
  • Loading branch information
Mongey committed Dec 28, 2017
1 parent b835b2e commit d57e817
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
11 changes: 10 additions & 1 deletion kafka/resource_kafka_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ func topicDelete(d *schema.ResourceData, meta interface{}) error {

func topicRead(d *schema.ResourceData, meta interface{}) error {
name := d.Id()
c := meta.(*Client).client
client := meta.(*Client)
c := client.client
topics, err := c.Topics()

if err != nil {
Expand All @@ -143,7 +144,15 @@ func topicRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] ReplicationFactor %d from Kafka", r)
d.Set("replication_factor", r)
}
configToSave, err := ConfigForTopic(t, client.config.Brokers)
if err != nil {
log.Printf("[ERROR] Could not get config for topic %s: %s", t, err)
return err
}

d.Set("config", configToSave)
}

return nil
}
}
Expand Down
33 changes: 33 additions & 0 deletions kafka/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,39 @@ type topicConfig struct {
Config map[string]*string
}

func ConfigForTopic(topic string, brokers []string) (map[string]string, error) {
confToSave := map[string]string{}
request := &samara.DescribeConfigsRequest{
Resources: []*samara.Resource{
&samara.Resource{
T: samara.TopicResource,
Name: topic,
ConfigNames: []string{"segment.ms"},
},
},
}

broker, err := AvailableBrokerFromList(brokers)
if err != nil {
return confToSave, err
}
cr, err := broker.DescribeConfigs(request)
if err != nil {
return confToSave, err
}

if len(cr.Resources) > 0 && len(cr.Resources[0].Configs) > 0 {
for _, conf := range cr.Resources[0].Configs {
if conf.Default {
continue
}
log.Printf("[DEBUG] configs %s", conf.Name)
log.Printf("[DEBUG] configs %s", conf.Value)
confToSave[conf.Name] = conf.Value
}
}
return confToSave, nil
}
func metaToTopicConfig(d *schema.ResourceData, meta interface{}) topicConfig {
topicName := d.Get("name").(string)
partitions := d.Get("partitions").(int)
Expand Down
2 changes: 1 addition & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ resource "kafka_topic" "foo2" {
partitions = 1

config = {
"segment.ms" = "10000"
"segment.ms" = "20000"
}
}

0 comments on commit d57e817

Please sign in to comment.