Skip to content

Commit ccceb56

Browse files
committed
fix validation issues
1 parent 08136dd commit ccceb56

File tree

5 files changed

+103
-35
lines changed

5 files changed

+103
-35
lines changed

docs/resources/vault_secrets_sync.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,11 @@ resource "hcp_vault_secrets_sync" "example_gitlab_project_sync" {
5252
<a id="nestedatt--gitlab_config"></a>
5353
### Nested Schema for `gitlab_config`
5454

55-
Required:
56-
57-
- `scope` (String) The scope to which values apply. The valid options are GROUP and PROJECT
58-
5955
Optional:
6056

6157
- `group_id` (String) ID of the group, if the scope is GROUP
6258
- `project_id` (String) ID of the project, if the scope is PROJECT
59+
- `scope` (String) The scope to which sync applies. Defaults to GROUP. The valid options are GROUP and PROJECT
6360

6461
## Import
6562

internal/provider/vaultsecrets/resource_vault_secrets_app_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestAccVaultSecretsResourceApp(t *testing.T) {
2525
description2 = "my description 2"
2626
projSyncName = generateRandomSlug()
2727
groupSyncName = generateRandomSlug()
28-
gitLabToken = checkRequiredEnvVarOrFail(t, "GITLAB_ACCESS_TOKEN")
28+
gitLabToken = checkRequiredEnvVarOrFail(t, "VAULTSECRETS_GITLAB_ACCESS_TOKEN")
2929
)
3030

3131
resource.Test(t, resource.TestCase{

internal/provider/vaultsecrets/resource_vault_secrets_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
)
1818

1919
func TestAccVaultSecretsResourceIntegrationGitLab(t *testing.T) {
20-
accessToken := checkRequiredEnvVarOrFail(t, "GITLAB_ACCESS_TOKEN")
20+
accessToken := checkRequiredEnvVarOrFail(t, "VAULTSECRETS_GITLAB_ACCESS_TOKEN")
2121

2222
integrationName1 := generateRandomSlug()
2323
integrationName2 := generateRandomSlug()

internal/provider/vaultsecrets/resource_vault_secrets_sync.go

Lines changed: 99 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,24 @@ func (r *resourceVaultSecretsSync) Schema(_ context.Context, _ resource.SchemaRe
8686
Optional: true,
8787
Attributes: map[string]schema.Attribute{
8888
"scope": schema.StringAttribute{
89-
Description: "The scope to which values apply. The valid options are GROUP and PROJECT",
89+
Description: "The scope to which sync applies. Defaults to GROUP. The valid options are GROUP and PROJECT",
9090
Optional: true,
91+
Computed: true,
9192
Validators: []validator.String{
9293
stringvalidator.OneOf("GROUP", "PROJECT"),
9394
},
9495
},
9596
"group_id": schema.StringAttribute{
9697
Description: "ID of the group, if the scope is GROUP",
9798
Optional: true,
99+
Validators: []validator.String{
100+
stringvalidator.ConflictsWith(
101+
path.MatchRelative().AtParent().AtName("project_id"),
102+
),
103+
stringvalidator.AtLeastOneOf(
104+
path.MatchRelative().AtParent().AtName("project_id"),
105+
),
106+
},
98107
},
99108
"project_id": schema.StringAttribute{
100109
Description: "ID of the project, if the scope is PROJECT",
@@ -237,14 +246,55 @@ func (s *Sync) initModel(ctx context.Context, orgID, projID string) diag.Diagnos
237246
return diags
238247
}
239248

240-
scope := secretmodels.SyncConfigGitlabScope(config.Scope.ValueString())
249+
scopeStr := config.Scope.ValueString()
250+
if scopeStr == "" {
251+
if !config.GroupID.IsNull() {
252+
scopeStr = "GROUP"
253+
} else if !config.ProjectID.IsNull() {
254+
scopeStr = "PROJECT"
255+
} else {
256+
return diag.Diagnostics{
257+
diag.NewErrorDiagnostic(
258+
"Invalid GitLab Configuration",
259+
"Either group_id or project_id must be specified",
260+
),
261+
}
262+
}
263+
}
264+
265+
scope := secretmodels.SyncConfigGitlabScope(scopeStr)
266+
267+
var groupIDVal, projectIDVal string
268+
if scope == secretmodels.SyncConfigGitlabScopeGROUP {
269+
if config.GroupID.IsNull() {
270+
return diag.Diagnostics{
271+
diag.NewErrorDiagnostic(
272+
"Invalid GitLab Configuration",
273+
"group_id is required when scope is GROUP",
274+
),
275+
}
276+
}
277+
groupIDVal = config.GroupID.ValueString()
278+
projectIDVal = ""
279+
} else if scope == secretmodels.SyncConfigGitlabScopePROJECT {
280+
if config.ProjectID.IsNull() {
281+
return diag.Diagnostics{
282+
diag.NewErrorDiagnostic(
283+
"Invalid GitLab Configuration",
284+
"project_id is required when scope is PROJECT",
285+
),
286+
}
287+
}
288+
groupIDVal = ""
289+
projectIDVal = config.ProjectID.ValueString()
290+
}
241291

242292
s.gitlabConfig = &secretmodels.Secrets20231128SyncConfigGitlab{
243-
GroupID: config.GroupID.ValueString(),
244-
ProjectID: config.ProjectID.ValueString(),
293+
Scope: &scope,
294+
GroupID: groupIDVal,
295+
ProjectID: projectIDVal,
245296
Protected: false,
246297
Raw: false,
247-
Scope: &scope,
248298
}
249299
}
250300

@@ -256,7 +306,10 @@ func (s *Sync) fromModel(ctx context.Context, orgID, projID string, model any) d
256306

257307
syncModel, ok := model.(*secretmodels.Secrets20231128Sync)
258308
if !ok {
259-
diags.AddError("Invalid model type, this is a bug on the provider.", fmt.Sprintf("Expected *secretmodels.Secrets20231128Sync, got: %T", model))
309+
diags.AddError(
310+
"Invalid model type, this is a bug on the provider.",
311+
fmt.Sprintf("Expected *secretmodels.Secrets20231128Sync, got: %T", model),
312+
)
260313
return diags
261314
}
262315

@@ -266,31 +319,49 @@ func (s *Sync) fromModel(ctx context.Context, orgID, projID string, model any) d
266319
s.OrganizationID = types.StringValue(orgID)
267320
s.ProjectID = types.StringValue(projID)
268321

269-
if syncModel.SyncConfigGitlab != nil {
270-
scope := *syncModel.SyncConfigGitlab.Scope
271-
var groupIDValue types.String
272-
var projectIDValue types.String
322+
if syncModel.SyncConfigGitlab == nil {
323+
s.GitlabConfig = types.ObjectNull(map[string]attr.Type{
324+
"scope": types.StringType,
325+
"group_id": types.StringType,
326+
"project_id": types.StringType,
327+
})
273328

274-
if syncModel.SyncConfigGitlab.GroupID == "" {
275-
groupIDValue = types.StringNull()
276-
} else {
277-
groupIDValue = types.StringValue(syncModel.SyncConfigGitlab.GroupID)
278-
}
329+
return diags
330+
}
279331

280-
if syncModel.SyncConfigGitlab.ProjectID == "" {
281-
projectIDValue = types.StringNull()
282-
} else {
283-
projectIDValue = types.StringValue(syncModel.SyncConfigGitlab.ProjectID)
284-
}
332+
scopeVal := types.StringNull()
333+
if syncModel.SyncConfigGitlab.Scope != nil {
334+
scopeVal = types.StringValue(string(*syncModel.SyncConfigGitlab.Scope))
335+
}
285336

286-
s.GitlabConfig, diags = types.ObjectValue(
287-
s.GitlabConfig.AttributeTypes(ctx),
288-
map[string]attr.Value{
289-
"scope": types.StringValue(string(scope)),
290-
"group_id": groupIDValue,
291-
"project_id": projectIDValue,
292-
},
293-
)
337+
var groupIDVal types.String
338+
if syncModel.SyncConfigGitlab.GroupID != "" {
339+
groupIDVal = types.StringValue(syncModel.SyncConfigGitlab.GroupID)
340+
} else {
341+
groupIDVal = types.StringNull()
342+
}
343+
344+
var projectIDVal types.String
345+
if syncModel.SyncConfigGitlab.ProjectID != "" {
346+
projectIDVal = types.StringValue(syncModel.SyncConfigGitlab.ProjectID)
347+
} else {
348+
projectIDVal = types.StringNull()
349+
}
350+
351+
s.GitlabConfig, diags = types.ObjectValue(
352+
map[string]attr.Type{
353+
"scope": types.StringType,
354+
"group_id": types.StringType,
355+
"project_id": types.StringType,
356+
},
357+
map[string]attr.Value{
358+
"scope": scopeVal,
359+
"group_id": groupIDVal,
360+
"project_id": projectIDVal,
361+
},
362+
)
363+
if diags.HasError() {
364+
return diags
294365
}
295366

296367
return diags

internal/provider/vaultsecrets/resource_vault_secrets_sync_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
func TestAccVaultSecretsResourceSync(t *testing.T) {
1313
syncName := generateRandomSlug()
1414
integrationName := generateRandomSlug()
15-
gitLabToken := checkRequiredEnvVarOrFail(t, "GITLAB_ACCESS_TOKEN")
15+
gitLabToken := checkRequiredEnvVarOrFail(t, "VAULTSECRETS_GITLAB_ACCESS_TOKEN")
1616

1717
resource.Test(t, resource.TestCase{
1818
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories,

0 commit comments

Comments
 (0)