Skip to content

Commit 547f021

Browse files
author
velom
authored
fix kafka resource (#40)
fix: nil pointer dereference in `doublecloud_kafka_cluster` on import.
1 parent 1ce7a2d commit 547f021

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

internal/provider/kafka_cluster_resource.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/doublecloud/go-genproto/doublecloud/kafka/v1"
8-
dcsdk "github.com/doublecloud/go-sdk"
9-
dcgen "github.com/doublecloud/go-sdk/gen/kafka"
107
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
118
"github.com/hashicorp/terraform-plugin-framework/attr"
129
"github.com/hashicorp/terraform-plugin-framework/diag"
@@ -21,6 +18,10 @@ import (
2118
"github.com/hashicorp/terraform-plugin-framework/types"
2219
"github.com/hashicorp/terraform-plugin-log/tflog"
2320
"google.golang.org/protobuf/types/known/wrapperspb"
21+
22+
"github.com/doublecloud/go-genproto/doublecloud/kafka/v1"
23+
dcsdk "github.com/doublecloud/go-sdk"
24+
dcgen "github.com/doublecloud/go-sdk/gen/kafka"
2425
)
2526

2627
// Ensure provider defined types fully satisfy framework interfaces.
@@ -46,7 +47,7 @@ type KafkaClusterModel struct {
4647
Name types.String `tfsdk:"name"`
4748
Description types.String `tfsdk:"description"`
4849
Version types.String `tfsdk:"version"`
49-
Resources KafkaResourcesModel `tfsdk:"resources"`
50+
Resources *KafkaResourcesModel `tfsdk:"resources"`
5051
NetworkId types.String `tfsdk:"network_id"`
5152
SchemaRegistry *schemaRegistryModel `tfsdk:"schema_registry"`
5253
Access *AccessModel `tfsdk:"access"`
@@ -358,7 +359,23 @@ func (r *KafkaClusterResource) Read(ctx context.Context, req resource.ReadReques
358359
data.Description = types.StringValue(rs.Description)
359360
data.CloudType = types.StringValue(rs.CloudType)
360361
data.RegionID = types.StringValue(rs.RegionId)
362+
data.NetworkId = types.StringValue(rs.NetworkId)
361363
data.Version = types.StringValue(rs.Version)
364+
data.Resources = &KafkaResourcesModel{
365+
Kafka: KafkaResourcesKafkaModel{
366+
ResourcePresetId: types.StringValue(rs.GetResources().GetKafka().GetResourcePresetId()),
367+
DiskSize: types.Int64Value(rs.GetResources().GetKafka().GetDiskSize().GetValue()),
368+
BrokerCount: types.Int64Value(rs.GetResources().GetKafka().GetBrokerCount().GetValue()),
369+
ZoneCount: types.Int64Value(rs.GetResources().GetKafka().GetZoneCount().GetValue()),
370+
},
371+
}
372+
373+
if access := rs.GetAccess(); access != nil {
374+
if data.Access == nil {
375+
data.Access = new(AccessModel)
376+
}
377+
diag.Append(data.Access.parse(access)...)
378+
}
362379

363380
if rs.SchemaRegistryConfig != nil {
364381
data.SchemaRegistry = &schemaRegistryModel{Enabled: types.BoolValue(rs.SchemaRegistryConfig.Enabled)}

internal/provider/kafka_cluster_resource_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66
"strings"
77
"testing"
88

9-
"github.com/doublecloud/go-genproto/doublecloud/kafka/v1"
109
"github.com/hashicorp/terraform-plugin-framework/types"
1110
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
11+
12+
"github.com/doublecloud/go-genproto/doublecloud/kafka/v1"
1213
)
1314

1415
var (
@@ -25,7 +26,7 @@ func TestAccKafkaClusterResource(t *testing.T) {
2526
CloudType: types.StringValue("aws"),
2627
NetworkId: types.StringValue(testNetworkId),
2728

28-
Resources: KafkaResourcesModel{
29+
Resources: &KafkaResourcesModel{
2930
Kafka: KafkaResourcesKafkaModel{
3031
ResourcePresetId: types.StringValue("s1-c2-m4"),
3132
DiskSize: types.Int64Value(34359738368),
@@ -41,6 +42,9 @@ func TestAccKafkaClusterResource(t *testing.T) {
4142

4243
m2 := m
4344
m2.Name = types.StringValue("terraform-kafka-changed")
45+
r1 := *m.Resources
46+
r2 := r1
47+
m2.Resources = &r2
4448
m2.Resources.Kafka.DiskSize = types.Int64Value(51539607552)
4549

4650
resource.Test(t, resource.TestCase{

0 commit comments

Comments
 (0)