Skip to content

Commit 4cc0725

Browse files
committed
Fix after review
1 parent dfe9a53 commit 4cc0725

7 files changed

Lines changed: 171 additions & 114 deletions

File tree

docs/data-sources/clickhouse.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,16 @@ Read-Only:
4141

4242
- `host` (String) Host to connect to
4343
- `https_port` (Number) Port to connect to using the HTTPS protocol
44+
- `https_port_ctls` (Number) Port to connect to using the HTTPS protocol with custom TLS certificate
4445
- `https_uri` (String) URI to connect to using the HTTPS protocol
46+
- `https_uri_ctls` (String) URI to connect to using the HTTPS protocol with custom TLS certificate
4547
- `jdbc_uri` (String) URI to connect to using the JDBC protocol
4648
- `native_protocol` (String) Connection string for the ClickHouse native protocol
49+
- `native_protocol_ctls` (String) Connection string for the ClickHouse native protocol with custom TLS certificate
4750
- `odbc_uri` (String) URI to connect to using the ODBC protocol
4851
- `password` (String, Sensitive) Password for the ClickHouse user
4952
- `tcp_port_secure` (Number) Port to connect to using the TCP/native protocol
53+
- `tcp_port_secure_ctls` (Number) Port to connect to using the TCP/native protocol with custom TLS certificate
5054
- `user` (String) ClickHouse user
5155

5256

@@ -67,10 +71,14 @@ Read-Only:
6771

6872
- `host` (String) Host to connect to
6973
- `https_port` (Number) Port to connect to using the HTTPS protocol
74+
- `https_port_ctls` (Number) Port to connect to using the HTTPS protocol with custom TLS certificate
7075
- `https_uri` (String) URI to connect to using the HTTPS protocol
76+
- `https_uri_ctls` (String) URI to connect to using the HTTPS protocol with custom TLS certificate
7177
- `jdbc_uri` (String) URI to connect to using the JDBC protocol
7278
- `native_protocol` (String) Connection string for the ClickHouse native protocol
79+
- `native_protocol_ctls` (String) Connection string for the ClickHouse native protocol with custom TLS certificate
7380
- `odbc_uri` (String) URI to connect to using the ODBC protocol
7481
- `password` (String, Sensitive) Password for the ClickHouse user
7582
- `tcp_port_secure` (Number) Port to connect to using the TCP/native protocol
83+
- `tcp_port_secure_ctls` (Number) Port to connect to using the TCP/native protocol with custom TLS certificate
7684
- `user` (String) ClickHouse user

docs/resources/clickhouse_cluster.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,16 @@ Read-Only:
233233

234234
- `host` (String) Host to connect to
235235
- `https_port` (Number) Port to connect to using the HTTPS protocol
236+
- `https_port_ctls` (Number) Port to connect to using the HTTPS protocol with custom TLS certificate
236237
- `https_uri` (String) URI to connect to using the HTTPS protocol
238+
- `https_uri_ctls` (String) URI to connect to using the HTTPS protocol with custom TLS certificate
237239
- `jdbc_uri` (String) URI to connect to using the JDBC protocol
238240
- `native_protocol` (String) Connection string for the ClickHouse native protocol
241+
- `native_protocol_ctls` (String) Connection string for the ClickHouse native protocol with custom TLS certificate
239242
- `odbc_uri` (String) URI to connect to using the ODBC protocol
240243
- `password` (String, Sensitive) Password for the ClickHouse user
241244
- `tcp_port_secure` (Number) Port to connect to using the TCP/native protocol
245+
- `tcp_port_secure_ctls` (Number) Port to connect to using the TCP/native protocol with custom TLS certificate
242246
- `user` (String) ClickHouse user
243247

244248

@@ -249,10 +253,14 @@ Read-Only:
249253

250254
- `host` (String) Host to connect to
251255
- `https_port` (Number) Port to connect to using the HTTPS protocol
256+
- `https_port_ctls` (Number) Port to connect to using the HTTPS protocol with custom TLS certificate
252257
- `https_uri` (String) URI to connect to using the HTTPS protocol
258+
- `https_uri_ctls` (String) URI to connect to using the HTTPS protocol with custom TLS certificate
253259
- `jdbc_uri` (String) URI to connect to using the JDBC protocol
254260
- `native_protocol` (String) Connection string for the ClickHouse native protocol
261+
- `native_protocol_ctls` (String) Connection string for the ClickHouse native protocol with custom TLS certificate
255262
- `odbc_uri` (String) URI to connect to using the ODBC protocol
256263
- `password` (String, Sensitive) Password for the ClickHouse user
257264
- `tcp_port_secure` (Number) Port to connect to using the TCP/native protocol
265+
- `tcp_port_secure_ctls` (Number) Port to connect to using the TCP/native protocol with custom TLS certificate
258266
- `user` (String) ClickHouse user

