-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
data_source_kafka_topic.go
executable file
·66 lines (58 loc) · 1.66 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
package kafka
import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
)
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, false)
if err != nil {
log.Printf("[ERROR] Error getting topics %s from Kafka", err)
_, ok := err.(TopicMissingError)
if ok {
d.SetId("")
return nil
}
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
}