Skip to content

Commit

Permalink
safely skip server if it does not implement ProviderServerWithResourc…
Browse files Browse the repository at this point in the history
…eIdentity
  • Loading branch information
ansgarm committed Feb 13, 2025
1 parent 6e5b920 commit 5ecec55
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
19 changes: 16 additions & 3 deletions tf5muxserver/mux_server_GetResourceIdentitySchemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,28 @@ func (s *muxServer) GetResourceIdentitySchemas(ctx context.Context, req *tfproto

// TODO: Remove and call server.GetResourceIdentitySchemas below directly once interface becomes required.
//nolint:staticcheck // Intentionally verifying interface implementation
resourceIdentityServer, err := server.(tfprotov5.ProviderServerWithResourceIdentity).GetResourceIdentitySchemas(ctx, req)
resourceIdentityServer, ok := server.(tfprotov5.ProviderServerWithResourceIdentity)

if !ok {
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "GetResourceIdentitySchemas Not Implemented",
Detail: "A GetResourceIdentitySchemas call was received by the provider, however the provider does not implement GetResourceIdentitySchemas. " +
"Either upgrade the provider to a version that implements GetResourceIdentitySchemas or this is a bug in Terraform that should be reported to the Terraform maintainers.",
})

continue
}

resourceIdentitySchemas, err := resourceIdentityServer.GetResourceIdentitySchemas(ctx, req)

if err != nil {
return resp, fmt.Errorf("error calling GetResourceIdentitySchemas for %T: %w", server, err)
}

resp.Diagnostics = append(resp.Diagnostics, resourceIdentityServer.Diagnostics...)
resp.Diagnostics = append(resp.Diagnostics, resourceIdentitySchemas.Diagnostics...)

for resourceIdentityType, schema := range resourceIdentityServer.IdentitySchemas {
for resourceIdentityType, schema := range resourceIdentitySchemas.IdentitySchemas {
if _, ok := resp.IdentitySchemas[resourceIdentityType]; ok {
resp.Diagnostics = append(resp.Diagnostics, resourceIdentityDuplicateError(resourceIdentityType))

Expand Down
20 changes: 17 additions & 3 deletions tf6muxserver/mux_server_GetResourceIdentitySchemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package tf6muxserver
import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-go/tfprotov6"

"github.com/hashicorp/terraform-plugin-mux/internal/logging"
Expand Down Expand Up @@ -34,15 +35,28 @@ func (s *muxServer) GetResourceIdentitySchemas(ctx context.Context, req *tfproto

// TODO: Remove and call server.GetResourceIdentitySchemas below directly once interface becomes required.
//nolint:staticcheck // Intentionally verifying interface implementation
resourceIdentityServer, err := server.(tfprotov6.ProviderServerWithResourceIdentity).GetResourceIdentitySchemas(ctx, req)
resourceIdentityServer, ok := server.(tfprotov6.ProviderServerWithResourceIdentity)

if !ok {
resp.Diagnostics = append(resp.Diagnostics, &tfprotov6.Diagnostic{
Severity: tfprotov6.DiagnosticSeverityError,
Summary: "GetResourceIdentitySchemas Not Implemented",
Detail: "A GetResourceIdentitySchemas call was received by the provider, however the provider does not implement GetResourceIdentitySchemas. " +
"Either upgrade the provider to a version that implements GetResourceIdentitySchemas or this is a bug in Terraform that should be reported to the Terraform maintainers.",
})

continue
}

resourceIdentitySchemas, err := resourceIdentityServer.GetResourceIdentitySchemas(ctx, req)

if err != nil {
return resp, fmt.Errorf("error calling GetResourceIdentitySchemas for %T: %w", server, err)
}

resp.Diagnostics = append(resp.Diagnostics, resourceIdentityServer.Diagnostics...)
resp.Diagnostics = append(resp.Diagnostics, resourceIdentitySchemas.Diagnostics...)

for resourceIdentityType, schema := range resourceIdentityServer.IdentitySchemas {
for resourceIdentityType, schema := range resourceIdentitySchemas.IdentitySchemas {
if _, ok := resp.IdentitySchemas[resourceIdentityType]; ok {
resp.Diagnostics = append(resp.Diagnostics, resourceIdentityDuplicateError(resourceIdentityType))

Expand Down

0 comments on commit 5ecec55

Please sign in to comment.