-
Notifications
You must be signed in to change notification settings - Fork 173
[TF-28674] Add HYOK data sources for HYOKCustomerKeyVersion and HYOKEncryptedDataKey #1842
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
Merged
dominic-retli-hashi
merged 16 commits into
feature/hyok
from
dominicretli/TF-28674/hyok-data-objects
Sep 26, 2025
Merged
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
202a208
WIP testing new hyok customer key version resource
dominic-retli-hashi 4060a6a
added data source for hyok encrypted data key and hyok customer key v…
dominic-retli-hashi 81ad65c
maded tests generic and dependent on env var
dominic-retli-hashi 34dae45
added mention of the env vars needed for the hyok data source tests i…
dominic-retli-hashi 8b846dc
updated change log
dominic-retli-hashi 2713372
Merge branch 'main' into dominicretli/TF-28674/hyok-data-objects
dominic-retli-hashi 5a1c9b2
Merge remote-tracking branch 'origin/main' into dominicretli/TF-28674…
dominic-retli-hashi 49d4a3a
Added website docs for hyok data sources
dominic-retli-hashi 6abdd35
Added workspaces secured, general cleanup
dominic-retli-hashi 8457a54
updated docs
dominic-retli-hashi 66f2a61
test cleanup
dominic-retli-hashi 44add6a
misc cleanup
dominic-retli-hashi afa401b
Merge branch 'feature/hyok' into dominicretli/TF-28674/hyok-data-objects
dominic-retli-hashi 9a469a4
renamed variable for lint
dominic-retli-hashi a7622dd
Merge branch 'dominicretli/TF-28674/hyok-data-objects' of github.com:…
dominic-retli-hashi 8a2e80c
Merge branch 'feature/hyok' into dominicretli/TF-28674/hyok-data-objects
iuri-slywitch-hashicorp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
115 changes: 115 additions & 0 deletions
115
internal/provider/data_source_hyok_customer_key_version.go
This file contains hidden or 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,115 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"time" | ||
) | ||
|
||
var ( | ||
_ datasource.DataSource = &dataSourceHYOKCustomerKeyVersion{} | ||
_ datasource.DataSourceWithConfigure = &dataSourceHYOKCustomerKeyVersion{} | ||
) | ||
|
||
func NewHYOKCustomerKeyVersionDataSource() datasource.DataSource { | ||
return &dataSourceHYOKCustomerKeyVersion{} | ||
} | ||
|
||
type dataSourceHYOKCustomerKeyVersion struct { | ||
config ConfiguredClient | ||
} | ||
|
||
type HYOKCustomerKeyVersionDataSourceModel struct { | ||
ID types.String `tfsdk:"id"` | ||
Status types.String `tfsdk:"status"` | ||
Error types.String `tfsdk:"error"` | ||
KeyVersion types.String `tfsdk:"key_version"` | ||
CreatedAt types.String `tfsdk:"created_at"` | ||
WorkspacesSecured types.Int64 `tfsdk:"workspaces_secured"` | ||
} | ||
|
||
func (d *dataSourceHYOKCustomerKeyVersion) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
client, ok := req.ProviderData.(ConfiguredClient) | ||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"Unexpected Data Source Configure Type", | ||
fmt.Sprintf("Expected tfe.ConfiguredClient, got %T. This is a bug in the tfe provider, so please report it on GitHub.", req.ProviderData), | ||
) | ||
|
||
return | ||
} | ||
d.config = client | ||
} | ||
|
||
func (d *dataSourceHYOKCustomerKeyVersion) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_hyok_customer_key_version" | ||
} | ||
|
||
func (d *dataSourceHYOKCustomerKeyVersion) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Description: "This data source can be used to retrieve a HYOK customer key version.", | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Description: "The ID of the HYOK customer key version.", | ||
Required: true, | ||
}, | ||
"status": schema.StringAttribute{ | ||
Description: "The status of the HYOK customer key version.", | ||
Computed: true, | ||
}, | ||
"error": schema.StringAttribute{ | ||
Description: "Any error message associated with the HYOK customer key version.", | ||
Computed: true, | ||
}, | ||
"key_version": schema.StringAttribute{ | ||
Description: "The version number of the customer key version.", | ||
Computed: true, | ||
}, | ||
"workspaces_secured": schema.Int64Attribute{ | ||
Description: "The number workspaces secured by this customer key version.", | ||
Computed: true, | ||
}, | ||
"created_at": schema.StringAttribute{ | ||
Description: "The timestamp when the key version was created.", | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (d *dataSourceHYOKCustomerKeyVersion) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var data HYOKCustomerKeyVersionDataSourceModel | ||
|
||
// Read Terraform configuration data into the model | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
// Make API call to fetch the HYOK customer key version | ||
keyVersion, err := d.config.Client.HYOKCustomerKeyVersions.Read(ctx, data.ID.ValueString()) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Unable to read HYOK customer key version", err.Error()) | ||
return | ||
} | ||
|
||
// Set the computed attributes from the API response | ||
data.Status = types.StringValue(string(keyVersion.Status)) | ||
data.KeyVersion = types.StringValue(keyVersion.KeyVersion) | ||
data.CreatedAt = types.StringValue(keyVersion.CreatedAt.Format(time.RFC3339)) | ||
data.WorkspacesSecured = types.Int64Value(int64(keyVersion.WorkspacesSecured)) | ||
data.Error = types.StringValue(keyVersion.Error) | ||
|
||
// Save data into Terraform state | ||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} |
40 changes: 40 additions & 0 deletions
40
internal/provider/data_source_hyok_customer_key_version_test.go
This file contains hidden or 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,40 @@ | ||
package provider | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
) | ||
|
||
func TestAccTFEHYOKCustomerKeyVersionDataSource_basic(t *testing.T) { | ||
hyokCustomerKeyVersionID := os.Getenv("HYOK_CUSTOMER_KEY_VERSION_ID") | ||
if hyokCustomerKeyVersionID == "" { | ||
t.Skip("HYOK_CUSTOMER_KEY_VERSION_ID environment variable must be set to run this test") | ||
} | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: testAccMuxedProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccTFEHYOKCustomerKeyVersionDataSourceConfig(hyokCustomerKeyVersionID), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.tfe_hyok_customer_key_version.test", "id", hyokCustomerKeyVersionID), | ||
resource.TestCheckResourceAttrSet("data.tfe_hyok_customer_key_version.test", "status"), | ||
resource.TestCheckResourceAttrSet("data.tfe_hyok_customer_key_version.test", "key_version"), | ||
resource.TestCheckResourceAttrSet("data.tfe_hyok_customer_key_version.test", "created_at"), | ||
resource.TestCheckResourceAttrSet("data.tfe_hyok_customer_key_version.test", "workspaces_secured"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccTFEHYOKCustomerKeyVersionDataSourceConfig(id string) string { | ||
return ` | ||
data "tfe_hyok_customer_key_version" "test" { | ||
id = "` + id + `" | ||
} | ||
` | ||
} |
103 changes: 103 additions & 0 deletions
103
internal/provider/data_source_hyok_encrypted_data_key.go
This file contains hidden or 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,103 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"time" | ||
) | ||
|
||
var ( | ||
_ datasource.DataSource = &dataSourceHYOKEncryptedDataKey{} | ||
_ datasource.DataSourceWithConfigure = &dataSourceHYOKEncryptedDataKey{} | ||
) | ||
|
||
func NewHYOKEncryptedDataKeyDataSource() datasource.DataSource { | ||
return &dataSourceHYOKEncryptedDataKey{} | ||
} | ||
|
||
type dataSourceHYOKEncryptedDataKey struct { | ||
config ConfiguredClient | ||
} | ||
|
||
type HYOKEncryptedDataKeyDataSourceModel struct { | ||
ID types.String `tfsdk:"id"` | ||
EncryptedDEK types.String `tfsdk:"encrypted_dek"` | ||
CustomerKeyName types.String `tfsdk:"customer_key_name"` | ||
CreatedAt types.String `tfsdk:"created_at"` | ||
} | ||
|
||
func (d *dataSourceHYOKEncryptedDataKey) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
client, ok := req.ProviderData.(ConfiguredClient) | ||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"Unexpected Data Source Configure Type", | ||
fmt.Sprintf("Expected tfe.ConfiguredClient, got %T. This is a bug in the tfe provider, so please report it on GitHub.", req.ProviderData), | ||
) | ||
|
||
return | ||
} | ||
d.config = client | ||
} | ||
|
||
func (d *dataSourceHYOKEncryptedDataKey) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_hyok_encrypted_data_key" | ||
} | ||
|
||
func (d *dataSourceHYOKEncryptedDataKey) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Description: "This data source can be used to retrieve a HYOK customer key version.", | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Description: "The ID of the HYOK encrypted data key.", | ||
Required: true, | ||
}, | ||
"encrypted_dek": schema.StringAttribute{ | ||
Description: "The encrypted data encryption key of the HYOK encrypted data key.", | ||
Computed: true, | ||
}, | ||
"customer_key_name": schema.StringAttribute{ | ||
Description: "The customer provided name of the HYOK encrypted data key.", | ||
Computed: true, | ||
}, | ||
"created_at": schema.StringAttribute{ | ||
Description: "The timestamp when the key version was created.", | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (d *dataSourceHYOKEncryptedDataKey) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var data HYOKEncryptedDataKeyDataSourceModel | ||
|
||
// Read Terraform configuration data into the model | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
// Make API call to fetch the HYOK customer key version | ||
keyVersion, err := d.config.Client.HYOKEncryptedDataKeys.Read(ctx, data.ID.ValueString()) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Unable to read HYOK customer key version", err.Error()) | ||
return | ||
} | ||
|
||
// Set the computed attributes from the API response | ||
data.EncryptedDEK = types.StringValue(keyVersion.EncryptedDEK) | ||
data.CustomerKeyName = types.StringValue(keyVersion.CustomerKeyName) | ||
data.CreatedAt = types.StringValue(keyVersion.CreatedAt.Format(time.RFC3339)) | ||
|
||
// Save data into Terraform state | ||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} |
39 changes: 39 additions & 0 deletions
39
internal/provider/data_source_hyok_encrypted_data_key_test.go
This file contains hidden or 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,39 @@ | ||
package provider | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
) | ||
|
||
func TestAccTFEHYOKEncryptedDataKeyDataSource_basic(t *testing.T) { | ||
hyokEncryptedDataKeyID := os.Getenv("HYOK_ENCRYPTED_DATA_KEY_ID") | ||
if hyokEncryptedDataKeyID == "" { | ||
t.Skip("HYOK_ENCRYPTED_DATA_KEY_ID environment variable must be set to run this test") | ||
} | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: testAccMuxedProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccTFEHYOKEncryptedDataKeyDataSourceConfig(hyokEncryptedDataKeyID), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.tfe_hyok_encrypted_data_key.test", "id", hyokEncryptedDataKeyID), | ||
resource.TestCheckResourceAttrSet("data.tfe_hyok_encrypted_data_key.test", "encrypted_dek"), | ||
resource.TestCheckResourceAttrSet("data.tfe_hyok_encrypted_data_key.test", "customer_key_name"), | ||
resource.TestCheckResourceAttrSet("data.tfe_hyok_encrypted_data_key.test", "created_at"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccTFEHYOKEncryptedDataKeyDataSourceConfig(id string) string { | ||
return ` | ||
data "tfe_hyok_encrypted_data_key" "test" { | ||
id = "` + id + `" | ||
} | ||
` | ||
} |
This file contains hidden or 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 hidden or 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,35 @@ | ||
--- | ||
layout: "tfe" | ||
page_title: "Terraform Enterprise: tfe_hyok_customer_key_version" | ||
description: |- | ||
Get information on a HYOK customer key version. | ||
--- | ||
|
||
# Data Source: tfe_hyok_customer_key_version | ||
|
||
Use this data source to get information about a Hold Your Own Keys (HYOK) customer key version. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "tfe_hyok_customer_key_version" "tfe_hyok_customer_key_version1" { | ||
id = "keyv-123" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `id` - (Required) The ID of the HYOK customer key version. | ||
|
||
## Attributes Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `created_at` - The time when the customer key version was created. | ||
* `error` - Any error message associated with the customer key version. | ||
* `id` - The ID of the customer key version. | ||
* `key_version` - The version number of the customer key. | ||
* `status` - The status of the customer key version. | ||
* `workspaces_secured` - The number of workspaces securefd by this customer key version. |
This file contains hidden or 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,33 @@ | ||
--- | ||
layout: "tfe" | ||
page_title: "Terraform Enterprise: tfe_hyok_encrypted_data_key" | ||
description: |- | ||
Get information on a HYOK encrypted data key. | ||
--- | ||
|
||
# Data Source: tfe_hyok_encrypted_data_key | ||
|
||
Use this data source to get information about a Hold Your Own Keys (HYOK) encrypted data key. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "tfe_hyok_encrypted_data_key" "tfe_hyok_encrypted_data_key1" { | ||
id = "dek-123" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `id` - (Required) The ID of the HYOK encrypted data key. | ||
|
||
## Attributes Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `created_at` - The time when the encrypted data key was created. | ||
* `customer_key_name` - The name of the customer key used to encrypt the data key. | ||
* `encrypted_dek` - The encrypted data encryption key (DEK). | ||
* `id` - The ID of the encrypted data key. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.