|
| 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 TestAccSumologicSCEContextAction_create_update(t *testing.T) { |
| 12 | + SkipCseTest(t) |
| 13 | + |
| 14 | + var ContextAction CSEContextAction |
| 15 | + nName := "Test Context Action" |
| 16 | + nType := "URL" |
| 17 | + nTemplate := "https://bar.com/?q={{value}}" |
| 18 | + nIocTypes := []string{"IP_ADDRESS"} |
| 19 | + nEntityTypes := []string{"_hostname"} |
| 20 | + nRecordFields := []string{"request_url"} |
| 21 | + nAllRecordFields := false |
| 22 | + nEnabled := true |
| 23 | + uIocTypes := []string{"MAC_ADDRESS"} |
| 24 | + uEnabled := false |
| 25 | + resourceName := "sumologic_cse_context_action.context_action" |
| 26 | + resource.Test(t, resource.TestCase{ |
| 27 | + PreCheck: func() { testAccPreCheck(t) }, |
| 28 | + Providers: testAccProviders, |
| 29 | + CheckDestroy: testAccCSEContextActionDestroy, |
| 30 | + Steps: []resource.TestStep{ |
| 31 | + { |
| 32 | + Config: testCreateCSEContextActionConfig(nName, nType, nTemplate, nIocTypes, nEntityTypes, nRecordFields, nAllRecordFields, nEnabled), |
| 33 | + Check: resource.ComposeTestCheckFunc( |
| 34 | + testCheckCSEContextActionExists(resourceName, &ContextAction), |
| 35 | + testCheckContextActionValues(&ContextAction, nName, nType, nTemplate, nIocTypes, nEntityTypes, nRecordFields, nAllRecordFields, nEnabled), |
| 36 | + resource.TestCheckResourceAttrSet(resourceName, "id"), |
| 37 | + ), |
| 38 | + }, |
| 39 | + { |
| 40 | + Config: testCreateCSEContextActionConfig(nName, nType, nTemplate, uIocTypes, nEntityTypes, nRecordFields, nAllRecordFields, uEnabled), |
| 41 | + Check: resource.ComposeTestCheckFunc( |
| 42 | + testCheckCSEContextActionExists(resourceName, &ContextAction), |
| 43 | + testCheckContextActionValues(&ContextAction, nName, nType, nTemplate, uIocTypes, nEntityTypes, nRecordFields, nAllRecordFields, uEnabled), |
| 44 | + ), |
| 45 | + }, |
| 46 | + }, |
| 47 | + }) |
| 48 | +} |
| 49 | + |
| 50 | +func testAccCSEContextActionDestroy(s *terraform.State) error { |
| 51 | + client := testAccProvider.Meta().(*Client) |
| 52 | + |
| 53 | + for _, rs := range s.RootModule().Resources { |
| 54 | + if rs.Type != "sumologic_cse_context_action" { |
| 55 | + continue |
| 56 | + } |
| 57 | + |
| 58 | + if rs.Primary.ID == "" { |
| 59 | + return fmt.Errorf("CSE Context Action destruction check: CSE Context Action ID is not set") |
| 60 | + } |
| 61 | + |
| 62 | + s, err := client.GetCSEContextAction(rs.Primary.ID) |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("Encountered an error: " + err.Error()) |
| 65 | + } |
| 66 | + if s != nil { |
| 67 | + return fmt.Errorf("Context Action still exists") |
| 68 | + } |
| 69 | + } |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +func testCreateCSEContextActionConfig(nName string, nType string, nTemplate string, nIocTypes []string, nEntityTypes []string, nRecordFields []string, nAllRecordFields bool, nEnabled bool) string { |
| 74 | + |
| 75 | + return fmt.Sprintf(` |
| 76 | +resource "sumologic_cse_context_action" "context_action" { |
| 77 | + name = "%s" |
| 78 | + type = "%s" |
| 79 | + template = "%s" |
| 80 | + ioc_types = ["%s"] |
| 81 | + entity_types = ["%s"] |
| 82 | + record_fields = ["%s"] |
| 83 | + all_record_fields = "%t" |
| 84 | + enabled = "%t" |
| 85 | +} |
| 86 | +`, nName, nType, nTemplate, nIocTypes[0], nEntityTypes[0], nRecordFields[0], nAllRecordFields, nEnabled) |
| 87 | +} |
| 88 | + |
| 89 | +func testCheckCSEContextActionExists(n string, ContextAction *CSEContextAction) resource.TestCheckFunc { |
| 90 | + return func(s *terraform.State) error { |
| 91 | + rs, ok := s.RootModule().Resources[n] |
| 92 | + if !ok { |
| 93 | + return fmt.Errorf("not found: %s", n) |
| 94 | + } |
| 95 | + |
| 96 | + if rs.Primary.ID == "" { |
| 97 | + return fmt.Errorf("Context Action ID is not set") |
| 98 | + } |
| 99 | + |
| 100 | + c := testAccProvider.Meta().(*Client) |
| 101 | + ContextActionResp, err := c.GetCSEContextAction(rs.Primary.ID) |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + *ContextAction = *ContextActionResp |
| 107 | + |
| 108 | + return nil |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func testCheckContextActionValues(ContextAction *CSEContextAction, nName string, nType string, nTemplate string, nIocTypes []string, nEntityTypes []string, nRecordFields []string, nAllRecordFields bool, nEnabled bool) resource.TestCheckFunc { |
| 113 | + return func(s *terraform.State) error { |
| 114 | + if ContextAction.Name != nName { |
| 115 | + return fmt.Errorf("bad name, expected \"%s\", got: %#v", nName, ContextAction.Name) |
| 116 | + } |
| 117 | + if ContextAction.Type != nType { |
| 118 | + return fmt.Errorf("bad type, expected \"%s\", got: %#v", nType, ContextAction.Type) |
| 119 | + } |
| 120 | + if ContextAction.Template != nTemplate { |
| 121 | + return fmt.Errorf("bad template, expected \"%s\", got: %#v", nTemplate, ContextAction.Template) |
| 122 | + } |
| 123 | + if ContextAction.IocTypes != nil { |
| 124 | + if len(ContextAction.IocTypes) != len(nIocTypes) { |
| 125 | + return fmt.Errorf("bad ioc_types list lenght, expected \"%d\", got: %d", len(nIocTypes), len(ContextAction.IocTypes)) |
| 126 | + } |
| 127 | + if ContextAction.IocTypes[0] != nIocTypes[0] { |
| 128 | + return fmt.Errorf("bad ioc_types in list, expected \"%s\", got: %s", nIocTypes[0], ContextAction.IocTypes[0]) |
| 129 | + } |
| 130 | + } |
| 131 | + if ContextAction.EntityTypes != nil { |
| 132 | + if len(ContextAction.EntityTypes) != len(nEntityTypes) { |
| 133 | + return fmt.Errorf("bad entity_types list lenght, expected \"%d\", got: %d", len(nEntityTypes), len(ContextAction.EntityTypes)) |
| 134 | + } |
| 135 | + if ContextAction.EntityTypes[0] != nEntityTypes[0] { |
| 136 | + return fmt.Errorf("bad entity_types in list, expected \"%s\", got: %s", nEntityTypes[0], ContextAction.EntityTypes[0]) |
| 137 | + } |
| 138 | + } |
| 139 | + if ContextAction.RecordFields != nil { |
| 140 | + if len(ContextAction.RecordFields) != len(nRecordFields) { |
| 141 | + return fmt.Errorf("bad record_fields list lenght, expected \"%d\", got: %d", len(nRecordFields), len(ContextAction.RecordFields)) |
| 142 | + } |
| 143 | + if ContextAction.RecordFields[0] != nRecordFields[0] { |
| 144 | + return fmt.Errorf("bad record_fields in list, expected \"%s\", got: %s", nRecordFields[0], ContextAction.RecordFields[0]) |
| 145 | + } |
| 146 | + } |
| 147 | + if ContextAction.AllRecordFields != nAllRecordFields { |
| 148 | + return fmt.Errorf("bad all_record_fields field, expected \"%t\", got: %#v", nAllRecordFields, ContextAction.AllRecordFields) |
| 149 | + } |
| 150 | + if ContextAction.Enabled != nEnabled { |
| 151 | + return fmt.Errorf("bad enabled field, expected \"%t\", got: %#v", nEnabled, ContextAction.Enabled) |
| 152 | + } |
| 153 | + |
| 154 | + return nil |
| 155 | + } |
| 156 | +} |
0 commit comments