From b5be95f4ac26b97145822b5d16d8edee184f39cc Mon Sep 17 00:00:00 2001 From: holistis Date: Wed, 9 Sep 2026 13:28:02 +0200 Subject: [PATCH] fix(provider): guard nil MaintenanceInfo on managed service instance create The Cloud Controller API omits maintenance_info (json omitempty) for some managed service instances. mapResourceServiceInstanceValuesToType dereferenced it unconditionally, crashing the provider with a nil pointer dereference during terraform apply. The data-source mapper already guarded the same field; this brings the resource mapper in line with it. Fixes #590 Co-Authored-By: Claude Sonnet 5 --- .../provider/types_service_instance.go | 8 +++- .../provider/types_service_instance_test.go | 41 +++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 cloudfoundry/provider/types_service_instance_test.go diff --git a/cloudfoundry/provider/types_service_instance.go b/cloudfoundry/provider/types_service_instance.go index d476895d..e68cdd3a 100644 --- a/cloudfoundry/provider/types_service_instance.go +++ b/cloudfoundry/provider/types_service_instance.go @@ -211,8 +211,12 @@ func mapResourceServiceInstanceValuesToType(ctx context.Context, value *resource if value.DashboardURL != nil { serviceInstanceType.DashboardURL = types.StringValue(*value.DashboardURL) } - serviceInstanceType.MaintenanceInfo, diags = types.ObjectValueFrom(ctx, maintenanceInfoAttrTypes, mapMaintenanceInfo(*value.MaintenanceInfo)) - diagnostics.Append(diags...) + if value.MaintenanceInfo != nil { + serviceInstanceType.MaintenanceInfo, diags = types.ObjectValueFrom(ctx, maintenanceInfoAttrTypes, mapMaintenanceInfo(*value.MaintenanceInfo)) + diagnostics.Append(diags...) + } else { + serviceInstanceType.MaintenanceInfo = types.ObjectNull(maintenanceInfoAttrTypes) + } if !paramCreds.IsNull() { serviceInstanceType.Parameters = jsontypes.NewNormalizedValue(paramCreds.ValueString()) diff --git a/cloudfoundry/provider/types_service_instance_test.go b/cloudfoundry/provider/types_service_instance_test.go new file mode 100644 index 00000000..3f787e98 --- /dev/null +++ b/cloudfoundry/provider/types_service_instance_test.go @@ -0,0 +1,41 @@ +package provider + +import ( + "context" + "testing" + "time" + + "github.com/cloudfoundry/go-cfclient/v3/resource" + "github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes" +) + +// Reproduces https://github.com/cloudfoundry/terraform-provider-cloudfoundry/issues/590: +// the Cloud Controller API omits maintenance_info (json tag "omitempty") for some managed +// service instances, and the resource mapper dereferenced it unconditionally, crashing the +// provider with a nil pointer dereference during terraform apply. +func TestMapResourceServiceInstanceValuesToType_NilMaintenanceInfo(t *testing.T) { + managedInstance := &resource.ServiceInstance{ + Name: "tf-test-rds", + Type: managedSerivceInstance, + Relationships: resource.ServiceInstanceRelationships{ + Space: &resource.ToOneRelationship{Data: &resource.Relationship{GUID: "space-guid"}}, + ServicePlan: &resource.ToOneRelationship{Data: &resource.Relationship{GUID: "plan-guid"}}, + }, + Metadata: &resource.Metadata{}, + MaintenanceInfo: nil, + Resource: resource.Resource{ + GUID: "instance-guid", + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + }, + } + + result, diags := mapResourceServiceInstanceValuesToType(context.Background(), managedInstance, jsontypes.NewNormalizedNull()) + + if diags.HasError() { + t.Fatalf("unexpected diagnostics: %v", diags) + } + if !result.MaintenanceInfo.IsNull() { + t.Errorf("expected MaintenanceInfo to be null when the API omits it, got %v", result.MaintenanceInfo) + } +}