Skip to content
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

HCPF-1673: Fix HVS ignoring default project change. #808

Merged
merged 4 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changelog/808.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:bug
Fixes the case where Vault secret resources ignore provider project changes.
```

```release-note:improvement
Vault secret resources can now be created with an optional project ID. If project ID is present, the resource will be created within that project.
```
2 changes: 1 addition & 1 deletion docs/resources/vault_secrets_app.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ resource "hcp_vault_secrets_app" "example" {
### Optional

- `description` (String) The Vault Secrets app description
- `project_id` (String) The ID of the HCP project where the HCP Vault Secrets app is located.

### Read-Only

- `id` (String) Required ID field that is set to the app name.
- `organization_id` (String) The ID of the HCP organization where the project the HCP Vault Secrets app is located.
- `project_id` (String) The ID of the HCP project where the HCP Vault Secrets app is located.
5 changes: 4 additions & 1 deletion docs/resources/vault_secrets_secret.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ resource "hcp_vault_secrets_secret" "example" {
- `secret_name` (String) The name of the secret
- `secret_value` (String, Sensitive) The value of the secret

### Optional

- `project_id` (String) The ID of the HCP project where the HCP Vault Secrets secret is located.

### Read-Only

- `id` (String) The id of the resource
- `organization_id` (String) The ID of the HCP organization where the project the HCP Vault Secrets secret is located.
- `project_id` (String) The ID of the HCP project where the HCP Vault Secrets secret is located.
44 changes: 40 additions & 4 deletions internal/provider/vaultsecrets/resource_vault_secrets_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ import (
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
clients "github.com/hashicorp/terraform-provider-hcp/internal/clients"
"github.com/hashicorp/terraform-provider-hcp/internal/provider/modifiers"
)

var _ resource.Resource = &resourceVaultsecretsApp{}
var _ resource.ResourceWithConfigure = &resourceVaultsecretsApp{}
var _ resource.ResourceWithModifyPlan = &resourceVaultsecretsApp{}

func NewVaultSecretsAppResource() resource.Resource {
return &resourceVaultsecretsApp{}
}
Expand Down Expand Up @@ -53,6 +60,11 @@ func (r *resourceVaultsecretsApp) Schema(_ context.Context, _ resource.SchemaReq
"project_id": schema.StringAttribute{
Description: "The ID of the HCP project where the HCP Vault Secrets app is located.",
Computed: true,
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
stringplanmodifier.UseStateForUnknown(),
},
},
"organization_id": schema.StringAttribute{
Description: "The ID of the HCP organization where the project the HCP Vault Secrets app is located.",
Expand All @@ -77,6 +89,10 @@ func (r *resourceVaultsecretsApp) Configure(_ context.Context, req resource.Conf
r.client = client
}

func (r *resourceVaultsecretsApp) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
modifiers.ModifyPlanForDefaultProjectChange(ctx, r.client.Config.ProjectID, req.State, req.Config, req.Plan, resp)
}

type VaultSecretsApp struct {
ID types.String `tfsdk:"id"`
AppName types.String `tfsdk:"app_name"`
Expand All @@ -93,9 +109,14 @@ func (r *resourceVaultsecretsApp) Create(ctx context.Context, req resource.Creat
return
}

projectID := r.client.Config.ProjectID
if !plan.ProjectID.IsUnknown() {
projectID = plan.ProjectID.ValueString()
}

loc := &sharedmodels.HashicorpCloudLocationLocation{
OrganizationID: r.client.Config.OrganizationID,
ProjectID: r.client.Config.ProjectID,
ProjectID: projectID,
}

res, err := clients.CreateVaultSecretsApp(ctx, r.client, loc, plan.AppName.ValueString(), plan.Description.ValueString())
Expand All @@ -121,9 +142,14 @@ func (r *resourceVaultsecretsApp) Read(ctx context.Context, req resource.ReadReq
return
}

projectID := r.client.Config.ProjectID
if !state.ProjectID.IsUnknown() {
projectID = state.ProjectID.ValueString()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: If I am getting this correctly, then idea is to read project id from plan for CUD apis, but read it from state for R, right? In that case, we can probably add a comment here explaining why Read api has state lookup and CUD apis have plan lookup.

}

loc := &sharedmodels.HashicorpCloudLocationLocation{
OrganizationID: r.client.Config.OrganizationID,
ProjectID: r.client.Config.ProjectID,
ProjectID: projectID,
}

res, err := clients.GetVaultSecretsApp(ctx, r.client, loc, state.AppName.ValueString())
Expand All @@ -145,9 +171,14 @@ func (r *resourceVaultsecretsApp) Update(ctx context.Context, req resource.Updat
return
}

projectID := r.client.Config.ProjectID
if !plan.ProjectID.IsUnknown() {
projectID = plan.ProjectID.ValueString()
}

loc := &sharedmodels.HashicorpCloudLocationLocation{
OrganizationID: r.client.Config.OrganizationID,
ProjectID: r.client.Config.ProjectID,
ProjectID: projectID,
}

res, err := clients.UpdateVaultSecretsApp(ctx, r.client, loc, plan.AppName.ValueString(), plan.Description.ValueString())
Expand All @@ -173,9 +204,14 @@ func (r *resourceVaultsecretsApp) Delete(ctx context.Context, req resource.Delet
return
}

projectID := r.client.Config.ProjectID
if !state.ProjectID.IsUnknown() {
projectID = state.ProjectID.ValueString()
}

loc := &sharedmodels.HashicorpCloudLocationLocation{
OrganizationID: r.client.Config.OrganizationID,
ProjectID: r.client.Config.ProjectID,
ProjectID: projectID,
}

err := clients.DeleteVaultSecretsApp(ctx, r.client, loc, state.AppName.ValueString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ func TestAccVaultSecretsResourceApp(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "hcp_project" "example" {
name = "test-project"
}
resource "hcp_vault_secrets_app" "example" {
app_name = %q
description = "Acceptance test run"
project_id = hcp_project.example.resource_id
}`, testAppName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("hcp_vault_secrets_app.example", "app_name", testAppName),
resource.TestCheckResourceAttr("hcp_vault_secrets_app.example", "description", "Acceptance test run"),
resource.TestCheckResourceAttrSet("hcp_vault_secrets_app.example", "project_id"),
),
},
{
Config: fmt.Sprintf(`
resource "hcp_vault_secrets_app" "example" {
Expand Down
44 changes: 40 additions & 4 deletions internal/provider/vaultsecrets/resource_vault_secrets_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ import (
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-provider-hcp/internal/clients"
"github.com/hashicorp/terraform-provider-hcp/internal/provider/modifiers"
)

var _ resource.Resource = &resourceVaultsecretsSecret{}
var _ resource.ResourceWithConfigure = &resourceVaultsecretsSecret{}
var _ resource.ResourceWithModifyPlan = &resourceVaultsecretsSecret{}

func NewVaultSecretsSecretResource() resource.Resource {
return &resourceVaultsecretsSecret{}
}
Expand Down Expand Up @@ -78,6 +85,11 @@ func (r *resourceVaultsecretsSecret) Schema(_ context.Context, _ resource.Schema
"project_id": schema.StringAttribute{
Description: "The ID of the HCP project where the HCP Vault Secrets secret is located.",
Computed: true,
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
stringplanmodifier.UseStateForUnknown(),
},
},
"organization_id": schema.StringAttribute{
Description: "The ID of the HCP organization where the project the HCP Vault Secrets secret is located.",
Expand All @@ -103,6 +115,10 @@ func (r *resourceVaultsecretsSecret) Configure(_ context.Context, req resource.C
r.client = client
}

func (r *resourceVaultsecretsSecret) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
modifiers.ModifyPlanForDefaultProjectChange(ctx, r.client.Config.ProjectID, req.State, req.Config, req.Plan, resp)
}

func (r *resourceVaultsecretsSecret) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan VaultSecretsSecret
diags := req.Plan.Get(ctx, &plan)
Expand All @@ -111,9 +127,14 @@ func (r *resourceVaultsecretsSecret) Create(ctx context.Context, req resource.Cr
return
}

projectID := r.client.Config.ProjectID
if !plan.ProjectID.IsUnknown() {
projectID = plan.ProjectID.ValueString()
}

loc := &sharedmodels.HashicorpCloudLocationLocation{
OrganizationID: r.client.Config.OrganizationID,
ProjectID: r.client.Config.ProjectID,
ProjectID: projectID,
}

res, err := clients.CreateVaultSecretsAppSecret(ctx, r.client, loc, plan.AppName.ValueString(), plan.SecretName.ValueString(), plan.SecretValue.ValueString())
Expand All @@ -138,9 +159,14 @@ func (r *resourceVaultsecretsSecret) Read(ctx context.Context, req resource.Read
return
}

projectID := r.client.Config.ProjectID
if !state.ProjectID.IsUnknown() {
projectID = state.ProjectID.ValueString()
}

loc := &sharedmodels.HashicorpCloudLocationLocation{
OrganizationID: r.client.Config.OrganizationID,
ProjectID: r.client.Config.ProjectID,
ProjectID: projectID,
}

res, err := clients.OpenVaultSecretsAppSecret(ctx, r.client, loc, state.AppName.ValueString(), state.SecretName.ValueString())
Expand All @@ -162,9 +188,14 @@ func (r *resourceVaultsecretsSecret) Update(ctx context.Context, req resource.Up
return
}

projectID := r.client.Config.ProjectID
if !plan.ProjectID.IsUnknown() {
projectID = plan.ProjectID.ValueString()
}

loc := &sharedmodels.HashicorpCloudLocationLocation{
OrganizationID: r.client.Config.OrganizationID,
ProjectID: r.client.Config.ProjectID,
ProjectID: projectID,
}

res, err := clients.CreateVaultSecretsAppSecret(ctx, r.client, loc, plan.AppName.ValueString(), plan.SecretName.ValueString(), plan.SecretValue.ValueString())
Expand All @@ -189,9 +220,14 @@ func (r *resourceVaultsecretsSecret) Delete(ctx context.Context, req resource.De
return
}

projectID := r.client.Config.ProjectID
if !state.ProjectID.IsUnknown() {
projectID = state.ProjectID.ValueString()
}

loc := &sharedmodels.HashicorpCloudLocationLocation{
OrganizationID: r.client.Config.OrganizationID,
ProjectID: r.client.Config.ProjectID,
ProjectID: projectID,
}

err := clients.DeleteVaultSecretsAppSecret(ctx, r.client, loc, state.AppName.ValueString(), state.SecretName.ValueString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,51 @@ import (
)

func TestAccVaultSecretsResourceSecret(t *testing.T) {
testAppName := generateRandomSlug()
testAppName1 := generateRandomSlug()
testAppName2 := generateRandomSlug()
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories,
Steps: []resource.TestStep{

{
PreConfig: func() {
createTestApp(t, testAppName)
createTestApp(t, testAppName1)
},
Config: fmt.Sprintf(`
resource "hcp_vault_secrets_secret" "example" {
app_name = %q
secret_name = "test_secret"
secret_value = "super secret"
}`, testAppName),
}`, testAppName1),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("hcp_vault_secrets_secret.example", "app_name", testAppName1),
resource.TestCheckResourceAttr("hcp_vault_secrets_secret.example", "secret_name", "test_secret"),
resource.TestCheckResourceAttr("hcp_vault_secrets_secret.example", "secret_value", "super secret"),
),
},
{
Config: fmt.Sprintf(`
resource "hcp_project" "example" {
name = "test-project"
}

resource "hcp_vault_secrets_app" "example" {
app_name = %q
description = "Acceptance test run"
project_id = hcp_project.example.resource_id
}

resource "hcp_vault_secrets_secret" "example" {
app_name = hcp_vault_secrets_app.example.app_name
secret_name = "test_secret"
secret_value = "super secret"
project_id = hcp_project.example.resource_id
}`, testAppName2),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("hcp_vault_secrets_secret.example", "app_name", testAppName),
resource.TestCheckResourceAttr("hcp_vault_secrets_secret.example", "app_name", testAppName2),
resource.TestCheckResourceAttr("hcp_vault_secrets_secret.example", "secret_name", "test_secret"),
resource.TestCheckResourceAttr("hcp_vault_secrets_secret.example", "secret_value", "super secret"),
resource.TestCheckResourceAttrSet("hcp_vault_secrets_secret.example", "project_id"),
),
},
},
Expand Down