-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use OpenTofu planner with Plugin Framework providers #2188
Changes from all commits
081d57b
485fd18
4bdf139
34b9e26
df689b5
758aa9a
3828837
c064bd4
f14431b
351e4e8
673a096
24042b6
028127e
15dbda0
f395b51
4fbd1d7
f8f08e9
3f8da61
f72c432
4007cff
9d03fe8
5bf30d0
63ba4ac
7dea5df
89eac4c
b26ab85
ffbd78d
c200e72
457e48a
1574963
3f907fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Copyright 2016-2024, Pulumi Corporation. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package pfutils | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/provider" | ||
"github.com/hashicorp/terraform-plugin-framework/providerserver" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
rschema "github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/hashicorp/terraform-plugin-go/tfprotov6" | ||
) | ||
|
||
// Assist converting Plugin framework schemata to proto schemata for a resource. | ||
func convertResourceSchemaToProto(ctx context.Context, s *rschema.Schema) (*tfprotov6.Schema, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a slightly convoluted way to convert rschema.Schema to protos. Perhaps could be avoided by vendoring internal code from the Plugin framework. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is indeed quite roundabout - could we add a comment with a TODO here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I don't intend revisiting though, I think alternatives are worse. |
||
p := &singleResourceProvider{s} | ||
|
||
mk := providerserver.NewProtocol6WithError(p) | ||
srv, err := mk() | ||
if err != nil { | ||
return nil, err | ||
} | ||
resp, err := srv.GetProviderSchema(ctx, &tfprotov6.GetProviderSchemaRequest{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var diagErrors []error | ||
for _, d := range resp.Diagnostics { | ||
if d.Severity == tfprotov6.DiagnosticSeverityError { | ||
diagErrors = append(diagErrors, d.Attribute.NewErrorf("%s\n%s", d.Summary, d.Detail)) | ||
} | ||
} | ||
if err := errors.Join(diagErrors...); err != nil { | ||
return nil, err | ||
} | ||
for _, r := range resp.ResourceSchemas { | ||
return r, nil | ||
} | ||
return nil, fmt.Errorf("GetProviderSchema did not return any resource schemas") | ||
} | ||
|
||
type singleResourceProvider struct { | ||
resourceSchema *rschema.Schema | ||
} | ||
|
||
var _ provider.Provider = &singleResourceProvider{} | ||
|
||
func (srp singleResourceProvider) Metadata( | ||
ctx context.Context, | ||
req provider.MetadataRequest, | ||
resp *provider.MetadataResponse, | ||
) { | ||
resp.TypeName = "p" | ||
} | ||
|
||
func (srp singleResourceProvider) Schema(context.Context, provider.SchemaRequest, *provider.SchemaResponse) { | ||
} | ||
|
||
func (srp singleResourceProvider) Configure(context.Context, provider.ConfigureRequest, *provider.ConfigureResponse) { | ||
} | ||
|
||
func (srp singleResourceProvider) DataSources(context.Context) []func() datasource.DataSource { | ||
return nil | ||
} | ||
|
||
func (srp singleResourceProvider) Resources(context.Context) []func() resource.Resource { | ||
mk := func() resource.Resource { | ||
return &schemaOnlyResource{srp.resourceSchema} | ||
} | ||
return []func() resource.Resource{mk} | ||
} | ||
|
||
type schemaOnlyResource struct { | ||
schema *rschema.Schema | ||
} | ||
|
||
var _ resource.Resource = &schemaOnlyResource{} | ||
|
||
func (r *schemaOnlyResource) Metadata( | ||
ctx context.Context, | ||
req resource.MetadataRequest, | ||
resp *resource.MetadataResponse, | ||
) { | ||
resp.TypeName = "r" | ||
} | ||
|
||
func (r *schemaOnlyResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { | ||
if r.schema != nil { | ||
resp.Schema = *r.schema | ||
} | ||
} | ||
|
||
func (r *schemaOnlyResource) Create(context.Context, resource.CreateRequest, *resource.CreateResponse) { | ||
} | ||
|
||
func (r *schemaOnlyResource) Read(context.Context, resource.ReadRequest, *resource.ReadResponse) { | ||
} | ||
|
||
func (r *schemaOnlyResource) Update(context.Context, resource.UpdateRequest, *resource.UpdateResponse) { | ||
} | ||
|
||
func (r *schemaOnlyResource) Delete(context.Context, resource.DeleteRequest, *resource.DeleteResponse) { | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a correct result, this used to be a bug. Likely a degenerate case because a required property cannot be not specified in the program.