Skip to content

Commit b8dcb6e

Browse files
feat: support new data expiration rule attribute in online archive (#1528)
* feat: support new data expiration rule attribute in online archive * addressing docs review
1 parent 5f51eed commit b8dcb6e

7 files changed

+141
-44
lines changed

examples/online-archives/main.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ resource "mongodbatlas_online_archive" "users_archive" {
1111
expire_after_days = 2
1212
}
1313

14+
data_expiration_rule {
15+
expire_after_days = 90
16+
}
17+
1418
partition_fields {
1519
field_name = "created"
1620
order = 0

mongodbatlas/data_source_mongodbatlas_online_archive.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ func schemaOnlineArchive() map[string]*schema.Schema {
118118
},
119119
},
120120
},
121+
"data_expiration_rule": {
122+
Type: schema.TypeList,
123+
Computed: true,
124+
Elem: &schema.Resource{
125+
Schema: map[string]*schema.Schema{
126+
"expire_after_days": {
127+
Type: schema.TypeInt,
128+
Computed: true,
129+
},
130+
},
131+
},
132+
},
121133
"schedule": {
122134
Type: schema.TypeList,
123135
Computed: true,

mongodbatlas/resource_mongodbatlas_online_archive.go

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ func getMongoDBAtlasOnlineArchiveSchema() map[string]*schema.Schema {
9898
},
9999
},
100100
},
101+
"data_expiration_rule": {
102+
Type: schema.TypeList,
103+
MinItems: 1,
104+
MaxItems: 1,
105+
Optional: true,
106+
Elem: &schema.Resource{
107+
Schema: map[string]*schema.Schema{
108+
"expire_after_days": {
109+
Type: schema.TypeInt,
110+
Required: true,
111+
},
112+
},
113+
},
114+
},
101115
"schedule": {
102116
Type: schema.TypeList,
103117
Optional: true,
@@ -352,6 +366,7 @@ func mapToArchivePayload(d *schema.ResourceData) admin.BackupOnlineArchiveCreate
352366
}
353367

354368
requestInput.Criteria = mapCriteria(d)
369+
requestInput.DataExpirationRule = mapDataExpirationRule(d)
355370
requestInput.Schedule = mapSchedule(d)
356371

