Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions cloudfoundry/provider/types_service_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
41 changes: 41 additions & 0 deletions cloudfoundry/provider/types_service_instance_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading