Skip to content

Commit 3e3e77d

Browse files
author
Pedro Montiel
committed
Merge branch 'master' into APP-1058_log-mappings
2 parents 57742bd + c4f33fe commit 3e3e77d

13 files changed

+853
-43
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
FEATURES:
66

77
* **New Resource:** sumologic_cse_rule_tuning_expression (GH-281)
8+
* **New Resource:** sumologic_cse_entity_criticality_config (GH-275)
9+
* **New Resource:** sumologic_cse_custom_entity_type (GH-275)
810
* **New Resource:** sumologic_cse_insights_resolution (GH-274)
911
* **New Resource:** sumologic_cse_insights_status (GH-274)
1012
* **New Resource:** sumologic_cse_insights_configuration (GH-274)
1113
* **New Resource:** sumologic_cse_log_mapping (GH-284)
1214
* **New Datasource:** sumologic_cse_log_mapping_vendor_product (GH-284)
1315

16+
BUG FIXES:
17+
18+
* Fix hierarchy without a filter not being accepted
1419

1520
## 2.10.0 (September 22, 2021)
1621

sumologic/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ func Provider() terraform.ResourceProvider {
4646
"sumologic_cse_log_mapping": resourceSumologicCSELogMapping(),
4747
"sumologic_cse_rule_tuning_expression": resourceSumologicCSERuleTuningExpression(),
4848
"sumologic_cse_network_block": resourceSumologicCSENetworkBlock(),
49+
"sumologic_cse_custom_entity_type": resourceSumologicCSECustomEntityType(),
50+
"sumologic_cse_entity_criticality_config": resourceSumologicCSEEntityCriticalityConfig(),
4951
"sumologic_cse_insights_configuration": resourceSumologicCSEInsightsConfiguration(),
5052
"sumologic_cse_insights_resolution": resourceSumologicCSEInsightsResolution(),
5153
"sumologic_cse_insights_status": resourceSumologicCSEInsightsStatus(),
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package sumologic
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
5+
"log"
6+
)
7+
8+
func resourceSumologicCSECustomEntityType() *schema.Resource {
9+
return &schema.Resource{
10+
Create: resourceSumologicCSECustomEntityTypeCreate,
11+
Read: resourceSumologicCSECustomEntityTypeRead,
12+
Delete: resourceSumologicCSECustomEntityTypeDelete,
13+
Update: resourceSumologicCSECustomEntityTypeUpdate,
14+
Importer: &schema.ResourceImporter{
15+
State: schema.ImportStatePassthrough,
16+
},
17+
18+
Schema: map[string]*schema.Schema{
19+
"name": {
20+
Type: schema.TypeString,
21+
Required: true,
22+
},
23+
"identifier": {
24+
Type: schema.TypeString,
25+
Required: true,
26+
},
27+
"fields": {
28+
Type: schema.TypeList,
29+
Required: true,
30+
Elem: &schema.Schema{
31+
Type: schema.TypeString,
32+
},
33+
},
34+
},
35+
}
36+
}
37+
38+
func resourceSumologicCSECustomEntityTypeRead(d *schema.ResourceData, meta interface{}) error {
39+
c := meta.(*Client)
40+
41+
var CSECustomEntityType *CSECustomEntityType
42+
id := d.Id()
43+
44+
CSECustomEntityType, err := c.GetCSECustomEntityType(id)
45+
if err != nil {
46+
log.Printf("[WARN] CSE Custom Entity Type not found when looking by id: %s, err: %v", id, err)
47+
48+
}
49+
50+
if CSECustomEntityType == nil {
51+
log.Printf("[WARN] CSE Custom Entity Type not found, removing from state: %v - %v", id, err)
52+
d.SetId("")
53+
return nil
54+
}
55+
56+
d.Set("name", CSECustomEntityType.Name)
57+
d.Set("identifier", CSECustomEntityType.Identifier)
58+
d.Set("fields", CSECustomEntityType.Fields)
59+
60+
return nil
61+
}
62+
63+
func resourceSumologicCSECustomEntityTypeDelete(d *schema.ResourceData, meta interface{}) error {
64+
c := meta.(*Client)
65+
66+
return c.DeleteCSECustomEntityType(d.Id())
67+
68+
}
69+
70+
func resourceSumologicCSECustomEntityTypeCreate(d *schema.ResourceData, meta interface{}) error {
71+
c := meta.(*Client)
72+
73+
if d.Id() == "" {
74+
id, err := c.CreateCSECustomEntityType(CSECustomEntityType{
75+
Name: d.Get("name").(string),
76+
Identifier: d.Get("identifier").(string),
77+
Fields: resourceFieldsToStringArray(d.Get("fields").([]interface{})),
78+
})
79+
80+
if err != nil {
81+
return err
82+
}
83+
84+
d.SetId(id)
85+
}
86+
87+
return resourceSumologicCSECustomEntityTypeRead(d, meta)
88+
}
89+
90+
func resourceSumologicCSECustomEntityTypeUpdate(d *schema.ResourceData, meta interface{}) error {
91+
CSECustomEntityType, err := resourceToCSECustomEntityType(d)
92+
if err != nil {
93+
return err
94+
}
95+
96+
c := meta.(*Client)
97+
if err = c.UpdateCSECustomEntityType(CSECustomEntityType); err != nil {
98+
return err
99+
}
100+
101+
return resourceSumologicCSECustomEntityTypeRead(d, meta)
102+
}
103+
104+
func resourceToCSECustomEntityType(d *schema.ResourceData) (CSECustomEntityType, error) {
105+
id := d.Id()
106+
if id == "" {
107+
return CSECustomEntityType{}, nil
108+
}
109+
110+
return CSECustomEntityType{
111+
ID: id,
112+
Name: d.Get("name").(string),
113+
Identifier: d.Get("identifier").(string),
114+
Fields: resourceFieldsToStringArray(d.Get("fields").([]interface{})),
115+
}, nil
116+
}
117+
118+
func resourceFieldsToStringArray(resourceFields []interface{}) []string {
119+
fields := make([]string, len(resourceFields))
120+
121+
for i, field := range resourceFields {
122+
fields[i] = field.(string)
123+
}
124+
125+
return fields
126+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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

Comments
 (0)