-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_source_kafka_topic.go
More file actions
111 lines (93 loc) · 3.22 KB
/
Copy pathdata_source_kafka_topic.go
File metadata and controls
111 lines (93 loc) · 3.22 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"context"
"fmt"
"strings"
axonopsClient "terraform-provider-axonops/client"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)
var _ datasource.DataSource = (*topicDataSource)(nil)
var _ datasource.DataSourceWithConfigure = (*topicDataSource)(nil)
type topicDataSource struct {
client *axonopsClient.AxonopsHttpClient
}
func NewKafkaTopicDataSource() datasource.DataSource {
return &topicDataSource{}
}
func (d *topicDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*axonopsClient.AxonopsHttpClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected DataSource Configure Type",
fmt.Sprintf("Expected *axonopsClient.AxonopsHttpClient, got: %T.", req.ProviderData),
)
return
}
d.client = client
}
func (d *topicDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_kafka_topic"
}
func (d *topicDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Reads a Kafka topic.",
Attributes: map[string]schema.Attribute{
"cluster_name": schema.StringAttribute{
Required: true,
Description: "The name of the Kafka cluster.",
},
"name": schema.StringAttribute{
Required: true,
Description: "The topic name.",
},
"partitions": schema.Int32Attribute{
Computed: true,
Description: "Number of partitions.",
},
"replication_factor": schema.Int32Attribute{
Computed: true,
Description: "Replication factor.",
},
"config": schema.MapAttribute{
Computed: true,
ElementType: types.StringType,
Description: "Topic configuration (keys use underscores instead of dots).",
},
},
}
}
type topicDataSourceData struct {
ClusterName types.String `tfsdk:"cluster_name"`
Name types.String `tfsdk:"name"`
Partitions types.Int32 `tfsdk:"partitions"`
ReplicationFactor types.Int32 `tfsdk:"replication_factor"`
Config map[string]types.String `tfsdk:"config"`
}
func (d *topicDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data topicDataSourceData
diags := req.Config.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
topic, err := d.client.GetTopic(data.Name.ValueString(), data.ClusterName.ValueString())
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read topic: %s", err))
return
}
data.Partitions = types.Int32Value(topic.Partitions)
data.ReplicationFactor = types.Int32Value(topic.ReplicationFactor)
config := make(map[string]types.String)
for _, c := range topic.Config {
key := strings.ReplaceAll(c.Name, ".", "_")
config[key] = types.StringValue(c.Value)
}
data.Config = config
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}