|
| 1 | +package sumologic |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 10 | +) |
| 11 | + |
| 12 | +func TestAccSumologicMacro_crud(t *testing.T) { |
| 13 | + var macro Macro |
| 14 | + testNameSuffix := acctest.RandString(16) |
| 15 | + |
| 16 | + testName := "terraform_test_macro_" + testNameSuffix |
| 17 | + |
| 18 | + resource.Test(t, resource.TestCase{ |
| 19 | + PreCheck: func() { testAccPreCheck(t) }, |
| 20 | + Providers: testAccProviders, |
| 21 | + CheckDestroy: testAccCheckMacroDestroy(macro), |
| 22 | + Steps: []resource.TestStep{ |
| 23 | + { |
| 24 | + // create |
| 25 | + Config: testAccSumologicMacro(testName), |
| 26 | + Check: resource.ComposeTestCheckFunc( |
| 27 | + resource.TestCheckResourceAttr("sumologic_macro.test", "name", "terraform_macro_"+testName), |
| 28 | + resource.TestCheckResourceAttr("sumologic_macro.test", "description", ""), |
| 29 | + resource.TestCheckResourceAttr("sumologic_macro.test", "definition", "_sourceCategory=stream {{arg1}} | count by _timeslice"), |
| 30 | + resource.TestCheckResourceAttr("sumologic_macro.test", "enabled", "true"), |
| 31 | + resource.TestCheckResourceAttr("sumologic_macro.test", "argument.0.name", "arg1"), |
| 32 | + resource.TestCheckResourceAttr("sumologic_macro.test", "argument.0.type", "String"), |
| 33 | + resource.TestCheckResourceAttr("sumologic_macro.test", "argument_validation.0.eval_expression", "arg1 > 3"), |
| 34 | + resource.TestCheckResourceAttr("sumologic_macro.test", "argument_validation.0.error_message", "This is an error"), |
| 35 | + ), |
| 36 | + }, |
| 37 | + |
| 38 | + { |
| 39 | + // update |
| 40 | + Config: testAccSumologicMacroEdit(testName), |
| 41 | + Check: resource.ComposeTestCheckFunc( |
| 42 | + resource.TestCheckResourceAttr("sumologic_macro.test", "name", "terraform_macro_"+testName), |
| 43 | + resource.TestCheckResourceAttr("sumologic_macro.test", "description", "edited"), |
| 44 | + resource.TestCheckResourceAttr("sumologic_macro.test", "definition", "_sourceCategory=stream {{arg2}} | count by _timeslice"), |
| 45 | + resource.TestCheckResourceAttr("sumologic_macro.test", "enabled", "false"), |
| 46 | + resource.TestCheckResourceAttr("sumologic_macro.test", "argument.0.name", "arg2"), |
| 47 | + resource.TestCheckResourceAttr("sumologic_macro.test", "argument.0.type", "Any"), |
| 48 | + resource.TestCheckResourceAttr("sumologic_macro.test", "argument_validation.0.eval_expression", "arg2 > 3"), |
| 49 | + resource.TestCheckResourceAttr("sumologic_macro.test", "argument_validation.0.error_message", "This is an updated error"), |
| 50 | + ), |
| 51 | + }, |
| 52 | + { |
| 53 | + ResourceName: "sumologic_macro.test", |
| 54 | + ImportState: true, |
| 55 | + ImportStateVerify: false, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }) |
| 59 | +} |
| 60 | + |
| 61 | +func testAccSumologicMacro(testName string) string { |
| 62 | + return fmt.Sprintf(` |
| 63 | + resource "sumologic_macro" "test" { |
| 64 | + name = "terraform_macro_%s" |
| 65 | + definition = "_sourceCategory=stream {{arg1}} | count by _timeslice" |
| 66 | + argument { |
| 67 | + name = "arg1" |
| 68 | + type = "String" |
| 69 | + } |
| 70 | + argument_validation { |
| 71 | + eval_expression = "arg1 > 3" |
| 72 | + error_message = "This is an error" |
| 73 | + } |
| 74 | + } |
| 75 | + `, testName) |
| 76 | +} |
| 77 | + |
| 78 | +func testAccSumologicMacroEdit(testName string) string { |
| 79 | + return fmt.Sprintf(` |
| 80 | + resource "sumologic_macro" "test" { |
| 81 | + name = "terraform_macro_%s" |
| 82 | + description = "edited" |
| 83 | + enabled = false |
| 84 | + definition = "_sourceCategory=stream {{arg2}} | count by _timeslice" |
| 85 | + argument { |
| 86 | + name = "arg2" |
| 87 | + type = "Any" |
| 88 | + } |
| 89 | + argument_validation { |
| 90 | + eval_expression = "arg2 > 3" |
| 91 | + error_message = "This is an updated error" |
| 92 | + } |
| 93 | + } |
| 94 | + `, testName) |
| 95 | +} |
| 96 | + |
| 97 | +func testAccCheckMacroDestroy(macro Macro) resource.TestCheckFunc { |
| 98 | + return func(s *terraform.State) error { |
| 99 | + client := testAccProvider.Meta().(*Client) |
| 100 | + for _, r := range s.RootModule().Resources { |
| 101 | + id := r.Primary.ID |
| 102 | + u, err := client.MutingSchedulesRead(id) |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("Encountered an error: " + err.Error()) |
| 105 | + } |
| 106 | + if u != nil { |
| 107 | + return fmt.Errorf("Macro %s still exists", id) |
| 108 | + } |
| 109 | + } |
| 110 | + return nil |
| 111 | + } |
| 112 | +} |
0 commit comments