-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ephemeral example resource (#264)
* 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
- Loading branch information
1 parent
9c964f7
commit 3cdbd2d
Showing
8 changed files
with
210 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "scaffolding_example Ephemeral Resource - scaffolding" | ||
subcategory: "" | ||
description: |- | ||
Example ephemeral resource | ||
--- | ||
|
||
# scaffolding_example (Ephemeral Resource) | ||
|
||
Example ephemeral resource | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
ephemeral "scaffolding_example" "example" { | ||
configurable_attribute = "some-value" | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `configurable_attribute` (String) Example configurable attribute | ||
|
||
### Read-Only | ||
|
||
- `value` (String) Example value |
3 changes: 3 additions & 0 deletions
3
examples/ephemeral-resources/scaffolding_example/ephemeral-resource.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
ephemeral "scaffolding_example" "example" { | ||
configurable_attribute = "some-value" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// 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)...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/knownvalue" | ||
"github.com/hashicorp/terraform-plugin-testing/statecheck" | ||
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath" | ||
"github.com/hashicorp/terraform-plugin-testing/tfversion" | ||
) | ||
|
||
func TestAccExampleEphemeralResource(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
// Ephemeral resources are only available in 1.10 and later | ||
TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
tfversion.SkipBelow(tfversion.Version1_10_0), | ||
}, | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactoriesWithEcho, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccExampleEphemeralResourceConfig("example"), | ||
ConfigStateChecks: []statecheck.StateCheck{ | ||
statecheck.ExpectKnownValue("echo.test", tfjsonpath.New("data").AtMapKey("value"), knownvalue.StringExact("token-123")), | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccExampleEphemeralResourceConfig(configurableAttribute string) string { | ||
return fmt.Sprintf(` | ||
ephemeral "scaffolding_example" "test" { | ||
configurable_attribute = %[1]q | ||
} | ||
provider "echo" { | ||
data = ephemeral.scaffolding_example.test | ||
} | ||
resource "echo" "test" {} | ||
`, configurableAttribute) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.