|
| 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 TestAccSumologicCSECustomInsight_createAndUpdate(t *testing.T) { |
| 12 | + var CustomInsight CSECustomInsight |
| 13 | + description := "Test description" |
| 14 | + enabled := true |
| 15 | + ordered := true |
| 16 | + name := "Test Custom Insight" |
| 17 | + severity := "HIGH" |
| 18 | + signalName1 := "Some Signal Name *" |
| 19 | + signalName2 := "Some Other Signal Name *" |
| 20 | + tag := "foo" |
| 21 | + |
| 22 | + nameUpdated := "Updated Custom Insight" |
| 23 | + severityUpdated := "LOW" |
| 24 | + |
| 25 | + resourceName := "sumologic_cse_custom_insight.custom_insight" |
| 26 | + resource.Test(t, resource.TestCase{ |
| 27 | + PreCheck: func() { testAccPreCheck(t) }, |
| 28 | + Providers: testAccProviders, |
| 29 | + CheckDestroy: testAccCSECustomInsightDestroy, |
| 30 | + Steps: []resource.TestStep{ |
| 31 | + { |
| 32 | + Config: testCreateCSECustomInsightConfig(description, enabled, |
| 33 | + ordered, name, severity, signalName1, signalName2, tag), |
| 34 | + Check: resource.ComposeTestCheckFunc( |
| 35 | + testCheckCSECustomInsightExists(resourceName, &CustomInsight), |
| 36 | + testCheckCustomInsightValues(&CustomInsight, description, enabled, |
| 37 | + ordered, name, severity, signalName1, signalName2, tag), |
| 38 | + resource.TestCheckResourceAttrSet(resourceName, "id"), |
| 39 | + ), |
| 40 | + }, |
| 41 | + { |
| 42 | + Config: testCreateCSECustomInsightConfig(description, enabled, |
| 43 | + ordered, nameUpdated, severityUpdated, signalName1, |
| 44 | + signalName2, tag), |
| 45 | + Check: resource.ComposeTestCheckFunc( |
| 46 | + testCheckCSECustomInsightExists(resourceName, &CustomInsight), |
| 47 | + testCheckCustomInsightValues(&CustomInsight, description, enabled, |
| 48 | + ordered, nameUpdated, severityUpdated, signalName1, |
| 49 | + signalName2, tag), |
| 50 | + resource.TestCheckResourceAttrSet(resourceName, "id"), |
| 51 | + ), |
| 52 | + }, |
| 53 | + }, |
| 54 | + }) |
| 55 | +} |
| 56 | + |
| 57 | +func testAccCSECustomInsightDestroy(s *terraform.State) error { |
| 58 | + client := testAccProvider.Meta().(*Client) |
| 59 | + |
| 60 | + for _, rs := range s.RootModule().Resources { |
| 61 | + if rs.Type != "sumologic_cse_custom_insight" { |
| 62 | + continue |
| 63 | + } |
| 64 | + |
| 65 | + if rs.Primary.ID == "" { |
| 66 | + return fmt.Errorf("CSE Custom Insight destruction check: CSE Custom Insight ID is not set") |
| 67 | + } |
| 68 | + |
| 69 | + s, err := client.GetCSECustomInsight(rs.Primary.ID) |
| 70 | + if err != nil { |
| 71 | + return fmt.Errorf("Encountered an error: " + err.Error()) |
| 72 | + } |
| 73 | + if s != nil { |
| 74 | + return fmt.Errorf("Custom Insight still exists") |
| 75 | + } |
| 76 | + } |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +func testCreateCSECustomInsightConfig( |
| 81 | + description string, enabled bool, ordered bool, name string, |
| 82 | + severity string, signalName1 string, signalName2 string, tag string) string { |
| 83 | + return fmt.Sprintf(` |
| 84 | +resource "sumologic_cse_custom_insight" "custom_insight" { |
| 85 | + description = "%s" |
| 86 | + enabled = %t |
| 87 | + ordered = %t |
| 88 | + name = "%s" |
| 89 | + severity = "%s" |
| 90 | + signal_names = ["%s", "%s"] |
| 91 | + tags = ["%s"] |
| 92 | +} |
| 93 | +`, description, enabled, ordered, name, severity, signalName1, |
| 94 | + signalName2, tag) |
| 95 | +} |
| 96 | + |
| 97 | +func testCheckCSECustomInsightExists(n string, CustomInsight *CSECustomInsight) resource.TestCheckFunc { |
| 98 | + return func(s *terraform.State) error { |
| 99 | + rs, ok := s.RootModule().Resources[n] |
| 100 | + if !ok { |
| 101 | + return fmt.Errorf("not found: %s", n) |
| 102 | + } |
| 103 | + |
| 104 | + if rs.Primary.ID == "" { |
| 105 | + return fmt.Errorf("chain rule ID is not set") |
| 106 | + } |
| 107 | + |
| 108 | + c := testAccProvider.Meta().(*Client) |
| 109 | + CustomInsightResp, err := c.GetCSECustomInsight(rs.Primary.ID) |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + |
| 114 | + *CustomInsight = *CustomInsightResp |
| 115 | + |
| 116 | + return nil |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func testCheckCustomInsightValues(CustomInsight *CSECustomInsight, description string, |
| 121 | + enabled bool, ordered bool, name string, severity string, signalName1 string, |
| 122 | + signalName2 string, tag string) resource.TestCheckFunc { |
| 123 | + return func(s *terraform.State) error { |
| 124 | + if CustomInsight.Description != description { |
| 125 | + return fmt.Errorf("bad description, expected \"%s\", got %#v", description, CustomInsight.Description) |
| 126 | + } |
| 127 | + if CustomInsight.Enabled != enabled { |
| 128 | + return fmt.Errorf("bad enabled, expected \"%t\", got %#v", enabled, CustomInsight.Enabled) |
| 129 | + } |
| 130 | + if CustomInsight.Ordered != ordered { |
| 131 | + return fmt.Errorf("bad ordered, expected \"%t\", got %#v", ordered, CustomInsight.Ordered) |
| 132 | + } |
| 133 | + if CustomInsight.Name != name { |
| 134 | + return fmt.Errorf("bad name, expected \"%s\", got %#v", name, CustomInsight.Name) |
| 135 | + } |
| 136 | + if CustomInsight.Severity != severity { |
| 137 | + return fmt.Errorf("bad severity, expected \"%s\", got %#v", severity, CustomInsight.Severity) |
| 138 | + } |
| 139 | + if CustomInsight.SignalNames[0] != signalName1 { |
| 140 | + return fmt.Errorf("bad signalName1, expected \"%s\", got %#v", signalName1, CustomInsight.SignalNames[0]) |
| 141 | + } |
| 142 | + if CustomInsight.SignalNames[1] != signalName2 { |
| 143 | + return fmt.Errorf("bad signalName2, expected \"%s\", got %#v", signalName2, CustomInsight.SignalNames[1]) |
| 144 | + } |
| 145 | + if CustomInsight.Tags[0] != tag { |
| 146 | + return fmt.Errorf("bad tag, expected \"%s\", got %#v", tag, CustomInsight.Tags[0]) |
| 147 | + } |
| 148 | + |
| 149 | + return nil |
| 150 | + } |
| 151 | +} |
0 commit comments