internal/provider/clickhouse_cluster_resource.go

Lines changed: 75 additions & 19 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"`
@@ -350,6 +384,26 @@ func clickhouseConenctionInfoSchema() map[string]schema.Attribute {
350384
MarkdownDescription: "URI to connect to using the ODBC protocol",
351385
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
352386
},
387+
"https_port_ctls": schema.Int64Attribute{
388+
Computed: true,
389+
MarkdownDescription: "Port to connect to using the HTTPS protocol with custom TLS certificate",
390+
PlanModifiers: []planmodifier.Int64{int64planmodifier.UseStateForUnknown()},
391+
},
392+
"tcp_port_secure_ctls": schema.Int64Attribute{
393+
Computed: true,
394+
MarkdownDescription: "Port to connect to using the TCP/native protocol with custom TLS certificate",
395+
PlanModifiers: []planmodifier.Int64{int64planmodifier.UseStateForUnknown()},
396+
},
397+
"native_protocol_ctls": schema.StringAttribute{
398+
Computed: true,
399+
MarkdownDescription: "Connection string for the ClickHouse native protocol with custom TLS certificate",
400+
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
401+
},
402+
"https_uri_ctls": schema.StringAttribute{
403+
Computed: true,
404+
MarkdownDescription: "URI to connect to using the HTTPS protocol with custom TLS certificate",
405+
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
406+
},
353407
}
354408
}
355409

@@ -359,16 +413,26 @@ func clickhouseCustomCertificateSchema() map[string]schema.Attribute {
359413
Optional: true,
360414
MarkdownDescription: "Public certificate",
361415
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
416+
Validators: []validator.String{
417+
stringvalidator.AlsoRequires(path.MatchRelative().AtParent().AtName("key")),
418+
},
362419
},
363420
"key": schema.StringAttribute{
364421
Optional: true,
365422
MarkdownDescription: "Private certificate key",
366423
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
424+
Validators: []validator.String{
425+
stringvalidator.AlsoRequires(path.MatchRelative().AtParent().AtName("certificate")),
426+
},
367427
},
368428
"root_ca": schema.StringAttribute{
369429
Optional: true,
370430
MarkdownDescription: "Root certificate",
371431
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
432+
Validators: []validator.String{
433+
stringvalidator.AlsoRequires(path.MatchRelative().AtParent().AtName("key")),
434+
stringvalidator.AlsoRequires(path.MatchRelative().AtParent().AtName("certificate")),
435+
},
372436
},
373437
}
374438
}
@@ -520,7 +584,6 @@ func (r *ClickhouseClusterResource) Schema(ctx context.Context, req resource.Sch
520584
Attributes: clickhouseCustomCertificateSchema(),
521585
PlanModifiers: []planmodifier.Object{objectplanmodifier.UseStateForUnknown()},
522586
MarkdownDescription: "Custom TLS certificate",
523-
Validators: []validator.Object{&clickhouseCustomCertificateValidator{}},
524587
},
525588
},
526589
}
@@ -575,6 +638,10 @@ func createClickhouseClusterRequest(m *clickhouseClusterModel) (*clickhouse.Crea
575638
}
576639
// TODO: mw
577640

641+
if m.CustomCertificate != nil {
642+
diags.AddError("custom_certificate exists", "custom_certificate can't be applied during cluster creation")
643+
}
644+
578645
return rq, diags
579646
}
580647

@@ -672,20 +739,9 @@ func updateClickhouseCluster(m *clickhouseClusterModel) (*clickhouse.UpdateClust
672739
rq.Access = access
673740
}
674741

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-
}
742+
cc, d := m.CustomCertificate.convert()
743+
rq.CustomCertificate = cc
744+
diags.Append(d...)
689745

690746
return rq, diags
691747
}
@@ -779,10 +835,10 @@ func (m *clickhouseClusterModel) parse(rs *clickhouse.Cluster) diag.Diagnostics
779835
}
780836

781837
oldKey := ""
782-
if key, ok := m.CustomCertificate.Attributes()["key"]; ok {
783-
oldKey = key.String()
838+
if m.CustomCertificate != nil && !m.CustomCertificate.Key.IsNull() {
839+
oldKey = m.CustomCertificate.Key.String()
784840
}
785-
m.CustomCertificate = parseClickhouseCustomCertificate(rs.GetCustomCertificate(), oldKey, diags).convert(diags)
841+
m.CustomCertificate = parseClickhouseCustomCertificate(rs.GetCustomCertificate(), oldKey, diags).convert()
786842

787843
// parse MW
788844
return diags

internal/provider/clickhouse_cluster_resource_test.go

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"text/template"
1111

1212
"github.com/doublecloud/go-genproto/doublecloud/clickhouse/v1"
13-
"github.com/hashicorp/terraform-plugin-framework/attr"
1413
"github.com/hashicorp/terraform-plugin-framework/types"
1514
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1615
)
@@ -121,18 +120,24 @@ func TestAccClickhouseClusterResource(t *testing.T) {
121120
}
122121

