Skip to content

Commit bd9aa74

Browse files
author
Pedro Montiel
committed
INVS-1627: Support cse tag schema
1 parent cae29a9 commit bd9aa74

File tree

7 files changed

+469
-1
lines changed

7 files changed

+469
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## 2.26.1 (Unreleased)
22
FEATURES:
3+
* **New Resource:** sumologic_cse_tag_schema (GH-575)
34
* **New Resource:** sumologic_cse_context_action (GH-573)
45

56
## 2.26.0 (September 7, 2023)

sumologic/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func Provider() terraform.ResourceProvider {
4343
},
4444
},
4545
ResourcesMap: map[string]*schema.Resource{
46+
"sumologic_cse_tag_schema": resourceSumologicCSETagSchema(),
4647
"sumologic_cse_context_action": resourceSumologicCSEContextAction(),
4748
"sumologic_cse_automation": resourceSumologicCSEAutomation(),
4849
"sumologic_cse_entity_normalization_configuration": resourceSumologicCSEEntityNormalizationConfiguration(),
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package sumologic
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
5+
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
6+
"log"
7+
)
8+
9+
func resourceSumologicCSETagSchema() *schema.Resource {
10+
return &schema.Resource{
11+
Create: resourceSumologicCSETagSchemaCreate,
12+
Read: resourceSumologicCSETagSchemaRead,
13+
Delete: resourceSumologicCSETagSchemaDelete,
14+
Update: resourceSumologicCSETagSchemaUpdate,
15+
Importer: &schema.ResourceImporter{
16+
State: schema.ImportStatePassthrough,
17+
},
18+
19+
Schema: map[string]*schema.Schema{
20+
"key": {
21+
Type: schema.TypeString,
22+
Required: true,
23+
},
24+
"label": {
25+
Type: schema.TypeString,
26+
Required: true,
27+
},
28+
"content_types": {
29+
Type: schema.TypeList,
30+
Optional: true,
31+
Elem: &schema.Schema{
32+
Type: schema.TypeString,
33+
ValidateFunc: validation.All(
34+
validation.StringIsNotEmpty,
35+
validation.StringInSlice([]string{"customInsight", "entity", "rule", "threatIntelligence"}, false)),
36+
},
37+
},
38+
"free_form": {
39+
Type: schema.TypeBool,
40+
Required: true,
41+
},
42+
"value_options": getValueOptionsSchema(),
43+
},
44+
}
45+
}
46+
47+
func getValueOptionsSchema() *schema.Schema {
48+
return &schema.Schema{
49+
Type: schema.TypeList,
50+
Optional: true,
51+
Elem: &schema.Resource{
52+
Schema: map[string]*schema.Schema{
53+
"value": {
54+
Type: schema.TypeString,
55+
Required: true,
56+
},
57+
"label": {
58+
Type: schema.TypeString,
59+
Optional: true,
60+
},
61+
"link": {
62+
Type: schema.TypeString,
63+
Optional: true,
64+
},
65+
},
66+
},
67+
}
68+
}
69+
70+
func resourceSumologicCSETagSchemaRead(d *schema.ResourceData, meta interface{}) error {
71+
c := meta.(*Client)
72+
73+
var CSETagSchema *CSETagSchema
74+
key := d.Id()
75+
76+
CSETagSchema, err := c.GetCSETagSchema(key)
77+
if err != nil {
78+
log.Printf("[WARN] CSE Tag Schema not found when looking by key: %s, err: %v", key, err)
79+
80+
}
81+
82+
if CSETagSchema == nil {
83+
log.Printf("[WARN] CSE tag Schema not found, removing from state: %v - %v", key, err)
84+
d.SetId("")
85+
return nil
86+
}
87+
88+
d.Set("key", CSETagSchema.Key)
89+
d.Set("label", CSETagSchema.Label)
90+
d.Set("content_types", CSETagSchema.ContentTypes)
91+
d.Set("value_options", valueOptionsArrayToResource(CSETagSchema.ValueOptionObjects))
92+
d.Set("free_form", CSETagSchema.FreeForm)
93+
94+
return nil
95+
}
96+
97+
func valueOptionsArrayToResource(valueOptions []ValueOption) []map[string]interface{} {
98+
result := make([]map[string]interface{}, len(valueOptions))
99+
100+
for i, valueOption := range valueOptions {
101+
result[i] = map[string]interface{}{
102+
"value": valueOption.Value,
103+
"label": valueOption.Label,
104+
"link": valueOption.Link,
105+
}
106+
}
107+
108+
return result
109+
}
110+
111+
func resourceSumologicCSETagSchemaDelete(d *schema.ResourceData, meta interface{}) error {
112+
c := meta.(*Client)
113+
114+
return c.DeleteCSETagSchema(d.Id())
115+
116+
}
117+
118+
func resourceSumologicCSETagSchemaCreate(d *schema.ResourceData, meta interface{}) error {
119+
c := meta.(*Client)
120+
121+
key, err := c.CreateCSETagSchema(CSECreateUpdateTagSchema{
122+
Key: d.Get("key").(string),
123+
Label: d.Get("label").(string),
124+
ContentTypes: resourceFieldsToStringArray(d.Get("content_types").([]interface{})),
125+
ValueOptions: resourceToValueOptionArray(d.Get("value_options").([]interface{})),
126+
FreeForm: d.Get("free_form").(bool),
127+
})
128+
129+
if err != nil {
130+
return err
131+
}
132+
133+
d.SetId(key)
134+
135+
return resourceSumologicCSETagSchemaRead(d, meta)
136+
}
137+
138+
func resourceToValueOptionArray(resouceValueOptions []interface{}) []ValueOption {
139+
result := make([]ValueOption, len(resouceValueOptions))
140+
141+
for i, resourceValueOption := range resouceValueOptions {
142+
result[i] = ValueOption{
143+
Value: resourceValueOption.(map[string]interface{})["value"].(string),
144+
Label: resourceValueOption.(map[string]interface{})["label"].(string),
145+
Link: resourceValueOption.(map[string]interface{})["link"].(string),
146+
}
147+
}
148+
149+
return result
150+
}
151+
152+
func resourceSumologicCSETagSchemaUpdate(d *schema.ResourceData, meta interface{}) error {
153+
CSETagSchema, err := resourceToCSETagSchema(d)
154+
if err != nil {
155+
return err
156+
}
157+
c := meta.(*Client)
158+
if err = c.UpdateCSETagSchema(CSETagSchema); err != nil {
159+
return err
160+
}
161+
162+
return resourceSumologicCSETagSchemaRead(d, meta)
163+
}
164+
165+
func resourceToCSETagSchema(d *schema.ResourceData) (CSECreateUpdateTagSchema, error) {
166+
key := d.Id()
167+
if key == "" {
168+
return CSECreateUpdateTagSchema{}, nil
169+
}
170+
171+
return CSECreateUpdateTagSchema{
172+
Key: key,
173+
Label: d.Get("label").(string),
174+
ContentTypes: resourceFieldsToStringArray(d.Get("content_types").([]interface{})),
175+
ValueOptions: resourceToValueOptionArray(d.Get("value_options").([]interface{})),
176+
FreeForm: d.Get("free_form").(bool),
177+
}, nil
178+
}
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 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

Comments
 (0)