Skip to content

Commit 3f04f81

Browse files
mmrqsMélanie MarquesCodelaxremyleone
authored
feat(sem): add secret path in terraform (#2425)
* feat(sem): add secret path in terraform * test(secret): add path tests * fix(secret): add default value and suppress func to secret path * test(secret): add golden * test(secret): add datasource path test * fix(secret): set datasource path field to optional * test(secret): add datasource cassette * doc(secret): add path attribute --------- Co-authored-by: Mélanie Marques <[email protected]> Co-authored-by: Jules Casteran <[email protected]> Co-authored-by: Rémy Léone <[email protected]>
1 parent a66e263 commit 3f04f81

File tree

8 files changed

+1539
-2
lines changed

8 files changed

+1539
-2
lines changed

docs/data-sources/secret.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ data "scaleway_secret" "by_name" {
3434
- `name` - (Optional) The secret name.
3535
Only one of `name` and `secret_id` should be specified.
3636

37+
- `path` - (Optional) The secret path.
38+
Conflicts with `secret_id`.
39+
3740
- `secret_id` - (Optional) The secret id.
3841
Only one of `name` and `secret_id` should be specified.
3942

docs/resources/secret.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ resource "scaleway_secret" "main" {
2525
The following arguments are supported:
2626

2727
- `name` - (Required) Name of the secret (e.g. `my-secret`).
28+
- `path` - (Optional) Path of the secret, defaults to `/`.
2829
- `description` - (Optional) Description of the secret (e.g. `my-new-description`).
2930
- `tags` - (Optional) Tags of the secret (e.g. `["tag", "secret"]`).
3031
- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions)

scaleway/data_source_secret.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@ func dataSourceScalewaySecret() *schema.Resource {
1414
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewaySecret().Schema)
1515

1616
// Set 'Optional' schema elements
17-
addOptionalFieldsToSchema(dsSchema, "name", "region")
17+
addOptionalFieldsToSchema(dsSchema, "name", "region", "path")
1818

1919
dsSchema["name"].ConflictsWith = []string{"secret_id"}
20+
dsSchema["path"].ConflictsWith = []string{"secret_id"}
2021
dsSchema["secret_id"] = &schema.Schema{
2122
Type: schema.TypeString,
2223
Optional: true,
2324
Description: "The ID of the secret",
2425
ValidateFunc: validationUUIDorUUIDWithLocality(),
25-
ConflictsWith: []string{"name"},
26+
ConflictsWith: []string{"name", "path"},
2627
}
2728
dsSchema["organization_id"] = organizationIDOptionalSchema()
2829
dsSchema["project_id"] = &schema.Schema{
@@ -52,6 +53,7 @@ func dataSourceScalewaySecretRead(ctx context.Context, d *schema.ResourceData, m
5253
Name: expandStringPtr(secretName),
5354
ProjectID: expandStringPtr(projectID),
5455
OrganizationID: expandStringPtr(d.Get("organization_id")),
56+
Path: expandStringPtr(d.Get("path")),
5557
}
5658

5759
res, err := api.ListSecrets(request, scw.WithContext(ctx))

scaleway/data_source_secret_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,39 @@ func TestAccScalewayDataSourceSecret_Basic(t *testing.T) {
5959
},
6060
})
6161
}
62+
63+
func TestAccScalewayDataSourceSecret_Path(t *testing.T) {
64+
tt := NewTestTools(t)
65+
defer tt.Cleanup()
66+
67+
resource.ParallelTest(t, resource.TestCase{
68+
PreCheck: func() { testAccPreCheck(t) },
69+
CheckDestroy: testAccCheckScalewaySecretDestroy(tt),
70+
ProviderFactories: tt.ProviderFactories,
71+
Steps: []resource.TestStep{
72+
{
73+
Config: `
74+
resource "scaleway_account_project" "project" {
75+
name = "tf-tests-secret-ds-path"
76+
}
77+
78+
resource "scaleway_secret" "main" {
79+
name = "test-secret-ds-path"
80+
path = "/test-secret-ds-path-path"
81+
project_id = scaleway_account_project.project.id
82+
}
83+
84+
data "scaleway_secret" "by_name" {
85+
name = scaleway_secret.main.name
86+
path = "/test-secret-ds-path-path"
87+
project_id = scaleway_account_project.project.id
88+
}
89+
`,
90+
Check: resource.ComposeTestCheckFunc(
91+
testAccCheckScalewaySecretExists(tt, "data.scaleway_secret.by_name"),
92+
resource.TestCheckResourceAttr("data.scaleway_secret.by_name", "name", "test-secret-ds-path"),
93+
),
94+
},
95+
},
96+
})
97+
}

scaleway/resource_secret.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package scaleway
22

33
import (
44
"context"
5+
"path/filepath"
56

67
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -61,6 +62,15 @@ func resourceScalewaySecret() *schema.Resource {
6162
Computed: true,
6263
Description: "Date and time of secret's creation (RFC 3339 format)",
6364
},
65+
"path": {
66+
Type: schema.TypeString,
67+
Optional: true,
68+
Description: "Location of the secret in the directory structure.",
69+
Default: "/",
70+
DiffSuppressFunc: func(_, oldValue, newValue string, _ *schema.ResourceData) bool {
71+
return filepath.Clean(oldValue) == filepath.Clean(newValue)
72+
},
73+
},
6474
"region": regionSchema(),
6575
"project_id": projectIDSchema(),
6676
},
@@ -89,6 +99,11 @@ func resourceScalewaySecretCreate(ctx context.Context, d *schema.ResourceData, m
8999
secretCreateRequest.Description = expandStringPtr(rawDescription)
90100
}
91101

