Skip to content

Commit f563a43

Browse files
committed
Add support for sync resource in HCP Vault Secrets
1 parent 63727a5 commit f563a43

File tree

8 files changed

+379
-12
lines changed

8 files changed

+379
-12
lines changed

.changelog/1196.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:improvement
2+
Add support for sync resource in HCP Vault Secrets
3+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# the provider is derived from the integration name
2+
resource "hcp_vault_secrets_sync" "example_aws_sync" {
3+
name = "my-aws-1"
4+
integration_name = "my-integration-1"
5+
}

internal/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ func (p *ProviderFramework) Resources(ctx context.Context) []func() resource.Res
157157
vaultsecrets.NewVaultSecretsIntegrationResource,
158158
vaultsecrets.NewVaultSecretsDynamicSecretResource,
159159
vaultsecrets.NewVaultSecretsRotatingSecretResource,
160+
vaultsecrets.NewVaultSecretsSyncResource,
160161
// Vault Secrets Deprecated
161162
vaultsecrets.NewVaultSecretsIntegrationAWSResource,
162163
vaultsecrets.NewVaultSecretsIntegrationAzureResource,

internal/provider/vaultsecrets/resource_vault_secrets_app.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import (
1010

1111
"github.com/hashicorp/hcp-sdk-go/clients/cloud-vault-secrets/stable/2023-11-28/client/secret_service"
1212
secretmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-vault-secrets/stable/2023-11-28/models"
13+
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
1314
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1415
"github.com/hashicorp/terraform-plugin-framework/diag"
1516
"github.com/hashicorp/terraform-plugin-framework/path"
1617
"github.com/hashicorp/terraform-plugin-framework/resource"
1718
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
19+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
1820
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1921
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
2022
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
@@ -30,6 +32,7 @@ type App struct {
3032
ProjectID types.String `tfsdk:"project_id"`
3133
OrganizationID types.String `tfsdk:"organization_id"`
3234
ResourceName types.String `tfsdk:"resource_name"`
35+
SyncNames types.Set `tfsdk:"sync_names"`
3336
}
3437

3538
var _ resource.Resource = &resourceVaultSecretsApp{}
@@ -90,7 +93,17 @@ func (r *resourceVaultSecretsApp) Schema(_ context.Context, _ resource.SchemaReq
9093
Computed: true,
9194
Description: "The app's resource name in the format secrets/project/<project ID>/app/<app Name>.",
9295
},
93-
},
96+
"sync_names": schema.ListAttribute{
97+
Description: "List of sync names to associate with this app.",
98+
Optional: true,
99+
ElementType: types.StringType,
100+
PlanModifiers: []planmodifier.List{
101+
listplanmodifier.RequiresReplace(),
102+
},
103+
Validators: []validator.List{
104+
listvalidator.UniqueValues(),
105+
},
106+
}},
94107
}
95108
}
96109

@@ -120,10 +133,13 @@ func (r *resourceVaultSecretsApp) Create(ctx context.Context, req resource.Creat
120133
return nil, fmt.Errorf("invalid resource type, expected *App, got: %T, this is a bug on the provider", i)
121134
}
122135

