Skip to content

Commit

Permalink
INTMDB-230: added property to maintenance window rs ds (#552)
Browse files Browse the repository at this point in the history
* added  property to maintenance window rs ds

* changed go.mod

* fix go.sum

* fixes lint

* fix go.sum

* fix maintenance window documentation

* fix maintenance window resource documentation
  • Loading branch information
abner-dou committed Sep 29, 2021
1 parent 4689a7f commit c008f6f
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 10 deletions.
19 changes: 13 additions & 6 deletions mongodbatlas/data_source_mongodbatlas_maintenance_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package mongodbatlas

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -33,6 +32,10 @@ func dataSourceMongoDBAtlasMaintenanceWindow() *schema.Resource {
Type: schema.TypeInt,
Computed: true,
},
"auto_defer_once_enabled": {
Type: schema.TypeBool,
Computed: true,
},
},
}
}
Expand All @@ -44,23 +47,27 @@ func dataSourceMongoDBAtlasMaintenanceWindowRead(ctx context.Context, d *schema.

maintenance, _, err := conn.MaintenanceWindows.Get(ctx, projectID)
if err != nil {
return diag.FromErr(fmt.Errorf(errorMaintenanceRead, projectID, err))
return diag.Errorf(errorMaintenanceRead, projectID, err)
}

if err := d.Set("day_of_week", maintenance.DayOfWeek); err != nil {
return diag.FromErr(fmt.Errorf(errorMaintenanceRead, projectID, err))
return diag.Errorf(errorMaintenanceRead, projectID, err)
}

if err := d.Set("hour_of_day", maintenance.HourOfDay); err != nil {
return diag.FromErr(fmt.Errorf(errorMaintenanceRead, projectID, err))
return diag.Errorf(errorMaintenanceRead, projectID, err)
}

if err := d.Set("number_of_deferrals", maintenance.NumberOfDeferrals); err != nil {
return diag.FromErr(fmt.Errorf(errorMaintenanceRead, projectID, err))
return diag.Errorf(errorMaintenanceRead, projectID, err)
}

if err := d.Set("start_asap", cast.ToBool(maintenance.StartASAP)); err != nil {
return diag.FromErr(fmt.Errorf(errorMaintenanceRead, projectID, err))
return diag.Errorf(errorMaintenanceRead, projectID, err)
}

if err := d.Set("auto_defer_once_enabled", cast.ToBool(maintenance.AutoDeferOnceEnabled)); err != nil {
return diag.Errorf(errorMaintenanceRead, projectID, err)
}

d.SetId(projectID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestAccDataSourceMongoDBAtlasMaintenanceWindow_basic(t *testing.T) {
resource.TestCheckResourceAttrSet("data.mongodbatlas_maintenance_window.test", "project_id"),
resource.TestCheckResourceAttrSet("data.mongodbatlas_maintenance_window.test", "day_of_week"),
resource.TestCheckResourceAttrSet("data.mongodbatlas_maintenance_window.test", "hour_of_day"),
resource.TestCheckResourceAttrSet("data.mongodbatlas_maintenance_window.test", "auto_defer_once_enabled"),
),
},
},
Expand All @@ -39,6 +40,7 @@ func testAccMongoDBAtlasDataSourceMaintenanceWindowConfig(projectID string, dayO
project_id = "%s"
day_of_week = %d
hour_of_day = %d
auto_defer_once_enabled = true
}
data "mongodbatlas_maintenance_window" "test" {
Expand Down
21 changes: 20 additions & 1 deletion mongodbatlas/resource_mongodbatlas_maintenance_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ func resourceMongoDBAtlasMaintenanceWindow() *schema.Resource {
Optional: true,
Computed: true,
},
"auto_defer_once_enabled": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -117,6 +122,10 @@ func resourceMongoDBAtlasMaintenanceWindowCreate(ctx context.Context, d *schema.
maintenanceWindowReq.NumberOfDeferrals = cast.ToInt(numberOfDeferrals)
}

if autoDeferOnceEnabled, ok := d.GetOk("auto_defer_once_enabled"); ok {
maintenanceWindowReq.AutoDeferOnceEnabled = pointy.Bool(autoDeferOnceEnabled.(bool))
}

_, err := conn.MaintenanceWindows.Update(ctx, projectID, maintenanceWindowReq)
if err != nil {
return diag.FromErr(fmt.Errorf(errorMaintenanceCreate, projectID, err))
Expand Down Expand Up @@ -153,12 +162,18 @@ func resourceMongoDBAtlasMaintenanceWindowRead(ctx context.Context, d *schema.Re
return diag.FromErr(fmt.Errorf(errorMaintenanceRead, d.Id(), err))
}
// start_asap is just display the state of the maintenance,
// and it doesn't able to set it because breacks the Terraform flow
// and it doesn't able to set it because breaks the Terraform flow
// it can be used via API
if err := d.Set("start_asap", maintenanceWindow.StartASAP); err != nil {
return diag.FromErr(fmt.Errorf(errorMaintenanceRead, d.Id(), err))
}

if maintenanceWindow.AutoDeferOnceEnabled != nil {
if err := d.Set("auto_defer_once_enabled", *maintenanceWindow.AutoDeferOnceEnabled); err != nil {
return diag.Errorf(errorMaintenanceRead, d.Id(), err)
}
}

if err := d.Set("project_id", d.Id()); err != nil {
return diag.FromErr(fmt.Errorf(errorMaintenanceRead, d.Id(), err))
}
Expand Down Expand Up @@ -198,6 +213,10 @@ func resourceMongoDBAtlasMaintenanceWindowUpdate(ctx context.Context, d *schema.
maintenanceWindowReq.NumberOfDeferrals = cast.ToInt(d.Get("number_of_deferrals"))
}

if d.HasChange("auto_defer_once_enabled") {
maintenanceWindowReq.AutoDeferOnceEnabled = pointy.Bool(d.Get("number_of_deferrals").(bool))
}

_, err := conn.MaintenanceWindows.Update(ctx, d.Id(), maintenanceWindowReq)
if err != nil {
return diag.FromErr(fmt.Errorf(errorMaintenanceUpdate, d.Id(), err))
Expand Down
44 changes: 44 additions & 0 deletions mongodbatlas/resource_mongodbatlas_maintenance_window_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,50 @@ func TestAccResourceMongoDBAtlasMaintenanceWindow_importBasic(t *testing.T) {
})
}

func TestAccResourceMongoDBAtlasMaintenanceWindow_autoDeferActivated(t *testing.T) {
var (
maintenance matlas.MaintenanceWindow
resourceName = "mongodbatlas_maintenance_window.test"
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID")
dayOfWeek = 7
hourOfDay = 3
)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckMongoDBAtlasMaintenanceWindowDestroy,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasMaintenanceWindowConfigAutoDeferEnabled(projectID, dayOfWeek, hourOfDay),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasMaintenanceWindowExists(resourceName, &maintenance),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttrSet(resourceName, "day_of_week"),
resource.TestCheckResourceAttrSet(resourceName, "hour_of_day"),

resource.TestCheckResourceAttr(resourceName, "day_of_week", cast.ToString(dayOfWeek)),
resource.TestCheckResourceAttr(resourceName, "hour_of_day", cast.ToString(hourOfDay)),
resource.TestCheckResourceAttr(resourceName, "number_of_deferrals", "0"),
resource.TestCheckResourceAttr(resourceName, "auto_defer_once_enabled", "true"),

testAccCheckMongoDBAtlasMaintenanceWindowAttributes("day_of_week", dayOfWeek, &maintenance.DayOfWeek),
),
},
},
})
}