102+
rawPath, pathExist := d.GetOk("path")
103+
if pathExist {
104+
secretCreateRequest.Path = expandStringPtr(rawPath)
105+
}
106+
92107
secretResponse, err := api.CreateSecret(secretCreateRequest, scw.WithContext(ctx))
93108
if err != nil {
94109
return diag.FromErr(err)
@@ -129,6 +144,7 @@ func resourceScalewaySecretRead(ctx context.Context, d *schema.ResourceData, met
129144
_ = d.Set("version_count", int(secretResponse.VersionCount))
130145
_ = d.Set("region", string(region))
131146
_ = d.Set("project_id", secretResponse.ProjectID)
147+
_ = d.Set("path", secretResponse.Path)
132148

133149
return nil
134150
}
@@ -161,6 +177,11 @@ func resourceScalewaySecretUpdate(ctx context.Context, d *schema.ResourceData, m
161177
hasChanged = true
162178
}
163179

180+
if d.HasChange("path") {
181+
updateRequest.Path = expandUpdatedStringPtr(d.Get("path"))
182+
hasChanged = true
183+
}
184+
164185
if hasChanged {
165186
_, err := api.UpdateSecret(updateRequest, scw.WithContext(ctx))
166187
if err != nil {

scaleway/resource_secret_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,82 @@ func TestAccScalewaySecret_Basic(t *testing.T) {
113113
})
114114
}
115115

116+
func TestAccScalewaySecret_Path(t *testing.T) {
117+
tt := NewTestTools(t)
118+
defer tt.Cleanup()
119+
120+
resource.ParallelTest(t, resource.TestCase{
121+
PreCheck: func() { testAccPreCheck(t) },
122+
ProviderFactories: tt.ProviderFactories,
123+
CheckDestroy: testAccCheckScalewaySecretDestroy(tt),
124+
Steps: []resource.TestStep{
125+
{
126+
Config: `
127+
resource "scaleway_secret" "main" {
128+
name = "test-secret-path-secret"
129+
}
130+
`,
131+
Check: resource.ComposeTestCheckFunc(
132+
testAccCheckScalewaySecretExists(tt, "scaleway_secret.main"),
133+
resource.TestCheckResourceAttr("scaleway_secret.main", "name", "test-secret-path-secret"),
134+
resource.TestCheckResourceAttr("scaleway_secret.main", "path", "/"),
135+
testCheckResourceAttrUUID("scaleway_secret.main", "id"),
136+
),
137+
},
138+
{
139+
Config: `
140+
resource "scaleway_secret" "main" {
141+
name = "test-secret-path-secret"
142+
path = "/test-secret-path"
143+
}
144+
`,
145+
Check: resource.ComposeTestCheckFunc(
146+
testAccCheckScalewaySecretExists(tt, "scaleway_secret.main"),
147+
resource.TestCheckResourceAttr("scaleway_secret.main", "name", "test-secret-path-secret"),
148+
resource.TestCheckResourceAttr("scaleway_secret.main", "path", "/test-secret-path"),
149+
testCheckResourceAttrUUID("scaleway_secret.main", "id"),
150+
),
151+
},
152+
{
153+
Config: `
154+
resource "scaleway_secret" "main" {
155+
name = "test-secret-path-secret"
156+
path = "/test-secret-path/"
157+
}
158+
`,
159+
PlanOnly: true,
160+
},
161+
{
162+
Config: `
163+
resource "scaleway_secret" "main" {
164+
name = "test-secret-path-secret"
165+
path = "/test-secret-path-change/"
166+
}
167+
`,
168+
Check: resource.ComposeTestCheckFunc(
169+
testAccCheckScalewaySecretExists(tt, "scaleway_secret.main"),
170+
resource.TestCheckResourceAttr("scaleway_secret.main", "name", "test-secret-path-secret"),
171+
resource.TestCheckResourceAttr("scaleway_secret.main", "path", "/test-secret-path-change"),
172+
testCheckResourceAttrUUID("scaleway_secret.main", "id"),
173+
),
174+
},
175+
{
176+
Config: `
177+
resource "scaleway_secret" "main" {
178+
name = "test-secret-path-secret"
179+
}
180+
`,
181+
Check: resource.ComposeTestCheckFunc(
182+
testAccCheckScalewaySecretExists(tt, "scaleway_secret.main"),
183+
resource.TestCheckResourceAttr("scaleway_secret.main", "name", "test-secret-path-secret"),
184+
resource.TestCheckResourceAttr("scaleway_secret.main", "path", "/"),
185+
testCheckResourceAttrUUID("scaleway_secret.main", "id"),
186+
),
187+
},
188+
},
189+
})
190+
}
191+
116192
func testAccCheckScalewaySecretExists(tt *TestTools, n string) resource.TestCheckFunc {
117193
return func(state *terraform.State) error {
118194
rs, ok := state.RootModule().Resources[n]

0 commit comments

Comments
 (0)