123122
m4 := m3
124-
cc, _ := types.ObjectValue(map[string]attr.Type{
125-
"certificate": types.StringType,
126-
"key": types.StringType,
127-
"root_ca": types.StringType,
128-
},
129-
map[string]attr.Value{
130-
"certificate": types.StringValue(testAccClickhouseTLSCert),
131-
"key": types.StringValue(testAccClickhouseTLSKey),
132-
"root_ca": types.StringValue(testAccClickhouseTLSRootCA),
123+
/*
124+
cc, _ := types.ObjectValue(map[string]attr.Type{
125+
"certificate": types.StringType,
126+
"key": types.StringType,
127+
"root_ca": types.StringType,
133128
},
134-
)
135-
m4.CustomCertificate = cc
129+
map[string]attr.Value{
130+
"certificate": types.StringValue(testAccClickhouseTLSCert),
131+
"key": types.StringValue(testAccClickhouseTLSKey),
132+
"root_ca": types.StringValue(testAccClickhouseTLSRootCA),
133+
},
134+
)
135+
*/
136+
m4.CustomCertificate = &clickhouseCustomCertificate{
137+
Certificate: types.StringValue(testAccClickhouseTLSCert),
138+
Key: types.StringValue(testAccClickhouseTLSKey),
139+
RootCA: types.StringValue(testAccClickhouseTLSRootCA),
140+
}
136141

137142
resource.Test(t, resource.TestCase{
138143
PreCheck: func() { testAccPreCheck(t) },
@@ -159,6 +164,10 @@ func TestAccClickhouseClusterResource(t *testing.T) {
159164
resource.TestCheckResourceAttr(testAccClickhouseId, "private_connection_info.user", "admin"),
160165
resource.TestCheckResourceAttr(testAccClickhouseId, "private_connection_info.https_port", "8443"),
161166
resource.TestCheckResourceAttr(testAccClickhouseId, "private_connection_info.tcp_port_secure", "9440"),
167+
resource.TestCheckResourceAttr(testAccClickhouseId, "connection_info.https_port_ctls", "0"),
168+
resource.TestCheckResourceAttr(testAccClickhouseId, "connection_info.tcp_port_secure_ctls", "0"),
169+
resource.TestCheckResourceAttr(testAccClickhouseId, "private_connection_info.https_port_ctls", "0"),
170+
resource.TestCheckResourceAttr(testAccClickhouseId, "private_connection_info.tcp_port_secure_ctls", "0"),
162171
),
163172
},
164173
// Update and Read testing
@@ -197,6 +206,10 @@ func TestAccClickhouseClusterResource(t *testing.T) {
197206
resource.TestCheckResourceAttr(testAccClickhouseId, "custom_certificate.certificate", testAccClickhouseTLSCert),
198207
resource.TestCheckResourceAttr(testAccClickhouseId, "custom_certificate.key", testAccClickhouseTLSKey),
199208
resource.TestCheckResourceAttr(testAccClickhouseId, "custom_certificate.root_ca", testAccClickhouseTLSRootCA),
209+
resource.TestCheckResourceAttr(testAccClickhouseId, "connection_info.https_port_ctls", "8444"),
210+
resource.TestCheckResourceAttr(testAccClickhouseId, "connection_info.tcp_port_secure_ctls", "9444"),
211+
resource.TestCheckResourceAttr(testAccClickhouseId, "private_connection_info.https_port_ctls", "8444"),
212+
resource.TestCheckResourceAttr(testAccClickhouseId, "private_connection_info.tcp_port_secure_ctls", "9444"),
200213
),
201214
},
202215
// Delete testing automatically occurs in TestCase
@@ -338,11 +351,11 @@ resource "doublecloud_clickhouse_cluster" "tf-acc-clickhouse" {
338351
]
339352
{{- end}}
340353
}
341-
{{- if not .CustomCertificate.IsNull }}
354+
{{- if ne .CustomCertificate nil }}
342355
custom_certificate {
343-
certificate = {{ .CustomCertificate.Attributes.certificate }}
344-
key = {{ .CustomCertificate.Attributes.key }}
345-
root_ca = {{ .CustomCertificate.Attributes.root_ca }}
356+
certificate = {{ .CustomCertificate.Certificate }}
357+
key = {{ .CustomCertificate.Key }}
358+
root_ca = {{ .CustomCertificate.RootCA }}
346359
}
347360
{{- end}}
348361
}`

0 commit comments

Comments
 (0)