Skip to content

Commit 436e6c5

Browse files
authored
Merge pull request #275 from SumoLogic/APP-1057_custom_entity_types-entity_criticality
Add support for CSE Custom Entity Type and Entity Criticality Config
2 parents a525168 + 27a8831 commit 436e6c5

10 files changed

+762
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
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)

sumologic/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ func Provider() terraform.ResourceProvider {
4545
ResourcesMap: map[string]*schema.Resource{
4646
"sumologic_cse_rule_tuning_expression": resourceSumologicCSERuleTuningExpression(),
4747
"sumologic_cse_network_block": resourceSumologicCSENetworkBlock(),
48+
"sumologic_cse_custom_entity_type": resourceSumologicCSECustomEntityType(),
49+
"sumologic_cse_entity_criticality_config": resourceSumologicCSEEntityCriticalityConfig(),
4850
"sumologic_cse_insights_configuration": resourceSumologicCSEInsightsConfiguration(),
4951
"sumologic_cse_insights_resolution": resourceSumologicCSEInsightsResolution(),
5052
"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+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package sumologic
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
5+
"log"
6+
)
7+
8+
func resourceSumologicCSEEntityCriticalityConfig() *schema.Resource {
9+
return &schema.Resource{
10+
Create: resourceSumologicCSEEntityCriticalityConfigCreate,
11+
Read: resourceSumologicCSEEntityCriticalityConfigRead,
12+
Delete: resourceSumologicCSEEntityCriticalityConfigDelete,
13+
Update: resourceSumologicCSEEntityCriticalityConfigUpdate,
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+
"severity_expression": {
24+
Type: schema.TypeString,
25+
Required: true,
26+
},
27+
},
28+
}
29+
}
30+
31+
func resourceSumologicCSEEntityCriticalityConfigRead(d *schema.ResourceData, meta interface{}) error {
32+
c := meta.(*Client)
33+
34+
var CSEEntityCriticalityConfig *CSEEntityCriticalityConfig
35+
id := d.Id()
36+
37+
CSEEntityCriticalityConfig, err := c.GetCSEEntityCriticalityConfig(id)
38+
if err != nil {
39+
log.Printf("[WARN] CSE Entity Criticality Config not found when looking by id: %s, err: %v", id, err)
40+
41+
}
42+
43+
if CSEEntityCriticalityConfig == nil {
44+
log.Printf("[WARN] CSE Entity Criticality Config not found, removing from state: %v - %v", id, err)
45+
d.SetId("")
46+
return nil
47+
}
48+
49+
d.Set("name", CSEEntityCriticalityConfig.Name)
50+
d.Set("severity_expression", CSEEntityCriticalityConfig.SeverityExpression)
51+
52+
return nil
53+
}
54+
55+
func resourceSumologicCSEEntityCriticalityConfigDelete(d *schema.ResourceData, meta interface{}) error {
56+
c := meta.(*Client)
57+
58+
return c.DeleteCSEEntityCriticalityConfig(d.Id())
59+
60+
}
61+
62+
func resourceSumologicCSEEntityCriticalityConfigCreate(d *schema.ResourceData, meta interface{}) error {
63+
c := meta.(*Client)
64+
65+
if d.Id() == "" {
66+
id, err := c.CreateCSEEntityCriticalityConfig(CSEEntityCriticalityConfig{
67+
Name: d.Get("name").(string),
68+
SeverityExpression: d.Get("severity_expression").(string),
69+
})
70+
71+
if err != nil {
72+
return err
73+
}
74+
75+
d.SetId(id)
76+
}
77+
78+
return resourceSumologicCSEEntityCriticalityConfigRead(d, meta)
79+
}
80+
81+
func resourceSumologicCSEEntityCriticalityConfigUpdate(d *schema.ResourceData, meta interface{}) error {
82+
CSEEntityCriticalityConfig, err := resourceToCSEEntityCriticalityConfig(d)
83+
if err != nil {
84+
return err
85+
}
86+
87+
c := meta.(*Client)
88+
if err = c.UpdateCSEEntityCriticalityConfig(CSEEntityCriticalityConfig); err != nil {
89+
return err
90+
}
91+
92+
return resourceSumologicCSEEntityCriticalityConfigRead(d, meta)
93+
}
94+
95+
func resourceToCSEEntityCriticalityConfig(d *schema.ResourceData) (CSEEntityCriticalityConfig, error) {
96+
id := d.Id()
97+
if id == "" {
98+
return CSEEntityCriticalityConfig{}, nil
99+
}
100+
101+
return CSEEntityCriticalityConfig{
102+
ID: id,
103+
SeverityExpression: d.Get("severity_expression").(string),
104+
Name: d.Get("name").(string),
105+
}, nil
106+
}

0 commit comments

Comments
 (0)