Skip to content

Commit 69c0565

Browse files
committed
Fix after review
1 parent dfe9a53 commit 69c0565

File tree

4 files changed

+83
-51
lines changed

4 files changed

+83
-51
lines changed

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_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
}`

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)