|
| 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 TestAccSumologicSCETagSchema_create_update(t *testing.T) { |
| 12 | + SkipCseTest(t) |
| 13 | + |
| 14 | + var TagSchema CSETagSchema |
| 15 | + nKey := "location" |
| 16 | + nLabel := "Label" |
| 17 | + nContentTypes := []string{"entity"} |
| 18 | + nFreeForm := true |
| 19 | + nVOValue := "value" |
| 20 | + nVOLabel := "label" |
| 21 | + nVOLink := "http://foo.bar.com" |
| 22 | + uLabel := "uLabel" |
| 23 | + resourceName := "sumologic_cse_tag_schema.tag_schema" |
| 24 | + resource.Test(t, resource.TestCase{ |
| 25 | + PreCheck: func() { testAccPreCheck(t) }, |
| 26 | + Providers: testAccProviders, |
| 27 | + CheckDestroy: testAccCSETagSchemaDestroy, |
| 28 | + Steps: []resource.TestStep{ |
| 29 | + { |
| 30 | + Config: testCreateCSETagSchemaConfig(nKey, nLabel, nContentTypes, nFreeForm, nVOValue, nVOLabel, nVOLink), |
| 31 | + Check: resource.ComposeTestCheckFunc( |
| 32 | + testCheckCSETagSchemaExists(resourceName, &TagSchema), |
| 33 | + testCheckTagSchemaValues(&TagSchema, nKey, nLabel, nContentTypes, nFreeForm, nVOValue, nVOLabel, nVOLink), |
| 34 | + resource.TestCheckResourceAttrSet(resourceName, "id"), |
| 35 | + ), |
| 36 | + }, |
| 37 | + { |
| 38 | + Config: testCreateCSETagSchemaConfig(nKey, uLabel, nContentTypes, nFreeForm, nVOValue, nVOLabel, nVOLink), |
| 39 | + Check: resource.ComposeTestCheckFunc( |
| 40 | + testCheckCSETagSchemaExists(resourceName, &TagSchema), |
| 41 | + testCheckTagSchemaValues(&TagSchema, nKey, uLabel, nContentTypes, nFreeForm, nVOValue, nVOLabel, nVOLink), |
| 42 | + ), |
| 43 | + }, |
| 44 | + }, |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +func testAccCSETagSchemaDestroy(s *terraform.State) error { |
| 49 | + client := testAccProvider.Meta().(*Client) |
| 50 | + |
| 51 | + for _, rs := range s.RootModule().Resources { |
| 52 | + if rs.Type != "sumologic_cse_tag_schema" { |
| 53 | + continue |
| 54 | + } |
| 55 | + |
| 56 | + if rs.Primary.ID == "" { |
| 57 | + return fmt.Errorf("CSE Tag Schema destruction check: CSE Tag Schema key is not set") |
| 58 | + } |
| 59 | + |
| 60 | + s, err := client.GetCSETagSchema(rs.Primary.ID) |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("Encountered an error: " + err.Error()) |
| 63 | + } |
| 64 | + if s != nil { |
| 65 | + return fmt.Errorf("Tag Schema still exists") |
| 66 | + } |
| 67 | + } |
| 68 | + return nil |
| 69 | +} |
| 70 | + |
| 71 | +func testCreateCSETagSchemaConfig(nKey string, nLabel string, nContentTypes []string, nFreeForm bool, nVOValue string, nVOLabel string, nVOLink string) string { |
| 72 | + |
| 73 | + return fmt.Sprintf(` |
| 74 | +resource "sumologic_cse_tag_schema" "tag_schema" { |
| 75 | + key = "%s" |
| 76 | + label = "%s" |
| 77 | + content_types = ["%s"] |
| 78 | + free_form = "%t" |
| 79 | + value_options { |
| 80 | + value = "%s" |
| 81 | + label = "%s" |
| 82 | + link = "%s" |
| 83 | + } |
| 84 | +} |
| 85 | +`, nKey, nLabel, nContentTypes[0], nFreeForm, nVOValue, nVOLabel, nVOLink) |
| 86 | +} |
| 87 | + |
| 88 | +func testCheckCSETagSchemaExists(n string, TagSchema *CSETagSchema) resource.TestCheckFunc { |
| 89 | + return func(s *terraform.State) error { |
| 90 | + rs, ok := s.RootModule().Resources[n] |
| 91 | + if !ok { |
| 92 | + return fmt.Errorf("not found: %s", n) |
| 93 | + } |
| 94 | + |
| 95 | + if rs.Primary.ID == "" { |
| 96 | + return fmt.Errorf("Tag Schema key is not set") |
| 97 | + } |
| 98 | + |
| 99 | + c := testAccProvider.Meta().(*Client) |
| 100 | + TagSchemaResp, err := c.GetCSETagSchema(rs.Primary.ID) |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + |
| 105 | + *TagSchema = *TagSchemaResp |
| 106 | + |
| 107 | + return nil |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func testCheckTagSchemaValues(TagSchema *CSETagSchema, nKey string, nLabel string, nContentTypes []string, nFreeForm bool, nVOValue string, nVOLabel string, nVOLink string) resource.TestCheckFunc { |
| 112 | + return func(s *terraform.State) error { |
| 113 | + if TagSchema.Key != nKey { |
| 114 | + return fmt.Errorf("bad key, expected \"%s\", got: %#v", nKey, TagSchema.Key) |
| 115 | + } |
| 116 | + if TagSchema.Label != nLabel { |
| 117 | + return fmt.Errorf("bad label, expected \"%s\", got: %#v", nLabel, TagSchema.Label) |
| 118 | + } |
| 119 | + if TagSchema.ContentTypes != nil { |
| 120 | + if len(TagSchema.ContentTypes) != len(nContentTypes) { |
| 121 | + return fmt.Errorf("bad content_types list lenght, expected \"%d\", got: %d", len(nContentTypes), len(TagSchema.ContentTypes)) |
| 122 | + } |
| 123 | + if TagSchema.ContentTypes[0] != nContentTypes[0] { |
| 124 | + return fmt.Errorf("bad content_types in list, expected \"%s\", got: %s", nContentTypes[0], TagSchema.ContentTypes[0]) |
| 125 | + } |
| 126 | + } |
| 127 | + if TagSchema.FreeForm != nFreeForm { |
| 128 | + return fmt.Errorf("bad free_form field, expected \"%t\", got: %#v", nFreeForm, TagSchema.FreeForm) |
| 129 | + } |
| 130 | + if TagSchema.ValueOptionObjects[0].Value != nVOValue { |
| 131 | + return fmt.Errorf("bad value_option.value field, expected \"%s\", got: %#v", nVOValue, TagSchema.ValueOptionObjects[0].Value) |
| 132 | + } |
| 133 | + if TagSchema.ValueOptionObjects[0].Label != nVOLabel { |
| 134 | + return fmt.Errorf("bad value_option.label field, expected \"%s\", got: %#v", nVOLabel, TagSchema.ValueOptionObjects[0].Label) |
| 135 | + } |
| 136 | + if TagSchema.ValueOptionObjects[0].Link != nVOLink { |
| 137 | + return fmt.Errorf("bad value_option.link field, expected \"%s\", got: %#v", nVOLink, TagSchema.ValueOptionObjects[0].Link) |
| 138 | + } |
| 139 | + |
| 140 | + return nil |
| 141 | + } |
| 142 | +} |
0 commit comments