Skip to content

Commit 3aaead1

Browse files
committed
fix new HVS sync resource creation
1 parent 0b5cf25 commit 3aaead1

File tree

4 files changed

+64
-22
lines changed

4 files changed

+64
-22
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# Vault Secrets Integration can be imported by specifying the name of the integration
2-
terraform import hcp_vault_secrets_sync.example my-sync-name
2+
terraform import hcp_vault_secrets_sync.example gitlab-proj-sync

internal/provider/vaultsecrets/resource_vault_secrets_app_test.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ func TestAccVaultSecretsResourceApp(t *testing.T) {
2323
appName2 = generateRandomSlug()
2424
description1 = "my description 1"
2525
description2 = "my description 2"
26-
syncName = generateRandomSlug()
26+
projSyncName = generateRandomSlug()
27+
groupSyncName = generateRandomSlug()
2728
gitLabToken = checkRequiredEnvVarOrFail(t, "GITLAB_ACCESS_TOKEN")
2829
)
2930

@@ -62,22 +63,30 @@ func TestAccVaultSecretsResourceApp(t *testing.T) {
6263
token = %q
6364
}
6465
}
65-
resource "hcp_vault_secrets_sync" "gitlab_sync" {
66+
resource "hcp_vault_secrets_sync" "gitlab_proj_sync" {
6667
name = %q
6768
integration_name = hcp_vault_secrets_integration.acc_test.name
6869
gitlab_config = {
6970
scope = "PROJECT"
70-
project_id = "1234"
71+
project_id = "123456789"
72+
}
73+
}
74+
resource "hcp_vault_secrets_sync" "gitlab_group_sync" {
75+
name = %q
76+
integration_name = hcp_vault_secrets_integration.acc_test.name
77+
gitlab_config = {
78+
scope = "GROUP"
79+
project_id = "987654321"
7180
}
7281
}
7382
resource "hcp_vault_secrets_app" "acc_test_app" {
7483
app_name = %q
7584
description = %q
7685
sync_names = [hcp_vault_secrets_sync.gitlab_sync.name]
7786
}
78-
`, integrationName1, gitLabToken, syncName, appName2, description2),
87+
`, integrationName1, gitLabToken, projSyncName, groupSyncName, appName2, description2),
7988
Check: resource.ComposeTestCheckFunc(
80-
appCheckFunc(appName2, description2, []string{syncName})...,
89+
appCheckFunc(appName2, description2, []string{projSyncName, groupSyncName})...,
8190
),
8291
},
8392
// Deleting the app out of band causes a recreation

internal/provider/vaultsecrets/resource_vault_secrets_integration.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ func (r *resourceVaultSecretsIntegration) Schema(_ context.Context, _ resource.S
296296
Sensitive: true,
297297
},
298298
},
299+
Validators: []validator.Object{
300+
exactlyOneIntegrationTypeFieldsValidator,
301+
},
299302
},
300303
}
301304

internal/provider/vaultsecrets/resource_vault_secrets_sync.go

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/hashicorp/hcp-sdk-go/clients/cloud-vault-secrets/stable/2023-11-28/client/secret_service"
1010
secretmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-vault-secrets/stable/2023-11-28/models"
11+
"github.com/hashicorp/terraform-plugin-framework-validators/objectvalidator"
1112
"github.com/hashicorp/terraform-plugin-framework/diag"
1213
"github.com/hashicorp/terraform-plugin-framework/path"
1314
"github.com/hashicorp/terraform-plugin-framework/resource"
@@ -18,11 +19,19 @@ import (
1819
"github.com/hashicorp/terraform-plugin-framework/types"
1920
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
2021
"github.com/hashicorp/terraform-provider-hcp/internal/clients"
22+
"github.com/hashicorp/terraform-provider-hcp/internal/provider/modifiers"
2123
)
2224

2325
var _ hvsResource = &Sync{}
2426

