|
| 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 TestAccSumologicSCEAutomation_create_update(t *testing.T) { |
| 12 | + SkipCseTest(t) |
| 13 | + |
| 14 | + var Automation CSEAutomation |
| 15 | + nPlaybookId := "63ece953d5f0cb2ec4d5794e" |
| 16 | + nCseResourceType := "INSIGHT" |
| 17 | + nExecutionTypes := []string{"NEW_INSIGHT"} |
| 18 | + nEnabled := true |
| 19 | + uExecutionTypes := []string{"ON_DEMAND"} |
| 20 | + uEnabled := false |
| 21 | + resourceName := "sumologic_cse_automation.automation" |
| 22 | + resource.Test(t, resource.TestCase{ |
| 23 | + PreCheck: func() { testAccPreCheck(t) }, |
| 24 | + Providers: testAccProviders, |
| 25 | + CheckDestroy: testAccCSEAutomationDestroy, |
| 26 | + Steps: []resource.TestStep{ |
| 27 | + { |
| 28 | + Config: testCreateCSEAutomationConfig(nPlaybookId, nCseResourceType, nExecutionTypes, nEnabled), |
| 29 | + Check: resource.ComposeTestCheckFunc( |
| 30 | + testCheckCSEAutomationExists(resourceName, &Automation), |
| 31 | + testCheckAutomationValues(&Automation, nPlaybookId, nCseResourceType, nExecutionTypes, nEnabled), |
| 32 | + resource.TestCheckResourceAttrSet(resourceName, "id"), |
| 33 | + ), |
| 34 | + }, |
| 35 | + { |
| 36 | + Config: testCreateCSEAutomationConfig(nPlaybookId, nCseResourceType, uExecutionTypes, uEnabled), |
| 37 | + Check: resource.ComposeTestCheckFunc( |
| 38 | + testCheckCSEAutomationExists(resourceName, &Automation), |
| 39 | + testCheckAutomationValues(&Automation, nPlaybookId, nCseResourceType, uExecutionTypes, uEnabled), |
| 40 | + ), |
| 41 | + }, |
| 42 | + }, |
| 43 | + }) |
| 44 | +} |
| 45 | + |
| 46 | +func testAccCSEAutomationDestroy(s *terraform.State) error { |
| 47 | + client := testAccProvider.Meta().(*Client) |
| 48 | + |
| 49 | + for _, rs := range s.RootModule().Resources { |
| 50 | + if rs.Type != "sumologic_cse_custom_entity_type" { |
| 51 | + continue |
| 52 | + } |
| 53 | + |
| 54 | + if rs.Primary.ID == "" { |
| 55 | + return fmt.Errorf("CSE Custom Entity Type Config destruction check: CSE Custom Entity Type Config ID is not set") |
| 56 | + } |
| 57 | + |
| 58 | + s, err := client.GetCSEAutomation(rs.Primary.ID) |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("Encountered an error: " + err.Error()) |
| 61 | + } |
| 62 | + if s != nil { |
| 63 | + return fmt.Errorf("entity Custom Entity Type still exists") |
| 64 | + } |
| 65 | + } |
| 66 | + return nil |
| 67 | +} |
| 68 | + |
| 69 | +func testCreateCSEAutomationConfig(nPlaybookId string, nCseResourceType string, nExecutionTypes []string, nEnabled bool) string { |
| 70 | + |
| 71 | + return fmt.Sprintf(` |
| 72 | +resource "sumologic_cse_automation" "automation" { |
| 73 | + playbook_id = "%s" |
| 74 | + cse_resource_type = "%s" |
| 75 | + execution_types = ["%s"] |
| 76 | + enabled = "%t" |
| 77 | +} |
| 78 | +`, nPlaybookId, nCseResourceType, nExecutionTypes[0], nEnabled) |
| 79 | +} |
| 80 | + |
| 81 | +func testCheckCSEAutomationExists(n string, Automation *CSEAutomation) resource.TestCheckFunc { |
| 82 | + return func(s *terraform.State) error { |
| 83 | + rs, ok := s.RootModule().Resources[n] |
| 84 | + if !ok { |
| 85 | + return fmt.Errorf("not found: %s", n) |
| 86 | + } |
| 87 | + |
| 88 | + if rs.Primary.ID == "" { |
| 89 | + return fmt.Errorf("entity Custom Entity Type ID is not set") |
| 90 | + } |
| 91 | + |
| 92 | + c := testAccProvider.Meta().(*Client) |
| 93 | + AutomationResp, err := c.GetCSEAutomation(rs.Primary.ID) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + |
| 98 | + *Automation = *AutomationResp |
| 99 | + |
| 100 | + return nil |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func testCheckAutomationValues(Automation *CSEAutomation, nPlaybookId string, nCseResourceType string, nExecutionTypes []string, nEnabled bool) resource.TestCheckFunc { |
| 105 | + return func(s *terraform.State) error { |
| 106 | + if Automation.PlaybookId != nPlaybookId { |
| 107 | + return fmt.Errorf("bad playbook_id, expected \"%s\", got: %#v", nPlaybookId, Automation.PlaybookId) |
| 108 | + } |
| 109 | + if Automation.CseResourceType != nCseResourceType { |
| 110 | + return fmt.Errorf("bad cse_resource_type, expected \"%s\", got: %#v", nCseResourceType, Automation.CseResourceType) |
| 111 | + } |
| 112 | + if Automation.ExecutionTypes != nil { |
| 113 | + if len(Automation.ExecutionTypes) != len(nExecutionTypes) { |
| 114 | + return fmt.Errorf("bad execution_types list, expected \"%d\", got: %d", len(nExecutionTypes), len(Automation.ExecutionTypes)) |
| 115 | + } |
| 116 | + if Automation.ExecutionTypes[0] != nExecutionTypes[0] { |
| 117 | + return fmt.Errorf("bad execution_types in list, expected \"%s\", got: %s", nExecutionTypes[0], Automation.ExecutionTypes[0]) |
| 118 | + } |
| 119 | + } |
| 120 | + if Automation.Enabled != nEnabled { |
| 121 | + return fmt.Errorf("bad enabled field, expected \"%t\", got: %#v", nEnabled, Automation.Enabled) |
| 122 | + } |
| 123 | + |
| 124 | + return nil |
| 125 | + } |
| 126 | +} |
0 commit comments