Skip to content

Commit fc393c3

Browse files
committed
fix: apply code review feedback for MongoDB snapshot action
1 parent b3a3ab2 commit fc393c3

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

internal/services/mongodb/action_instance_snapshot_action.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var (
2525
// InstanceSnapshotAction creates a snapshot for a MongoDB instance.
2626
type InstanceSnapshotAction struct {
2727
mongodbAPI *mongodb.API
28+
meta *meta.Meta
2829
}
2930

3031
func (a *InstanceSnapshotAction) Configure(_ context.Context, req action.ConfigureRequest, resp *action.ConfigureResponse) {
@@ -42,6 +43,7 @@ func (a *InstanceSnapshotAction) Configure(_ context.Context, req action.Configu
4243
return
4344
}
4445

46+
a.meta = m
4547
a.mongodbAPI = newAPI(m)
4648
}
4749

@@ -111,7 +113,7 @@ func (a *InstanceSnapshotAction) Invoke(ctx context.Context, req action.InvokeRe
111113
return
112114
}
113115

114-
if data.InstanceID.IsNull() || data.InstanceID.ValueString() == "" {
116+
if data.InstanceID.IsNull() || data.InstanceID.IsUnknown() || data.InstanceID.ValueString() == "" {
115117
resp.Diagnostics.AddError(
116118
"Missing instance_id",
117119
"The instance_id attribute is required to create a MongoDB snapshot.",
@@ -122,29 +124,48 @@ func (a *InstanceSnapshotAction) Invoke(ctx context.Context, req action.InvokeRe
122124

123125
instanceID := locality.ExpandID(data.InstanceID.ValueString())
124126

125-
var (
126-
region scw.Region
127-
err error
128-
)
127+
var region scw.Region
129128

130-
if !data.Region.IsNull() && data.Region.ValueString() != "" {
129+
if !data.Region.IsNull() && !data.Region.IsUnknown() && data.Region.ValueString() != "" {
131130
region = scw.Region(data.Region.ValueString())
132131
} else {
133132
// Try to derive region from the instance_id if it is a regional ID.
134133
if derivedRegion, id, parseErr := regional.ParseID(data.InstanceID.ValueString()); parseErr == nil {
135134
region = derivedRegion
136135
instanceID = id
136+
} else if a.meta != nil {
137+
// Fallback to provider default region
138+
defaultRegion, exists := a.meta.ScwClient().GetDefaultRegion()
139+
if !exists {
140+
resp.Diagnostics.AddError(
141+
"Unable to determine region",
142+
"Failed to get default region from provider configuration. Please set the region attribute, use a regional instance_id, or configure a default region in the provider.",
143+
)
144+
145+
return
146+
}
147+
148+
region = defaultRegion
137149
}
138150
}
139151

152+
if region == "" {
153+
resp.Diagnostics.AddError(
154+
"Missing region",
155+
"Could not determine region for MongoDB snapshot. Please set the region attribute, use a regional instance_id, or configure a default region in the provider.",
156+
)
157+
158+
return
159+
}
160+
140161
snapshotName := data.Name.ValueString()
141162
if snapshotName == "" {
142163
snapshotName = "tf-mongodb-snapshot"
143164
}
144165

145166
var expirationTime *time.Time
146167

147-
if !data.ExpiresAt.IsNull() && data.ExpiresAt.ValueString() != "" {
168+
if !data.ExpiresAt.IsNull() && !data.ExpiresAt.IsUnknown() && data.ExpiresAt.ValueString() != "" {
148169
expirationRaw := data.ExpiresAt.ValueString()
149170

150171
parsedTime, err := time.Parse(time.RFC3339, expirationRaw)

internal/services/mongodb/action_instance_snapshot_action_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestAccActionMongoDBInstanceSnapshot_Basic(t *testing.T) {
2828
Config: `
2929
resource "scaleway_mongodb_instance" "main" {
3030
name = "test-mongodb-action-snapshot"
31-
version = "7.0.12"
31+
version = "7.0"
3232
node_type = "MGDB-PLAY2-NANO"
3333
node_number = 1
3434
user_name = "my_initial_user"
@@ -56,7 +56,7 @@ func TestAccActionMongoDBInstanceSnapshot_Basic(t *testing.T) {
5656
Config: `
5757
resource "scaleway_mongodb_instance" "main" {
5858
name = "test-mongodb-action-snapshot"
59-
version = "7.0.12"
59+
version = "7.0"
6060
node_type = "MGDB-PLAY2-NANO"
6161
node_number = 1
6262
user_name = "my_initial_user"

0 commit comments

Comments
 (0)