27+
var exactlyOneSyncConfigFieldsValidator = objectvalidator.ExactlyOneOf(
28+
path.Expressions{
29+
path.MatchRoot("gitlab_config"),
30+
}...,
31+
)
32+
2533
type Sync struct {
34+
ID types.String `tfsdk:"id"`
2635
Name types.String `tfsdk:"name"`
2736
IntegrationName types.String `tfsdk:"integration_name"`
2837
ProjectID types.String `tfsdk:"project_id"`
@@ -35,11 +44,14 @@ type Sync struct {
3544
gitlabConfig *secretmodels.Secrets20231128SyncConfigGitlab `tfsdk:"-"`
3645
}
3746

47+
func (s *Sync) projectID() types.String {
48+
return s.ProjectID
49+
}
50+
3851
type gitlabConfigParams struct {
39-
EnvironmentScope types.String `tfsdk:"environment_scope"`
40-
Scope types.String `tfsdk:"scope"`
41-
GroupID types.String `tfsdk:"group_id"`
42-
ProjectID types.String `tfsdk:"project_id"`
52+
Scope types.String `tfsdk:"scope"`
53+
GroupID types.String `tfsdk:"group_id"`
54+
ProjectID types.String `tfsdk:"project_id"`
4355
}
4456

4557
func (s *Sync) initModel(ctx context.Context, orgID, projID string) diag.Diagnostics {
@@ -56,23 +68,18 @@ func (s *Sync) initModel(ctx context.Context, orgID, projID string) diag.Diagnos
5668
scope := secretmodels.SyncConfigGitlabScope(config.Scope.ValueString())
5769

5870
s.gitlabConfig = &secretmodels.Secrets20231128SyncConfigGitlab{
59-
EnvironmentScope: config.EnvironmentScope.ValueString(),
60-
GroupID: config.GroupID.ValueString(),
61-
ProjectID: config.ProjectID.ValueString(),
62-
Protected: false,
63-
Raw: false,
64-
Scope: &scope,
71+
GroupID: config.GroupID.ValueString(),
72+
ProjectID: config.ProjectID.ValueString(),
73+
Protected: false,
74+
Raw: false,
75+
Scope: &scope,
6576
}
6677
}
6778

6879
return diag.Diagnostics{}
6980
}
7081

71-
func (s *Sync) projectID() types.String {
72-
return s.ProjectID
73-
}
74-
75-
func (s *Sync) fromModel(_ context.Context, orgID, projID string, model any) diag.Diagnostics {
82+
func (s *Sync) fromModel(ctx context.Context, orgID, projID string, model any) diag.Diagnostics {
7683
diags := diag.Diagnostics{}
7784

7885
syncModel, ok := model.(*secretmodels.Secrets20231128Sync)
@@ -85,11 +92,15 @@ func (s *Sync) fromModel(_ context.Context, orgID, projID string, model any) dia
8592
s.IntegrationName = types.StringValue(syncModel.IntegrationName)
8693
s.OrganizationID = types.StringValue(orgID)
8794
s.ProjectID = types.StringValue(projID)
95+
s.ID = types.StringValue(syncModel.ResourceID)
8896

8997
return diags
9098
}
9199

92100
var _ resource.Resource = &resourceVaultSecretsSync{}
101+
var _ resource.ResourceWithConfigure = &resourceVaultSecretsSync{}
102+
var _ resource.ResourceWithModifyPlan = &resourceVaultSecretsSync{}
103+
var _ resource.ResourceWithImportState = &resourceVaultSecretsSync{}
93104

94105
func NewVaultSecretsSyncResource() resource.Resource {
95106
return &resourceVaultSecretsSync{}
@@ -149,7 +160,7 @@ func (r *resourceVaultSecretsSync) Schema(_ context.Context, _ resource.SchemaRe
149160
},
150161
},
151162
Validators: []validator.Object{
152-
exactlyOneIntegrationTypeFieldsValidator,
163+
exactlyOneSyncConfigFieldsValidator,
153164
},
154165
},
155166
}
@@ -162,6 +173,25 @@ func (r *resourceVaultSecretsSync) Schema(_ context.Context, _ resource.SchemaRe
162173
}
163174
}
164175

176+
func (r *resourceVaultSecretsSync) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
177+
if req.ProviderData == nil {
178+
return
179+
}
180+
client, ok := req.ProviderData.(*clients.Client)
181+
if !ok {
182+
resp.Diagnostics.AddError(
183+
"Unexpected Data Source Configure Type",
184+
fmt.Sprintf("Expected *clients.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
185+
)
186+
return
187+
}
188+
r.client = client
189+
}
190+
191+
func (r *resourceVaultSecretsSync) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
192+
modifiers.ModifyPlanForDefaultProjectChange(ctx, r.client.Config.ProjectID, req.State, req.Config, req.Plan, resp)
193+
}
194+
165195
func (r *resourceVaultSecretsSync) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
166196
resp.Diagnostics.Append(decorateOperation[*Sync](ctx, r.client, &resp.State, req.State.Get, "reading", func(i hvsResource) (any, error) {
167197
sync, ok := i.(*Sync)

0 commit comments

Comments
 (0)