|
| 1 | +// ---------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** |
| 4 | +// |
| 5 | +// ---------------------------------------------------------------------------- |
| 6 | +// |
| 7 | +// This file is automatically generated by Sumo Logic and manual |
| 8 | +// changes will be clobbered when the file is regenerated. Do not submit |
| 9 | +// changes to this file. |
| 10 | +// |
| 11 | +// ---------------------------------------------------------------------------- |
| 12 | +package sumologic |
| 13 | + |
| 14 | +import ( |
| 15 | + "fmt" |
| 16 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 17 | + "github.com/hashicorp/terraform-plugin-sdk/terraform" |
| 18 | + "strconv" |
| 19 | + "strings" |
| 20 | + "testing" |
| 21 | +) |
| 22 | + |
| 23 | +func TestAccSumologicSourceTemplate_basic(t *testing.T) { |
| 24 | + var sourceTemplate SourceTemplate |
| 25 | + testSchemaRef := "type = \"Mac2\" \n version = \"1.0.2\" " |
| 26 | + |
| 27 | + testSelector := |
| 28 | + "tags = [\n[\n{\n key = \"tag\"\n values= [\"Value\"]\n}\n]\n] \n names = [\"TestCollector1\"]" |
| 29 | + |
| 30 | + testConfig := "apache.yaml.example" |
| 31 | + testInputJson := "jsonencode({\n\"name\": \"hostmetrics_test_source_template_acc\",\n\"description\": \"Host metric source\" ,\n\"receivers\": {\n\"hostmetrics\": {\n\"receiverType\": \"hostmetrics\",\n\"collection_interval\": \"5m\",\n\"cpu_scraper_enabled\": true,\n\"disk_scraper_enabled\": true,\n\"load_scraper_enabled\": true,\n\"filesystem_scraper_enabled\": true,\n\"memory_scraper_enabled\": true,\n\"network_scraper_enabled\": true,\n\"process_scraper_enabled\": true,\n\"paging_scraper_enabled\": true\n}\n},\n\"processors\": {\n\"resource\": {\n\"processorType\": \"resource\",\n\"user_attributes\": [\n{\n\"key\": \"_sourceCategory\",\n\"value\": \"otel/host\"\n}\n],\n\"default_attributes\": [\n{\n\"key\": \"sumo.datasource\",\n\"value\": \"apache\"\n},\n]\n}\n}\n})" |
| 32 | + |
| 33 | + resource.Test(t, resource.TestCase{ |
| 34 | + PreCheck: func() { testAccPreCheck(t) }, |
| 35 | + Providers: testAccProviders, |
| 36 | + CheckDestroy: testAccCheckSourceTemplateDestroy(sourceTemplate), |
| 37 | + Steps: []resource.TestStep{ |
| 38 | + { |
| 39 | + Config: testAccCheckSumologicSourceTemplateConfigImported(testSchemaRef, testSelector, testConfig, testInputJson), |
| 40 | + }, |
| 41 | + { |
| 42 | + ResourceName: "sumologic_source_template.foo", |
| 43 | + ImportState: true, |
| 44 | + ImportStateVerify: true, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }) |
| 48 | +} |
| 49 | + |
| 50 | +func TestAccSumologicSourceTemplate_create(t *testing.T) { |
| 51 | + var sourceTemplate SourceTemplate |
| 52 | + testSchemaRef := "type = \"Mac2\" \n version = \"1.0.2\" " |
| 53 | + testSelector := "tags = [\n[\n{\n key = \"tag\"\n values= [\"Value\"]\n}\n]\n]\n names = [\"TestCollector1\"]" |
| 54 | + |
| 55 | + testInputJson := "jsonencode({\n\"name\": \"hostmetrics_test_source_template_acc\",\n\"description\": \"Host metric source\" ,\n\"receivers\": {\n\"hostmetrics\": {\n\"receiverType\": \"hostmetrics\",\n\"collection_interval\": \"5m\",\n\"cpu_scraper_enabled\": true,\n\"disk_scraper_enabled\": true,\n\"load_scraper_enabled\": true,\n\"filesystem_scraper_enabled\": true,\n\"memory_scraper_enabled\": true,\n\"network_scraper_enabled\": true,\n\"process_scraper_enabled\": true,\n\"paging_scraper_enabled\": true\n}\n},\n\"processors\": {\n\"resource\": {\n\"processorType\": \"resource\",\n\"user_attributes\": [\n{\n\"key\": \"_sourceCategory\",\n\"value\": \"otel/host\"\n}\n],\n\"default_attributes\": [\n{\n\"key\": \"sumo.datasource\",\n\"value\": \"apache\"\n},\n]\n}\n}\n})" |
| 56 | + |
| 57 | + resource.Test(t, resource.TestCase{ |
| 58 | + PreCheck: func() { testAccPreCheck(t) }, |
| 59 | + Providers: testAccProviders, |
| 60 | + CheckDestroy: testAccCheckSourceTemplateDestroy(sourceTemplate), |
| 61 | + Steps: []resource.TestStep{ |
| 62 | + { |
| 63 | + Config: testAccSumologicSourceTemplate(testSchemaRef, testSelector, testInputJson), |
| 64 | + Check: resource.ComposeTestCheckFunc( |
| 65 | + testAccCheckSourceTemplateExists("sumologic_source_template.test", &sourceTemplate, t), |
| 66 | + testAccCheckSourceTemplateAttributes("sumologic_source_template.test"), |
| 67 | + resource.TestCheckResourceAttr("sumologic_source_template.test", "schema_ref.0.type", "Mac2"), |
| 68 | + resource.TestCheckResourceAttr("sumologic_source_template.test", "schema_ref.0.version", "1.0.2"), |
| 69 | + resource.TestCheckResourceAttr("sumologic_source_template.test", "selector.0.names.0", "TestCollector1"), |
| 70 | + ), |
| 71 | + }, |
| 72 | + }, |
| 73 | + }) |
| 74 | +} |
| 75 | + |
| 76 | +func TestAccSumologicSourceTemplate_update(t *testing.T) { |
| 77 | + var sourceTemplate SourceTemplate |
| 78 | + |
| 79 | + testSchemaRef := "type = \"Mac2\" \n version = \"1.0.2\" " |
| 80 | + testSelector := "tags = [\n[\n{\n key = \"tag\"\n values= [\"Value\"]\n}\n]\n]" |
| 81 | + testInputJson := "jsonencode({\n\"name\": \"hostmetrics_test_source_template_acc\",\n\"description\": \"Host metric source\" ,\n\"receivers\": {\n\"hostmetrics\": {\n\"receiverType\": \"hostmetrics\",\n\"collection_interval\": \"5m\",\n\"cpu_scraper_enabled\": true,\n\"disk_scraper_enabled\": true,\n\"load_scraper_enabled\": true,\n\"filesystem_scraper_enabled\": true,\n\"memory_scraper_enabled\": true,\n\"network_scraper_enabled\": true,\n\"process_scraper_enabled\": true,\n\"paging_scraper_enabled\": true\n}\n},\n\"processors\": {\n\"resource\": {\n\"processorType\": \"resource\",\n\"user_attributes\": [\n{\n\"key\": \"_sourceCategory\",\n\"value\": \"otel/host\"\n}\n],\n\"default_attributes\": [\n{\n\"key\": \"sumo.datasource\",\n\"value\": \"apache\"\n},\n]\n}\n}\n})" |
| 82 | + |
| 83 | + testUpdatedSchemaRef := "type = \"Mac2\" \n version = \"1.0.2\" " |
| 84 | + testUpdatedSelector := "tags = [\n[\n{\n key = \"updatedTag\"\n values= [\"Value\"]\n}\n]\n] \n names = [\"TestCollector1\"]" |
| 85 | + testUpdatedInputJson := "jsonencode({\n\"name\": \"hostmetrics_test_source_template_acc\",\n\"description\": \"Host metric source\" ,\n\"receivers\": {\n\"hostmetrics\": {\n\"receiverType\": \"hostmetrics\",\n\"collection_interval\": \"5m\",\n\"cpu_scraper_enabled\": true,\n\"disk_scraper_enabled\": true,\n\"load_scraper_enabled\": true,\n\"filesystem_scraper_enabled\": true,\n\"memory_scraper_enabled\": true,\n\"network_scraper_enabled\": true,\n\"process_scraper_enabled\": true,\n\"paging_scraper_enabled\": true\n}\n},\n\"processors\": {\n\"resource\": {\n\"processorType\": \"resource\",\n\"user_attributes\": [\n{\n\"key\": \"_sourceCategory\",\n\"value\": \"otel/hostupdated\"\n}\n],\n\"default_attributes\": [\n{\n\"key\": \"sumo.datasource\",\n\"value\": \"apache\"\n},\n]\n}\n}\n})" |
| 86 | + |
| 87 | + resource.Test(t, resource.TestCase{ |
| 88 | + PreCheck: func() { testAccPreCheck(t) }, |
| 89 | + Providers: testAccProviders, |
| 90 | + CheckDestroy: testAccCheckSourceTemplateDestroy(sourceTemplate), |
| 91 | + Steps: []resource.TestStep{ |
| 92 | + { |
| 93 | + Config: testAccSumologicSourceTemplate(testSchemaRef, testSelector, testInputJson), |
| 94 | + Check: resource.ComposeTestCheckFunc( |
| 95 | + testAccCheckSourceTemplateExists("sumologic_source_template.test", &sourceTemplate, t), |
| 96 | + testAccCheckSourceTemplateAttributes("sumologic_source_template.test"), |
| 97 | + resource.TestCheckResourceAttr("sumologic_source_template.test", "schema_ref.0.type", "Mac2"), |
| 98 | + resource.TestCheckResourceAttr("sumologic_source_template.test", "schema_ref.0.version", "1.0.2"), |
| 99 | + resource.TestCheckResourceAttr("sumologic_source_template.test", "selector.0.tags.0.0.key", "tag"), |
| 100 | + ), |
| 101 | + }, |
| 102 | + { |
| 103 | + Config: testAccSumologicSourceTemplateUpdate(testUpdatedSchemaRef, testUpdatedSelector, testUpdatedInputJson), |
| 104 | + Check: resource.ComposeTestCheckFunc( |
| 105 | + resource.TestCheckResourceAttr("sumologic_source_template.test", "schema_ref.0.type", "Mac2"), |
| 106 | + resource.TestCheckResourceAttr("sumologic_source_template.test", "schema_ref.0.version", "1.0.2"), |
| 107 | + resource.TestCheckResourceAttr("sumologic_source_template.test", "selector.0.tags.0.0.key", "updatedTag"), |
| 108 | + ), |
| 109 | + }, |
| 110 | + }, |
| 111 | + }) |
| 112 | +} |
| 113 | + |
| 114 | +func testAccCheckSourceTemplateDestroy(sourceTemplate SourceTemplate) resource.TestCheckFunc { |
| 115 | + return func(s *terraform.State) error { |
| 116 | + client := testAccProvider.Meta().(*Client) |
| 117 | + for _, r := range s.RootModule().Resources { |
| 118 | + id := r.Primary.ID |
| 119 | + u, err := client.GetSourceTemplate(id) |
| 120 | + if err != nil { |
| 121 | + return fmt.Errorf("Encountered an error: " + err.Error()) |
| 122 | + } |
| 123 | + if u != nil { |
| 124 | + return fmt.Errorf("SourceTemplate %s still exists", id) |
| 125 | + } |
| 126 | + } |
| 127 | + return nil |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func testAccCheckSourceTemplateExists(name string, sourceTemplate *SourceTemplate, t *testing.T) resource.TestCheckFunc { |
| 132 | + return func(s *terraform.State) error { |
| 133 | + rs, ok := s.RootModule().Resources[name] |
| 134 | + if !ok { |
| 135 | + //need this so that we don't get an unused import error for strconv in some cases |
| 136 | + return fmt.Errorf("Error = %s. SourceTemplate not found: %s", strconv.FormatBool(ok), name) |
| 137 | + } |
| 138 | + |
| 139 | + //need this so that we don't get an unused import error for strings in some cases |
| 140 | + if strings.EqualFold(rs.Primary.ID, "") { |
| 141 | + return fmt.Errorf("SourceTemplate ID is not set") |
| 142 | + } |
| 143 | + |
| 144 | + id := rs.Primary.ID |
| 145 | + client := testAccProvider.Meta().(*Client) |
| 146 | + newSourceTemplate, err := client.GetSourceTemplate(id) |
| 147 | + if err != nil { |
| 148 | + return fmt.Errorf("SourceTemplate %s not found", id) |
| 149 | + } |
| 150 | + sourceTemplate = newSourceTemplate |
| 151 | + return nil |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +func testAccCheckSumologicSourceTemplateConfigImported(schemaRef string, selector string, config string, inputJson string) string { |
| 156 | + return fmt.Sprintf(` |
| 157 | +resource "sumologic_source_template" "foo" { |
| 158 | + schema_ref { |
| 159 | + %s |
| 160 | + } |
| 161 | + selector { |
| 162 | + %s |
| 163 | + } |
| 164 | +
|
| 165 | + input_json = %s |
| 166 | +} |
| 167 | +`, schemaRef, selector, inputJson) |
| 168 | +} |
| 169 | + |
| 170 | +func testAccSumologicSourceTemplate(schemaRef string, selector string, inputJson string) string { |
| 171 | + return fmt.Sprintf(` |
| 172 | +resource "sumologic_source_template" "test" { |
| 173 | + schema_ref { |
| 174 | + %s |
| 175 | + } |
| 176 | + selector { |
| 177 | + %s |
| 178 | + } |
| 179 | + input_json = %s |
| 180 | +} |
| 181 | +`, schemaRef, selector, inputJson) |
| 182 | +} |
| 183 | + |
| 184 | +func testAccSumologicSourceTemplateUpdate(schemaRef string, selector string, inputJson string) string { |
| 185 | + return fmt.Sprintf(` |
| 186 | +resource "sumologic_source_template" "test" { |
| 187 | + schema_ref { |
| 188 | + %s |
| 189 | + } |
| 190 | + selector { |
| 191 | + %s |
| 192 | + } |
| 193 | + input_json = %s |
| 194 | +} |
| 195 | +`, schemaRef, selector, inputJson) |
| 196 | +} |
| 197 | + |
| 198 | +func testAccCheckSourceTemplateAttributes(name string) resource.TestCheckFunc { |
| 199 | + return func(s *terraform.State) error { |
| 200 | + f := resource.ComposeTestCheckFunc( |
| 201 | + resource.TestCheckResourceAttrSet(name, "total_collector_linked"), |
| 202 | + resource.TestCheckResourceAttrSet(name, "input_json"), |
| 203 | + resource.TestCheckResourceAttrSet(name, "config"), |
| 204 | + resource.TestCheckResourceAttrSet(name, "total_collector_linked"), |
| 205 | + resource.TestCheckResourceAttrSet(name, "modified_at"), |
| 206 | + resource.TestCheckResourceAttrSet(name, "modified_by"), |
| 207 | + resource.TestCheckResourceAttrSet(name, "created_at"), |
| 208 | + resource.TestCheckResourceAttrSet(name, "created_by"), |
| 209 | + ) |
| 210 | + return f(s) |
| 211 | + } |
| 212 | +} |
0 commit comments