-
Notifications
You must be signed in to change notification settings - Fork 132
feat(mongodb): add mongodb instance snapshot action #3494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jremy42
wants to merge
9
commits into
master
Choose a base branch
from
feat/mongodb-instance-snapshot-action
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8432199
feat: add mongodb instance snapshot action
jremy42 043911a
Merge branch 'master' into feat/mongodb-instance-snapshot-action
jremy42 fe0e400
Merge branch 'master' into feat/mongodb-instance-snapshot-action
jremy42 47aa4d3
feat: make expires_at optional in MongoDB snapshot action and add aud…
jremy42 08ad79a
feat: replace audit trail verification with direct snapshot check for…
jremy42 22190b1
feat: compress MongoDB snapshot action test cassette
jremy42 79b94b4
fix: correct linting issues (wsl_v5) and regenerate docs
jremy42 655f666
fix: move expires_at to Optional section in docs
jremy42 b3a3ab2
Merge branch 'master' into feat/mongodb-instance-snapshot-action
jremy42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| --- | ||
| subcategory: "MongoDB" | ||
| page_title: "Scaleway: scaleway_mongodb_instance_snapshot_action" | ||
| --- | ||
|
|
||
| # scaleway_mongodb_instance_snapshot_action (Action) | ||
|
|
||
| <!-- action schema generated by tfplugindocs --> | ||
| ## Schema | ||
|
|
||
| ### Required | ||
|
|
||
| - `expires_at` (String) Expiration date of the snapshot in RFC3339 format (ISO 8601). | ||
| - `instance_id` (String) MongoDB instance ID to snapshot. Can be a plain UUID or a regional ID. | ||
|
|
||
| ### Optional | ||
|
|
||
| - `name` (String) Name of the snapshot. If not set, a name will be generated. | ||
| - `region` (String) Region of the MongoDB instance. If not set, the region is derived from the instance_id when possible or from the provider configuration. | ||
| - `wait` (Boolean) Wait for the snapshot to reach a terminal state before returning. | ||
|
|
||
|
|
202 changes: 202 additions & 0 deletions
202
internal/services/mongodb/action_instance_snapshot_action.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| package mongodb | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" | ||
| "github.com/hashicorp/terraform-plugin-framework/action" | ||
| "github.com/hashicorp/terraform-plugin-framework/action/schema" | ||
| "github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
| "github.com/hashicorp/terraform-plugin-framework/types" | ||
| mongodb "github.com/scaleway/scaleway-sdk-go/api/mongodb/v1" | ||
| "github.com/scaleway/scaleway-sdk-go/scw" | ||
| "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality" | ||
| "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional" | ||
| "github.com/scaleway/terraform-provider-scaleway/v2/internal/meta" | ||
| ) | ||
|
|
||
| var ( | ||
| _ action.Action = (*InstanceSnapshotAction)(nil) | ||
| _ action.ActionWithConfigure = (*InstanceSnapshotAction)(nil) | ||
| ) | ||
|
|
||
| // InstanceSnapshotAction creates a snapshot for a MongoDB instance. | ||
| type InstanceSnapshotAction struct { | ||
| mongodbAPI *mongodb.API | ||
| } | ||
|
|
||
| func (a *InstanceSnapshotAction) Configure(_ context.Context, req action.ConfigureRequest, resp *action.ConfigureResponse) { | ||
| if req.ProviderData == nil { | ||
| return | ||
| } | ||
|
|
||
| m, ok := req.ProviderData.(*meta.Meta) | ||
| if !ok { | ||
| resp.Diagnostics.AddError( | ||
| "Unexpected Action Configure Type", | ||
| fmt.Sprintf("Expected *meta.Meta, got: %T. Please report this issue to the provider developers.", req.ProviderData), | ||
| ) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| a.mongodbAPI = newAPI(m) | ||
| } | ||
|
|
||
| func (a *InstanceSnapshotAction) Metadata(_ context.Context, req action.MetadataRequest, resp *action.MetadataResponse) { | ||
| resp.TypeName = req.ProviderTypeName + "_mongodb_instance_snapshot_action" | ||
| } | ||
|
|
||
| type InstanceSnapshotActionModel struct { | ||
| InstanceID types.String `tfsdk:"instance_id"` | ||
| Region types.String `tfsdk:"region"` | ||
| Name types.String `tfsdk:"name"` | ||
| ExpiresAt types.String `tfsdk:"expires_at"` | ||
| Wait types.Bool `tfsdk:"wait"` | ||
| } | ||
|
|
||
| // NewInstanceSnapshotAction returns a new MongoDB instance snapshot action. | ||
| func NewInstanceSnapshotAction() action.Action { | ||
| return &InstanceSnapshotAction{} | ||
| } | ||
|
|
||
| func (a *InstanceSnapshotAction) Schema(_ context.Context, _ action.SchemaRequest, resp *action.SchemaResponse) { | ||
| resp.Schema = schema.Schema{ | ||
| Attributes: map[string]schema.Attribute{ | ||
| "instance_id": schema.StringAttribute{ | ||
| Required: true, | ||
| Description: "MongoDB instance ID to snapshot. Can be a plain UUID or a regional ID.", | ||
| Validators: []validator.String{ | ||
| stringvalidator.LengthAtLeast(1), | ||
| }, | ||
| }, | ||
| "region": schema.StringAttribute{ | ||
| Optional: true, | ||
| Description: "Region of the MongoDB instance. If not set, the region is derived from the instance_id when possible or from the provider configuration.", | ||
| }, | ||
| "name": schema.StringAttribute{ | ||
| Optional: true, | ||
| Description: "Name of the snapshot. If not set, a name will be generated.", | ||
| }, | ||
| "expires_at": schema.StringAttribute{ | ||
| Required: true, | ||
| Description: "Expiration date of the snapshot in RFC3339 format (ISO 8601).", | ||
| }, | ||
| "wait": schema.BoolAttribute{ | ||
| Optional: true, | ||
| Description: "Wait for the snapshot to reach a terminal state before returning.", | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (a *InstanceSnapshotAction) Invoke(ctx context.Context, req action.InvokeRequest, resp *action.InvokeResponse) { | ||
| var data InstanceSnapshotActionModel | ||
|
|
||
| resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
|
|
||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
|
|
||
| if a.mongodbAPI == nil { | ||
| resp.Diagnostics.AddError( | ||
| "Unconfigured mongodbAPI", | ||
| "The action was not properly configured. The Scaleway client is missing. "+ | ||
| "This is usually a bug in the provider. Please report it to the maintainers.", | ||
| ) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| if data.InstanceID.IsNull() || data.InstanceID.ValueString() == "" { | ||
| resp.Diagnostics.AddError( | ||
| "Missing instance_id", | ||
| "The instance_id attribute is required to create a MongoDB snapshot.", | ||
| ) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| instanceID := locality.ExpandID(data.InstanceID.ValueString()) | ||
|
|
||
| var ( | ||
| region scw.Region | ||
| err error | ||
| ) | ||
|
|
||
| if !data.Region.IsNull() && data.Region.ValueString() != "" { | ||
| region = scw.Region(data.Region.ValueString()) | ||
| } else { | ||
| // Try to derive region from the instance_id if it is a regional ID. | ||
| if derivedRegion, id, parseErr := regional.ParseID(data.InstanceID.ValueString()); parseErr == nil { | ||
| region = derivedRegion | ||
| instanceID = id | ||
| } | ||
| } | ||
|
|
||
| snapshotName := data.Name.ValueString() | ||
| if snapshotName == "" { | ||
| snapshotName = "tf-mongodb-snapshot" | ||
| } | ||
|
|
||
| expirationRaw := data.ExpiresAt.ValueString() | ||
|
|
||
| expirationTime, err := time.Parse(time.RFC3339, expirationRaw) | ||
| if err != nil { | ||
| resp.Diagnostics.AddError( | ||
| "Invalid expires_at value", | ||
| fmt.Sprintf("The expires_at attribute must be a valid RFC3339 timestamp. Got %q: %s", expirationRaw, err), | ||
| ) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| createReq := &mongodb.CreateSnapshotRequest{ | ||
| InstanceID: instanceID, | ||
| Name: snapshotName, | ||
| ExpiresAt: &expirationTime, | ||
| } | ||
|
|
||
| if region != "" { | ||
| createReq.Region = region | ||
| } | ||
|
|
||
| snapshot, err := a.mongodbAPI.CreateSnapshot(createReq, scw.WithContext(ctx)) | ||
| if err != nil { | ||
| resp.Diagnostics.AddError( | ||
| "Error executing MongoDB CreateSnapshot action", | ||
| fmt.Sprintf("Failed to create snapshot for instance %s: %s", instanceID, err), | ||
| ) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| if data.Wait.ValueBool() { | ||
| waitRegion := snapshot.Region | ||
| if waitRegion == "" && region != "" { | ||
| waitRegion = region | ||
| } | ||
|
|
||
| if waitRegion == "" { | ||
| resp.Diagnostics.AddError( | ||
| "Missing region for wait operation", | ||
| "Could not determine region to wait for MongoDB snapshot completion.", | ||
| ) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| _, err = waitForSnapshot(ctx, a.mongodbAPI, waitRegion, instanceID, snapshot.ID, defaultMongodbSnapshotTimeout) | ||
| if err != nil { | ||
| resp.Diagnostics.AddError( | ||
| "Error waiting for MongoDB snapshot completion", | ||
| fmt.Sprintf("Snapshot %s for instance %s did not reach a terminal state: %s", snapshot.ID, instanceID, err), | ||
| ) | ||
|
|
||
| return | ||
| } | ||
| } | ||
| } | ||
51 changes: 51 additions & 0 deletions
51
internal/services/mongodb/action_instance_snapshot_action_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package mongodb_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" | ||
| ) | ||
|
|
||
| func TestAccActionMongoDBInstanceSnapshot_Basic(t *testing.T) { | ||
| if acctest.IsRunningOpenTofu() { | ||
| t.Skip("Skipping TestAccActionMongoDBInstanceSnapshot_Basic because actions are not yet supported on OpenTofu") | ||
| } | ||
|
|
||
| tt := acctest.NewTestTools(t) | ||
| defer tt.Cleanup() | ||
|
|
||
| resource.ParallelTest(t, resource.TestCase{ | ||
| ProtoV6ProviderFactories: tt.ProviderFactories, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: ` | ||
| resource "scaleway_mongodb_instance" "main" { | ||
| name = "test-mongodb-action-snapshot" | ||
| version = "7.0.12" | ||
| node_type = "MGDB-PLAY2-NANO" | ||
| node_number = 1 | ||
| user_name = "my_initial_user" | ||
| password = "thiZ_is_v&ry_s3cret" | ||
|
|
||
| lifecycle { | ||
| action_trigger { | ||
| events = [after_create] | ||
| actions = [action.scaleway_mongodb_instance_snapshot_action.main] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| action "scaleway_mongodb_instance_snapshot_action" "main" { | ||
| config { | ||
| instance_id = scaleway_mongodb_instance.main.id | ||
| name = "tf-acc-mongodb-instance-snapshot-action" | ||
| expires_at = "2026-11-01T00:00:00Z" | ||
| wait = true | ||
| } | ||
| } | ||
| `, | ||
| }, | ||
| }, | ||
| }) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why should it be required in case I don't want it to expire ?