-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
data_source_kafka_topic.go
executable file
·68 lines (58 loc) · 1.69 KB
/
data_source_kafka_topic.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
package kafka
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func kafkaTopicDataSource() *schema.Resource {
return &schema.Resource{
Read: dataSourceTopicRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the topic.",
},
"partitions": {
Type: schema.TypeInt,
Computed: true,
Description: "Number of partitions.",
},
"replication_factor": {
Type: schema.TypeInt,
Computed: true,
Description: "Number of replicas.",
},
"config": {
Type: schema.TypeMap,
Computed: true,
Description: "A map of string k/v attributes.",
Elem: schema.TypeString,
},
},
}
}
func dataSourceTopicRead(d *schema.ResourceData, meta interface{}) error {
// Unlike the resource topicRead, there is no pre-existing ID. We must use the 'name' to look up the resource.
// See https://learn.hashicorp.com/tutorials/terraform/provider-create?in=terraform/providers#implement-read
name := d.Get("name").(string)
client := meta.(*LazyClient)
topic, err := client.ReadTopic(name, true)
if err != nil {
log.Printf("[ERROR] Error getting topic %s from Kafka: %s", name, err)
_, ok := err.(TopicMissingError)
if ok {
return fmt.Errorf("Could not find topic '%s'", name)
}
return err
}
log.Printf("[DEBUG] Setting the state from Kafka %v", topic)
errSet := errSetter{d: d}
errSet.Set("name", topic.Name)
errSet.Set("partitions", topic.Partitions)
errSet.Set("replication_factor", topic.ReplicationFactor)
errSet.Set("config", topic.Config)
// Set the id to the name
d.SetId(name)
return errSet.err
}