-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_source_cassandra_adaptive_repair.go
More file actions
147 lines (128 loc) · 5 KB
/
data_source_cassandra_adaptive_repair.go
File metadata and controls
147 lines (128 loc) · 5 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
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
package main
import (
"context"
"fmt"
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 = (*cassandraAdaptiveRepairDataSource)(nil)
var _ datasource.DataSourceWithConfigure = (*cassandraAdaptiveRepairDataSource)(nil)
type cassandraAdaptiveRepairDataSource struct {
client *axonopsClient.AxonopsHttpClient
}
func NewCassandraAdaptiveRepairDataSource() datasource.DataSource {
return &cassandraAdaptiveRepairDataSource{}
}
func (d *cassandraAdaptiveRepairDataSource) 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 *cassandraAdaptiveRepairDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_cassandra_adaptive_repair"
}
func (d *cassandraAdaptiveRepairDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Reads Cassandra adaptive repair settings for a cluster.",
Attributes: map[string]schema.Attribute{
"cluster_name": schema.StringAttribute{
Required: true,
Description: "The name of the cluster.",
},
"cluster_type": schema.StringAttribute{
Optional: true,
Computed: true,
Description: "The cluster type (cassandra or dse). Default: cassandra",
},
"active": schema.BoolAttribute{
Computed: true,
Description: "Whether adaptive repair is enabled.",
},
"parallelism": schema.Int64Attribute{
Computed: true,
Description: "Number of tables to repair concurrently.",
},
"gc_grace_threshold": schema.Int64Attribute{
Computed: true,
Description: "GC grace period threshold in seconds.",
},
"blacklisted_tables": schema.ListAttribute{
ElementType: types.StringType,
Computed: true,
Description: "List of tables excluded from repair.",
},
"filter_twcs_tables": schema.BoolAttribute{
Computed: true,
Description: "Whether TWCS tables are excluded.",
},
"segment_retries": schema.Int64Attribute{
Computed: true,
Description: "Maximum retry attempts per segment.",
},
"segments_per_vnode": schema.Int64Attribute{
Computed: true,
Description: "Number of segments per vnode.",
},
"segment_target_size_mb": schema.Int64Attribute{
Computed: true,
Description: "Target segment size in MB.",
},
},
}
}
type cassandraAdaptiveRepairDataSourceData struct {
ClusterName types.String `tfsdk:"cluster_name"`
ClusterType types.String `tfsdk:"cluster_type"`
Active types.Bool `tfsdk:"active"`
Parallelism types.Int64 `tfsdk:"parallelism"`
GcGraceThreshold types.Int64 `tfsdk:"gc_grace_threshold"`
BlacklistedTables types.List `tfsdk:"blacklisted_tables"`
FilterTwcsTables types.Bool `tfsdk:"filter_twcs_tables"`
SegmentRetries types.Int64 `tfsdk:"segment_retries"`
SegmentsPerVnode types.Int64 `tfsdk:"segments_per_vnode"`
SegmentTargetSizeMB types.Int64 `tfsdk:"segment_target_size_mb"`
}
func (d *cassandraAdaptiveRepairDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data cassandraAdaptiveRepairDataSourceData
diags := req.Config.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
clusterType := data.ClusterType.ValueString()
if clusterType == "" {
clusterType = "cassandra"
}
settings, err := d.client.GetCassandraAdaptiveRepair(clusterType, data.ClusterName.ValueString())
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read adaptive repair settings: %s", err))
return
}
data.ClusterType = types.StringValue(clusterType)
data.Active = types.BoolValue(settings.Active)
data.Parallelism = types.Int64Value(int64(settings.TableParallelism))
data.GcGraceThreshold = types.Int64Value(int64(settings.GcGraceThreshold))
data.FilterTwcsTables = types.BoolValue(settings.FilterTWCSTables)
data.SegmentRetries = types.Int64Value(int64(settings.SegmentRetries))
data.SegmentsPerVnode = types.Int64Value(int64(settings.SegmentsPerVnode))
data.SegmentTargetSizeMB = types.Int64Value(int64(settings.SegmentTargetSizeMB))
blacklisted := settings.BlacklistedTables
if blacklisted == nil {
blacklisted = []string{}
}
data.BlacklistedTables, diags = types.ListValueFrom(ctx, types.StringType, blacklisted)
resp.Diagnostics.Append(diags...)
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}