
* eph: add ephemeral resource Adds a new example ephemeral resource using a required configurable attribute and an example ID. * tools: bump terraform-plugin-docs Bump to the latest stable release, as it includes support for ephemeral resource documentation generation. * docs: add ephemeral resource documentation * provider: register new example ephemeral resource * eph: improve alphabetical ordering of struct fields * eph: use a computed 'value' attribute To help provider developers easily understand the concept of ephemeral resources, we use a 'value' attribute in the schema, which is set to an example token (token-123). * docs: update ephemeral resource docs * provider: create a separate echo provider factory container Since not every acceptance test requires an ephemeral provider server for the CLI to connect to and interact with, this introduces a separate provider factory container that includes the echo provider. * eph: add basic acceptance test for ephemeral resource * docs: rename ephemeral resource configuration file * eph: improve http response diag
78 lines
2.7 KiB
Go
78 lines
2.7 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package provider
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework/ephemeral"
|
|
"github.com/hashicorp/terraform-plugin-framework/ephemeral/schema"
|
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
|
)
|
|
|
|
// Ensure provider defined types fully satisfy framework interfaces.
|
|
var _ ephemeral.EphemeralResource = &ExampleEphemeralResource{}
|
|
|
|
func NewExampleEphemeralResource() ephemeral.EphemeralResource {
|
|
return &ExampleEphemeralResource{}
|
|
}
|
|
|
|
// ExampleEphemeralResource defines the ephemeral resource implementation.
|
|
type ExampleEphemeralResource struct {
|
|
// client *http.Client // If applicable, a client can be initialized here.
|
|
}
|
|
|
|
// ExampleEphemeralResourceModel describes the ephemeral resource data model.
|
|
type ExampleEphemeralResourceModel struct {
|
|
ConfigurableAttribute types.String `tfsdk:"configurable_attribute"`
|
|
Value types.String `tfsdk:"value"`
|
|
}
|
|
|
|
func (r *ExampleEphemeralResource) Metadata(_ context.Context, req ephemeral.MetadataRequest, resp *ephemeral.MetadataResponse) {
|
|
resp.TypeName = req.ProviderTypeName + "_example"
|
|
}
|
|
|
|
func (r *ExampleEphemeralResource) Schema(ctx context.Context, _ ephemeral.SchemaRequest, resp *ephemeral.SchemaResponse) {
|
|
resp.Schema = schema.Schema{
|
|
// This description is used by the documentation generator and the language server.
|
|
MarkdownDescription: "Example ephemeral resource",
|
|
|
|
Attributes: map[string]schema.Attribute{
|
|
"configurable_attribute": schema.StringAttribute{
|
|
MarkdownDescription: "Example configurable attribute",
|
|
Required: true, // Ephemeral resources expect their dependencies to already exist.
|
|
},
|
|
"value": schema.StringAttribute{
|
|
Computed: true,
|
|
// Sensitive: true, // If applicable, mark the attribute as sensitive.
|
|
MarkdownDescription: "Example value",
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (r *ExampleEphemeralResource) Open(ctx context.Context, req ephemeral.OpenRequest, resp *ephemeral.OpenResponse) {
|
|
var data ExampleEphemeralResourceModel
|
|
|
|
// Read Terraform config data into the model
|
|
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
// If applicable, this is a great opportunity to initialize any necessary
|
|
// provider client data and make a call using it.
|
|
// httpResp, err := r.client.Do(httpReq)
|
|
// if err != nil {
|
|
// resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read example, got error: %s", err))
|
|
// return
|
|
// }
|
|
//
|
|
// However, this example hardcodes setting the token attribute to a specific value for brevity.
|
|
data.Value = types.StringValue("token-123")
|
|
|
|
// Save data into ephemeral result data
|
|
resp.Diagnostics.Append(resp.Result.Set(ctx, &data)...)
|
|
}
|