Skip to content

Commit 7166bf2

Browse files
committed
fix(key_manager): add next_rotation_at in key manager resource
1 parent d888258 commit 7166bf2

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

internal/services/keymanager/helpers.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package keymanager
22

33
import (
4+
"fmt"
45
"time"
56

67
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -85,7 +86,7 @@ func ExpandKeyRotationPolicy(v any) (*key_manager.KeyRotationPolicy, error) {
8586

8687
periodStr, ok := m["rotation_period"].(string)
8788
if !ok || periodStr == "" {
88-
return nil, nil
89+
return nil, fmt.Errorf("rotation_period is required when rotation_policy block is specified")
8990
}
9091

9192
period, err := time.ParseDuration(periodStr)
@@ -95,6 +96,7 @@ func ExpandKeyRotationPolicy(v any) (*key_manager.KeyRotationPolicy, error) {
9596

9697
return &key_manager.KeyRotationPolicy{
9798
RotationPeriod: scw.NewDurationFromTimeDuration(period),
99+
NextRotationAt: nil,
98100
}, nil
99101
}
100102

internal/services/keymanager/key_resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func ResourceKeyManagerKey() *schema.Resource {
5252
Description: "Key rotation policy.",
5353
Elem: &schema.Resource{
5454
Schema: map[string]*schema.Schema{
55-
"rotation_period": {Type: schema.TypeString, Optional: true, Description: "Time interval between two key rotations. The minimum duration is 24 hours and the maximum duration is 1 year (876000 hours)."},
55+
"rotation_period": {Type: schema.TypeString, Required: true, Description: "Time interval between two key rotations. The minimum duration is 24 hours and the maximum duration is 1 year (876000 hours)."},
5656
"next_rotation_at": {Type: schema.TypeString, Computed: true, Description: "Timestamp indicating the next scheduled rotation."},
5757
},
5858
},

internal/services/keymanager/key_resource_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,37 @@ func IsKeyManagerKeyDestroyed(tt *acctest.TestTools) resource.TestCheckFunc {
123123
return nil
124124
}
125125
}
126+
127+
func TestAccKeyManagerKey_WithRotationPolicy(t *testing.T) {
128+
tt := acctest.NewTestTools(t)
129+
defer tt.Cleanup()
130+
131+
resource.ParallelTest(t, resource.TestCase{
132+
PreCheck: func() { acctest.PreCheck(t) },
133+
ProviderFactories: tt.ProviderFactories,
134+
CheckDestroy: IsKeyManagerKeyDestroyed(tt),
135+
Steps: []resource.TestStep{
136+
{
137+
Config: `
138+
resource "scaleway_key_manager_key" "main" {
139+
name = "tf-test-kms-key-rotation"
140+
region = "fr-par"
141+
usage = "symmetric_encryption"
142+
description = "Test key with rotation policy"
143+
unprotected = true
144+
145+
rotation_policy {
146+
rotation_period = "720h"
147+
}
148+
}
149+
`,
150+
Check: resource.ComposeTestCheckFunc(
151+
resource.TestCheckResourceAttr("scaleway_key_manager_key.main", "name", "tf-test-kms-key-rotation"),
152+
resource.TestCheckResourceAttr("scaleway_key_manager_key.main", "usage", "symmetric_encryption"),
153+
resource.TestCheckResourceAttr("scaleway_key_manager_key.main", "description", "Test key with rotation policy"),
154+
resource.TestCheckResourceAttr("scaleway_key_manager_key.main", "rotation_policy.0.rotation_period", "720h0m0s"),
155+
),
156+
},
157+
},
158+
})
159+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
version: 2
3+
interactions: []

0 commit comments

Comments
 (0)