@@ -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
5757type 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+
115149type 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
0 commit comments