357372
if partitions, ok := d.GetOk("partition_fields"); ok {
@@ -391,31 +406,42 @@ func resourceMongoDBAtlasOnlineArchiveUpdate(ctx context.Context, d *schema.Reso
391406
projectID := ids["project_id"]
392407
clusterName := ids["cluster_name"]
393408

394-
// if the criteria or the paused is enable then perform an update
395-
paused := d.HasChange("paused")
396-
criteria := d.HasChange("criteria")
397-
schedule := d.HasChange("schedule")
409+
// if the criteria or the pausedHasChange is enable then perform an update
410+
pausedHasChange := d.HasChange("paused")
411+
criteriaHasChange := d.HasChange("criteria")
412+
dataExpirationRuleHasChange := d.HasChange("data_expiration_rule")
413+
scheduleHasChange := d.HasChange("schedule")
398414

399415
collectionTypeHasChange := d.HasChange("collection_type")
400416

401417
// nothing to do, let's go
402-
if !paused && !criteria && !collectionTypeHasChange && !schedule {
418+
if !pausedHasChange && !criteriaHasChange && !collectionTypeHasChange && !scheduleHasChange && !dataExpirationRuleHasChange {
403419
return nil
404420
}
405421

406422
request := admin.BackupOnlineArchive{}
407423

408424
// reading current value
409-
if paused {
425+
if pausedHasChange {
410426
request.Paused = pointy.Bool(d.Get("paused").(bool))
411427
}
412428

413-
if criteria {
429+
if criteriaHasChange {
414430
newCriteria := mapCriteria(d)
415431
request.Criteria = &newCriteria
416432
}
417433

418-
if schedule {
434+
if dataExpirationRuleHasChange {
435+
newExpirationRule := mapDataExpirationRule(d)
436+
if newExpirationRule == nil {
437+
// expiration rule has been removed from tf config, empty dataExpirationRule object needs to be sent in patch request
438+
request.DataExpirationRule = &admin.DataExpirationRule{}
439+
} else {
440+
request.DataExpirationRule = newExpirationRule
441+
}
442+
}
443+
444+
if scheduleHasChange {
419445
request.Schedule = mapSchedule(d)
420446
}
421447

@@ -491,6 +517,14 @@ func fromOnlineArchiveToMap(in *admin.BackupOnlineArchive) map[string]interface{
491517
schemaVals["schedule"] = []interface{}{schedule}
492518
}
493519

520+
var dataExpirationRule map[string]interface{}
521+
if in.DataExpirationRule != nil && in.DataExpirationRule.ExpireAfterDays != nil {
522+
dataExpirationRule = map[string]interface{}{
523+
"expire_after_days": in.DataExpirationRule.ExpireAfterDays,
524+
}
525+
schemaVals["data_expiration_rule"] = []interface{}{dataExpirationRule}
526+
}
527+
494528
// partitions fields
495529
if len(in.PartitionFields) == 0 {
496530
return schemaVals
@@ -511,6 +545,18 @@ func fromOnlineArchiveToMap(in *admin.BackupOnlineArchive) map[string]interface{
511545
return schemaVals
512546
}
513547

548+
func mapDataExpirationRule(d *schema.ResourceData) *admin.DataExpirationRule {
549+
if dataExpireRules, ok := d.GetOk("data_expiration_rule"); ok && len(dataExpireRules.([]interface{})) > 0 {
550+
dataExpireRule := dataExpireRules.([]interface{})[0].(map[string]interface{})
551+
result := admin.DataExpirationRule{}
552+
if expireAfterDays, ok := dataExpireRule["expire_after_days"]; ok {
553+
result.ExpireAfterDays = pointy.Int(expireAfterDays.(int))
554+
}
555+
return &result
556+
}
557+
return nil
558+
}
559+
514560
func mapCriteria(d *schema.ResourceData) admin.Criteria {
515561
criteriaList := d.Get("criteria").([]interface{})
516562

mongodbatlas/resource_mongodbatlas_online_archive_test.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ import (
1616

1717
func TestAccBackupRSOnlineArchive(t *testing.T) {
1818
var (
19-
cluster matlas.Cluster
20-
resourceName = "mongodbatlas_cluster.online_archive_test"
21-
onlineArchiveResourceName = "mongodbatlas_online_archive.users_archive"
22-
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
23-
projectName = acctest.RandomWithPrefix("test-acc")
24-
name = fmt.Sprintf("test-acc-%s", acctest.RandString(10))
19+
cluster matlas.Cluster
20+
resourceName = "mongodbatlas_cluster.online_archive_test"
21+
onlineArchiveResourceName = "mongodbatlas_online_archive.users_archive"
22+
onlineArchiveDataSourceName = "data.mongodbatlas_online_archive.read_archive"
23+
onlineArchivesDataSourceName = "data.mongodbatlas_online_archives.all"
24+
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
25+
projectName = acctest.RandomWithPrefix("test-acc")
26+
name = fmt.Sprintf("test-acc-%s", acctest.RandString(10))
2527
)
2628

2729
resource.ParallelTest(t, resource.TestCase{
@@ -38,7 +40,7 @@ func TestAccBackupRSOnlineArchive(t *testing.T) {
3840
),
3941
},
4042
{
41-
Config: testAccBackupRSOnlineArchiveConfigWithDailySchedule(orgID, projectName, name, 1),
43+
Config: testAccBackupRSOnlineArchiveConfigWithDailySchedule(orgID, projectName, name, 1, 7),
4244
Check: resource.ComposeTestCheckFunc(
4345
resource.TestCheckResourceAttrSet(onlineArchiveResourceName, "state"),
4446
resource.TestCheckResourceAttrSet(onlineArchiveResourceName, "archive_id"),
@@ -48,10 +50,13 @@ func TestAccBackupRSOnlineArchive(t *testing.T) {
4850
resource.TestCheckResourceAttrSet(onlineArchiveResourceName, "schedule.0.end_minute"),
4951
resource.TestCheckResourceAttrSet(onlineArchiveResourceName, "schedule.0.start_hour"),
5052
resource.TestCheckResourceAttrSet(onlineArchiveResourceName, "schedule.0.start_minute"),
53+
resource.TestCheckResourceAttr(onlineArchiveResourceName, "data_expiration_rule.0.expire_after_days", "7"),
54+
resource.TestCheckResourceAttr(onlineArchiveDataSourceName, "data_expiration_rule.0.expire_after_days", "7"),
55+
resource.TestCheckResourceAttr(onlineArchivesDataSourceName, "results.0.data_expiration_rule.0.expire_after_days", "7"),
5156
),
5257
},
5358
{
54-
Config: testAccBackupRSOnlineArchiveConfigWithDailySchedule(orgID, projectName, name, 2),
59+
Config: testAccBackupRSOnlineArchiveConfigWithDailySchedule(orgID, projectName, name, 2, 8),
5560
Check: resource.ComposeTestCheckFunc(
5661
resource.TestCheckResourceAttrSet(onlineArchiveResourceName, "state"),
5762
resource.TestCheckResourceAttrSet(onlineArchiveResourceName, "archive_id"),
@@ -61,6 +66,9 @@ func TestAccBackupRSOnlineArchive(t *testing.T) {
6166
resource.TestCheckResourceAttrSet(onlineArchiveResourceName, "schedule.0.end_minute"),
6267
resource.TestCheckResourceAttrSet(onlineArchiveResourceName, "schedule.0.start_hour"),
6368
resource.TestCheckResourceAttrSet(onlineArchiveResourceName, "schedule.0.start_minute"),
69+
resource.TestCheckResourceAttr(onlineArchiveResourceName, "data_expiration_rule.0.expire_after_days", "8"),
70+
resource.TestCheckResourceAttr(onlineArchiveDataSourceName, "data_expiration_rule.0.expire_after_days", "8"),
71+
resource.TestCheckResourceAttr(onlineArchivesDataSourceName, "results.0.data_expiration_rule.0.expire_after_days", "8"),
6472
),
6573
},
6674
{
@@ -140,7 +148,7 @@ func TestAccBackupRSOnlineArchiveBasic(t *testing.T) {
140148
),
141149
},
142150
{
143-
Config: testAccBackupRSOnlineArchiveConfigWithDailySchedule(orgID, projectName, name, 1),
151+
Config: testAccBackupRSOnlineArchiveConfigWithDailySchedule(orgID, projectName, name, 1, 1),
144152
Check: resource.ComposeTestCheckFunc(
145153
resource.TestCheckResourceAttrSet(onlineArchiveResourceName, "state"),
146154
resource.TestCheckResourceAttrSet(onlineArchiveResourceName, "archive_id"),
@@ -214,9 +222,9 @@ func populateWithSampleData(resourceName string, cluster *matlas.Cluster) resour
214222
}
215223
}
216224

217-
func testAccBackupRSOnlineArchiveConfigWithDailySchedule(orgID, projectName, clusterName string, startHour int) string {
225+
func testAccBackupRSOnlineArchiveConfigWithDailySchedule(orgID, projectName, clusterName string, startHour, deleteExpirationDays int) string {
218226
return fmt.Sprintf(`
219-
%s
227+
%[1]s
220228
resource "mongodbatlas_online_archive" "users_archive" {
221229
project_id = mongodbatlas_cluster.online_archive_test.project_id
222230
cluster_name = mongodbatlas_cluster.online_archive_test.name
@@ -231,11 +239,15 @@ func testAccBackupRSOnlineArchiveConfigWithDailySchedule(orgID, projectName, clu
231239
expire_after_days = 2
232240
}
233241
242+
data_expiration_rule {
243+
expire_after_days = %[3]d
244+
}
245+
234246
schedule {
235247
type = "DAILY"
236248
end_hour = 1
237249
end_minute = 1
238-
start_hour = %d
250+
start_hour = %[2]d
239251
start_minute = 1
240252
}
241253
@@ -267,7 +279,7 @@ func testAccBackupRSOnlineArchiveConfigWithDailySchedule(orgID, projectName, clu
267279
project_id = mongodbatlas_online_archive.users_archive.project_id
268280
cluster_name = mongodbatlas_online_archive.users_archive.cluster_name
269281
}
270-
`, testAccBackupRSOnlineArchiveConfigFirstStep(orgID, projectName, clusterName), startHour)
282+
`, testAccBackupRSOnlineArchiveConfigFirstStep(orgID, projectName, clusterName), startHour, deleteExpirationDays)
271283
}
272284

273285
func testAccBackupRSOnlineArchiveConfigWithoutSchedule(orgID, projectName, clusterName string) string {

website/docs/d/online_archive.html.markdown

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ data "mongodbatlas_online_archive" "test" {
3333
## Attributes reference
3434
* `db_name` - Name of the database that contains the collection.
3535
* `coll_name` - Name of the collection.
36-
* `collection_type` - Classification of MongoDB database collection that you want to return, "TIMESERIES" or "STANDARD". Default is "STANDARD".
36+
* `collection_type` - Type of MongoDB collection that you want to return. This value can be "TIMESERIES" or "STANDARD". Default is "STANDARD".
37+
* `criteria` - Criteria to use for archiving data. See [criteria](#criteria).
38+
* `data_expiration_rule` - Rule for specifying when data should be deleted from the archive. See [data expiration rule](#data-expiration-rule).
39+
* `schedule` - Regular frequency and duration when archiving process occurs. See [schedule](#schedule).
40+
* `partition_fields` - Fields to use to partition data. You can specify up to two frequently queried fields to use for partitioning data. Queries that don’t contain the specified fields require a full collection scan of all archived documents, which takes longer and increases your costs. To learn more about how partition improves query performance, see [Data Structure in S3](https://docs.mongodb.com/datalake/admin/optimize-query-performance/#data-structure-in-s3). The value of a partition field can be up to a maximum of 700 characters. Documents with values exceeding 700 characters are not archived. See [partition fields](#partition).
41+
* `paused` - State of the online archive. This is required for pausing an active online archive or resuming a paused online archive. If the collection has another active online archive, the resume request fails.
3742
* `state` - Status of the online archive. Valid values are: Pending, Archiving, Idle, Pausing, Paused, Orphaned and Deleted
3843

3944
### Criteria
@@ -43,6 +48,10 @@ data "mongodbatlas_online_archive" "test" {
4348
* `expire_after_days` - Number of days after the value in the criteria.dateField when MongoDB Cloud archives data in the specified cluster. Set this parameter when `type` is `DATE`.
4449
* `query` - JSON query to use to select documents for archiving. Atlas uses the specified query with the db.collection.find(query) command. The empty document {} to return all documents is not supported. Set this parameter when `type` is `CUSTOM`.
4550

51+
### Data Expiration Rule
52+
* `expire_after_days` - Number of days used in the date criteria for nominating documents for deletion. Value must be between 7 and 9215.
53+
54+
4655
### Schedule
4756

4857
* `type` - Type of schedule. Valid values: `DEFAULT`, `DAILY`, `MONTHLY`, `WEEKLY`.

website/docs/d/online_archives.html.markdown

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,35 @@ In addition to all arguments above, the following attributes are exported:
3636

3737
# Online Archive
3838
## Attributes reference
39-
* `db_name` - Name of the database that contains the collection.
40-
* `coll_name` - Name of the collection.
41-
* `collection_type` - Classification of MongoDB database collection that you want to return, "TIMESERIES" or "STANDARD". Default is "STANDARD".
42-
* `state` - Status of the online archive. Valid values are: Pending, Archiving, Idle, Pausing, Paused, Orphaned and Deleted
39+
* `db_name` - Name of the database that contains the collection.
40+
* `coll_name` - Name of the collection.
41+
* `collection_type` - Type of MongoDB collection that you want to return. This value can be "TIMESERIES" or "STANDARD". Default is "STANDARD".
42+
* `criteria` - Criteria to use for archiving data. See [criteria](#criteria).
43+
* `data_expiration_rule` - Rule for specifying when data should be deleted from the archive. See [data expiration rule](#data-expiration-rule).
44+
* `schedule` - Regular frequency and duration when archiving process occurs. See [schedule](#schedule).
45+
* `partition_fields` - Fields to use to partition data. You can specify up to two frequently queried fields to use for partitioning data. Queries that don’t contain the specified fields require a full collection scan of all archived documents, which takes longer and increases your costs. To learn more about how partition improves query performance, see [Data Structure in S3](https://docs.mongodb.com/datalake/admin/optimize-query-performance/#data-structure-in-s3). The value of a partition field can be up to a maximum of 700 characters. Documents with values exceeding 700 characters are not archived. See [partition fields](#partition).
46+
* `paused` - State of the online archive. This is required for pausing an active online archive or resuming a paused online archive. If the collection has another active online archive, the resume request fails.
47+
* `state` - Status of the online archive. Valid values are: Pending, Archiving, Idle, Pausing, Paused, Orphaned and Deleted
4348

4449
### Criteria
45-
* `type` - Type of criteria (DATE, CUSTOM)
46-
* `date_field` - Indexed database parameter that stores the date that determines when data moves to the online archive. MongoDB Cloud archives the data when the current date exceeds the date in this database parameter plus the number of days specified through the expireAfterDays parameter. Set this parameter when `type` is `DATE`.
47-
* `date_format` - Syntax used to write the date after which data moves to the online archive. Date can be expressed as ISO 8601 or Epoch timestamps. The Epoch timestamp can be expressed as nanoseconds, milliseconds, or seconds. Set this parameter when `type` is `DATE`. You must set `type` to `DATE` if `collectionType` is `TIMESERIES`. Valid values: ISODATE (default), EPOCH_SECONDS, EPOCH_MILLIS, EPOCH_NANOSECONDS.
50+
* `type` - Type of criteria (DATE, CUSTOM)
51+
* `date_field` - Indexed database parameter that stores the date that determines when data moves to the online archive. MongoDB Cloud archives the data when the current date exceeds the date in this database parameter plus the number of days specified through the expireAfterDays parameter. Set this parameter when `type` is `DATE`.
52+
* `date_format` - Syntax used to write the date after which data moves to the online archive. Date can be expressed as ISO 8601 or Epoch timestamps. The Epoch timestamp can be expressed as nanoseconds, milliseconds, or seconds. Set this parameter when `type` is `DATE`. You must set `type` to `DATE` if `collectionType` is `TIMESERIES`. Valid values: ISODATE (default), EPOCH_SECONDS, EPOCH_MILLIS, EPOCH_NANOSECONDS.
4853
* `expire_after_days` - Number of days after the value in the criteria.dateField when MongoDB Cloud archives data in the specified cluster. Set this parameter when `type` is `DATE`.
4954
* `query` - JSON query to use to select documents for archiving. Atlas uses the specified query with the db.collection.find(query) command. The empty document {} to return all documents is not supported. Set this parameter when `type` is `CUSTOM`.
5055

56+
### Data Expiration Rule
57+
* `expire_after_days` - Number of days used in the date criteria for nominating documents for deletion. Value must be between 7 and 9215.
58+
5159
### Schedule
5260

53-
* `type` - Type of schedule (`DAILY`, `MONTHLY`, `WEEKLY`).
54-
* `start_hour` - Hour of the day when the when the scheduled window to run one online archive starts.
55-
* `end_hour` - Hour of the day when the scheduled window to run one online archive ends.
56-
* `start_minute` - Minute of the hour when the scheduled window to run one online archive starts.
57-
* `end_minute` - Minute of the hour when the scheduled window to run one online archive ends.
58-
* `day_of_month` - Day of the month when the scheduled archive starts.
59-
* `day_of_week` - Day of the week when the scheduled archive starts. The week starts with Monday (1) and ends with Sunday (7).
61+
* `type` - Type of schedule (`DAILY`, `MONTHLY`, `WEEKLY`).
62+
* `start_hour` - Hour of the day when the when the scheduled window to run one online archive starts.
63+
* `end_hour` - Hour of the day when the scheduled window to run one online archive ends.
64+
* `start_minute` - Minute of the hour when the scheduled window to run one online archive starts.
65+
* `end_minute` - Minute of the hour when the scheduled window to run one online archive ends.
66+
* `day_of_month` - Day of the month when the scheduled archive starts.
67+
* `day_of_week` - Day of the week when the scheduled archive starts. The week starts with Monday (1) and ends with Sunday (7).
6068

6169
### Partition
6270
* `field_name` - Human-readable label that identifies the parameter that MongoDB Cloud uses to partition data. To specify a nested parameter, use the dot notation.

0 commit comments

Comments
 (0)