/
virtual_cluster_data_source.go
206 lines (184 loc) · 5.75 KB
/
virtual_cluster_data_source.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/warpstreamlabs/terraform-provider-warpstream/internal/provider/api"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &virtualClusterDataSource{}
_ datasource.DataSourceWithConfigure = &virtualClusterDataSource{}
)
// helper function to simplify the provider implementation.
func NewVirtualClusterDataSource() datasource.DataSource {
return &virtualClusterDataSource{}
}
// virtualClusterDataSource is the data source implementation.
type virtualClusterDataSource struct {
client *api.Client
}
// Metadata returns the data source type name.
func (d *virtualClusterDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_virtual_cluster"
}
// Schema defines the schema for the data source.
func (d *virtualClusterDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
Optional: true,
},
"name": schema.StringAttribute{
Computed: true,
Optional: true,
},
"type": schema.StringAttribute{
Computed: true,
},
"agent_pool_id": schema.StringAttribute{
Computed: true,
},
"agent_pool_name": schema.StringAttribute{
Computed: true,
},
"created_at": schema.StringAttribute{
Computed: true,
},
"default": schema.BoolAttribute{
Optional: true,
},
"configuration": schema.SingleNestedAttribute{
Attributes: map[string]schema.Attribute{
"auto_create_topic": schema.BoolAttribute{
Computed: true,
},
"default_num_partitions": schema.Int64Attribute{
Computed: true,
},
"default_retention_millis": schema.Int64Attribute{
Computed: true,
},
"enable_acls": schema.BoolAttribute{
Computed: true,
},
},
Computed: true,
},
"cloud": schema.SingleNestedAttribute{
Attributes: map[string]schema.Attribute{
"region": schema.StringAttribute{
Computed: true,
},
"provider": schema.StringAttribute{
Computed: true,
},
},
Computed: true,
},
},
}
}
func (d *virtualClusterDataSource) ConfigValidators(ctx context.Context) []datasource.ConfigValidator {
return []datasource.ConfigValidator{
datasourcevalidator.ExactlyOneOf(
path.MatchRoot("id"),
path.MatchRoot("name"),
path.MatchRoot("default"),
),
}
}
// Read refreshes the Terraform state with the latest data.
func (d *virtualClusterDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data virtualClusterModel
diags := req.Config.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
var vc *api.VirtualCluster
var err error
if data.Default.ValueBool() {
vc, err = d.client.GetDefaultCluster()
} else if data.Name.ValueString() != "" {
vc, err = d.client.FindVirtualCluster(data.Name.ValueString())
} else {
vc, err = d.client.GetVirtualCluster(data.ID.ValueString())
}
if err != nil {
resp.Diagnostics.AddError(
"Unable to Read WarpStream Virtual Cluster",
err.Error(),
)
return
}
tflog.Debug(ctx, fmt.Sprintf("Virtual Cluster: %+v", *vc))
// Map response body to model
state := virtualClusterModel{
ID: types.StringValue(vc.ID),
Name: types.StringValue(vc.Name),
Type: types.StringValue(vc.Type),
AgentPoolID: types.StringValue(vc.AgentPoolID),
AgentPoolName: types.StringValue(vc.AgentPoolName),
CreatedAt: types.StringValue(vc.CreatedAt),
Configuration: data.Configuration,
Cloud: data.Cloud,
}
// Set state
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
cldState := virtualClusterCloudModel{
Provider: types.StringValue(vc.CloudProvider),
Region: types.StringValue(vc.Region),
}
diags = resp.State.SetAttribute(ctx, path.Root("cloud"), cldState)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Read virtual cluster configuration
cfg, err := d.client.GetConfiguration(*vc)
if err != nil {
resp.Diagnostics.AddError(
"Unable to Read configuration of Virtual Cluster with ID="+vc.ID,
err.Error(),
)
return
}
cfgState := virtualClusterConfigurationModel{
AclsEnabled: types.BoolValue(cfg.AclsEnabled),
AutoCreateTopic: types.BoolValue(cfg.AutoCreateTopic),
DefaultNumPartitions: types.Int64Value(cfg.DefaultNumPartitions),
DefaultRetention: types.Int64Value(cfg.DefaultRetentionMillis),
}
// Set configuration state
diags = resp.State.SetAttribute(ctx, path.Root("configuration"), cfgState)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
// Configure adds the provider configured client to the data source.
func (d *virtualClusterDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*api.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *api.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = client
}