Skip to content

Commit 37b9f86

Browse files
committed
Fix after review
1 parent dfe9a53 commit 37b9f86

3 files changed

Lines changed: 54 additions & 35 deletions

File tree

internal/provider/clickhouse_cluster_resource.go

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type clickhouseClusterModel struct {
5151
// https://github.com/doublecloud/api/blob/main/doublecloud/v1/maintenance.proto
5252
// MaintenanceWindow *maintenanceWindow `tfsdk:"maintenance_window"`
5353

54-
CustomCertificate types.Object `tfsdk:"custom_certificate"`
54+
CustomCertificate *clickhouseCustomCertificate `tfsdk:"custom_certificate"`
5555
}
5656

5757
type clickhouseClusterResources struct {
@@ -112,6 +112,40 @@ func (m *clickhouseClusterResources) convert() (*clickhouse.ClusterResources, di
112112
return &r, diags
113113
}
114114

115+
type clickhouseCustomCertificate struct {
116+
Certificate types.String `tfsdk:"certificate"`
117+
Key types.String `tfsdk:"key"`
118+
RootCA types.String `tfsdk:"root_ca"`
119+
}
120+
121+
func (cc *clickhouseCustomCertificate) convert() (*clickhouse.CustomCertificate, diag.Diagnostics) {
122+
res := clickhouse.CustomCertificate{
123+
Enabled: false,
124+
}
125+
126+
var diags diag.Diagnostics
127+
128+
if cc != nil {
129+
if !cc.Certificate.IsNull() && !cc.Key.IsNull() {
130+
res.Enabled = true
131+
res.Certificate = &wrappers.BytesValue{Value: []byte(cc.Certificate.ValueString())}
132+
res.Key = &wrappers.BytesValue{Value: []byte(cc.Key.ValueString())}
133+
if !cc.RootCA.IsNull() {
134+
res.RootCa = &wrappers.BytesValue{Value: []byte(cc.RootCA.ValueString())}
135+
}
136+
} else {
137+
if cc.Certificate.IsNull() {
138+
diags.AddError("missed certificate", "for custom certificate must be both certificate and key")
139+
}
140+
if cc.Key.IsNull() {
141+
diags.AddError("missed certificate", "for custom certificate must be both certificate and key")
142+
}
143+
}
144+
}
145+
146+
return &res, diags
147+
}
148+
115149
type clickhouseClusterResourcesClickhouse struct {
116150
ResourcePresetId types.String `tfsdk:"resource_preset_id"`
117151
MinResourcePresetId types.String `tfsdk:"min_resource_preset_id"`
@@ -575,6 +609,10 @@ func createClickhouseClusterRequest(m *clickhouseClusterModel) (*clickhouse.Crea
575609
}
576610
// TODO: mw
577611

612+
if m.CustomCertificate != nil {
613+
diags.AddError("custom_certificate exists", "custom_certificate can't be applied during cluster creation")
614+
}
615+
578616
return rq, diags
579617
}
580618

@@ -672,20 +710,9 @@ func updateClickhouseCluster(m *clickhouseClusterModel) (*clickhouse.UpdateClust
672710
rq.Access = access
673711
}
674712

675-
cc := m.CustomCertificate.Attributes()
676-
rq.CustomCertificate = &clickhouse.CustomCertificate{
677-
Enabled: false,
678-
}
679-
certificate, certOk := cc["certificate"]
680-
key, keyOk := cc["key"]
681-
rq.CustomCertificate.Enabled = certOk && keyOk
682-
if rq.CustomCertificate.Enabled {
683-
rq.CustomCertificate.Certificate = &wrappers.BytesValue{Value: []byte(certificate.(types.String).ValueString())}
684-
rq.CustomCertificate.Key = &wrappers.BytesValue{Value: []byte(key.(types.String).ValueString())}
685-
if rootCa, ok := cc["root_ca"]; ok {
686-
rq.CustomCertificate.RootCa = &wrappers.BytesValue{Value: []byte(rootCa.(types.String).ValueString())}
687-
}
688-
}
713+
cc, d := m.CustomCertificate.convert()
714+
rq.CustomCertificate = cc
715+
diags.Append(d...)
689716

690717
return rq, diags
691718
}
@@ -779,10 +806,10 @@ func (m *clickhouseClusterModel) parse(rs *clickhouse.Cluster) diag.Diagnostics
779806
}
780807

781808
oldKey := ""
782-
if key, ok := m.CustomCertificate.Attributes()["key"]; ok {
783-
oldKey = key.String()
809+
if m.CustomCertificate != nil && !m.CustomCertificate.Key.IsNull() {
810+
oldKey = m.CustomCertificate.Key.String()
784811
}
785-
m.CustomCertificate = parseClickhouseCustomCertificate(rs.GetCustomCertificate(), oldKey, diags).convert(diags)
812+
m.CustomCertificate = parseClickhouseCustomCertificate(rs.GetCustomCertificate(), oldKey, diags).convert()
786813

787814
// parse MW
788815
return diags

internal/provider/clickhouse_data_source.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,16 @@ type ClickhouseCustomCertificate struct {
8989
RootCA types.String `tfsdk:"root_ca"`
9090
}
9191

92-
func (cc *ClickhouseCustomCertificate) convert(diags diag.Diagnostics) types.Object {
93-
attrTypeMap := map[string]attr.Type{
94-
"certificate": types.StringType,
95-
"key": types.StringType,
96-
"root_ca": types.StringType,
97-
}
92+
func (cc *ClickhouseCustomCertificate) convert() *clickhouseCustomCertificate {
9893
if cc == nil {
99-
return types.ObjectNull(attrTypeMap)
94+
return nil
10095
}
101-
res, d := types.ObjectValue(attrTypeMap,
102-
map[string]attr.Value{
103-
"certificate": cc.Certificate,
104-
"key": cc.Key,
105-
"root_ca": cc.RootCA,
106-
},
107-
)
108-
diags.Append(d...)
109-
return res
96+
res := clickhouseCustomCertificate{
97+
Certificate: cc.Certificate,
98+
Key: cc.Key,
99+
RootCA: cc.RootCA,
100+
}
101+
return &res
110102
}
111103

112104
func (d *ClickhouseDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {

internal/provider/helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func (*clickhouseCustomCertificateValidator) ValidateObject(ctx context.Context,
162162
if (certificatePresent && !keyPresent) || (!certificatePresent && keyPresent) {
163163
rsp.Diagnostics.Append(validatordiag.InvalidAttributeCombinationDiagnostic(
164164
req.Path,
165-
`Must be both attributea "certificate" and "key"`,
165+
`Must be both attributes "certificate" and "key"`,
166166
))
167167
}
168168

0 commit comments

Comments
 (0)