|
| 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 TestAccSumologicSCECustomEntityType_create(t *testing.T) { |
| 12 | + var customEntityType CSECustomEntityType |
| 13 | + nName := "New Custom Entity Type" |
| 14 | + nIdentifier := "identifier" |
| 15 | + nFields := []string{"field1"} |
| 16 | + resourceName := "sumologic_cse_custom_entity_type.custom_entity_type" |
| 17 | + resource.Test(t, resource.TestCase{ |
| 18 | + PreCheck: func() { testAccPreCheck(t) }, |
| 19 | + Providers: testAccProviders, |
| 20 | + CheckDestroy: testAccCSECustomEntityTypeDestroy, |
| 21 | + Steps: []resource.TestStep{ |
| 22 | + { |
| 23 | + Config: testCreateCSECustomEntityTypeConfig(nName, nIdentifier, nFields), |
| 24 | + Check: resource.ComposeTestCheckFunc( |
| 25 | + testCheckCSECustomEntityTypeExists(resourceName, &customEntityType), |
| 26 | + testCheckCustomEntityTypeValues(&customEntityType, nName, nIdentifier, nFields), |
| 27 | + resource.TestCheckResourceAttrSet(resourceName, "id"), |
| 28 | + ), |
| 29 | + }, |
| 30 | + }, |
| 31 | + }) |
| 32 | +} |
| 33 | + |
| 34 | +func TestAccSumologicSCECustomEntityType_update(t *testing.T) { |
| 35 | + var customEntityType CSECustomEntityType |
| 36 | + nName := "New Custom Entity Type" |
| 37 | + nIdentifier := "identifier" |
| 38 | + nFields := []string{"field1"} |
| 39 | + uName := "Changed type" |
| 40 | + uFields := []string{"field2"} |
| 41 | + resourceName := "sumologic_cse_custom_entity_type.custom_entity_type" |
| 42 | + resource.Test(t, resource.TestCase{ |
| 43 | + PreCheck: func() { testAccPreCheck(t) }, |
| 44 | + Providers: testAccProviders, |
| 45 | + CheckDestroy: testAccCSECustomEntityTypeDestroy, |
| 46 | + Steps: []resource.TestStep{ |
| 47 | + { |
| 48 | + Config: testCreateCSECustomEntityTypeConfig(nName, nIdentifier, nFields), |
| 49 | + Check: resource.ComposeTestCheckFunc( |
| 50 | + testCheckCSECustomEntityTypeExists(resourceName, &customEntityType), |
| 51 | + testCheckCustomEntityTypeValues(&customEntityType, nName, nIdentifier, nFields), |
| 52 | + resource.TestCheckResourceAttrSet(resourceName, "id"), |
| 53 | + ), |
| 54 | + }, |
| 55 | + { |
| 56 | + Config: testCreateCSECustomEntityTypeConfig(uName, nIdentifier, uFields), |
| 57 | + Check: resource.ComposeTestCheckFunc( |
| 58 | + testCheckCSECustomEntityTypeExists(resourceName, &customEntityType), |
| 59 | + testCheckCustomEntityTypeValues(&customEntityType, uName, nIdentifier, uFields), |
| 60 | + ), |
| 61 | + }, |
| 62 | + }, |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +func testAccCSECustomEntityTypeDestroy(s *terraform.State) error { |
| 67 | + client := testAccProvider.Meta().(*Client) |
| 68 | + |
| 69 | + for _, rs := range s.RootModule().Resources { |
| 70 | + if rs.Type != "sumologic_cse_custom_entity_type" { |
| 71 | + continue |
| 72 | + } |
| 73 | + |
| 74 | + if rs.Primary.ID == "" { |
| 75 | + return fmt.Errorf("CSE Custom Entity Type Config destruction check: CSE Custom Entity Type Config ID is not set") |
| 76 | + } |
| 77 | + |
| 78 | + s, err := client.GetCSECustomEntityType(rs.Primary.ID) |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("Encountered an error: " + err.Error()) |
| 81 | + } |
| 82 | + if s != nil { |
| 83 | + return fmt.Errorf("entity Custom Entity Type still exists") |
| 84 | + } |
| 85 | + } |
| 86 | + return nil |
| 87 | +} |
| 88 | + |
| 89 | +func testCreateCSECustomEntityTypeConfig(nName string, nIdentifier string, nFields []string) string { |
| 90 | + |
| 91 | + return fmt.Sprintf(` |
| 92 | +resource "sumologic_cse_custom_entity_type" "custom_entity_type" { |
| 93 | + name = "%s" |
| 94 | + identifier = "%s" |
| 95 | + fields = ["%s"] |
| 96 | +} |
| 97 | +`, nName, nIdentifier, nFields[0]) |
| 98 | +} |
| 99 | + |
| 100 | +func testCheckCSECustomEntityTypeExists(n string, customEntityType *CSECustomEntityType) resource.TestCheckFunc { |
| 101 | + return func(s *terraform.State) error { |
| 102 | + rs, ok := s.RootModule().Resources[n] |
| 103 | + if !ok { |
| 104 | + return fmt.Errorf("not found: %s", n) |
| 105 | + } |
| 106 | + |
| 107 | + if rs.Primary.ID == "" { |
| 108 | + return fmt.Errorf("entity Custom Entity Type ID is not set") |
| 109 | + } |
| 110 | + |
| 111 | + c := testAccProvider.Meta().(*Client) |
| 112 | + customEntityTypeResp, err := c.GetCSECustomEntityType(rs.Primary.ID) |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + *customEntityType = *customEntityTypeResp |
| 118 | + |
| 119 | + return nil |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func testCheckCustomEntityTypeValues(customEntityType *CSECustomEntityType, nName string, nIdentifier string, nFields []string) resource.TestCheckFunc { |
| 124 | + return func(s *terraform.State) error { |
| 125 | + if customEntityType.Name != nName { |
| 126 | + return fmt.Errorf("bad name, expected \"%s\", got: %#v", nName, customEntityType.Name) |
| 127 | + } |
| 128 | + if customEntityType.Identifier != nIdentifier { |
| 129 | + return fmt.Errorf("bad identifier, expected \"%s\", got: %#v", nIdentifier, customEntityType.Identifier) |
| 130 | + } |
| 131 | + if customEntityType.Fields != nil { |
| 132 | + if len(customEntityType.Fields) != len(nFields) { |
| 133 | + return fmt.Errorf("bad fields list, expected \"%d\", got: %d", len(nFields), len(customEntityType.Fields)) |
| 134 | + } |
| 135 | + if customEntityType.Fields[0] != nFields[0] { |
| 136 | + return fmt.Errorf("bad field in list, expected \"%s\", got: %s", customEntityType.Fields[0], nFields[0]) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return nil |
| 141 | + } |
| 142 | +} |
0 commit comments