Skip to content

Commit 61de717

Browse files
authored
feature: connection_info (#28)
feature: add `connection_info` and `private_connection_info` as computed fields for `doublecloud_kafka_cluster`
1 parent 8132368 commit 61de717

2 files changed

Lines changed: 121 additions & 12 deletions

File tree

internal/provider/kafka_cluster_resource.go

Lines changed: 112 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import (
88
dcsdk "github.com/doublecloud/go-sdk"
99
dcgen "github.com/doublecloud/go-sdk/gen/kafka"
1010
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
11+
"github.com/hashicorp/terraform-plugin-framework/attr"
1112
"github.com/hashicorp/terraform-plugin-framework/diag"
1213
"github.com/hashicorp/terraform-plugin-framework/path"
1314
"github.com/hashicorp/terraform-plugin-framework/resource"
1415
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
16+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier"
1517
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1618
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
1719
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
@@ -37,17 +39,19 @@ type KafkaClusterResource struct {
3739
}
3840

3941
type KafkaClusterModel struct {
40-
Id types.String `tfsdk:"id"`
41-
ProjectID types.String `tfsdk:"project_id"`
42-
CloudType types.String `tfsdk:"cloud_type"`
43-
RegionID types.String `tfsdk:"region_id"`
44-
Name types.String `tfsdk:"name"`
45-
Description types.String `tfsdk:"description"`
46-
Version types.String `tfsdk:"version"`
47-
Resources KafkaResourcesModel `tfsdk:"resources"`
48-
NetworkId types.String `tfsdk:"network_id"`
49-
SchemaRegistry *schemaRegistryModel `tfsdk:"schema_registry"`
50-
Access *AccessModel `tfsdk:"access"`
42+
Id types.String `tfsdk:"id"`
43+
ProjectID types.String `tfsdk:"project_id"`
44+
CloudType types.String `tfsdk:"cloud_type"`
45+
RegionID types.String `tfsdk:"region_id"`
46+
Name types.String `tfsdk:"name"`
47+
Description types.String `tfsdk:"description"`
48+
Version types.String `tfsdk:"version"`
49+
Resources KafkaResourcesModel `tfsdk:"resources"`
50+
NetworkId types.String `tfsdk:"network_id"`
51+
SchemaRegistry *schemaRegistryModel `tfsdk:"schema_registry"`
52+
Access *AccessModel `tfsdk:"access"`
53+
ConnectionInfo types.Object `tfsdk:"connection_info"`
54+
PrivateConnectionInfo types.Object `tfsdk:"private_connection_info"`
5155
}
5256

5357
type schemaRegistryModel struct {
@@ -122,6 +126,16 @@ func (r *KafkaClusterResource) Schema(ctx context.Context, req resource.SchemaRe
122126
MarkdownDescription: "Version of Apache Kafka",
123127
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
124128
},
129+
"connection_info": schema.SingleNestedAttribute{
130+
Computed: true,
131+
Attributes: kafkaConnectionInfoResSchema(),
132+
PlanModifiers: []planmodifier.Object{objectplanmodifier.UseStateForUnknown()},
133+
},
134+
"private_connection_info": schema.SingleNestedAttribute{
135+
Computed: true,
136+
Attributes: kafkaConnectionInfoResSchema(),
137+
PlanModifiers: []planmodifier.Object{objectplanmodifier.UseStateForUnknown()},
138+
},
125139
},
126140
Blocks: map[string]schema.Block{
127141
"resources": schema.SingleNestedBlock{
@@ -268,6 +282,36 @@ func (r *KafkaClusterResource) Create(ctx context.Context, req resource.CreateRe
268282
return
269283
}
270284
data.Version = types.StringValue(cluster.Version)
285+
if info := cluster.GetConnectionInfo(); info != nil {
286+
o, d := types.ObjectValue(map[string]attr.Type{
287+
"connection_string": types.StringType,
288+
"user": types.StringType,
289+
"password": types.StringType,
290+
},
291+
map[string]attr.Value{
292+
"connection_string": types.StringValue(info.GetConnectionString()),
293+
"user": types.StringValue(info.GetUser()),
294+
"password": types.StringValue(info.GetPassword()),
295+
},
296+
)
297+
resp.Diagnostics.Append(d...)
298+
data.ConnectionInfo = o
299+
}
300+
if info := cluster.GetPrivateConnectionInfo(); info != nil {
301+
o, d := types.ObjectValue(map[string]attr.Type{
302+
"connection_string": types.StringType,
303+
"user": types.StringType,
304+
"password": types.StringType,
305+
},
306+
map[string]attr.Value{
307+
"connection_string": types.StringValue(info.GetConnectionString()),
308+
"user": types.StringValue(info.GetUser()),
309+
"password": types.StringValue(info.GetPassword()),
310+
},
311+
)
312+
resp.Diagnostics.Append(d...)
313+
data.PrivateConnectionInfo = o
314+
}
271315

272316
tflog.Info(ctx, fmt.Sprintf("doublecloud_kafka_cluster has been created: %s", op.ResourceId()))
273317

@@ -279,7 +323,10 @@ func getKafkaClusterResourceRequest(m *KafkaClusterModel) (*kafka.GetClusterRequ
279323
if m.Id == types.StringNull() {
280324
return nil, diag.Diagnostics{diag.NewErrorDiagnostic("Unknown identifier", "missed one of required fields: cluster_id or name")}
281325
}
282-
return &kafka.GetClusterRequest{ClusterId: m.Id.ValueString()}, nil
326+
return &kafka.GetClusterRequest{
327+
ClusterId: m.Id.ValueString(),
328+
Sensitive: true,
329+
}, nil
283330
}
284331

285332
func (r *KafkaClusterResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
@@ -315,6 +362,39 @@ func (r *KafkaClusterResource) Read(ctx context.Context, req resource.ReadReques
315362

316363
if rs.SchemaRegistryConfig != nil {
317364
data.SchemaRegistry = &schemaRegistryModel{Enabled: types.BoolValue(rs.SchemaRegistryConfig.Enabled)}
365+
} else {
366+
data.SchemaRegistry = nil
367+
}
368+
369+
if info := rs.GetConnectionInfo(); info != nil {
370+
o, d := types.ObjectValue(map[string]attr.Type{
371+
"connection_string": types.StringType,
372+
"user": types.StringType,
373+
"password": types.StringType,
374+
},
375+
map[string]attr.Value{
376+
"connection_string": types.StringValue(info.GetConnectionString()),
377+
"user": types.StringValue(info.GetUser()),
378+
"password": types.StringValue(info.GetPassword()),
379+
},
380+
)
381+
resp.Diagnostics.Append(d...)
382+
data.ConnectionInfo = o
383+
}
384+
if info := rs.GetPrivateConnectionInfo(); info != nil {
385+
o, d := types.ObjectValue(map[string]attr.Type{
386+
"connection_string": types.StringType,
387+
"user": types.StringType,
388+
"password": types.StringType,
389+
},
390+
map[string]attr.Value{
391+
"connection_string": types.StringValue(info.GetConnectionString()),
392+
"user": types.StringValue(info.GetUser()),
393+
"password": types.StringValue(info.GetPassword()),
394+
},
395+
)
396+
resp.Diagnostics.Append(d...)
397+
data.PrivateConnectionInfo = o
318398
}
319399

320400
// Save updated data into Terraform state
@@ -418,3 +498,23 @@ func (r *KafkaClusterResource) Delete(ctx context.Context, req resource.DeleteRe
418498
func (r *KafkaClusterResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
419499
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
420500
}
501+
502+
func kafkaConnectionInfoResSchema() map[string]schema.Attribute {
503+
return map[string]schema.Attribute{
504+
"connection_string": schema.StringAttribute{
505+
Computed: true,
506+
MarkdownDescription: "String to use in clients",
507+
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
508+
},
509+
"user": schema.StringAttribute{
510+
Computed: true,
511+
MarkdownDescription: "Apache Kafka® user",
512+
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
513+
},
514+
"password": schema.StringAttribute{
515+
Computed: true,
516+
MarkdownDescription: "Password for Apache Kafka® user",
517+
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
518+
},
519+
}
520+
}

internal/provider/kafka_cluster_resource_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,18 @@ func TestAccKafkaClusterResource(t *testing.T) {
5555
resource.TestCheckResourceAttr(testAccKafkaId, "name", testAccKafkaName),
5656
resource.TestCheckResourceAttr(testAccKafkaId, "resources.kafka.disk_size", "34359738368"),
5757
resource.TestCheckResourceAttr(testAccKafkaId, "schema_registry.enabled", "false"),
58+
5859
resource.TestCheckResourceAttr(testAccKafkaId, "access.data_services.0", "transfer"),
5960
resource.TestCheckResourceAttr(testAccKafkaId, "access.ipv4_cidr_blocks.0.value", "10.0.0.0/8"),
6061
resource.TestCheckResourceAttr(testAccKafkaId, "access.ipv4_cidr_blocks.0.description", "Office in Berlin"),
62+
63+
resource.TestCheckResourceAttrSet(testAccKafkaId, "connection_info.connection_string"),
64+
resource.TestCheckResourceAttr(testAccKafkaId, "connection_info.user", "admin"),
65+
resource.TestCheckResourceAttrSet(testAccKafkaId, "connection_info.password"),
66+
67+
resource.TestCheckResourceAttrSet(testAccKafkaId, "private_connection_info.connection_string"),
68+
resource.TestCheckResourceAttr(testAccKafkaId, "private_connection_info.user", "admin"),
69+
resource.TestCheckResourceAttrSet(testAccKafkaId, "private_connection_info.password"),
6170
),
6271
},
6372
// Update and Read testing

0 commit comments

Comments
 (0)