|
| 1 | +package sumologic |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/terraform" |
| 9 | +) |
| 10 | + |
| 11 | +func TestAccSumologicCSEMatchRule_createAndUpdate(t *testing.T) { |
| 12 | + var matchRule CSEMatchRule |
| 13 | + descriptionExpression := "Test description" |
| 14 | + enabled := true |
| 15 | + entitySelectorEntityType := "_ip" |
| 16 | + entitySelectorExpression := "srcDevice_ip" |
| 17 | + expression := "foo = bar" |
| 18 | + isPrototype := false |
| 19 | + name := "Test Match Rule" |
| 20 | + nameExpression := "Signal Name" |
| 21 | + severityMappingType := "constant" |
| 22 | + severityMappingDefault := 5 |
| 23 | + summaryExpression := "Signal Summary" |
| 24 | + tag := "foo" |
| 25 | + |
| 26 | + nameUpdated := "Updated Match Rule" |
| 27 | + tagUpdated := "bar" |
| 28 | + |
| 29 | + resourceName := "sumologic_cse_match_rule.match_rule" |
| 30 | + resource.Test(t, resource.TestCase{ |
| 31 | + PreCheck: func() { testAccPreCheck(t) }, |
| 32 | + Providers: testAccProviders, |
| 33 | + CheckDestroy: testAccCSEMatchRuleDestroy, |
| 34 | + Steps: []resource.TestStep{ |
| 35 | + { |
| 36 | + Config: testCreateCSEMatchRuleConfig(descriptionExpression, enabled, |
| 37 | + entitySelectorEntityType, entitySelectorExpression, expression, |
| 38 | + isPrototype, name, nameExpression, severityMappingType, severityMappingDefault, |
| 39 | + summaryExpression, tag), |
| 40 | + Check: resource.ComposeTestCheckFunc( |
| 41 | + testCheckCSEMatchRuleExists(resourceName, &matchRule), |
| 42 | + testCheckMatchRuleValues(&matchRule, descriptionExpression, enabled, |
| 43 | + entitySelectorEntityType, entitySelectorExpression, expression, |
| 44 | + isPrototype, name, nameExpression, severityMappingType, severityMappingDefault, |
| 45 | + summaryExpression, tag), |
| 46 | + resource.TestCheckResourceAttrSet(resourceName, "id"), |
| 47 | + ), |
| 48 | + }, |
| 49 | + { |
| 50 | + Config: testCreateCSEMatchRuleConfig(descriptionExpression, enabled, |
| 51 | + entitySelectorEntityType, entitySelectorExpression, expression, |
| 52 | + isPrototype, nameUpdated, nameExpression, severityMappingType, severityMappingDefault, |
| 53 | + summaryExpression, tagUpdated), |
| 54 | + Check: resource.ComposeTestCheckFunc( |
| 55 | + testCheckCSEMatchRuleExists(resourceName, &matchRule), |
| 56 | + testCheckMatchRuleValues(&matchRule, descriptionExpression, enabled, |
| 57 | + entitySelectorEntityType, entitySelectorExpression, expression, |
| 58 | + isPrototype, nameUpdated, nameExpression, severityMappingType, severityMappingDefault, |
| 59 | + summaryExpression, tagUpdated), |
| 60 | + resource.TestCheckResourceAttrSet(resourceName, "id"), |
| 61 | + ), |
| 62 | + }, |
| 63 | + }, |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +func testAccCSEMatchRuleDestroy(s *terraform.State) error { |
| 68 | + client := testAccProvider.Meta().(*Client) |
| 69 | + |
| 70 | + for _, rs := range s.RootModule().Resources { |
| 71 | + if rs.Type != "sumologic_cse_match_rule" { |
| 72 | + continue |
| 73 | + } |
| 74 | + |
| 75 | + if rs.Primary.ID == "" { |
| 76 | + return fmt.Errorf("CSE Match Rule destruction check: CSE Match Rule ID is not set") |
| 77 | + } |
| 78 | + |
| 79 | + s, err := client.GetCSEMatchRule(rs.Primary.ID) |
| 80 | + if err != nil { |
| 81 | + return fmt.Errorf("Encountered an error: " + err.Error()) |
| 82 | + } |
| 83 | + if s != nil { |
| 84 | + return fmt.Errorf("Match rule still exists") |
| 85 | + } |
| 86 | + } |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +func testCreateCSEMatchRuleConfig( |
| 91 | + descriptionExpression string, enabled bool, entitySelectorEntityType string, |
| 92 | + entitySelectorExpression string, expression string, isPrototype bool, name string, |
| 93 | + nameExpression string, severityMappingType string, severityMappingDefault int, |
| 94 | + summaryExpression string, tag string) string { |
| 95 | + return fmt.Sprintf(` |
| 96 | +resource "sumologic_cse_match_rule" "match_rule" { |
| 97 | + description_expression = "%s" |
| 98 | + enabled = %t |
| 99 | + entity_selectors { |
| 100 | + entity_type = "%s" |
| 101 | + expression = "%s" |
| 102 | + } |
| 103 | + expression = "%s" |
| 104 | + is_prototype = %t |
| 105 | + name = "%s" |
| 106 | + name_expression = "%s" |
| 107 | + severity_mapping { |
| 108 | + type = "%s" |
| 109 | + default = %d |
| 110 | + } |
| 111 | + summary_expression = "%s" |
| 112 | + tags = ["%s"] |
| 113 | +} |
| 114 | +`, descriptionExpression, enabled, entitySelectorEntityType, entitySelectorExpression, |
| 115 | + expression, isPrototype, name, nameExpression, severityMappingType, severityMappingDefault, |
| 116 | + summaryExpression, tag) |
| 117 | +} |
| 118 | + |
| 119 | +func testCheckCSEMatchRuleExists(n string, matchRule *CSEMatchRule) resource.TestCheckFunc { |
| 120 | + return func(s *terraform.State) error { |
| 121 | + rs, ok := s.RootModule().Resources[n] |
| 122 | + if !ok { |
| 123 | + return fmt.Errorf("not found: %s", n) |
| 124 | + } |
| 125 | + |
| 126 | + if rs.Primary.ID == "" { |
| 127 | + return fmt.Errorf("match rule ID is not set") |
| 128 | + } |
| 129 | + |
| 130 | + c := testAccProvider.Meta().(*Client) |
| 131 | + matchRuleResp, err := c.GetCSEMatchRule(rs.Primary.ID) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + *matchRule = *matchRuleResp |
| 137 | + |
| 138 | + return nil |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func testCheckMatchRuleValues(matchRule *CSEMatchRule, descriptionExpression string, enabled bool, |
| 143 | + entitySelectorEntityType string, entitySelectorExpression string, expression string, |
| 144 | + isPrototype bool, name string, nameExpression string, severityMappingType string, severityMappingDefault int, |
| 145 | + summaryExpression string, tag string) resource.TestCheckFunc { |
| 146 | + return func(s *terraform.State) error { |
| 147 | + if matchRule.DescriptionExpression != descriptionExpression { |
| 148 | + return fmt.Errorf("bad descriptionExpression, expected \"%s\", got %#v", descriptionExpression, matchRule.DescriptionExpression) |
| 149 | + } |
| 150 | + if matchRule.Enabled != enabled { |
| 151 | + return fmt.Errorf("bad enabled, expected \"%t\", got %#v", enabled, matchRule.Enabled) |
| 152 | + } |
| 153 | + if matchRule.EntitySelectors[0].EntityType != entitySelectorEntityType { |
| 154 | + return fmt.Errorf("bad entitySelectorEntityType, expected \"%s\", got %#v", entitySelectorEntityType, matchRule.EntitySelectors[0].EntityType) |
| 155 | + } |
| 156 | + if matchRule.EntitySelectors[0].Expression != entitySelectorExpression { |
| 157 | + return fmt.Errorf("bad entitySelectorExpression, expected \"%s\", got %#v", entitySelectorExpression, matchRule.EntitySelectors[0].Expression) |
| 158 | + } |
| 159 | + if matchRule.Expression != expression { |
| 160 | + return fmt.Errorf("bad expression, expected \"%s\", got %#v", expression, matchRule.Expression) |
| 161 | + } |
| 162 | + if matchRule.IsPrototype != isPrototype { |
| 163 | + return fmt.Errorf("bad isPrototype, expected \"%t\", got %#v", isPrototype, matchRule.IsPrototype) |
| 164 | + } |
| 165 | + if matchRule.Name != name { |
| 166 | + return fmt.Errorf("bad name, expected \"%s\", got %#v", name, matchRule.Name) |
| 167 | + } |
| 168 | + if matchRule.NameExpression != nameExpression { |
| 169 | + return fmt.Errorf("bad nameExpression, expected \"%s\", got %#v", nameExpression, matchRule.NameExpression) |
| 170 | + } |
| 171 | + if matchRule.SeverityMapping.Type != severityMappingType { |
| 172 | + return fmt.Errorf("bad severityMappingType, expected \"%s\", got %#v", severityMappingType, matchRule.SeverityMapping.Type) |
| 173 | + } |
| 174 | + if matchRule.SeverityMapping.Default != severityMappingDefault { |
| 175 | + return fmt.Errorf("bad severityMappingDefault, expected \"%d\", got %#v", severityMappingDefault, matchRule.SeverityMapping.Default) |
| 176 | + } |
| 177 | + if matchRule.SummaryExpression != summaryExpression { |
| 178 | + return fmt.Errorf("bad summaryExpression, expected \"%s\", got %#v", summaryExpression, matchRule.SummaryExpression) |
| 179 | + } |
| 180 | + if matchRule.Tags[0] != tag { |
| 181 | + return fmt.Errorf("bad tag, expected \"%s\", got %#v", tag, matchRule.Tags[0]) |
| 182 | + } |
| 183 | + |
| 184 | + return nil |
| 185 | + } |
| 186 | +} |
0 commit comments