Open is part of the Terraform lifecycle for an ephemeral resource, which is different from the managed resource lifecycle. During any Terraform operation (like terraform plan
or terraform apply
), when an ephemeral resource's data is needed, Terraform calls the provider OpenEphemeralResource
RPC, in which the framework calls the ephemeral.EphemeralResource
interface Open
method. The request contains the configuration supplied to Terraform for the ephemeral resource. The response contains the ephemeral result data. The data is defined by the schema of the ephemeral resource.
Open
is the only required lifecycle implementation for an ephemeral resource, optional lifecycle implementations include:
In this example, an ephemeral resource named examplecloud_thing
with hardcoded behavior is defined:
// ThingEphemeralResource defines the ephemeral resource implementation.
// Some ephemeral.EphemeralResource interface methods are omitted for brevity.
type ThingEphemeralResource struct {}
type ThingEphemeralResourceModel struct {
Name types.String `tfsdk:"name"`
Token types.String `tfsdk:"token"`
}
func (e *ThingEphemeralResource) Schema(ctx context.Context, req ephemeral.SchemaRequest, resp *ephemeral.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Description: "Name of the thing to retrieve a token for.",
Required: true,
},
"token": schema.StringAttribute{
Description: "Token for the thing.",
Computed: true,
},
},
}
}
func (e *ThingEphemeralResource) Open(ctx context.Context, req ephemeral.OpenRequest, resp *ephemeral.OpenResponse) {
var data ThingEphemeralResourceModel
// Read Terraform config data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
// Typically ephemeral resources will make external calls, however this example
// hardcodes setting the token attribute to a specific value for brevity.
data.Token = types.StringValue("token-123")
// Save data into ephemeral result data
resp.Diagnostics.Append(resp.Result.Set(ctx, &data)...)
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4