Skip to content

Commit

Permalink
Merge pull request #1 from jxie418/SUMO-226102-add-create-muting-sche…
Browse files Browse the repository at this point in the history
…dular-second-part

SUMO-226523: Added more validation in the TF
  • Loading branch information
jxie418 authored Aug 28, 2023
2 parents 0b65bd9 + cfcb39b commit 9d093f6
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package sumologic

import (
"fmt"
"log"
"regexp"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
Expand Down Expand Up @@ -59,6 +61,7 @@ func getMutingScheduleBaseSchema() map[string]*schema.Schema {
Elem: &schema.Resource{
Schema: getMonitorScopeSchema(),
},
AtLeastOneOf: monitorAtleastOneKey,
},

"schedule": {
Expand Down Expand Up @@ -106,20 +109,26 @@ func getScheduleDefinitionSchemma() map[string]*schema.Schema {
Type: schema.TypeString,
ForceNew: false,
Required: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`),
"start date in format of yyyy-mm-dd"),
ValidateFunc: validation.All(
validation.StringMatch(regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`),
"start date in format of yyyy-mm-dd"),
StartDateIsAfterYesterday(),
),
},
"start_time": {
Type: schema.TypeString,
Required: true,
ForceNew: false,
ValidateFunc: validation.StringLenBetween(5, 5),
Type: schema.TypeString,
Required: true,
ForceNew: false,
ValidateFunc: validation.All(
validation.StringMatch(regexp.MustCompile(`^(?:[01]\d|2[0-3]):[0-5]\d$`),
"start time in format of 00:00"),
),
},
"duration": {
Type: schema.TypeInt,
Required: true,
ForceNew: false,
ValidateFunc: validation.IntAtLeast(0),
ValidateFunc: validation.IntAtLeast(15),
},
"rrule": {
Type: schema.TypeString,
Expand Down Expand Up @@ -301,6 +310,33 @@ func getScheduleDefinition(d *schema.ResourceData) ScheduleDefinition {
return scheduleDefinition
}

func StartDateIsAfterYesterday() schema.SchemaValidateFunc {
return func(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
return warnings, []error{fmt.Errorf("expected type of %q to be string", k)}
}

date, err := time.Parse("2006-01-02", v)

if err != nil {
return warnings, []error{fmt.Errorf("expected %q to be an valid start date yyyy-mm-dd : got %v", k, v)}
}

yesterday := time.Now().AddDate(0, 0, -1).Truncate(24 * time.Hour)

if date.Before(yesterday) {
return warnings, []error{fmt.Errorf("expected %q to be an valid start date : got %v", k, v)}
}
return warnings, errors
}
}

var monitorAtleastOneKey = []string{
"monitor.0.ids",
"monitor.0.all",
}

func resourceToMutingSchedulesLibraryMutingSchedule(d *schema.ResourceData) MutingSchedulesLibraryMutingSchedule {
monitorScope := getMonitorScope(d)
scheduleDefinition := getScheduleDefinition(d)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sumologic

import (
"fmt"
"regexp"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -160,6 +161,25 @@ func TestAccSumologicMutingSchedulesLibraryMutingSchedule_update(t *testing.T) {
})
}

func TestAccSumologicMutingSchedulesLibraryMutingSchedule_monitorScopeValidations(t *testing.T) {
var mutingSchedulesLibraryMutingSchedule MutingSchedulesLibraryMutingSchedule
testNameSuffix := acctest.RandString(16)
testName := "terraform_test_muting_schedule_" + testNameSuffix
config := testAccSumologicMutingSchedulesLibraryMutingScheduleBadMonitorScope(testName)
expectedError := regexp.MustCompile("An argument named \"abc\" is not expected here. Did you mean \"all\"?")
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckMutingSchedulesLibraryMutingScheduleDestroy(mutingSchedulesLibraryMutingSchedule),
Steps: []resource.TestStep{
{
Config: config,
PlanOnly: true,
ExpectError: expectedError,
},
},
})
}

func testAccCheckMutingSchedulesLibraryMutingScheduleDestroy(mutingSchedulesLibraryMutingSchedule MutingSchedulesLibraryMutingSchedule) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*Client)
Expand Down Expand Up @@ -267,3 +287,27 @@ func testAccSumologicMutingSchedulesLibraryMutingScheduleUpdate(testName string)
}
`, testName, startDate)
}

func testAccSumologicMutingSchedulesLibraryMutingScheduleBadMonitorScope(testName string) string {
tomorrow := time.Now().AddDate(0, 0, 1)
startDate := tomorrow.Format("2006-01-02")
return fmt.Sprintf(`
resource "sumologic_muting_schedule" "test" {
name = "terraform_test_muting_schedule_%s"
description = "terraform_test_muting_schedule_description"
type = "MutingSchedulesLibraryMutingSchedule"
content_type = "MutingSchedule"
monitor {
abc="not right"
}
schedule {
timezone = "America/Los_Angeles"
start_date = "%s"
start_time = "01:00"
duration = 50
rrule = "FREQ=DAILY;INTERVAL=1"
is_form = false
}
}
`, testName, startDate)
}

0 comments on commit 9d093f6

Please sign in to comment.