-
Notifications
You must be signed in to change notification settings - Fork 645
/
data_source_ibm_event_streams_topic.go
75 lines (70 loc) · 2.19 KB
/
data_source_ibm_event_streams_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
69
70
71
72
73
74
75
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package eventstreams
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceIBMEventStreamsTopic() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMEventStreamsTopicRead,
Schema: map[string]*schema.Schema{
"resource_instance_id": {
Type: schema.TypeString,
Required: true,
Description: "The CRN of the Event Streams instance",
},
"kafka_http_url": {
Type: schema.TypeString,
Computed: true,
Description: "The API endpoint for interacting with Event Streams REST API",
},
"kafka_brokers_sasl": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Kafka brokers addresses for interacting with Kafka native API",
},
"name": {
Type: schema.TypeString,
Description: "The name of the topic",
Required: true,
},
"partitions": {
Type: schema.TypeInt,
Description: "The number of partitions of the topic",
Computed: true,
},
"config": {
Type: schema.TypeMap,
Description: "The configuration parameters of the topic.",
Computed: true,
},
},
}
}
func dataSourceIBMEventStreamsTopicRead(d *schema.ResourceData, meta interface{}) error {
adminClient, instanceCRN, err := createSaramaAdminClient(d, meta)
if err != nil {
log.Printf("[DEBUG]dataSourceIBMEventStreamsTopicRead createSaramaAdminClient err %s", err)
return err
}
topics, err := adminClient.ListTopics()
if err != nil {
log.Printf("[DEBUG]dataSourceIBMEventStreamsTopicRead ListTopics err %s", err)
return err
}
topicName := d.Get("name").(string)
for name := range topics {
if name == topicName {
topicID := getTopicID(instanceCRN, topicName)
d.SetId(topicID)
log.Printf("[INFO]dataSourceIBMEventStreamsTopicRead set topic ID to %s", topicID)
d.Set("resource_instance_id", instanceCRN)
return nil
}
}
log.Printf("[DEBUG]dataSourceIBMEventStreamsTopicRead topic %s does not exist", topicName)
return fmt.Errorf("topic %s does not exist", topicName)
}