136+
syncNames := make([]string, len(app.SyncNames.Elements()))
137+
123138
response, err := r.client.VaultSecrets.CreateApp(&secret_service.CreateAppParams{
124139
Body: &secretmodels.SecretServiceCreateAppBody{
125140
Name: app.AppName.ValueString(),
126141
Description: app.Description.ValueString(),
142+
SyncNames: syncNames,
127143
},
128144
OrganizationID: app.OrganizationID.ValueString(),
129145
ProjectID: app.ProjectID.ValueString(),

internal/provider/vaultsecrets/resource_vault_secrets_app_test.go

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package vaultsecrets_test
66
import (
77
"fmt"
88
"os"
9+
"strings"
910
"testing"
1011

1112
"github.com/hashicorp/hcp-sdk-go/clients/cloud-vault-secrets/stable/2023-11-28/client/secret_service"
@@ -17,11 +18,15 @@ import (
1718
)
1819

1920
func TestAccVaultSecretsResourceApp(t *testing.T) {
20-
appName1 := generateRandomSlug()
21-
appName2 := generateRandomSlug()
22-
23-
description1 := "my description 1"
24-
description2 := "my description 2"
21+
var (
22+
integrationName1 = generateRandomSlug()
23+
appName1 = generateRandomSlug()
24+
appName2 = generateRandomSlug()
25+
description1 = "my description 1"
26+
description2 = "my description 2"
27+
syncName = generateRandomSlug()
28+
gitLabToken = checkRequiredEnvVarOrFail(t, "GITLAB_ACCESS_TOKEN")
29+
)
2530

2631
resource.Test(t, resource.TestCase{
2732
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories,
@@ -30,21 +35,50 @@ func TestAccVaultSecretsResourceApp(t *testing.T) {
3035
{
3136
Config: appConfig(appName1, description1),
3237
Check: resource.ComposeTestCheckFunc(
33-
appCheckFunc(appName1, description1)...,
38+
appCheckFunc(appName1, description1, nil)...,
3439
),
3540
},
3641
// Changing an immutable field causes a recreation
3742
{
3843
Config: appConfig(appName2, description1),
3944
Check: resource.ComposeTestCheckFunc(
40-
appCheckFunc(appName2, description1)...,
45+
appCheckFunc(appName2, description1, nil)...,
4146
),
4247
},
4348
// Changing mutable fields causes an update
4449
{
4550
Config: appConfig(appName2, description2),
4651
Check: resource.ComposeTestCheckFunc(
47-
appCheckFunc(appName2, description2)...,
52+
appCheckFunc(appName2, description2, nil)...,
53+
),
54+
},
55+
// Changing the sync_names causes an update
56+
{
57+
Config: fmt.Sprintf(`
58+
resource "hcp_vault_secrets_integration" "acc_test" {
59+
name = %q
60+
capabilities = ["SYNC"]
61+
provider_type = "gitlab"
62+
gitlab_access = {
63+
token = %q
64+
}
65+
}
66+
resource "hcp_vault_secrets_sync" "gitlab_sync" {
67+
name = %q
68+
integration_name = hcp_vault_secrets_integration.acc_test.name
69+
gitlab_config {
70+
scope = "PROJECT"
71+
project_id = "1234"
72+
}
73+
}
74+
resource "hcp_vault_secrets_app" "acc_test_app" {
75+
app_name = %q
76+
description = %q
77+
sync_names = [hcp_vault_secrets_sync.gitlab_sync.name]
78+
}
79+
`, integrationName1, gitLabToken, syncName, appName2, description2),
80+
Check: resource.ComposeTestCheckFunc(
81+
appCheckFunc(appName2, description2, []string{syncName})...,
4882
),
4983
},
5084
// Deleting the app out of band causes a recreation
@@ -63,7 +97,7 @@ func TestAccVaultSecretsResourceApp(t *testing.T) {
6397
},
6498
Config: appConfig(appName2, description2),
6599
Check: resource.ComposeTestCheckFunc(
66-
appCheckFunc(appName2, description2)...,
100+
appCheckFunc(appName2, description2, nil)...,
67101
),
68102
PlanOnly: true,
69103
ExpectNonEmptyPlan: true,
@@ -87,7 +121,7 @@ func TestAccVaultSecretsResourceApp(t *testing.T) {
87121
},
88122
Config: appConfig(appName2, description2),
89123
Check: resource.ComposeTestCheckFunc(
90-
appCheckFunc(appName2, description2)...,
124+
appCheckFunc(appName2, description2, nil)...,
91125
),
92126
ResourceName: "hcp_vault_secrets_app.acc_test_app",
93127
ImportStateId: appName2,
@@ -114,14 +148,19 @@ func appConfig(appName, description string) string {
114148
}`, appName, description)
115149
}
116150

117-
func appCheckFunc(appName, description string) []resource.TestCheckFunc {
151+
func appCheckFunc(appName, description string, syncNames []string) []resource.TestCheckFunc {
152+
formattedSyncs := ""
153+
if len(syncNames) > 0 {
154+
formattedSyncs = fmt.Sprintf("[%s]", strings.Join(syncNames, ","))
155+
}
118156
return []resource.TestCheckFunc{
119157
resource.TestCheckResourceAttrSet("hcp_vault_secrets_app.acc_test_app", "organization_id"),
120158
resource.TestCheckResourceAttrSet("hcp_vault_secrets_app.acc_test_app", "id"),
121159
resource.TestCheckResourceAttrSet("hcp_vault_secrets_app.acc_test_app", "resource_name"),
122160
resource.TestCheckResourceAttr("hcp_vault_secrets_app.acc_test_app", "project_id", os.Getenv("HCP_PROJECT_ID")),
123161
resource.TestCheckResourceAttr("hcp_vault_secrets_app.acc_test_app", "app_name", appName),
124162
resource.TestCheckResourceAttr("hcp_vault_secrets_app.acc_test_app", "description", description),
163+
resource.TestCheckResourceAttr("hcp_vault_secrets_app.acc_test_app", "sync_names", formattedSyncs),
125164
}
126165
}
127166

0 commit comments

Comments
 (0)