Repository: opentofu/opentofu
File: internal/tofu/provider_for_test_framework.go (Go)

Code before:
	}

	resSchema, _ := p.schema.SchemaForResourceType(addrs.ManagedResourceMode, r.TypeName)

	var resp providers.PlanResourceChangeResponse

	resp.PlannedState, resp.Diagnostics = newMockValueComposer(r.TypeName).
		ComposeBySchema(resSchema, r.Config, p.overrideValues)

	return resp
}

func (p providerForTest) ApplyResourceChange(_ context.Context, r providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
	return providers.ApplyResourceChangeResponse{
		NewState: r.PlannedState,
	}
}

func (p providerForTest) ReadDataSource(_ context.Context, r providers.ReadDataSourceRequest) providers.ReadDataSourceResponse {
	resSchema, _ := p.schema.SchemaForResourceType(addrs.DataResourceMode, r.TypeName)

	var resp providers.ReadDataSourceResponse

	resp.State, resp.Diagnostics = newMockValueComposer(r.TypeName).
		ComposeBySchema(resSchema, r.Config, p.overrideValues)

	return resp
}

func (p providerForTest) OpenEphemeralResource(_ context.Context, _ providers.OpenEphemeralResourceRequest) (resp providers.OpenEphemeralResourceResponse) {
	// TODO ephemeral testing support - implement me when adding testing support
	panic("implement me")
}

func (p providerForTest) RenewEphemeralResource(_ context.Context, _ providers.RenewEphemeralResourceRequest) (resp providers.RenewEphemeralResourceResponse) {
	// TODO ephemeral testing support - implement me when adding testing support
	panic("implement me")
}

func (p providerForTest) CloseEphemeralResource(_ context.Context, _ providers.CloseEphemeralResourceRequest) (resp providers.CloseEphemeralResourceResponse) {
	// TODO ephemeral testing support - implement me when adding testing support
	panic("implement me")
}

// ValidateProviderConfig is irrelevant when provider is mocked or overridden.
func (p providerForTest) ValidateProviderConfig(_ context.Context, _ providers.ValidateProviderConfigRequest) providers.ValidateProviderConfigResponse {
	return providers.ValidateProviderConfigResponse{}
}

// GetProviderSchema is also used to perform additional validation outside of the provider
// implementation. We are excluding parts of the schema related to provider since it is

Review comment (suggestion):
Checked the error returned by `cty.Transform` and seems that in this particular case is safe to ignore it. Just want to highlight this for others to be sure that I didn't miss a thing.

Code after:
		}
	}

	resSchema, _ := p.schema.SchemaForResourceType(addrs.ManagedResourceMode, r.TypeName)

	// Filter out computed-only attributes from the schema to avoid them being used incorrectly
	// later on. This resolves https://github.com/opentofu/opentofu/issues/3644
	filteredConfig := filterComputedOnlyAttributes(resSchema, r.Config)

	var resp providers.PlanResourceChangeResponse
	resp.PlannedState, resp.Diagnostics = newMockValueComposer(r.TypeName).
		ComposeBySchema(resSchema, filteredConfig, p.overrideValues)

	return resp
}

// filterComputedOnlyAttributes returns a copy of value where all computed-only attributes
// (i.e. computed and not optional) defined in resSchema are replaced with null values.
func filterComputedOnlyAttributes(resSchema *configschema.Block, value cty.Value) cty.Value {
	if resSchema == nil || value.IsNull() || !value.IsKnown() {
		// Nothing to filter here, or we dont know how to filter
		// so we should move on
		return value
	}

	ret, _ := cty.Transform(value, func(path cty.Path, v cty.Value) (cty.Value, error) {
		attr := resSchema.AttributeByPath(path)
		if attr != nil && attr.Computed && !attr.Optional {
			return cty.NullVal(v.Type()), nil
		}
		return v, nil
	})
	return ret
}

func (p providerForTest) ApplyResourceChange(_ context.Context, r providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
	return providers.ApplyResourceChangeResponse{
		NewState: r.PlannedState,
	}
}

func (p providerForTest) ReadDataSource(_ context.Context, r providers.ReadDataSourceRequest) providers.ReadDataSourceResponse {
	resSchema, _ := p.schema.SchemaForResourceType(addrs.DataResourceMode, r.TypeName)

	var resp providers.ReadDataSourceResponse

	resp.State, resp.Diagnostics = newMockValueComposer(r.TypeName).
		ComposeBySchema(resSchema, r.Config, p.overrideValues)

	return resp
}