-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from terraform-providers/maintenance_window
Maintenance window rs
- Loading branch information
Showing
19 changed files
with
1,210 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
182 changes: 182 additions & 0 deletions
182
mongodbatlas/resource_mongodbatlas_maintenance_window.go
This file contains 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,182 @@ | ||
package mongodbatlas | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/mwielbut/pointy" | ||
"github.com/spf13/cast" | ||
|
||
matlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas" | ||
) | ||
|
||
const ( | ||
errorMaintenanceCreate = "error creating the MongoDB Atlas Maintenance Window (%s): %s" | ||
errorMaintenanceUpdate = "error updating the MongoDB Atlas Maintenance Window (%s): %s" | ||
errorMaintenanceRead = "error reading the MongoDB Atlas Maintenance Window (%s): %s" | ||
errorMaintenanceDelete = "error deleting the MongoDB Atlas Maintenance Window (%s): %s" | ||
errorMaintenanceDefer = "error deferring the MongoDB Atlas Maintenance Window (%s): %s" | ||
) | ||
|
||
func resourceMongoDBAtlasMaintenanceWindow() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceMongoDBAtlasMaintenanceWindowCreate, | ||
Read: resourceMongoDBAtlasMaintenanceWindowRead, | ||
Update: resourceMongoDBAtlasMaintenanceWindowUpdate, | ||
Delete: resourceMongoDBAtlasMaintenanceWindowDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"project_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"day_of_week": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
Computed: true, | ||
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) { | ||
v := val.(int) | ||
if v < 1 || v > 7 { | ||
errs = append(errs, fmt.Errorf("%q value should be between 1 and 7, got: %d", key, v)) | ||
} | ||
return | ||
}, | ||
}, | ||
"hour_of_day": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
Computed: true, | ||
ConflictsWith: []string{"start_asap"}, | ||
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) { | ||
v := val.(int) | ||
if v < 0 || v > 23 { | ||
errs = append(errs, fmt.Errorf("%q value should be between 0 and 23, got: %d", key, v)) | ||
} | ||
return | ||
}, | ||
}, | ||
"start_asap": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
"number_of_deferrals": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"defer": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceMongoDBAtlasMaintenanceWindowCreate(d *schema.ResourceData, meta interface{}) error { | ||
//Get the client connection. | ||
conn := meta.(*matlas.Client) | ||
|
||
projectID := d.Get("project_id").(string) | ||
|
||
if deferValue := d.Get("defer").(bool); deferValue { | ||
_, err := conn.MaintenanceWindows.Defer(context.Background(), projectID) | ||
if err != nil { | ||
return fmt.Errorf(errorMaintenanceDefer, projectID, err) | ||
} | ||
} | ||
|
||
maintenanceWindowReq := &matlas.MaintenanceWindow{} | ||
if dayOfWeek, ok := d.GetOk("day_of_week"); ok { | ||
maintenanceWindowReq.DayOfWeek = cast.ToInt(dayOfWeek) | ||
} | ||
if hourOfDay, ok := d.GetOkExists("hour_of_day"); ok { | ||
maintenanceWindowReq.HourOfDay = pointy.Int(cast.ToInt(hourOfDay)) | ||
} | ||
if numberOfDeferrals, ok := d.GetOk("number_of_deferrals"); ok { | ||
maintenanceWindowReq.NumberOfDeferrals = cast.ToInt(numberOfDeferrals) | ||
} | ||
|
||
_, err := conn.MaintenanceWindows.Update(context.Background(), projectID, maintenanceWindowReq) | ||
if err != nil { | ||
return fmt.Errorf(errorMaintenanceCreate, projectID, err) | ||
} | ||
|
||
d.SetId(projectID) | ||
|
||
return resourceMongoDBAtlasMaintenanceWindowRead(d, meta) | ||
} | ||
|
||
func resourceMongoDBAtlasMaintenanceWindowRead(d *schema.ResourceData, meta interface{}) error { | ||
//Get the client connection. | ||
conn := meta.(*matlas.Client) | ||
|
||
maintenanceWindow, _, err := conn.MaintenanceWindows.Get(context.Background(), d.Id()) | ||
if err != nil { | ||
return fmt.Errorf(errorMaintenanceRead, d.Id(), err) | ||
} | ||
|
||
if err := d.Set("day_of_week", maintenanceWindow.DayOfWeek); err != nil { | ||
return fmt.Errorf(errorMaintenanceRead, d.Id(), err) | ||
} | ||
if err := d.Set("hour_of_day", maintenanceWindow.HourOfDay); err != nil { | ||
return fmt.Errorf(errorMaintenanceRead, d.Id(), err) | ||
} | ||
if err := d.Set("number_of_deferrals", maintenanceWindow.NumberOfDeferrals); err != nil { | ||
return 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 | ||
// it can be used via API | ||
if err := d.Set("start_asap", maintenanceWindow.StartASAP); err != nil { | ||
return fmt.Errorf(errorMaintenanceRead, d.Id(), err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceMongoDBAtlasMaintenanceWindowUpdate(d *schema.ResourceData, meta interface{}) error { | ||
//Get the client connection. | ||
conn := meta.(*matlas.Client) | ||
|
||
maintenanceWindowReq := &matlas.MaintenanceWindow{} | ||
|
||
if d.HasChange("defer") { | ||
_, err := conn.MaintenanceWindows.Defer(context.Background(), d.Id()) | ||
if err != nil { | ||
return fmt.Errorf(errorMaintenanceDefer, d.Id(), err) | ||
} | ||
} | ||
|
||
if d.HasChange("day_of_week") { | ||
maintenanceWindowReq.DayOfWeek = cast.ToInt(d.Get("day_of_week")) | ||
} | ||
if d.HasChange("hour_of_day") { | ||
maintenanceWindowReq.HourOfDay = pointy.Int(cast.ToInt(d.Get("hour_of_day"))) | ||
} | ||
if d.HasChange("number_of_deferrals") { | ||
maintenanceWindowReq.NumberOfDeferrals = cast.ToInt(d.Get("number_of_deferrals")) | ||
} | ||
|
||
_, err := conn.MaintenanceWindows.Update(context.Background(), d.Id(), maintenanceWindowReq) | ||
if err != nil { | ||
return fmt.Errorf(errorMaintenanceUpdate, d.Id(), err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceMongoDBAtlasMaintenanceWindowDelete(d *schema.ResourceData, meta interface{}) error { | ||
//Get the client connection. | ||
conn := meta.(*matlas.Client) | ||
|
||
_, err := conn.MaintenanceWindows.Reset(context.Background(), d.Id()) | ||
if err != nil { | ||
return fmt.Errorf(errorMaintenanceDelete, d.Id(), err) | ||
} | ||
|
||
return nil | ||
} |
172 changes: 172 additions & 0 deletions
172
mongodbatlas/resource_mongodbatlas_maintenance_window_test.go
This file contains 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,172 @@ | ||
package mongodbatlas | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"testing" | ||
|
||
"github.com/go-test/deep" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
matlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas" | ||
"github.com/spf13/cast" | ||
) | ||
|
||
func TestAccResourceMongoDBAtlasMaintenanceWindow_basic(t *testing.T) { | ||
var maintenance matlas.MaintenanceWindow | ||
resourceName := "mongodbatlas_maintenance_window.test" | ||
|
||
projectID := os.Getenv("MONGODB_ATLAS_PROJECT_ID") | ||
dayOfWeek := 7 | ||
hourOfDay := 3 | ||
|
||
dayOfWeekUpdated := 4 | ||
hourOfDayUpdated := 5 | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckMongoDBAtlasMaintenanceWindowDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccMongoDBAtlasMaintenanceWindowConfig(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"), | ||
|
||
testAccCheckMongoDBAtlasMaintenanceWindowAttributes("day_of_week", dayOfWeek, &maintenance.DayOfWeek), | ||
), | ||
}, | ||
{ | ||
Config: testAccMongoDBAtlasMaintenanceWindowConfig(projectID, dayOfWeekUpdated, hourOfDayUpdated), | ||
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(dayOfWeekUpdated)), | ||
resource.TestCheckResourceAttr(resourceName, "hour_of_day", cast.ToString(hourOfDayUpdated)), | ||
resource.TestCheckResourceAttr(resourceName, "number_of_deferrals", "0"), | ||
|
||
testAccCheckMongoDBAtlasMaintenanceWindowAttributes("day_of_week", dayOfWeekUpdated, &maintenance.DayOfWeek), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccResourceMongoDBAtlasMaintenanceWindow_importBasic(t *testing.T) { | ||
var maintenance matlas.MaintenanceWindow | ||
resourceName := "mongodbatlas_maintenance_window.test" | ||
|
||
projectID := os.Getenv("MONGODB_ATLAS_PROJECT_ID") | ||
dayOfWeek := 1 | ||
hourOfDay := 3 | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckMongoDBAtlasMaintenanceWindowDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccMongoDBAtlasMaintenanceWindowConfig(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, "start_asap", "false"), | ||
|
||
testAccCheckMongoDBAtlasMaintenanceWindowAttributes("day_of_week", dayOfWeek, &maintenance.DayOfWeek), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
ImportStateIdFunc: testAccCheckMongoDBAtlasMaintenanceWindowImportStateIDFunc(resourceName), | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
ImportStateVerifyIgnore: []string{"project_id"}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckMongoDBAtlasMaintenanceWindowExists(resourceName string, maintenance *matlas.MaintenanceWindow) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*matlas.Client) | ||
|
||
rs, ok := s.RootModule().Resources[resourceName] | ||
if !ok { | ||
return fmt.Errorf("not found: %s", resourceName) | ||
} | ||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("no ID is set") | ||
} | ||
|
||
log.Printf("[DEBUG] projectID: %s", rs.Primary.ID) | ||
|
||
maintenanceWindow, _, err := conn.MaintenanceWindows.Get(context.Background(), rs.Primary.ID) | ||
if err != nil { | ||
return fmt.Errorf("Maintenance Window (%s) does not exist", rs.Primary.ID) | ||
} | ||
*maintenance = *maintenanceWindow | ||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckMongoDBAtlasMaintenanceWindowAttributes(attr string, expected int, got *int) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
if diff := deep.Equal(expected, *got); diff != nil { | ||
return fmt.Errorf("Bad %s \n got = %#v\nwant = %#v \ndiff = %#v", attr, expected, *got, diff) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckMongoDBAtlasMaintenanceWindowDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*matlas.Client) | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "mongodbatlas_maintenance_window" { | ||
continue | ||
} | ||
|
||
_, _, err := conn.MaintenanceWindows.Get(context.Background(), rs.Primary.ID) | ||
if err != nil { | ||
return fmt.Errorf("Maintenance Window (%s) does not exist", rs.Primary.ID) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func testAccCheckMongoDBAtlasMaintenanceWindowImportStateIDFunc(resourceName string) resource.ImportStateIdFunc { | ||
return func(s *terraform.State) (string, error) { | ||
rs, ok := s.RootModule().Resources[resourceName] | ||
if !ok { | ||
return "", fmt.Errorf("Not found: %s", resourceName) | ||
} | ||
return rs.Primary.ID, nil | ||
} | ||
} | ||
|
||
func testAccMongoDBAtlasMaintenanceWindowConfig(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 | ||
}`, projectID, dayOfWeek, hourOfDay) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.