diff --git a/thousandeyes/acceptance_resources/ftp_server/alerts_enabled.tf b/thousandeyes/acceptance_resources/ftp_server/alerts_enabled.tf index 5925deb..3f7623d 100644 --- a/thousandeyes/acceptance_resources/ftp_server/alerts_enabled.tf +++ b/thousandeyes/acceptance_resources/ftp_server/alerts_enabled.tf @@ -5,7 +5,7 @@ data "thousandeyes_agent" "test" { resource "thousandeyes_ftp_server" "test" { password = "test_password" username = "test_username" - test_name = "Acceptance Test - FTP" + test_name = "Acceptance Test - FTP Alerts Enabled" description = "description" request_type = "Download" ftp_time_limit = 10 @@ -13,9 +13,14 @@ resource "thousandeyes_ftp_server" "test" { interval = 900 alerts_enabled = true network_measurements = false + bgp_measurements = false url = "ftp://speedtest.tele2.net/" agents { agent_id = data.thousandeyes_agent.test.agent_id } + + alert_rules { + rule_id = 921623 #FTP Default Alert Rule + } } diff --git a/thousandeyes/acceptance_resources/ftp_server/alerts_enabled_multiple_alert_rules.tf b/thousandeyes/acceptance_resources/ftp_server/alerts_enabled_multiple_alert_rules.tf index 2ef442b..6ecfe99 100644 --- a/thousandeyes/acceptance_resources/ftp_server/alerts_enabled_multiple_alert_rules.tf +++ b/thousandeyes/acceptance_resources/ftp_server/alerts_enabled_multiple_alert_rules.tf @@ -3,20 +3,18 @@ data "thousandeyes_agent" "test" { } resource "thousandeyes_alert_rule" "alert-rule-ftp-test" { - rule_name = "API Team: (${var.environment}) Alert Slack" - alert_type = "HTTP Server" - expression = "((probDetail != \"\"))" + rule_name = "ftp server test" + alert_type = "FTP" + expression = "((ftpErrorType != \"None\"))" + rounds_violating_out_of = 1 + rounds_violating_required = 1 minimum_sources_pct = 80 - notify_on_clear = true - rounds_violating_out_of = 2 - rounds_violating_required = 2 - rounds_violating_mode = "ANY" } resource "thousandeyes_ftp_server" "test" { password = "test_password" username = "test_username" - test_name = "Acceptance Test - FTP" + test_name = "Acceptance Test - FTP Multiple Alert Rules" description = "description" request_type = "Download" ftp_time_limit = 10 @@ -29,4 +27,12 @@ resource "thousandeyes_ftp_server" "test" { agents { agent_id = data.thousandeyes_agent.test.agent_id } + + alert_rules { + rule_id = 921623 #FTP Default Alert Rule + } + + alert_rules { + rule_id = thousandeyes_alert_rule.alert-rule-ftp-test.id + } } diff --git a/thousandeyes/provider_test.go b/thousandeyes/provider_test.go index 4384d47..dedd222 100644 --- a/thousandeyes/provider_test.go +++ b/thousandeyes/provider_test.go @@ -3,13 +3,21 @@ package thousandeyes import ( "context" "fmt" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "github.com/thousandeyes/thousandeyes-sdk-go/v2" "os" + "strconv" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) +type ResourceType struct { + Name string + ResourceName string + GetResource func(id int64) (interface{}, error) +} + var testClient *thousandeyes.Client var providerFactories = map[string]func() (*schema.Provider, error){ @@ -43,3 +51,21 @@ func testAccPreCheck(t *testing.T) { os.Exit(1) } } + +func testAccCheckResourceDestroy(resources []ResourceType, s *terraform.State) error { + for _, resource := range resources { + for _, rs := range s.RootModule().Resources { + if rs.Type == resource.ResourceName { + id, err := strconv.ParseInt(rs.Primary.ID, 10, 64) + if err != nil { + return err + } + _, err = testClient.GetAlertRule(id) + if err == nil { + return fmt.Errorf("%s with id %s still exists", resource.ResourceName, rs.Primary.ID) + } + } + } + } + return nil +} diff --git a/thousandeyes/resource_ftp_server_test.go b/thousandeyes/resource_ftp_server_test.go index 1fa888f..5bae5a9 100644 --- a/thousandeyes/resource_ftp_server_test.go +++ b/thousandeyes/resource_ftp_server_test.go @@ -1,9 +1,7 @@ package thousandeyes import ( - "fmt" "os" - "strconv" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -21,13 +19,35 @@ func TestAccThousandEyesFTPServer(t *testing.T) { name: "basic", resourceFile: "acceptance_resources/ftp_server/basic.tf", resourceName: "thousandeyes_ftp_server.test", - checkDestroyFunction: testAccCheckThousandEyesFTPServerDestroy, + checkDestroyFunction: testAccCheckDefaultResourceDestroy, }, { name: "alerts_enabled", resourceFile: "acceptance_resources/ftp_server/alerts_enabled.tf", resourceName: "thousandeyes_ftp_server.test", - checkDestroyFunction: testAccCheckThousandEyesFTPServerDestroy, + checkDestroyFunction: testAccCheckDefaultResourceDestroy, + }, + { + name: "alerts_enabled_multiple_alert_rules", + resourceFile: "acceptance_resources/ftp_server/alerts_enabled_multiple_alert_rules.tf", + resourceName: "thousandeyes_ftp_server.test", + checkDestroyFunction: func(state *terraform.State) error { + resourceList := []ResourceType{ + { + Name: "FTP Server Test", + ResourceName: "thousandeyes_ftp_server", + GetResource: func(id int64) (interface{}, error) { + return testClient.GetFTPServer(id) + }}, + { + Name: "Alert Rules", + ResourceName: "thousandeyes_alert_rule", + GetResource: func(id int64) (interface{}, error) { + return testClient.GetAlertRule(id) + }}, + } + return testAccCheckResourceDestroy(resourceList, state) + }, }, } @@ -52,33 +72,16 @@ func TestAccThousandEyesFTPServer(t *testing.T) { } } -func testAccCheckThousandEyesFTPServerDestroy(s *terraform.State) error { - for _, rs := range s.RootModule().Resources { - if rs.Type == "thousandeyes_ftp_server" { - id, err := strconv.ParseInt(rs.Primary.ID, 10, 64) - if err != nil { - return err - } - _, err = testClient.GetFTPServer(id) - if err == nil { - return fmt.Errorf("FTPServer %s still exists", rs.Primary.ID) - } - } - } - return nil -} - -func testAccCheckThousandEyesFTPServerSteps(testResource, resourceName string) []resource.TestStep { - return []resource.TestStep{ +func testAccCheckDefaultResourceDestroy(s *terraform.State) error { + resourceList := []ResourceType{ { - Config: testAccThousandEyesFTPServerConfig(testResource), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(resourceName, "password", "test_password"), - resource.TestCheckResourceAttr(resourceName, "username", "test_username"), - // Add more checks based on the resource attributes - ), - }, + Name: "FTP Server Test", + ResourceName: "thousandeyes_ftp_server", + GetResource: func(id int64) (interface{}, error) { + return testClient.GetFTPServer(id) + }}, } + return testAccCheckResourceDestroy(resourceList, s) } func testAccThousandEyesFTPServerConfig(testResource string) string {