func testAccMongoDBAtlasMaintenanceWindowConfigAutoDeferEnabled(projectID string, dayOfWeek, hourOfDay int) string {
return fmt.Sprintf(`
resource "mongodbatlas_maintenance_window" "test" {
project_id = "%s"
day_of_week = %d
hour_of_day = %d
auto_defer_once_enabled = true
}`, projectID, dayOfWeek, hourOfDay)
}

func testAccCheckMongoDBAtlasMaintenanceWindowExists(resourceName string, maintenance *matlas.MaintenanceWindow) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*MongoDBClient).Atlas
Expand Down
3 changes: 2 additions & 1 deletion website/docs/d/maintenance_window.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ resource "mongodbatlas_maintenance_window" "test" {
project_id = "<your-project-id>"
day_of_week = 3
hour_of_day = 4
auto_defer_once_enabled = true
}
data "mongodbatlas_maintenance_window" "test" {
Expand Down Expand Up @@ -50,5 +51,5 @@ In addition to all arguments above, the following attributes are exported:
* `hour_of_day` - Hour of the day when you would like the maintenance window to start. This parameter uses the 24-hour clock, where midnight is 0, noon is 12 (Time zone is UTC).
* `start_asap` - Flag indicating whether project maintenance has been directed to start immediately. If you request that maintenance begin immediately, this field returns true from the time the request was made until the time the maintenance event completes.
* `number_of_deferrals` - Number of times the current maintenance event for this project has been deferred, you can set a maximum of 2 deferrals.

* `auto_defer_once_enabled` - Flag that indicates whether you want to defer all maintenance windows one week they would be triggered.
For more information see: [MongoDB Atlas API Reference.](https://docs.atlas.mongodb.com/reference/api/maintenance-windows/)
5 changes: 3 additions & 2 deletions website/docs/r/maintenance_window.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ Once maintenance is scheduled for your cluster, you cannot change your maintenan
* `hour_of_day` - Hour of the day when you would like the maintenance window to start. This parameter uses the 24-hour clock, where midnight is 0, noon is 12 (Time zone is UTC).
* `start_asap` - Flag indicating whether project maintenance has been directed to start immediately. If you request that maintenance begin immediately, this field returns true from the time the request was made until the time the maintenance event completes.
* `number_of_deferrals` - Number of times the current maintenance event for this project has been deferred, you can set a maximum of 2 deferrals.
* `defer` - Defer maintenance for the given project for one week.
* `auto_defer` - Automatically defer any maintenance for the given project for one week.
* `defer` - Defer the next scheduled maintenance for the given project for one week.
* `auto_defer` - Defer any scheduled maintenance for the given project for one week.
* `auto_defer_once_enabled` - Flag that indicates whether you want to defer all maintenance windows one week they would be triggered.

-> **NOTE:** The `start_asap` attribute can't be used because of breaks the Terraform flow, but you can enable via API.

Expand Down

0 comments on commit c008f6f

Please sign in to comment.