Skip to content

Commit 37d1491

Browse files
committed
Adding Acc tests and terraform docs
1 parent fac3210 commit 37d1491

File tree

5 files changed

+381
-2
lines changed

5 files changed

+381
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## X.Y.Z (Unreleased)
22
* Add new change notes here
3+
FEATURES:
4+
* **New Resource:** sumologic_source_template (GH-708)
5+
* **New Resource:** sumologic_ot_collector (GH-708)
36

47
## 3.0.0 (December 09, 2024)
58
**REMOVALS:**

sumologic/resource_sumologic_source_template.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func resourceSumologicSourceTemplate() *schema.Resource {
129129

130130
"config": {
131131
Type: schema.TypeString,
132-
Optional: true,
132+
Computed: true,
133133
},
134134

135135
"input_json": {
@@ -291,7 +291,6 @@ func resourceToOtTag(data interface{}) []OtTag {
291291
for i, v := range otTagSlice {
292292
otTagObj := v.(map[string]interface{})
293293
otTag[i].Key = otTagObj["key"].(string)
294-
log.Println("ot tag key: ", otTag[i].Key)
295294

296295
valuesData := otTagObj["values"].([]interface{})
297296
values := make([]string, len(valuesData))
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
layout: "sumologic"
3+
page_title: "SumoLogic: sumologic_ot_collector"
4+
description: |-
5+
Provides a Sumologic OT Collector
6+
---
7+
8+
# sumologic_OT_Collector
9+
Provides a [Sumologic OT Collector][1]
10+
**NOTE**: This resource can only be imported and deleted by terraform. Creation/Updation of this resource via terrform is not supported.
11+
Tag Edit functionality will be supported in future.
12+
13+
## Example Usage
14+
```hcl
15+
resource "sumologic_ot_collector" "example_ot_collector" {
16+
description = "Testing OT collector using terraform"
17+
time_zone = "UTC"
18+
category = "apache"
19+
is_remotely_managed = "true"
20+
ephemeral = "false"
21+
name = "test OT Collector"
22+
}
23+
```
24+
## Argument reference
25+
26+
The following arguments are supported:
27+
28+
- `description` - (Optional) Description of the OT Collector.
29+
- `time_zone` - (Optional) The time zone to use for this collector. The value follows the [tzdata][4] naming convention.
30+
- `category` - (Optional) The default source category for any source attached to this collector. Can be overridden in the configuration of said sources.
31+
- `is_remotely_managed` - (Optional) Management Status of the OT Collector based on if it is remotely or locally managed.
32+
- `ephemeral` - (Optional) When true, the collector will be deleted after 12 hours of inactivity. For more information, see [Setting a Collector as Ephemeral][2].
33+
- `name` - (Required) Name of the OT Collector.
34+
- `tags` - (Optional) Map containing [key/value pairs][3].
35+
36+
The following attributes are exported:
37+
38+
- `id` - The internal ID of the OT collector
39+
40+
## Import
41+
OT Collectors can be imported using the collector id, e.g.:
42+
43+
```hcl
44+
terraform import sumologic_ot_collector.test 00005AF3107A4007
45+
```
46+
47+
[1]: https://help.sumologic.com/docs/send-data/opentelemetry-collector
48+
[2]: https://help.sumologic.com/03Send-Data/Installed-Collectors/05Reference-Information-for-Collector-Installation/11Set-a-Collector-as-Ephemeral
49+
[3]: https://help.sumologic.com/Manage/Fields
50+
[4]: https://en.wikipedia.org/wiki/Tz_database

0 commit comments

Comments
 (0)