@@ -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"`
@@ -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
0 commit comments