Skip to content

Commit 47aa4d3

Browse files
committed
feat: make expires_at optional in MongoDB snapshot action and add audit trail verification
1 parent fe0e400 commit 47aa4d3

File tree

2 files changed

+74
-11
lines changed

2 files changed

+74
-11
lines changed

internal/services/mongodb/action_instance_snapshot_action.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ func (a *InstanceSnapshotAction) Schema(_ context.Context, _ action.SchemaReques
8181
Description: "Name of the snapshot. If not set, a name will be generated.",
8282
},
8383
"expires_at": schema.StringAttribute{
84-
Required: true,
85-
Description: "Expiration date of the snapshot in RFC3339 format (ISO 8601).",
84+
Optional: true,
85+
Description: "Expiration date of the snapshot in RFC3339 format (ISO 8601). If not set, the snapshot will not expire.",
8686
},
8787
"wait": schema.BoolAttribute{
8888
Optional: true,
@@ -142,22 +142,27 @@ func (a *InstanceSnapshotAction) Invoke(ctx context.Context, req action.InvokeRe
142142
snapshotName = "tf-mongodb-snapshot"
143143
}
144144

145-
expirationRaw := data.ExpiresAt.ValueString()
145+
var expirationTime *time.Time
146+
if !data.ExpiresAt.IsNull() && data.ExpiresAt.ValueString() != "" {
147+
expirationRaw := data.ExpiresAt.ValueString()
146148

147-
expirationTime, err := time.Parse(time.RFC3339, expirationRaw)
148-
if err != nil {
149-
resp.Diagnostics.AddError(
150-
"Invalid expires_at value",
151-
fmt.Sprintf("The expires_at attribute must be a valid RFC3339 timestamp. Got %q: %s", expirationRaw, err),
152-
)
149+
parsedTime, err := time.Parse(time.RFC3339, expirationRaw)
150+
if err != nil {
151+
resp.Diagnostics.AddError(
152+
"Invalid expires_at value",
153+
fmt.Sprintf("The expires_at attribute must be a valid RFC3339 timestamp. Got %q: %s", expirationRaw, err),
154+
)
153155

154-
return
156+
return
157+
}
158+
159+
expirationTime = &parsedTime
155160
}
156161

157162
createReq := &mongodb.CreateSnapshotRequest{
158163
InstanceID: instanceID,
159164
Name: snapshotName,
160-
ExpiresAt: &expirationTime,
165+
ExpiresAt: expirationTime,
161166
}
162167

163168
if region != "" {

internal/services/mongodb/action_instance_snapshot_action_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package mongodb_test
22

33
import (
4+
"errors"
5+
"strings"
46
"testing"
57

68
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-testing/terraform"
710
"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
811
)
912

@@ -46,6 +49,61 @@ func TestAccActionMongoDBInstanceSnapshot_Basic(t *testing.T) {
4649
}
4750
`,
4851
},
52+
{
53+
Config: `
54+
resource "scaleway_mongodb_instance" "main" {
55+
name = "test-mongodb-action-snapshot"
56+
version = "7.0.12"
57+
node_type = "MGDB-PLAY2-NANO"
58+
node_number = 1
59+
user_name = "my_initial_user"
60+
password = "thiZ_is_v&ry_s3cret"
61+
62+
lifecycle {
63+
action_trigger {
64+
events = [after_create]
65+
actions = [action.scaleway_mongodb_instance_snapshot_action.main]
66+
}
67+
}
68+
}
69+
70+
action "scaleway_mongodb_instance_snapshot_action" "main" {
71+
config {
72+
instance_id = scaleway_mongodb_instance.main.id
73+
name = "tf-acc-mongodb-instance-snapshot-action"
74+
expires_at = "2026-11-01T00:00:00Z"
75+
wait = true
76+
}
77+
}
78+
79+
data "scaleway_audit_trail_event" "mongodb" {
80+
resource_type = "mongodb_instance"
81+
resource_id = scaleway_mongodb_instance.main.id
82+
method_name = "CreateSnapshot"
83+
}
84+
`,
85+
Check: resource.ComposeTestCheckFunc(
86+
resource.TestCheckResourceAttrSet("data.scaleway_audit_trail_event.mongodb", "events.#"),
87+
func(state *terraform.State) error {
88+
rs, ok := state.RootModule().Resources["data.scaleway_audit_trail_event.mongodb"]
89+
if !ok {
90+
return errors.New("not found: data.scaleway_audit_trail_event.mongodb")
91+
}
92+
93+
for key, value := range rs.Primary.Attributes {
94+
if !strings.Contains(key, "request_body") {
95+
continue
96+
}
97+
98+
if strings.Contains(value, "tf-acc-mongodb-instance-snapshot-action") {
99+
return nil
100+
}
101+
}
102+
103+
return errors.New("did not find the CreateSnapshot event")
104+
},
105+
),
106+
},
49107
},
50108
})
51109
}

0 commit comments

Comments
 (0)